From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_WHOIS,HTML_MESSAGE autolearn=disabled version=3.1.3 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 21D4CBC6B for ; Sun, 25 Nov 2007 09:46:25 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAFLFSEfAXQImh2dsb2JhbAARgi01jEsBAQEICimHRQ X-IronPort-AV: E=Sophos;i="4.21,463,1188770400"; d="scan'208";a="4577497" Received: from discorde.inria.fr ([192.93.2.38]) by mail2-smtp-roc.national.inria.fr with ESMTP; 25 Nov 2007 09:46:24 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id lAP8kOQH027187 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Sun, 25 Nov 2007 09:46:24 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAFLFSEfRSbJbn2dsb2JhbAARgi01jEsBAQEBBwQGCSCHRQ X-IronPort-AV: E=Sophos;i="4.21,463,1188770400"; d="scan'208";a="4873224" Received: from web60123.mail.yahoo.com ([209.73.178.91]) by mail1-smtp-roc.national.inria.fr with SMTP; 25 Nov 2007 09:46:23 +0100 Received: (qmail 70507 invoked by uid 60001); 25 Nov 2007 08:46:22 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=IQQBPcnRy2i74XUXLcuKfUWs09dOZGa8ImC8yp7DKN0nptlZzl++n6gOrMW7FAoDJAGdIBvoVU9cpReDD59c7WYQEmW65NkanPId3+SoX+lc4qvidE4I2rK8FJwk9HsbosNNu/YpserFC01+Xj6K66hKOmFxrdiX+WbgLZdpg6w=; X-YMail-OSG: _K1YFesVM1lTrQCMFDHT.P9YXRMT5NnwEv_asu.Xj60xjwZE6KmScz6K54LS2CAIIq3AW3CZdrfJ6lYGk8JCZVsSCKvemmunUFKUwqvjVOAAXebl36Ta Received: from [71.240.7.189] by web60123.mail.yahoo.com via HTTP; Sun, 25 Nov 2007 00:46:22 PST Date: Sun, 25 Nov 2007 00:46:22 -0800 (PST) From: echinuz echinuz Subject: How to Write a Pretty Printer for a DSL Using camlp4 To: caml-list@inria.fr MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-291161182-1195980382=:64773" Content-Transfer-Encoding: 8bit Message-ID: <838783.64773.qm@web60123.mail.yahoo.com> X-Miltered: at discorde with ID 47493660.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; camlp:01 camlp:01 ocaml:01 toplevel:01 expr:01 expr:01 self-:01 syntax:01 syntax:01 struct:01 algebra:01 algebra:01 sig:01 sig:01 struct:01 --0-291161182-1195980382=:64773 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit 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. --0-291161182-1195980382=:64773 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: 8bit 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. --0-291161182-1195980382=:64773--