ANTLR - NoViableAltException

I am trying to learn ANTLR by writing grammars (I use eclipse with ANTLR plugins) and everything will be fine until I encounter an error:

NoViableAltException: line 0:-1 no viable alternative at input '<EOF>' 

When I try to check the args parser rule,

 typedident : (INT|CHAR) IDENT; args : (typedident ( COMMA typedident)*)?; 

An identifier is a letter followed by any character, it works, I tested it. typedident also works for the test.

I use input int a12q2efwe, char a12eqdsf (completely random) and the tree seems accurate 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.

Any help would be greatly appreciated.

Thanks.

+4
source share
2 answers

I assume that you are using the built-in interpreter. No, it's a buggy. Either create your own test class yourself, or use the ANTLRWorks debugger (I believe that the Eclipse plugin uses the same debugger as ANTLRWorks). Just never use an interpreter.

In ANTLRWorks, the input "int a12q2efwe, char eq45dsf" parsed (using the debugger) as follows:

enter image description here

As you can see, using this little 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();}; 
+5
source

Perhaps this answer will help you:

ANTLR NoViableAltException with JAVA

Accordingly, there is no EOF token in your grammar. You showed us the full grammar, right?

+1
source

Source: https://habr.com/ru/post/1396463/


All Articles