[antlr-interest] Why cant += be used without an "output=" option (and other Q's)

Gavin Lambert antlr at mirality.co.nz
Sun Feb 17 23:23:07 PST 2008


At 12:15 18/02/2008, Benjamin Shropshire wrote:
 >One cases (abet not common) would be where the programmer is
 >trying to generate faster (or less memory intensive) code. By
 >taking total control of the AST I can build the final AST that
 >/I/ want in the first pass rather than in another pass with a
 >tree parser. this would allow total control over the node types
 >and also allow the use of much less generic constructs.

Way back in the very early beta days of ANTLR3, I used it to parse 
a fairly simple DSL, generating C++ source and header files as 
outputs.

Since StringTemplate and ASTs were new and unfamiliar to me at the 
time (and there was almost no documentation), I didn't use them; 
instead writing things the ANTLR2 way by using embedded code 
fragments.

It was very straightforward to do something like:
   { AddParameter($type.text, $name.text); }
to add a newly-parsed parameter from the source language to the 
in-memory data structures that modelled the content.

At the end of the whole process, I called the "WriteSourceFile" 
and "WriteHeaderFile" routines, each of which iterated through the 
data structure and generated the appropriate output.  (I probably 
could have done it in one pass, but speed wasn't a priority.)

So it's certainly possible to parse into custom data structures; 
I'm not really sure what you're having trouble with.

(Nowadays I probably would end up using an AST, but I'd still not 
use StringTemplate, since I'm primarily using the C target and 
there isn't a port yet.  I also wouldn't have used a tree parser, 
because I still haven't seen any point in using them :)  I 
probably would have used scopes more, though, avoiding the need to 
track some of the context outside the parser.)



More information about the antlr-interest mailing list