[antlr-interest] New User help!
Andy Tripp
antlr at jazillian.com
Mon Jul 3 07:36:27 PDT 2006
Walter,
ANTLR has whole "treewalker" ability, where you basically embed code inside
a grammar that describes the AST structure. Producing pretty code from
an AST is
probably the simplest example of where you'd use this. Here's Terrence's
treewalker
that does what you want:
http://antlr.org/share/1101058268251/java.tree.g
Or, if you don't want to get your mind around the whole "treewalker"
thing, you
could use my "just walk the tree by hand" approach:
http://www.jazillian.com/antlr/emitter.html
Andy
Walter Schilling wrote:
>I'm just starting to use ANTLR and I've got a question that I think
>should be obvious but I have yet to see an answer for. I'm sure I'm
>just looking in the wrong spot. I'm writing a Java analysis tool that
>uses the Java ANTLR grammar. On various occasions in my design, I need
>to take the AST that I have generated and translate / walk that back to
>the original text. For example, if I had the java construct:
>
>if ((x==0) && (y==true))
>
>the AST would be represented as :
>if
> EXPR
> &&
> ==
> x
> 0
> ==
> y
> true
> {
>...
>
>Given that a portion of my data structure is an AST starting with the
>EXPR node, is there any easy way I can walk the tree and regenerate my
>original text? I'm thinking this should be something easy and obvious,
>but short of hand coding a java recursive loop (which so far has not
>achieved quite what I want, I haven't found an easy way to do this.) It
>feels like there should be some way to use the existing tree walked I
>have to do this, but I'm thus far drawing a blank.
>
>Thanks for your assistance,
>
>Walt Schilling
>
>
>
More information about the antlr-interest
mailing list