[antlr-interest] Tree parsers and real world example
Xue Yong Zhi
zhixueyong at hotmail.com
Mon Apr 16 11:04:02 PDT 2007
Matt Holmes wrote:
> While I am waiting for 3.0 to have better CSharp output support, so I
> can continue my project, I am continuing to learn ANTLR 2.77 and develop
> a parser for my language. I am trying to wrap my head around tree
> parsers, and it's just not clicking for me yet. I've looked in the ANTLR
> wiki and read the docs, but I am curious if anyone knows of a real good
> "real world" example of one in use? Preferably how (or if?) they are
> involved in the code generation* process for a scripting language. I
> have a working parser that gives me back basic expressions and the like,
> and now I am trying to figure out how I would interface that with my
> (soon to be written) code generator. I sense that tree parsers are meant
> to ease this, but looking at the examples in the CSharp examples with
> 2.77 and reading the docs, I am just not grasping it. I am hoping to see
> it "in action" to give me a better idea of how it all fits together.
>
> * In this case, by code generation, I mean turning the parsed AST in to
> byte code to be later executed by my VM.
>
ANTLR (2.x) is being used in several compiler projects:
http://seclib.blogspot.com/2005/06/parser-generator-used-for-popular.html
You can check their source code to see how they handle it.
For simple language, you can embed code generator in the semantic
actions of treeparser (even parser). In my own project (XRuby), I used
treeparser to turn ANTLR's AST into a DOM-like structure, then use
Visitor approach to generate bytecode:
http://xruby.googlecode.com/svn/trunk/src/com/xruby/compiler/parser/ruby.tree.g
--
Xue Yong Zhi
XRuby (Ruby to Java bytecode compiler):
http://xruby.blogspot.com
More information about the antlr-interest
mailing list