views:

94

answers:

2

I'd like to be able to get the AST for a given OCaml program (I'd like to walk the AST and generate an instrumented version of the code or do some kind of transformation, for example). Do any of the OCaml tools support this functionality?

+2  A: 

What you're looking for is [camlp4][1]. I haven't used camlp4 before, so I can't attest to it's virtues as software. I have heard of people using camlp5 [http://pauillac.inria.fr/~ddr/camlp5/] which, according to wikipedia, has better documentation than the current version of camlp4.

Axle
aneccodeal
+3  A: 

camlp4 is a way to go. Here is a motivating example. The docs are sparse - true, but one can make his way reading through wiki, existing examples, tutorials, and maybe even camlp4 sources.

ygrek
What does that motivating example do?
aneccodeal
Insert calls co Camlp4prof.count (read "arbitrary") function at the beginning of each parsed function definition passing name and location parameters.
ygrek
An Active discussion on resources to learn camlp4: http://groups.google.com/group/fa.caml/browse_thread/thread/1751a2c308742ac3?hl=en
nlucaroni