[antlr-interest] generating multiple files from a single input file

Arnulf Heller aheller at gmx.at
Tue Oct 14 11:16:36 PDT 2008


At 20:49 13.10.2008, Tom Smith wrote:
>Hello,
>
>Right now I have a multipass translator to generate code based on a
>program written in a DSL (using StringTemplate rewrites).  I just
>realized that I have enough information in my AST to generate some
>other supporting files as well.  Is there a straightforward way to
>structure this task?

If performance matters you should try to run your parser once and the 
code generation multiple times with the generated AST.

If you want to make use of the string template rewrite mechanism (the 
"->" notation), I think the best way is to have one tree walker and 
run it multiple times with different string template groups (with 
setTemplateLib). If your target files are very similar (like .h and 
.cpp files) consider using the template inheritance mechanism.

If you have a simple grammar, you can also build up a simple data 
structure (e.g. lists of java objects) that you can throw at your 
string templates after parsing. This is probably the fastest way.

A good starting point are the ANTLR sources, particularly the code generator 



More information about the antlr-interest mailing list