[antlr-interest] Simple grammar with error
    Gavin Lambert 
    antlr at mirality.co.nz
       
    Sun Sep 16 00:55:47 PDT 2007
    
    
  
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')+;
    
    
More information about the antlr-interest
mailing list