[antlr-interest] How to generate code from AST.

Andy Tripp antlr at jazillian.com
Fri Oct 27 06:48:46 PDT 2006


davood khoshfetrat wrote:

> Hi every body.
>
> I've created my grammer and it works perfectly. Now I have AST. My
> problem is that I have to generate code to 3 different languages and
> they are complex. Meaning iterating throgh the AST won't cut it. I'm
> looking for some kind of algorithm or framework. some thing flexible.
> If you have any clue please guide me.
>
> Looking forward to hear from you.
> Thanks.
>
Hi Davood,
My JavaEmitter code http://jazillian.com/antlr/emitter.html
pretty-prints Java code from an ANTLR-generated Java AST.
Needing a similar thing for other languages, I started to make a general
AST-pretty-printer that could apply to "any" language. Alas,
I got busy and never finished.

But the idea was to let a programmer specify how each type of node should
be handled by the pretty-printer. So you might say something like:
prettyPrinter.setFollowingString(STATEMENT, ";\n");
to indicate that it should append a semicolon and newline
after printing a STATEMENT node.

So this approach would be an alternative to the StringTemplate approach,
allowing programatic specification of the "look" of each particular 
language.

Andy


More information about the antlr-interest mailing list