如何在ANTLR4中生成AST? [英] How to generate AST in ANTLR4?

查看:1445
本文介绍了如何在ANTLR4中生成AST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我必须为给定的程序生成抽象语法树。这里的程序可以在任何主流的编程语言。在ANTLR4中生成AST的标准方法应该是什么?我只知道ANTLR4的基础知识,我能够为给定的程序生成Parse树。

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.

推荐答案

ANTLR 4自动生成解析树而不是依赖手动结构化的AST。这个决定是在观察了多年的开发之后做出的,之前的方法遇到了极端的可维护性挑战,特别是当涉及多个树解析器时。

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.

如果你需要一个抽象表示你的源代码,您应该创建一个准确表示您语言中的构造的对象模型,而不是依赖弱类型和通常非结构化的AST节点。

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.

我不建议使用ANTLR 3来处理任何新项目。

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

这篇关于如何在ANTLR4中生成AST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆