[antlr-interest] Problem with Simple Grammar : NoViableAltException

Rory Winston rory.winston at gmail.com
Sun Mar 27 20:49:19 PDT 2011


>
> Hi all
>
> Im having some problems with a simple grammar that I have defined. The
> grammar is as follows:
>
> grammar RvMessage;
>
> START_MSG: '{';
> END_MSG: '}';
>
> parse     :     message+ EOF;
>
> message : START_MSG field+ END_MSG;
>
> field : name '=' value;
>
> fragment LOWER: 'a'..'z';
> fragment UPPER: 'A'..'Z';
> LETTER: LOWER | UPPER;
>
> fragment DIGIT: '0'..'9';
>
> name : LETTER (LETTER | DIGIT | '_')*;
>
> NEWLINE: ('\r'? '\n')+ { $channel=HIDDEN; };
>
> fragment SPACE: ' ' | '\t';
> WHITESPACE: SPACE+ { $channel = HIDDEN; };
>
> fragment NONCONTROL_CHAR: LETTER | DIGIT | SYMBOL | SPACE;
>
> fragment SYMBOL: '!' | '#'..'/' | ':'..'@' | '['..'`' | '{'..'~';
>
> STRING_LITERAL : '"' NONCONTROL_CHAR* '"';
> string : STRING_LITERAL;
> SIGN: '+' | '-';
>
> FLOAT: INTEGER '.' DIGIT+;
> INTEGER: '0' | SIGN? '1'..'9' DIGIT*;
> int : INTEGER;
> float : FLOAT;
>
> value : (string | int | float | message);
>
>
> And it can parse simple inputs like the following with no problems:
>
> {
> a=1
> b=2
> c="dddd"
> d=3.14159
> e= { a=1 }
> f={a=1 b=2 c="1111111111"}
> g="hello" h="world"
> }
>
>
> But an input like the following throws it:
>
> {
> foo_bar_1=1
> }
>
> I get NoViableAltException: no viable alternative at input '1'. The issue
> seems to be variable names (the 'name' rule in my grammar above) that
> contain a digit - even though I have defined this as part of the generating
> rule. Can anyone see if there is anything glaringly obvious in my grammar
> that would prevent the above from working? Im pretty new to Antlr, so any
> help is much appreciated.
>
> Thanks!
> -- Rory
>
>
>


More information about the antlr-interest mailing list