How to generate AST in ANTLR4?

大憨熊 提交于 2019-12-01 05:05:58

问题


I'm working on a project in which I have to generate Abstract Syntax Tree for a given program. Here program can be in any mainstream programming languages. What should be the standard way of generating AST in ANTLR4? I know only basics of ANTLR4 and I'm able to generate Parse tree for a given program.


回答1:


ANTLR 4 automatically generates parse trees instead of relying on manually-structured ASTs. This decision was made after observing years of development with prior approaches encountering extreme maintainability challenges, especially when multiple tree parsers were involved.

If you need an abstract representation of your source code, you should create an object model that accurately represents the constructs in your language, rather than rely on weakly typed and generally unstructured AST nodes. You then walk the parse trees instead of ASTs to create your object model.

I would not advise going with ANTLR 3 for any new project.



来源:https://stackoverflow.com/questions/23084533/how-to-generate-ast-in-antlr4

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