I am converting an ANTLR3 grammar to an ANTLR4 grammar, this means that I had to remove all tree rewriting rules because they now have to be implemented in code and by visiting ParseTree
with a custom Visitor
it's possible to generate an AST.
The old grammar had rewrite rules that prevented ambiguities and automatically generated the wanted tree. I couldn't find any useful resource online showing how this can be done with the visitor/listener paradigm.
- A
Visitor
needs a type that is used as return type for all thevisit*
functions. If I need to generate an AST, what return type should I use? ANTLR3 usedCommonTree
objects. - When entering a
visit*
node, I can create a node of the tree, but how should I track its parent node? - Is there any example on how to convert a tree rewrite rule to a method generating a node of the AST? If not, would it be possible to have an example for a rule like the one below?
Here the rule for (3):
ctor_initializer: '::'? identifier '(' expr? ')' -> ^(CTOR_INITIALIZER^(INITIALIZER_ID '::'? identifier) ^(CTOR_EXPR expr?) );