ANTLR - NoViableAltException [英] ANTLR - NoViableAltException
问题描述
我正在尝试通过写一个语法学习ANTLR(我正在使用带有ANTLR插件的eclipse),而且直到遇到错误:
$ b $ $
NoViableAltException:行0:-1在输入'没有可行的替代'< EOF>'
当我尝试测试我的args解析器规则;
typedident:(INT | CHAR )IDENT;
args:(typedident(COMMA typedident)*)?
一个ident是一个字母,后跟任何字符,这个工作,我已经测试了。 typedident也适用于测试。
我使用的输入是 int a12q2efwe,char a12eqdsf
(完全随机)和树在翻译器中显示正确,唯一的问题是args有四个分支,而不是3,typedident,逗号,typedident,然后是最后一个错误。
谢谢。
int a12q2efwe,char eq45dsf正在被解析(
使用调试器)如下:
您可以看到自己使用这种小语法:
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' )*;
空格:''{skip();};
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();};
这篇关于ANTLR - NoViableAltException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!