Eliminating Immediate Left Recursion

两盒软妹~` 提交于 2019-12-20 07:09:57

问题


I understand that in order to eliminate an immediate left recursion from a grammar containing production of the form A⇒Aα i need to replace it by A⇒βA'and A'⇒αA/∈

Im having the following productions,i need to eliminate immediate left recursion

E⇒E+T/T

E⇒E+T/T

T⇒T*F/T

F⇒(E)/(id)

I can see that after elimination the first production becomes

E⇒TE'

E'⇒+TE'/T∈

Can somebody explain how this comes


回答1:


It's really just a matter of following the algorithm. Let's look at the general case. According to the algorithm a rule of the form:

A => A a1 | ... | A aN | b1 | .. | bN

where A a1, ..., A aN are nonzero left recursive sequences of terminals and nonterminals and b1, ..., bN are sequences of terminals and nonterminals that does not start with the terminal A.

The algorithm says we need to replace this by

A => b1 A' | ... | bN A'
A' => a1 A' | ... | aN A' | epsilon

Let's look at your case. Here we have

E => E + T | T

So you can think of a1 is the sequence + T since E + T is a left recursive sequence of terminals and nonterminals. Likewise you can think of B1 as T since this is a nonleft recursive sequence. We now use this to define the new nonterminal E as:

E => b1 E'

And since b1 is T this becomes

E => T E'

Defining E' we get

E' => a1 E' | epsilon

And since a1 is + T this becomes

E' => + T E' | epsilon 

Thus you end up with the grammar

E => T E'
E' => + T E' | epsilon


来源:https://stackoverflow.com/questions/21253560/eliminating-immediate-left-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!