ANTLR 中的初学者递归,没有调用堆栈?

发布于 2024-12-07 23:41:52 字数 324 浏览 0 评论 0原文

ANTLR 中是否存在递归(即存在调用堆栈)?示例:

parenset
:   LPAREN
    parenset*
    RPAREN
;

LPAREN: '(';
RPAREN: ')';

应该验证左括号和右括号的数量一样多。然而,在 ANTLRWorks 1.4.3 中,当我在解释器中输入 '((()))' 时,我得到

enter image description这里

我的其他右父母在哪里?!我做错了什么吗?谢谢!

Is there recursion in ANTLR in the sense that there is a call stack? Example:

parenset
:   LPAREN
    parenset*
    RPAREN
;

LPAREN: '(';
RPAREN: ')';

Should just verify that there are as many left parenthesis as there right. However in ANTLRWorks 1.4.3, in the interpreter when I type in '((()))', I get

enter image description here

Where are my other right parens?! Am I doing something incorrect? Thanks!

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

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

发布评论

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

评论(1

混浊又暗下来 2024-12-14 23:41:52

不要使用 ANTLRWorks 的解释器:众所周知,它有很多错误。

如果我在 ANTLRWorks 中使用调试器(与解释器不同!),语法为:

grammar T;

parenset
  :  LPAREN parenset* RPAREN
  ;

LPAREN : '(';
RPAREN : ')';

并提供输入 ((())) 我得到以下解析树:

在此处输入图像描述

因此,回答您的问题:

我的其他右括号在哪里?!我做错了什么吗?

不,你没有做错任何事:ANTLRWorks 的解释器把你的事情搞砸了。每当您的语法包含谓词或递归规则调用时,最好使用调试器或编写您自己的测试类。

Don't use ANTLRWorks' interpreter: it is notoriously buggy.

If I use the debugger in ANTLRWorks (not the same as the interpreter!) with the grammar:

grammar T;

parenset
  :  LPAREN parenset* RPAREN
  ;

LPAREN : '(';
RPAREN : ')';

and provide the input ((())) I get the following parse-tree:

enter image description here

So, to answer your question:

Where are my other right parens?! Am I doing something incorrect?

No, you're not doing anything wrong: ANTLRWorks' interpreter is messing things up for you. Whenever your grammar contains predicates or recursive rule-invocations, better use the debugger or write your own test class.

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