简单的 ANTLR 错误
我从 ANTLR 开始,但出现了一些错误,我真的不明白为什么。
这是我非常简单的语法
grammar Expr;
options {backtrack=true;}
@header {}
@members {}
expr returns [String s]
: (LETTER SPACE DIGIT | TKDC) {$s = $DIGIT.text + $TKDC.text;}
;
// TOKENS
SPACE : ' ' ;
LETTER : 'd' ;
DIGIT : '0'..'9' ;
TKDC returns [String s] : 'd' SPACE 'C' {$s = "d C";} ;
这是JAVA源代码,我只要求“expr”结果:
import org.antlr.runtime.*;
class Testantlr {
public static void main(String[] args) throws Exception {
ExprLexer lex = new ExprLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);
ExprParser parser = new ExprParser(tokens);
try {
System.out.println(parser.expr());
} catch (RecognitionException e) {
e.printStackTrace();
}
}
}
当我的输入文件具有以下内容d 9时,问题就出现了。
我收到以下错误:
x line 1:2 mismatched character '9' expecting 'C'
x line 1:3 no viable alternative at input '<EOF>'
有人知道这里的问题吗?
I'm starting with ANTLR, but I get some errors and I really don't understand why.
Here you have my really simple grammar
grammar Expr;
options {backtrack=true;}
@header {}
@members {}
expr returns [String s]
: (LETTER SPACE DIGIT | TKDC) {$s = $DIGIT.text + $TKDC.text;}
;
// TOKENS
SPACE : ' ' ;
LETTER : 'd' ;
DIGIT : '0'..'9' ;
TKDC returns [String s] : 'd' SPACE 'C' {$s = "d C";} ;
This is the JAVA source, where I only ask for the "expr" result:
import org.antlr.runtime.*;
class Testantlr {
public static void main(String[] args) throws Exception {
ExprLexer lex = new ExprLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);
ExprParser parser = new ExprParser(tokens);
try {
System.out.println(parser.expr());
} catch (RecognitionException e) {
e.printStackTrace();
}
}
}
The problem comes when my input file has the following content d 9.
I get the following error:
x line 1:2 mismatched character '9' expecting 'C'
x line 1:3 no viable alternative at input '<EOF>'
Does anyone knwos the problem here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的语法有一些问题:
options
部分中的backtrack=true
并不适用于词法分析器规则,这就是为什么您会得到不匹配的字符 '9' 期望 ' C'
(没有回溯!);expr
规则的内容:(LETTER SPACE DIGIT | TKDC) {$s = $DIGIT.text + $TKDC.text;}
没有多大意义(大部头书)。您想要匹配LETTER SPACE DIGIT
或TKDC
,但您试图获取这两个选项的text
:$DIGIT .text
和$TKDC.text
。在我看来,
TKDC
需要“升级”为解析器规则。我认为您简化了您的示例,无法说明您所面临的问题。也许更好的主意是解释你的实际问题:你想准确解析什么?
There are a few things wrong with your grammar:
Token
s, soreturns [String s]
is ignored afterTKDC
;backtrack=true
in youroptions
section does not apply to lexer rules, that is why you getmismatched character '9' expecting 'C'
(no backtracking there!);expr
rule:(LETTER SPACE DIGIT | TKDC) {$s = $DIGIT.text + $TKDC.text;}
doesn't make much sense (to me). You either want to matchLETTER SPACE DIGIT
orTKDC
, yet you're trying to grab thetext
of both choices:$DIGIT.text
and$TKDC.text
.It looks to me
TKDC
needs to be "promoted" to a parser rule instead.I think you dumbed down your example a bit too much to illustrate the problem you were facing. Perhaps it's a better idea to explain your actual problem instead: what are you trying to parse exactly?