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

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

问题描述

我想知道我们是否可以使用Antlr版本4构建AST。我找不到使用antlr4构建AST的任何参考。
一个答案是说,使用仅生成分析树的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天全站免登陆