简单的 ANTLR 错误

发布于 2024-11-18 19:46:51 字数 1067 浏览 7 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2024-11-25 19:46:51

您的语法有一些问题:

  • 词法分析器规则只能返回 Token,因此在 TKDC 之后会忽略返回 [String s] ;
  • options 部分中的 backtrack=true 并不适用于词法分析器规则,这就是为什么您会得到 不匹配的字符 '9' 期望 ' C'(没有回溯!);
  • expr 规则的内容:(LETTER SPACE DIGIT | TKDC) {$s = $DIGIT.text + $TKDC.text;} 没有多大意义(大部头书)。您想要匹配 LETTER SPACE DIGITTKDC,但您试图获取这两个选项的 text$DIGIT .text $TKDC.text

在我看来,TKDC 需要“升级”为解析器规则。

我认为您简化了您的示例,无法说明您所面临的问题。也许更好的主意是解释你的实际问题:你想准确解析什么?

There are a few things wrong with your grammar:

  • lexer rules can only return Tokens, so returns [String s] is ignored after TKDC;
  • backtrack=true in your options section does not apply to lexer rules, that is why you get mismatched character '9' expecting 'C' (no backtracking there!);
  • the contents of your expr rule: (LETTER SPACE DIGIT | TKDC) {$s = $DIGIT.text + $TKDC.text;} doesn't make much sense (to me). You either want to match LETTER SPACE DIGIT or TKDC, yet you're trying to grab the text 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文