Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR - NoViableAltException

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.

like image 992
djcmm476 Avatar asked Feb 14 '12 18:02

djcmm476


1 Answers

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:

enter image description here

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();};
like image 143
Bart Kiers Avatar answered Oct 19 '22 06:10

Bart Kiers