ANTLR - NoViableAltException [英] ANTLR - NoViableAltException

查看:17
本文介绍了ANTLR - NoViableAltException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过编写语法来学习 ANTLR(我正在使用带有 ANTLR 插件的 eclipse),并且一切顺利,直到我遇到错误:

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>'

当我尝试测试我的 args 解析器规则时;

When I try to test my args parser rule;

typedident  :   (INT|CHAR) IDENT;

args    :   (typedident ( COMMA typedident)*)?;

ident 是一个字母后跟任何字符,这是有效的,我已经测试过了.typedident 也适用于测试.

An ident is a letter followed by any character, this works, I've tested it. typedident also works for the test.

我正在使用 int a12q2efwe, char a12eqdsf 的输入(完全随机)并且树在解释器中看起来很好,唯一的问题是 args 有四个分支而不是 3 个,typedident,逗号,typedident,然后是最后一个错误.

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.

任何帮助将不胜感激.

谢谢.

推荐答案

我假设您正在使用内置解释器.不要,它是越野车.要么自己创建自定义测试类,要么使用 ANTLRWorks 的调试器(我相信 Eclipse 插件使用与 ANTLRWorks 相同的调试器).永远不要使用解释器.

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.

在 ANTLRWorks 中,输入 "int a12q2efwe, char eq45dsf" 被解析(使用调试器)如下:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆