[antlr-interest] Nondeterminism

Vijai Kalyan vijai.kalyan at gmail.com
Thu May 10 12:49:51 PDT 2007


Hi All,
I have been trying to put together an expression grammar. I have got
most things right, but I still keep getting non-determinism warnings.
I looked around the mailing list archives as well as the jGuru forums,
but nothing seems to work. Here is my specification:

class FormulaParser extends Parser;

options
  {
  k = 1;
  }

statement : sumExpr EOI;
sumExpr   : prodExpr (|sumSubExpr);
sumSubExpr: PLUS prodExpr | MINUS prodExpr;
prodExpr  : unaryExpr (|prdSubExpr) ;
prdSubExpr: DIV unaryExpr | MUL unaryExpr;
unaryExpr : LB sumExpr RB | MINUS sumExpr | atom;
atom      : number | varorfunc;
varorfunc : ID (LB (argList)? RB)?;
argList   : specialarg | sumExpr ( | subArgList);
subArgList: COMMA sumExpr ( |subArgList);
number    : NUMBER;
specialarg: "PREVIOUS" | "ERROR" | "IGNORE";

class FormulaLexer extends Lexer;

options
  {
    k = 2;
    charVocabulary = '\0'..'\377';
  }

PLUS  : '+' ;
MINUS : '-' ;
MUL   : '*' ;
DIV   : '/' ;
LB    : '(';
RB    : ')';
COMMA : ',';
DOT   : '.';

WS: (' '|'\t')
    {$setType(Token.SKIP);}
  ;

EOI: ('\r' '\n' | '\n' | ';')
    ;

ID:
  ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9'|'$'|'_'|"'")*
  ;

protected
INT:
  ('0'..'9')+
  ;

NUMBER:
  INT (DOT INT)?
  ;

I tried using syntactic predicates (maybe I am using them incorrectly)
as follows:

sumExpr   : prodExpr (|sumSubExpr);
sumSubExpr: (PLUS prodExpr) => PLUS prodExpr | (MINUS prodExpr) => prodExpr;

but that doesn't work anyway. Can someone tell what am I doing wrong here?

Thanks,

Vijai.


More information about the antlr-interest mailing list