简体   繁体   English

消除左递归

[英]Eliminating Left Recursion

So I have some grammar that doesn't work for a top-down parser due to it having left recursion: 所以我有一些语法对于自上而下的解析器不起作用,因为它有递归:

L::= A a | B b
A::= L b | a a
B::= b B b | b a

So in order to fix this, I have to remove the left recursion. 因此,为了解决此问题,我必须删除左递归。 To do this, I do a little substitute-like-thing: 为此,我做了一些类似替代的事情:

L::= A a | B b
A::= A a b | B b b | a a (I plugged in the possible values of "L")

A then turns to (I believe): 然后A转向(我相信):

A::= a a A' | B b b
A'::= a b A' | ε

I'm fairly certain that I'm correct up to there (wouldn't be surprised if I'm not, though). 我可以肯定的是,我是正确的(尽管我不会,但也不会感到惊讶)。 Where I'm struggling is now removing the left recursion out of "B bb". 我现在正在努力的地方是从“ B bb”中删除左递归。 I've tried going about this so many ways, and I don't think any of them work. 我已经尝试了很多方法,但我认为其中任何一个都行不通。 Here's one that seems most logical, but ugly as well (thus saying it's probably wrong). 这似乎是最合逻辑的,但也很丑陋(因此说这可能是错误的)。 Starting by manipulating B::= b B b | 从操作B :: = b B b | ba BA

B::= b B b | b a
B::= b B' (they both start with b, so maybe i can pull it out?)
B'::= B b | a
B'::= b B' b | a (substituted B's value in)
B'::= b B" | a
B"::= b B" |a B" | ε

So I guess to show what the finalized B's would be: 所以我想证明最终的B是什么:

B::= b B'
B'::= b B" | a
B"::= b B" | a B" | ε

This seems way too ugly to be correct. 这似乎太丑陋了以至于不正确。 Especially since I'd have to plug that into the new "A" terminals that I created. 特别是因为我必须将其插入我创建的新“ A”终端中。

Can someone help me out? 有人可以帮我吗? No idea if I'm going about this the right way. 不知道我是否要按照正确的方式进行操作。 I'm supposed to be able to create an LL(1) parse table afterward (should be able to do that part on my own). 我应该能够在以后创建LL(1)解析表(应该可以自己完成该部分)。

Thanks. 谢谢。

In a parser that tries to expand nonterminals from the left, if some nonterminal can expand to a string with itself on the left, the parser may expand that nonterminal forever without actually parsing anything. 在试图从左侧扩展非终结符的解析器中,如果某个非终结符可以扩展为自身位于左侧的字符串,则解析器可以永远扩展该非终结符,而无需实际解析任何内容。 This is left recursion. 这是左递归。 On the other hand, it is perfectly fine if a nonterminal expands to a string with some different nonterminal on the left, as long as no chain of expansions produces a string with the original nonterminal on the left. 另一方面,如果一个非末端扩展为左侧带有一些不同非末端的字符串,那也很好,只要没有扩展链产生一个原始非末端位于左侧的字符串即可。 Similarly, it is fine if nonterminals aren't all the way to the right in the expansion, as long as they're not all the way to the left. 同样,只要非终结符不在扩展中一直向右移动,也可以,只要它们不一直向左移动即可。

tl;dr There's nothing wrong with B bb or b B b . tl; dr B bbb B b没错。 You've removed all the left recursion. 您已删除了所有剩余的递归。 You don't need to keep going. 您不需要继续前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM