antlr - Generate AST using ANTLR4 generated visitor -


i converting antlr3 grammar antlr4 grammar, means had remove tree rewriting rules because have implemented in code , visiting parsetree with custom visitor it's possible generate ast.

the old grammar had rewrite rules prevented ambiguities , automatically generated wanted tree. couldn't find useful resource online showing how can done visitor/listener paradigm.

  1. a visitor needs type used return type visit* functions. if need generate ast, return type should use? antlr3 used commontree objects.
  2. when entering visit* node, can create node of tree, how should track parent node?
  3. is there example on how convert tree rewrite rule method generating node of ast? if not, possible have example rule 1 below?

here rule (3):

ctor_initializer: '::'? identifier '(' expr? ')' -> ^(ctor_initializer^(initializer_id '::'? identifier) ^(ctor_expr expr?) ); 

idiomatically, antlr4 creates , supports use of parse-trees. there no direct support creating , modifying asts.

some of motivating factors preferring parse-trees on asts summarized here.

antlr4 not preclude constructing asts -- structure can defined in manner desired. examples here , here.


Comments