[antlr-interest] Simple grammar with error

Kevin J. Cummings cummings at kjchome.homeip.net
Sat Sep 15 17:05:59 PDT 2007


Alfonso wrote:
> I'm using ANTLRWorks to learn the basics of ANTLR. Can someone explain
> me why this grammar
> 
> 
> grammar Failed;
> 
> expression
>    : term ( (PLUS|MINUS) expression)?
     : term ( (PLUS|MINUS) term )*
>    EOF
>    ;
> 
> term    :
>      atom ( (DIVIDE | ASTERISK) term)?
       atom ( (DIVIDE | ASTERISK) atom)*
>    ;
> 
> atom    :
>      NUMBER
>    | OPEN_PAREN expression CLOSE_PAREN
>    ;
> 
> NUMBER    : '0'..'9' ('0'..'9')*;
>   PLUS     : '+';
> MINUS    : '-';
> DIVIDE  : '/';
> ASTERISK : '*';
> 
> OPEN_PAREN : '(' ;
> CLOSE_PAREN: ')' ;
> 
> Generates a "NoViableAltException" after the "5" when facing the
> expression 2*(3+5)+1 in the interpreter?

Unless I misunderstand what you are trying to do, it looks like you
translated a LALR(1) grammar (or BNF) verbatim, and that doesn't do what
you expect with LL(k).

> Surely this is a basic question, but for god's sake I'm not able to
> understand it.

I've done this with every grammar I've implemented with ANTLR.  It works
just fine.

> Thanks in advance


-- 
Kevin J. Cummings
kjchome at rcn.com
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)


More information about the antlr-interest mailing list