1

I am working on a simple Xquery processor and using Antlr4 to parse the grammar. I use the visitor pattern to walk through the parse tree. Now I want to rewrite a query if the query meet the some condition. The processor now can process a query if the query directly use the keyword like "join" and meet the "join" grammar.

I want to first rewrite the parse tree if the query can be changed to a join query or do nothing if not. Is there a way to manually manipulate the parse tree? Like adding a rule context or construct a new parse tree?

Zeyu Hu
  • 11
  • 3

1 Answers1

2

For Antlr4, the idiomatic approach is to decorate tree nodes with analysis products, rather than mutating the tree structure. That is, one or more tree walks can be uses to identify and mark the nodes that could be merged into a join and a final walk to output the results.

Of course, the parse-tree could be walked to generate a separate AST that, in turn, could be walked and further structurally modified. Antlr4 does not provide support for the building and walking of such an AST.

GRosenberg
  • 5,843
  • 2
  • 19
  • 23