Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] How to Write a Pretty Printer for a DSL Using camlp4

10 views
Skip to first unread message

echinuz echinuz

unread,
Nov 25, 2007, 3:46:51 AM11/25/07
to caml...@inria.fr
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.

Nicolas Pouillard

unread,
Nov 25, 2007, 5:38:16 AM11/25/07
to echinuz echinuz, caml-list
Excerpts from echinuz echinuz's message of Sun Nov 25 09:46:22 +0100 2007:

> 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:

[...]

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

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

[...]

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

module M = Camlp4.Register.OCamlPrinter(Id)(Make)

HTH,

--
Nicolas Pouillard aka Ertai

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

0 new messages