尝试示例语法时解析器的奇怪行为

发布于 2024-08-13 14:49:53 字数 379 浏览 4 评论 0原文

我试图了解 antlr3,我粘贴了 表达式评估器< /a> 进入 ANTLRWorks 窗口(最新版本)并编译它。它成功编译并启动,但有两个问题:

  • 尝试使用 1+2*4/3; 的输入导致解析器的实际输入为 1+2*43代码>.
  • 它在图形解析器树中显示的错误之一是 MissingTokenException(0!=0)

由于我是 antlr 的新手,有人可以帮忙吗?

I'm trying to get the feel for antlr3, and i pasted the Expression evaluator into an ANTLRWorks window (latest version) and compiled it. It compiled successfully and started, but two problems:

  • Attempting to use a input of 1+2*4/3; resulted in the actual input for the parser being 1+2*43.
  • One of the errors it shows in it's graphical parser tree is MissingTokenException(0!=0).

As i'm new to antlr, can someone help?

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

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

发布评论

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

评论(2

魔法少女 2024-08-20 14:49:53

您链接到的示例不支持除法(只需查看代码,您就会发现这里没有除法:

expr returns [int value]
    :   e=multExpr {$value = $e.value;}
        (   '+' e=multExpr {$value += $e.value;}
        |   '-' e=multExpr {$value -= $e.value;}
        )*

The example you linked to doesn't support division (just look at the code, you'll notice there's no division here:

expr returns [int value]
    :   e=multExpr {$value = $e.value;}
        (   '+' e=multExpr {$value += $e.value;}
        |   '-' e=multExpr {$value -= $e.value;}
        )*
雨巷深深 2024-08-20 14:49:53

我们常常会得到

MissingTokenException(0!=0)

当我们犯错误时, 。我认为这意味着它找不到它正在寻找的令牌,并且可能是由不正确的令牌生成的。解析器有时可能会根据语法“恢复”。

还请记住,词法分析器在解析器之前运行,您应该检查实际传递给解析器的标记。 AntlrWorks 调试器在这里非常有帮助。

We often get

MissingTokenException(0!=0)

when we make mistakes. I think it means that it cannot find a token it's looking for, and could be produced by an incorrect token. It's possible for the parser to "recover" sometimes depending on the grammar.

Remember also that the LEXER operates before the parser and your should check what tokens are actually passed to the parser. The AntlrWorks debugger can be very helpful here.

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