[antlr-interest] Is ANTLR the right tool for the job?

Andy Tripp antlr at jazillian.com
Thu Jan 4 13:05:17 PST 2007


Alan Larkin wrote:
> I need to modify some C++ code, performing tasks such as replacing
> primitive numeric variables with new objects and converting arithmetic
> expressions to different forms.
>
> I would like very much to do this using JavaCC as I have had very
> positive experiences using it on Java in the past. By first passing the
> grammar file through Java Tree Builder (JTB) the resulting parser will
> return an abstract syntax tree for the inputted code, which supports the
> visitor design pattern. I could then write small visitors to locate and
> replace the relevant sections of code, reformat the code using a
> formatting visitor, and finally dump the new code to file.
>
> Unfortunately the C++ grammars for JavaCC are woefully substandard (10
> years old!). ANTLR looks like it has a better C++ grammar but I am
> unsure if it can be used to satisfy the above objectives.
>
> Can the list please advise on whether or not ANTLR is suitable for this
> task and possibly direct me to some useful examples of such processes?
>
> Many thanks,
> Alan.
>
>   
Hi Alan,
I use ANTLR to process C, and I'm using JavaCC with JTB to process COBOL 
because there
doesn't seem to be a ANTLR COBOL grammar.

Yes, ANTLR can certainly be used to do what you need. The "official" way 
to do what you want
is to create an ANTLR "treewalker". You basically create a grammar that 
describes the AST
structure, and then add actions withing that grammar as needed. I find 
that a bit cumbersome...
see this blog for details: 
http://www.jazillian.com/articles/treewalkers.html

So instead of treewalkers, what you could do is just walk the tree "by 
hand", visiting each node
and its children. That way you don't spend your time worrying about the 
exact structure of the AST,
and rather spend it thinking "If I hit this kind of node in the AST, do 
the following...".

It's been about a year since I've used the ANTLR C++ grammar, but I'll 
give my two cents anyway.
I think it's pretty solid, but not nearly as solid as the C, Java, and 
other ANTLR grammars. This is
mostly because C++ is just a horribly complex language, and there are 
just so many little holes in
the standard that it seems like the ANTLR C++ grammar would sometimes 
fail on particular C++ programs.

Andy


More information about the antlr-interest mailing list