[antlr-interest] NoViableAltException

Kevin J. Cummings cummings at kjchome.homeip.net
Sun Dec 13 11:57:49 PST 2009


On 12/13/2009 02:49 PM, Johannes Bittner wrote:
> Hello,
> 
> The Java code generated for the following grammar produces a
> NoViableAltException when using "void foo (int a, int b) { foo }" as
> input, i.e. when the second token is "foo", it works otherwise. I
> tried this with antlrworks 1.3.1. Could somebody clearify why this
> happens?

Because 'foo' is a literal in your parser, it can never be an ID.

This is the difference between reserved words and keywords.  If you need
to be able to match a literal as an ID, you need to make a parser rule like:

id: ID | 'foo' | 'int' | 'void';

> Thanks, Johannes
> 
> method
>     : type ID '(' args ')' ';'
>     | type ID '(' args ')' '{' 'foo' '}'
>     ;
> 
> type: 'void' | 'int';
> args: arg (',' arg)*;
> arg: 'int' ID;
> 
> ID  :   ('a'..'z'|'A'..'Z')+ ;
> INT :   '0'..'9'+ ;
> WS  :   (' '|'\t'|'\r'|'\n')+ {skip();} ;
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


-- 
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