How to rewrite Antlr4 Parse Tree manually?

你。 提交于 2019-12-11 10:28:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/30635444/how-to-rewrite-antlr4-parse-tree-manually

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!