[antlr-interest] Antlr too eager? Writing a template engine.

Jim Idle jimi at temporal-wave.com
Mon May 18 17:04:25 PDT 2009


Oliver Christensen wrote:
> Hi,
>
>
>
> PROBLEM: Given the following grammer, how do i make it so that when 
> given the input "users reversed=true" the expression rule will only 
> match the first "users", so that i can use the assignment rule after 
> to pull out the assignment tree.
>
> ------------ test.g ----------
> grammar test;
> options{output = AST;ASTLabelType = CommonTree;language  =  CSharp;}
>
> expression : value | assignment;
> assignment : ID '=' expression;
> value    : ID | INT;
>    
> ID      : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* ;
> INT     : ('0'..'9')+;
> WS    : ('\r'|'\n'|' '|'\t')+ {Skip();};
> --------------------------------
>
> I've been stuck on this for days, and i'm hoping somebody in the know 
> will help me.
Even though you are calling the rules yourself, you still need to 
organize the expression tree in a proper LL oriented manner. The problem 
is with your ID '=' expression, which sends things haywire. If in doubt, 
copy an expression tree from something like the Java parser and adapt it.

What you need is:

expression : value ('=' value)*;

value   
    : ID
    | INT
    ;

Note that if you did not mean to allow x = y  = z = j, then that * 
should be a ?

Jim


More information about the antlr-interest mailing list