如何处理Treetop左递归

发布于 2024-11-09 20:09:51 字数 740 浏览 0 评论 0原文

我有一个用于新的通用编程的 语法文件我正在尝试构建的语言。我试图使该语言变得健壮且易于使用(它很大程度上受到 Ruby 等的启发),为此我引入了一些左递归规则。

我看到一些例子似乎表明了以下左递归规则:

rule l_recurse
  l_recurse / 'something else'
end

可以通过将其更改为非左递归规则:

rule r_recurse
  'something else' / r_recurse
end

对我来说,这看起来会遇到不同的问题并且仍然会失败。我是对的,还是这“有效”?

我试图(查找和)消除的特定左递归可以在 语法文件。我不确定哪些规则受到影响,但至少一些规则被指出已经离开-递归。 (顺便说一句,我试图通过收紧范围规则来消除他提到的特定范围问题。)

I have a grammar file for a new general-purpose programming language I'm trying to build. I'm trying to make the language robust and natural to use (it is heavily inspired by Ruby, among others), and in doing so I have introduced some left-recursive rules.

I've seen some examples that seem to indicate the following left-recursive rule:

rule l_recurse
  l_recurse / 'something else'
end

can be made non-left-recursive by changing it to:

rule r_recurse
  'something else' / r_recurse
end

To me this looks like it would have the a different problem and would still fail. Am I right, or will this "just work"?

The specific left-recursions I'm trying to (find and) eliminate are found in this grammar file. I'm not sure which rules are affected, but at least some were pointed out to have left-recursion. (By the way I have tried to eliminate the specific range issue he mentioned by tightening up range's rule.)

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

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

发布评论

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

评论(1

知足的幸福 2024-11-16 20:09:51

特定情况

rule l_recurse
  l_recurse / 'something else'
end

简化为

rule l_recurse
   'something_else'
end

(正确的递归规则也是如此),所以我需要查看您的具体示例以找出您想知道的内容。 这个问题的答案给出了左递归消除的一般规则。

典型的易于消除的左递归情况之一是列表:

rule l_list
    item | l_list ',' item
end

并且可以将其更改为右递归

rule r_list
    item r_tail?
end

rule r_tail
    ',' r_list
end

(这是一般递归消除的特殊情况)。

The particular case

rule l_recurse
  l_recurse / 'something else'
end

simplifies to

rule l_recurse
   'something_else'
end

(and so does the right recursion rule) so I need to see your specific example to find out what you want to know. The answer to this question gives the general rule for left recursion elimination.

One of the typical easy-to-remove left recursion cases is lists:

rule l_list
    item | l_list ',' item
end

and that can be changed to right recursion

rule r_list
    item r_tail?
end

rule r_tail
    ',' r_list
end

(which is a special case of the general recursion elimination).

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