[antlr-interest] Best way to describe "order" of AST tree nodes

Johannes Luber jaluber at gmx.de
Thu Aug 21 03:10:58 PDT 2008


public.web.josh schrieb:
> Hello all!
> 
> I am creating a grammar file that uses an AST tree as its output. So
> far, so good, and I have been excited to learn and use ANTLR.
> Unfortunately, I have hit a snag that my limited experience is
> preventing me from overcoming.
> 
> When parsing the input, I can create the specific sub nodes in a tree
> root as desired, but I now have the need to mark the order that they
> appeared in the input. Specifically, in my case, I am trying to parse
> something similar to a multi-dimensional array declaration:
> 
> object[x,y,z]
> 
> Currently I can produce a tree that looks similar to:
> 
> ^(OBJECT ^(DIM x) ^(DIM y) ^(DIM z))
> 
> My desire is to produce a tree similar to:
> 
> ^(OBJECT ^(DIM ^(POS 0) ^(SIZE x)) ^(DIM ^(POS 1) ^(SIZE y)) ^(DIM ^(POS
> 2) ^(SIZE z)))
> 
> The closet I got was setting a rule return value to a scope variable
> that was incremented as each dimension was processed. This worked well,
> except for the fact that I could not figure out a way to get that value
> into the tree.
> 
> Am I on the right track, or is there an easier way to do this?
> 
> Thanks ahead of time for any help you can provide!!

Is there are reason why you need to encode the position in such way?
^(OBJECT ^(DIM x) ^(DIM y) ^(DIM z)) does already contain the required
information. You have merely to count DIMs in the tree grammar. Or do
you plan to reorder the tree? In that case, there is way, but I can't
remember how it exactly went, only that Ter posted it once...

Johannes
> 
> -josh
> 
> 
> ------------------------------------------------------------------------
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 



More information about the antlr-interest mailing list