如何使用ANTLR4构建AST? [英] How can I build an AST using ANTLR4?

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

问题描述

我有一个ANTLR3语法,可构建抽象语法树。我正在考虑升级到ANTLR4。但是,似乎ANTLR4仅构建解析树,而不构建抽象语法树。例如,不再识别 output = AST 选项。此外, AST 抽象语法 均未出现在权威ANTLR4参考 的文本中。

I have an ANTLR3 grammar that builds an abstract syntax tree. I'm looking into upgrading to ANTLR4. However, it appears that ANTLR4 only builds parse trees and not abstract syntax trees. For example, the output=AST option is no longer recognized. Furthermore neither "AST" nor "abstract syntax" appears in the text of "The Definitive ANTLR4 reference".

我想知道是否丢失了什么。

I'm wondering if I'm missing something.

我的应用程序当前知道如何爬网ANTLR3生成的AST。更改它以处理解析树不是不可能的,但是会有些工作。我想确定在开始那条路之前是有必要的。

My application currently knows how to crawl over the AST produced by ANTLR3. Changing it to process a parse tree isn't impossible but it will be a bit of work. I want to be sure it's necessary before I start down that road.

推荐答案

ANTLR 4根据语法而不是语法生成解析树基于任意AST运算符和/或重写规则的AST。这样,ANTLR 4可以自动生成可以使用语法在代码中实现的侦听器和访问者接口。

ANTLR 4 produces parse trees based on the grammar instead of ASTs based on arbitrary AST operators and/or rewrite rules. This allows ANTLR 4 to automatically produce listener and visitor interfaces that you can implement in the code using your grammar.

对于从版本3升级现有应用程序的用户而言,这一变化可能是巨大的,但总体上来说,新系统更易于使用和维护(尤其是维护)。

The change can be dramatic for users upgrading existing applications from version 3, but as a whole the new system is much easier to use and (especially) maintain.

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

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