djcmm476
djcmm476

Reputation: 1813

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.

Upvotes: 6

Views: 20804

Answers (2)

Bart Kiers
Bart Kiers

Reputation: 170148

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();};

Upvotes: 7

A.H.
A.H.

Reputation: 66243

Perhaps this answer will help you:

ANTLR NoViableAltException with JAVA

According to it a EOF token is missing in your grammar. You did show us the complete grammar, didn't you?

Upvotes: 1

Related Questions