I'm trying to learn ANTLR by writing a grammer (I'm using eclipse with the plugins for ANTLR), and it was going alright until I ran into the error:
NoViableAltException: line 0:-1 no viable alternative at input '<EOF>'
When I try to test my args parser rule;
typedident : (INT|CHAR) IDENT;
args : (typedident ( COMMA typedident)*)?;
An ident is a letter followed by any character, this works, I've tested it. typedident also works for the test.
I'm using the input of int a12q2efwe, char a12eqdsf
(totally random) and the tree appears fine in the interpreter, the only problem is that args has four branches instead of 3, typedident, comma, typedident and then the error in the last one.
Any help would be greatly appreciated.
Thanks.
I'm assuming you're using the built-in interpreter. Don't, it's buggy. Either create a custom test-class yourself, or use ANTLRWorks' debugger (I believe the Eclipse plugin uses the same debugger as ANTLRWorks). Just never use the interpreter.
In ANTLRWorks, the input "int a12q2efwe, char eq45dsf"
is being parsed (using the debugger) as follows:
As you can see yourself using this small grammar:
grammar T;
args : (typedident (COMMA typedident)*)? EOF;
typedident : (INT | CHAR) IDENT;
COMMA : ',';
INT : 'int';
CHAR : 'char';
IDENT : ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9')*;
SPACE : ' ' {skip();};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With