Jeffrey Zhao
Jeffrey Zhao

Reputation: 5023

ANTLR: Why the invalid input could match the grammar definition

I've written a very simple grammar definition for a calculation expression:

grammar SimpleCalc;

options {
    output=AST;
}

tokens {
    PLUS  = '+' ;
    MINUS = '-' ;
    MULT = '*' ;
    DIV = '/' ;
}

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/

ID  : ('a'..'z' | 'A' .. 'Z' | '0' .. '9')+ ;

WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+    { Skip(); } ;

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

start: expr EOF;

expr : multExpr ((PLUS | MINUS)^ multExpr)*;

multExpr : atom ((MULT | DIV)^ atom )*;

atom : ID
     | '(' expr ')' -> expr;

I've tried the invalid expression ABC &* DEF by start but it passed. It looks like the & charactor is ignored. What's the problem here?

Upvotes: 1

Views: 138

Answers (1)

Andremoniy
Andremoniy

Reputation: 34900

Actually your invalid expression ABC &= DEF hasn't been passed; it causes NoViableAltException.

enter image description here

Upvotes: 1

Related Questions