[antlr-interest] Simple grammar with error

Alfonso barbolani at ya.com
Sun Sep 16 01:59:13 PDT 2007


Gavin Lambert escribió:
> At 21:11 15/09/2007, Alfonso wrote:
> >expression
> > : term ( (PLUS|MINUS) expression)?
> > EOF
> > ;
> >
> >term :
> > atom ( (DIVIDE | ASTERISK) term)?
> > ;
> >
> >atom :
> > NUMBER
> > | OPEN_PAREN expression CLOSE_PAREN
> > ;
> >
> >NUMBER : '0'..'9' ('0'..'9')*;
> [...]
> >Generates a "NoViableAltException" after the "5" when facing the
> >expression 2*(3+5)+1 in the interpreter?
>
> Well, you've specified that 'expression' must end with an EOF and yet 
> a CLOSE_PAREN must follow it. I'd be surprised if it did anything else :)
>
> Remove the EOF from 'expression' and put it into another rule that 
> uses it (and then use that as your starting rule instead), like so:
>
> file : expression EOF;
>
> Incidentally, you can define NUMBER like this instead:
>
> NUMBER : ('0'..'9')+;
>
>

Thanks Gavin, your explanation is clear. However, given that the 
debugger seems to accept and parse this correctly and the interpreter 
does not, I'm not sure of which one of the two has a bug.


More information about the antlr-interest mailing list