Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: echinuz echinuz <echinuz@yahoo.com>
To: caml-list@inria.fr
Subject: How to Write a Pretty Printer for a DSL Using camlp4
Date: Sun, 25 Nov 2007 00:46:22 -0800 (PST)	[thread overview]
Message-ID: <838783.64773.qm@web60123.mail.yahoo.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 3228 bytes --]

Hi,
     I'm writing a DSL where I'd like to leverage camlp4 for parsing and printing.  Right now, I'm having trouble with the pretty printer.  As an example, let's take the simple calculator example.  With the following code, I can replace the OCaml toplevel and and print the AST for a simple algebraic expression:

---------------------------------------------------------------------------
$ cat calc_types.ml 
type alg=
| Add of alg*alg
| Sub of alg*alg
| Mul of alg*alg
| Div of alg*alg
| Pow of alg*alg
| Int of int
;;

$ cat pa_calc_ast.ml 
open Camlp4.PreCast
open Calc_types

let expression = Gram.Entry.mk "expression";;

EXTEND Gram
GLOBAL: expression;
expression:
    [ "add" LEFTA
        [ x=SELF; "+"; y=SELF -> <:expr< Calc_types.Add($x$,$y$) >>
        | x=SELF; "-"; y=SELF -> <:expr< Calc_types.Sub($x$,$y$) >>]
    | "mul" LEFTA
        [ x=SELF; "*"; y=SELF -> <:expr< Calc_types.Mul($x$,$y$) >> 
        | x=SELF; "/"; y=SELF -> <:expr< Calc_types.Div($x$,$y$) >>]
    | "pow" RIGHTA
        [ x=SELF; "**"; y=SELF-> <:expr< Calc_types.Pow($x$,$y$) >>]
    | "simple" NONA
        [x=INT -> <:expr< Calc_types.Int $int:x$ >> 
        |"("; x=SELF; ")" -> x]
    ];
END;;

Gram.Entry.clear Syntax.str_item ;;
EXTEND Gram
    Syntax.str_item : [ [ e = expression ->
        <:str_item< let _ = $e$;; >> ] ];
END;;
---------------------------------------------------------------------------

I now want to write a  pretty printer so that the expressions returned on the top level are not so ugly.  The following code does not work:

---------------------------------------------------------------------------
$ cat pr_calc_ast.ml 
open Camlp4.PreCast

module Id = struct
    let name = "Algebra Pretty Printer"
    let version = "$Id: Algebra Printer Version 1$"
end

module Make (Syntax : Camlp4.Sig.Syntax) : Camlp4.Sig.Printer(Syntax.Ast).S =
struct
    module Ast = Syntax.Ast

    let print_interf ?input_file ?output_file ast =
        Printf.printf "Not implemented\n"

    let print_implem ?input_file ?output_file ast =
        match ast with
        | <:str_item< $exp:e$ >> ->
            (match e with
            | <:expr< Calc_types.Add($x$,$y$) >> -> Printf.printf "Add\n"
            | <:expr< Calc_types.Sub($x$,$y$) >> -> Printf.printf "Sub\n"
            | <:expr< Calc_types.Mul($x$,$y$) >> -> Printf.printf "Mult\n"
            | <:expr< Calc_types.Div($x$,$y$) >> -> Printf.printf "Div\n"
            | <:expr< Calc_types.Pow($x$,$y$) >> -> Printf.printf "Pow\n"
            | <:expr< Calc_types.Int $x$ >> -> Printf.printf "Int\n"
            | _ -> Printf.printf "Undefined operation\n")
        | _ -> Printf.printf "Only expressions allowed\n"
end

module M = Camlp4.Register.Printer(Id)(Make)
---------------------------------------------------------------------------

It fails to compile with the message:

ocamlfind ocamlc -package camlp4 -pp camlp4of camlp4lib.cma calc_types.cmo pr_calc_ast.ml -o pr_calc_ast
File "pr_calc_ast.ml", line 17, characters 16-16:
Unbound constructor Ast.StSem
make: *** [all] Error 2

How can I fix the above code in order to create a valid pretty printer?

       
---------------------------------
Never miss a thing.   Make Yahoo your homepage.

[-- Attachment #2: Type: text/html, Size: 4844 bytes --]

             reply	other threads:[~2007-11-25  8:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-25  8:46 echinuz echinuz [this message]
2007-11-25 10:37 ` [Caml-list] " Nicolas Pouillard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=838783.64773.qm@web60123.mail.yahoo.com \
    --to=echinuz@yahoo.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox