1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52

1 Answers1

1

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

enter image description here

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • Sorry I've made a mistake in the question. The invalid expression is `ABC &* DEF`. It's `*` instead of `=`. – Jeffrey Zhao Dec 18 '12 at 01:59
  • @JeffreyZhao Check out some of the questions and answers regarding ANTLR error handling, for example [this one that I answered](http://stackoverflow.com/questions/13889941/how-to-stop-antlr-from-suppressing-syntax-errors) about "disappearing" syntax errors. My guess is that ANTLR is detecting the error, writing to `System.err`, and removing the bad input, but none of the details are reaching you/the user. – user1201210 Dec 18 '12 at 02:28
  • @JeffreyZhao, this doesn't matter, the same result as I mentioned above is for "ABC &* DEF". Your grammar is unable to parse expressions with "&" symbol. Have you tried test this grammar in ANTLRWorks? – Andremoniy Dec 18 '12 at 02:30