Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

antlr - build AST in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  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.

  2. 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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...