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