为什么两个 ANLTR 解析器对同一个字符串的解释不同?

发布于 2025-01-01 16:50:52 字数 1115 浏览 2 评论 0原文

我有以下 ANTLR 语法:

grammar Tasks;

options {
  language = Java;
}

tokens {
  TODO   = 'TODO';
}

plan : block;

block:  '(' TODO ( TODO | block )* ')';

WS : ( ' ' | '\t' | '\r' | '\n' | '\v' ) { $channel = HIDDEN; } ;

I 和以下字符串:

(TODO(TODO TODO(TODO)TODO))

它被 ANTRL 生成的解析器从语法中成功解析,例如,使用以下演示:

   import org.antlr.runtime.ANTLRStringStream;
   import org.antlr.runtime.CommonTokenStream;

   public class ANTLRDemo {
     public static void main(String[] args) throws Exception {
       ANTLRStringStream in = new ANTLRStringStream("(TODO (TODO TODO (TODO) TODO))");
       TasksLexer lexer = new TasksLexer(in);
       CommonTokenStream tokens = new CommonTokenStream(lexer);
       TasksParser parser = new TasksParser(tokens);
       parser.block();
     }
   } 

但是,Eclipse 插件 ANTLR IDE Tools 2.1.1 在解释相同字符串时返回错误:

MismatchedTokenException:第 1:6 行不匹配输入“(”期望 '\u0007'

两个程序之间不一致的原因可能是什么?

I have the following ANTLR grammar:

grammar Tasks;

options {
  language = Java;
}

tokens {
  TODO   = 'TODO';
}

plan : block;

block:  '(' TODO ( TODO | block )* ')';

WS : ( ' ' | '\t' | '\r' | '\n' | '\v' ) { $channel = HIDDEN; } ;

I and the following string:

(TODO (TODO TODO (TODO) TODO))

It is sucessfully parsed by the parser generated by ANTRL from the grammar, e.g., using the following demo:

   import org.antlr.runtime.ANTLRStringStream;
   import org.antlr.runtime.CommonTokenStream;

   public class ANTLRDemo {
     public static void main(String[] args) throws Exception {
       ANTLRStringStream in = new ANTLRStringStream("(TODO (TODO TODO (TODO) TODO))");
       TasksLexer lexer = new TasksLexer(in);
       CommonTokenStream tokens = new CommonTokenStream(lexer);
       TasksParser parser = new TasksParser(tokens);
       parser.block();
     }
   } 

However, Eclipse plugin ANTLR IDE Tools 2.1.1 returns error when interpreting the same string:

MismatchedTokenException: line 1:6 mismatched input '(' expecting
'\u0007'

What can be the reason of this inconsistency between both programs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

○闲身 2025-01-08 16:50:52

两个程序之间不一致的原因可能是什么?

解释器有问题:你的语法没有任何问题。

What can be the reason of this inconsistency between both programs?

The interpreter is buggy: there's nothing wrong with your grammar.

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