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

Gavin Lambert antlr at mirality.co.nz
Thu Aug 21 13:14:22 PDT 2008


At 17:07 21/08/2008, public.web.josh wrote:
>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.

You can insert arbitrary values into the generated tree like this:

   -> ^(DIM ^(POS INT[$dim::position.toString()]) ^(SIZE $size))

(Basically, you need to provide both a token type and a value, and 
the value must be a string.)

Unless you're going to reorder the contents of the DIM element, 
though, I would probably simplify it a bit:

   -> ^(DIM POS[$dim::position.toString()] $size)

(or s/POS/INT/.)



More information about the antlr-interest mailing list