自上而下的解析器希望在“代码”中有像样的左递归案例。

发布于 2024-09-29 13:09:02 字数 609 浏览 2 评论 0原文

大家好,Stack over Flow 成员。

我正在学习编译器课程。 我确实理解自顶向下解析器应该避免左递归,而转变为右递归方式。

问题是,

a)我是否正确理解自上而下解析器等于 LL 和自下而上解析器等于 LR ?

b) 我发现左递归是一个自称为 ex) Expr :== Expr '+' Term | 的规则可能导致无限循环查找 Expr 的项。但无论如何,有考虑用 C 或 Java 输入的示例代码吗? (我不需要解析器或扫描器代码)我需要的是带有句子形式的案例代码示例,通过左递归发生无限循环。

c) 在自顶向下解析器中使用右递归实际上有什么区别?

ANS c) 消除了回溯的需要。但还有别的事吗?

ANS b) x - 2 * y 但还有其他什么?因为这个采用回溯解析方式。

我发现了非左递归和左递归的例子。

左递归语法

A -> Ax

非左递归语法

A -> Bx
B -> Ay

两者都陷入无限循环。

谢谢您并感谢您所有的专家。

Hello fellow stack over flow members.

I'm studying for compiler class.
I did understand Top-Down Parser should avoid left-recursion, and transform into right-recursion way.

Questions are,

a) am I understanding right Top-Down Parser is equal to LL and Bottom-Up Parser is equal to LR ?

b) I've found out left-recursion is Rule that calls itself ex) Expr :== Expr '+' Term | Term which can cause infinite loop to find Expr. But anyhow, any example code of consider input in C or Java? ( I don't want the parser or scanner code ) what I need is case code example with sentential form that occur infinite loop by left recursion.

c) What actually makes difference in a way using Right Recursion in Top-Down Parser?

ANS c) Eliminating the need to backtrack. but something else?

ANS b) x - 2 * y but also something else? because this one works with backtrack way of parsing.

Case example that I have found out the both non-left recursion and left recursion.

Left Recursion Grammar

A -> Ax

Non-Left Recursion Grammar

A -> Bx
B -> Ay

Both are getting into infinite loop.

Thank you and appreciated for all your expert.

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

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

发布评论

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

评论(1

§普罗旺斯的薰衣草 2024-10-06 13:09:02

a)我是否理解正确自上而下解析器等于 LL 和自下而上解析器等于 LR ?
是的,

自上而下的解析器会进入左递归的无限循环,因为产生式在代码中看起来像这样:

A() {
  A(); match(x);
}

A 永远调用自己,并且永远不会从输入流中删除任何内容。

左递归不一定是立即的,所以你的“非左递归语法”仍然是左递归的:

A -> Bx | z
B -> Ay

如果你用它的产生式替换 B,你可以看到它是左递归的:

A -> Ayx | z

这是一个正确转换左递归语法的示例到右递归语法:
左递归:

E -> E + T | T

右递归:

E -> T B
B -> + T B | Lambda

E -> TB以来,规则E中-> E + T |完成规则应用后,T、T 将始终出现在最左侧。由于最左侧由 E -> 处理。 TB,我们可以自由地构造我们用 B -> 所做的字符串的右侧。 + T B. 我们需要一个 lambda 产生式来为我们提供递归的停止点。

A->斧头和A-> xA 是等价语法,只有一个是左递归的,另一个是右递归的。

a) am I understanding right Top-Down Parser is equal to LL and Bottom-Up Parser is equal to LR ?
yes

Top down parsers get into an infinite loop with left-recursion since the productions, in code, look like:

A() {
  A(); match(x);
}

A calls itself forever and never removes anything from the input stream.

Left recursion doesn't have to be immediate so your "non left recursive grammar" is still left recursive:

A -> Bx | z
B -> Ay

You can see that it is left recursive if you replace B by its production:

A -> Ayx | z

Here is an example of correctly converting a left-recursive grammar to a right-recursive grammar:
Left recursive:

E -> E + T | T

Right recursive:

E -> T B
B -> + T B | Lambda

E -> T B since, in the rule E -> E + T | T, T will ALWAYS appear on the left most side after finishing the application of the rule. Since the left most side is taken care of by E -> T B, we are free to construct the right side of the string which we do with B -> + T B. We need a lambda production to give us a stopping point for the recursion.

A -> Ax and A -> xA would be equivalent grammars, just one is left and the other is right recursive.

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