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=-0.0 required=5.0 tests=NO_RELAYS autolearn=disabled version=3.1.3 Received: by yquem.inria.fr (Postfix, from userid 25991) id 5DEDDBC69; Mon, 20 Aug 2007 13:13:50 +0200 (CEST) Date: Mon, 20 Aug 2007 13:13:50 +0200 From: Daniel de Rauglaudre To: Caml mailing list Subject: Re: [Caml-list] How can I generate an AST? Message-ID: <20070820111350.GA22843@yquem.inria.fr> References: <6f9f8f4a0708191039o2119f94bm10959175cc887eef@mail.gmail.com> <20070819181634.GA797@yquem.inria.fr> <6f9f8f4a0708200122k710b2f3dnd723a7244140575e@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6f9f8f4a0708200122k710b2f3dnd723a7244140575e@mail.gmail.com> User-Agent: Mutt/1.5.9i X-Spam: no; 0.00; rauglaudre:01 rauglaudre:01 0200,:01 camlp:01 camlp:01 ocaml:01 syntax:01 syntax:01 mlast:01 expr:01 ocaml:01 parser:01 parser:01 compiler:01 utils:01 Hi, On Mon, Aug 20, 2007 at 10:22:17AM +0200, Loup Vaillant wrote: > This may be a better approach. However, I am not eager to use camlp*, > for it will introduce a additional dependency. I may choose this > however, if the camlp* AST is simpler than the Ocaml AST. Where can I > find it? Solution using camlp5: For the syntax tree, look at: http://pauillac.inria.fr/~ddr/camlp5/doc/html/ml_ast.html You can generate abstract syntax tree by using concrete syntax. For example, the syntax tree for the "if" statement is (in revised syntax): MLast.ExIfe loc e1 e2 e3 but you can write it with quotations (in revised syntax): <:expr< if $e1$ then $e2$ else $e3$ >> and build syntax trees as complicated as you want using quotations. But, well, you need to know camlp5, understand the quotation system, perhaps the revised syntax too, in one word, read the doc, and I can understand that if you did not use camlp5 before, it is some work... Solution using camlp4: The principle is the same but the syntax tree is different. Other solution not using camlp*: Read the ocaml sources, file "parsing/parser.mly" to understand how the ocaml syntax tree works. Program your parser to generate this syntax tree. Once done, to give your syntax tree directly to the ocaml compiler, output: 1/ the magic number you can find in ocaml sources at utils/config.ml - ast_impl_magic_number for an implementation ast_intf_magic_number for an interface 2/ your input file name 3/ your syntax tree in binary with "output_value" -- Daniel de Rauglaudre http://pauillac.inria.fr/~ddr/