[antlr-interest] Need some help with AST creation

Junkman j at junkwallah.org
Fri Aug 6 10:10:11 PDT 2010


Hi Luis,

You can try this:

tokens {
    // Semantic tokens
    FIELD;
    INDEX;
}

...

fieldExpr: (atom -> atom)
    ( '.' identifier -> ^(FIELD $fieldExpr identifier)
    | '[' expr ']' -> ^(INDEX $fieldExpr expr)
    )*
    ;

If you need the semantic tokens to have the input stream context data,
there is a way to create them out of another token, copying its context
data, for example in this case say FIELD to copy the context of '.' and
INDEX to '['.  The notation for this escapes me for the moment, but I
think the info won't be difficult to find in the wiki/documentation on
Antlr's website.

Hope that helps,

Jay

Luis Pureza wrote:
> Hi,
> 
> I need some help from the ANTLR wizards :)
> 
> I'm trying to match expressions with field accesses and array indexes.
> For example:
> 
> costumers.length
> costumers[0].address
> costumers[costumers.length - 1].orders[0].total
> 
> 
> The following rule seems to work:
> 
> fieldExpr      : atom ('.'^ identifier | ('['^ expr ']'!))*;
> 
> However, it creates trees with notes annotated with '[', and I'd
> prefer to have a dummy token like INDEX. For example, costumers[0] now
> returns
> 
> ([ (ID costumers) (INT 0))
> 
> But I'd like it to return
> 
> (INDEX (ID costumers) (INT 0))
> 
> I tried to create the AST manually with -> ^(...), but I ended up
> nowhere. Maybe I should've tried to refactor the grammar, but that
> would make it a little less readable, so I didn't do it.
> How do you suggest I do this?
> 
> Thank you!
> 
> 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