在 antlr4 中构建 AST [英] build AST in antlr4

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

问题描述

我想知道我们是否可以使用 Antlr 版本 4 构建 AST.我找不到关于使用 antlr4 构建它的任何参考.一个 SO 答案说使用仅生成解析树的 antlr4 会很容易,但我的问题是效率怎么样?

I was wondering whether we could build an AST using Antlr version 4. I couldn't find any reference on building it using antlr4. One SO answer says that it would be easy to use antlr4 which produces only parse tree but my question is what about the efficiency?

它迫使我们爬取整个解析树而不是抽象语法树,这不是遍历整个树并使用访问者执行任务的有效方法.

It forces us to crawl whole parse tree instead of an abstract syntax tree which is not efficient way to walk through the whole tree and perform task using visitors.

推荐答案

我想首先指出两个关键事项:

There are two key items I'd like to point out first:

  1. 效率不仅仅是简单的执行速度.它还适用于内存开销、可维护性以及团队创建和/或使用软件的长期开发策略的有效性.

  1. Efficiency covers more than simple speed of execution. It can also apply to memory overhead, maintainability, and the effectiveness of the long-term development strategy used by the team creating and/or using a piece of software.

你在争论解析树比 AST 慢的论点.多年来,我一直在测试 ANTLR 3 和 ANTLR 4 的性能,在这一点上,ANTLR 4 的解析树毫无疑问消除了 ANTLR 3 中 AST 的性能.

You are hand-waving in your argument that parse trees are slower than ASTs. I've been testing performance of ANTLR 3 and ANTLR 4 for years, and there is no question at this point that ANTLR 4's parse trees obliterate the performance of ASTs in ANTLR 3.

也就是说,仅在 ANTLR 4 中使用解析树的决定是基于以下观察:实际 ANTLR 3 中具有 AST 的分离解析器和树解析器的可维护性不符合 预期 可维护性,因此降低了整体效率,以至于它们不再是推荐的做法.

That said, the decision to use parse trees exclusively in ANTLR 4 was based on observations that the actual maintainability of separated parsers and tree parsers with ASTs in ANTLR 3 did not meet the expected maintainability, and therefore decreased overall efficiency to the point that they were no longer the recommended practice.

此外,ANTLR 4 中的自动解析树构建允许我实现声明性规则版本控制,然后我 应用于整个 ANTLRWorks 2 的代码.这使我能够根据解析树的形状快速纠正大型代码库的每一部分,而不会引入回归最终产品中的错误.

In addition, automated parse tree construction in ANTLR 4 allowed me to implement declarative rule versioning, which I then applied to code throughout ANTLRWorks 2. This allowed me to rapidly correct every portion of a large codebase relying on the shape of the parse tree, without introducing regression bugs in the final product.

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

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