[antlr-interest] ANTLR vs lex/yacc

Mike Bresnahan mbresnah at visi.com
Sun Jan 16 20:40:32 PST 2005

Here's an example of where I find LL(k)/ANTLR obtuse.  Take the following
standard expression example that is similar to many on the web.

class ExpressionParser extends Parser;

expr     : sumExpr SEMI!;
sumExpr  : prodExpr ((PLUS^|MINUS^) prodExpr)* ;
prodExpr : powExpr ((MUL^|DIV^|MOD^) powExpr)* ;
powExpr  : atom (POW^ atom)? ;
atom     : INT ;

class ExpressionLexer extends Lexer;

PLUS  : '+' ;
MINUS : '-' ;
MUL   : '*' ;
DIV   : '/' ;
MOD   : '%' ;
POW   : '^' ;
SEMI  : ';' ;
protected DIGIT : '0'..'9' ;
INT   : (DIGIT)+ ;

What I find really weird about this grammar is that a "sumExpr" can be not
only "x + y" as I would expect, but it can also be "x * y" and "x^2" and
"5".  That's unintuitive to me.  I don't remember having this issue with
LALR/yacc.  Is the unintuitive to me only because I learned it differently
the first time (i.e. in yacc)?

What's also confusing is that the structure of the grammar above determines
the operator precedence.  With yacc operator precedence is specified more
clearly via special keywords.  Again perhaps I only think this way because I
learned it differently the first time.

More information about the antlr-interest mailing list