开发者

ANTLR Trees necessary?

开发者 https://www.devze.com 2023-01-07 05:46 出处:网络
What is the purpose of using AST while building a compiler (with ANTLR). Is it necessary to have one? What is th开发者_开发百科e so called TreeParser and how can one use it? Is it possible to build a

What is the purpose of using AST while building a compiler (with ANTLR). Is it necessary to have one? What is th开发者_开发百科e so called TreeParser and how can one use it? Is it possible to build a compiler without any trees? If not, are there any good tutorials describing the topic in details?


AST lets you separate parsing from other compiler tasks (name binding, typechecking, code generation) -- it presents the structure of program more conveniently than plain text. When you do binding or typechecking or codegen then you care about structure not about textual layout of the program.

For really simple language it might be possible to do everyting in parser actions (ANTLR reference has an example), but for nontrivial programming languages, AST is the way to go.

(You don't necessarily need to use ANTLR trees and tree grammars, in rule actions you can construct and your own data structures)

If you're a Java guy, then this tutorial about Java AST in Eclipse might be interesting: http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html

0

精彩评论

暂无评论...
验证码 换一张
取 消