[antlr-interest] Wrong rule in parser gets matched

Bryan Ewbank ewbank at gmail.com
Sun Feb 26 17:18:51 PST 2006


Dan,

The problem is, I think, that you didn't do what you thought you did in the
grammar.  Specifically, IDENT does not match an expression because
unaryExpression **requires** either a PLUS or MINUS before the identifier.
What you probably want is this:

    // first and second alternative consume unary operators; third alternative
    // is matched when there are no unary operators left...
    unaryExpression
            :       MINUS^ unaryExpression 
{System.out.println("matched UNARY MINUS");}
            |       PLUS^ unaryExpression  
{System.out.println("matched UNARY PLUS");}
            |       atom                   
{System.out.println("matched bar atom");}
            ;

sensorCont still looks a bit suspect with the "is" alternative -- why
is there ( )*
around the trailing "as" suffix, and why is the tree-shape different than for
the "as" alternative?

> statement
>         :       sensorDec
>         |       expression
>         ;
>
> expression
>         :       addExpr
>                 ( ASSIGN^ expression
>                 | "is"^ type ("as" mode)* // require addExpr to be IDENT
>                 | "as"^ mode              // require addExpr to be IDENT
>                 )?
>         ;

This focuses on the syntax of the language, rather than the meaning of that
syntax.

Hope this helps,
- Bryan


More information about the antlr-interest mailing list