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

查看:33
本文介绍了如何使用 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天全站免登陆