caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* How to get an AST of OCaml source?
@ 2005-02-24  3:25 mulhern
  2005-02-24  7:02 ` [Caml-list] " Sylvain LE GALL
  0 siblings, 1 reply; 2+ messages in thread
From: mulhern @ 2005-02-24  3:25 UTC (permalink / raw)
  To: caml-list

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 


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

* Re: [Caml-list] How to get an AST of OCaml source?
  2005-02-24  3:25 How to get an AST of OCaml source? mulhern
@ 2005-02-24  7:02 ` Sylvain LE GALL
  0 siblings, 0 replies; 2+ messages in thread
From: Sylvain LE GALL @ 2005-02-24  7:02 UTC (permalink / raw)
  To: mulhern; +Cc: caml-list

Hello,

On Wed, Feb 23, 2005 at 09:25:59PM -0600, mulhern wrote:
> 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.
> 

Well, i have written a kind of library called ocaml-ast-analyze. It use
camlp4 to register a printer of source code. You can inject function to
do code analysis using this module and you can be able to analyze
certain part of the code.

I don't have released this library. If you are intersted in it, i can
send you a copy. Can you take a look at the file, and then told me if
you want the source ?

URL : 
http://www.gallu.homelinux.org/cgi-bin/viewcvs.cgi/ocaml-ast-analyze/trunk/?root=svn

And look at :
http://www.gallu.homelinux.org/cgi-bin/viewcvs.cgi/ocaml-gettext/trunk/libgettext-ocaml/pr_gettext.ml?rev=359&root=svn&view=auto

for example.

Kind regard
Sylvain Le Gall

ps : it use camlp4, so it is limited to what camlp4 do, for example i
should use the same command line as camlp4 invocation...


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

end of thread, other threads:[~2005-02-24  7:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-24  3:25 How to get an AST of OCaml source? mulhern
2005-02-24  7:02 ` [Caml-list] " Sylvain LE GALL

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).