From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id CCEBCBC75 for ; Thu, 24 Feb 2005 04:26:02 +0100 (CET) Received: from sabe.cs.wisc.edu (sabe.cs.wisc.edu [128.105.6.20]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j1O3Q1cH011044 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=FAIL) for ; Thu, 24 Feb 2005 04:26:02 +0100 Received: from [172.16.1.36] (adsl-68-78-237-252.dsl.mdsnwi.ameritech.net [68.78.237.252]) (authenticated bits=0) by sabe.cs.wisc.edu (8.13.1/8.13.1) with ESMTP id j1O3Q0ZU010397 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NO) for ; Wed, 23 Feb 2005 21:26:01 -0600 Mime-Version: 1.0 (Apple Message framework v619.2) Content-Transfer-Encoding: 7bit Message-Id: Content-Type: text/plain; charset=US-ASCII; format=flowed To: caml-list@yquem.inria.fr From: mulhern Subject: How to get an AST of OCaml source? Date: Wed, 23 Feb 2005 21:25:59 -0600 X-Mailer: Apple Mail (2.619.2) X-Miltered: at nez-perce with ID 421D4949.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ast:01 ocaml:01 ocaml:01 binary:01 ast:01 compiler:01 pointers:01 compiler:01 dumped:01 ocamlc:01 parsing:01 dumped:01 ocamlc:01 parsed:01 tree:02 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: Hi, I need to use OCaml source code as input to a tool that generates a related sort of output. It's not a source-to-source translator; the code generated will not do what the OCaml source code does. It's not a pretty printer either, the relationship isn't so direct. I'ld like to write the tool itself in OCaml and am hoping to use CamlTemplate. What is the most direct way to get a useful, easily traversable, representation of OCaml source code in OCaml? Clearly, there is one embedded in the various ocaml tools. I understand that Camlp4 will dump out a binary AST, which is then input to the OCaml compiler. If that is the best way to go, could somebody give me some pointers about how to traverse this AST? I have been unable to extract the information from the Camlp4 documentation. I have also been looking at the OCaml src code distribution. I realize it's possible to pass the compiler a flag (dparsetree) that will cause a pretty-printed version of the parse tree to be dumped out. On examination of compiler.ml I can see how that ast that gets pretty printed is constructed. Is my best bet to write an ocaml application that just uses a a large chunk of the ocamlc source code modeling the application as best I can on the compiler.ml source? Or would I be better off parsing the pretty-printed stuff that gets dumped out? Or, could I write my own printer that is not so pretty and dumps a textual representation that is very easily parsed so that the AST can be reconstructed and insert that into my local version of ocamlc? I'm sure people have encountered similar problems before. Any advice based on your experience would be very much appreciated. -mulhern