caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* How to Write a Pretty Printer for a DSL Using camlp4
@ 2007-11-25  8:46 echinuz echinuz
  2007-11-25 10:37 ` [Caml-list] " Nicolas Pouillard
  0 siblings, 1 reply; 2+ messages in thread
From: echinuz echinuz @ 2007-11-25  8:46 UTC (permalink / raw)
  To: caml-list

[-- 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 --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] How to Write a Pretty Printer for a DSL Using camlp4
  2007-11-25  8:46 How to Write a Pretty Printer for a DSL Using camlp4 echinuz echinuz
@ 2007-11-25 10:37 ` Nicolas Pouillard
  0 siblings, 0 replies; 2+ messages in thread
From: Nicolas Pouillard @ 2007-11-25 10:37 UTC (permalink / raw)
  To: echinuz echinuz; +Cc: 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-11-25 10:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-25  8:46 How to Write a Pretty Printer for a DSL Using camlp4 echinuz echinuz
2007-11-25 10:37 ` [Caml-list] " Nicolas Pouillard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).