ANTLR - NoViableAltException
我正在尝试通过编写语法来学习 ANTLR(我正在使用 Eclipse 和 ANTLR 插件),一切进展顺利,直到我遇到错误:
NoViableAltException: line 0:-1 no viable alternative at input '<EOF>'
当我尝试测试我的 args 解析器规则时;
typedident : (INT|CHAR) IDENT;
args : (typedident ( COMMA typedident)*)?;
ident 是一个字母后跟任何字符,这有效,我已经测试过。 typedident 也适用于测试。
我使用 int a12q2efwe, char a12eqdsf (完全随机)的输入,并且树在解释器中显示正常,唯一的问题是 args 有四个分支而不是 3 个, typedident, comma, typedident然后是最后一个错误。
任何帮助将不胜感激。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您正在使用内置解释器。别这样,有bug要么自己创建一个自定义测试类,要么使用 ANTLRWorks 的调试器(我相信 Eclipse 插件使用与 ANTLRWorks 相同的调试器)。只是永远不要使用口译员。
在 ANTLRWorks 中,输入
"int a12q2efwe, char eq45dsf"
正在被解析(使用调试器),如下所示:正如您所看到的,您自己正在使用这个小语法:
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:
也许这个答案会对您有所帮助:
ANTLR NoViableAltException with JAVA
根据它
EOF<您的语法中缺少 /code> 标记。你确实向我们展示了完整的语法,不是吗?
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?