[antlr-interest] Migrating from V2 to V3

Andreas Meyer andreas.meyer at smartshift.de
Thu Apr 16 05:35:01 PDT 2009


Robert van der Hulst schrieb:
> Guys,
>
> I have 'inherited' a large project that uses Antlr V2 (CPP target).
>
> Is there a step by step guide somewhere on how to convert the Antlr V2
> grammar files to Antlr V3.
> I would not mind switching from the CPP target to for example C# too.
>
>   

There is a wiki-page: 
http://www.antlr.org/wiki/display/ANTLR3/Migrating+from+ANTLR+2+to+ANTLR+3#MigratingfromANTLR2toANTLR3-NewinANTLR3
But be warned, that this page is only concerned about the grammar 
syntax. Among the other changes are also: a new tree model (instead of 
child-sibling), and a new parsing algorithm (LL*) that may cause 
problems if you have lots of ambiguities in your grammar. ANTLR2 does 
not seem to have this problem. So, if your inherited code is large 
enough and happens to manipulate the AST, using functions like 
"setNextChild", "getFirstChild", "setHiddenAfter" etc, you will run into 
trouble. Some weeks ago, I already posted a mail about this topic. Part 
of it was:

For example, an antlr2 AST node can be a singular node,or 
a node with a sibling, which has another sibling. So, it can be either a 
node or a list of nodes. If your code depends on this, you end up 
replacing many type definitions with List<AST>. Or you add the nodes you 
actually want to return to a dummy parent, return the first child of 
that parent, and let the caller find out if the returned node has a 
parent and if so, regard all its childs as the rest of the list. If you 
did not write your program in such a fashion, maybe the translation to 
antlr3 gets easier then. But still, there is a function called 
"setNextSibling" in antlr2. How do you emulate that in antlr3? You have 
to distinguish lots of special cases to find out if it means: addChild, 
deleteChild, deleteRangeOfChildren .. whatever.


If that applies to you, another solution could be to use ANTLR3 only for 
parsing, but keep using the ANTLR2 AST nodes for representation and 
manipulation. You probably could do this using a TreeAdaptor, which is 
new in ANTLR3 and is used from within the tool-generated code to 
build/traverse/manipulate the AST.

Best,
Andreas Meyer


More information about the antlr-interest mailing list