This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

How to Write a Pretty Printer for a DSL Using camlp4
• echinuz echinuz
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2007-11-25 (08:46) From: echinuz echinuz Subject: How to Write a Pretty Printer for a DSL Using camlp4
```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=
| 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:
[ 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.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.
```