ANTLR - NoViableAltException

流过昼夜 提交于 2019-12-07 04:39:25

问题


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.


回答1:


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



回答2:


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?



来源:https://stackoverflow.com/questions/9282087/antlr-noviablealtexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!