Elminaion of Left Recuvsion
it haS nonterminal A.
A gramY ts left recuTsive if a
Such that there ls a derivation A>Ad for Some Shing d.
To p- down parng me thod Camnot handle left- recurs ve
yraars, So a transfomation îs mceded to e liminat lefE
Tecursion
The lcft - r e c u s i o n par of producti on
Coutd be
Teplaced by the
mon-left recuTsive produchons
ABA
Immediai left recursion Can be Eliminated by the dollouotng
techniaues, h ich ork for ony number of A-produchons
RTSE, qroup the ProducRon as
Ad Ada Adn| PPa-- |Pn
Where mo Br begqtns otth an
A. Then, replacethe
A-producion by
ABe'A'- ---|Pr
Algonthrm Eliminating Left Recursion
Tnput: Grarmnar G o t h no cycles o e producton
Outpu An apivalent qrarnmar t h mo left recursion
Method10:
ATrange the mon-teiminal in Some Order Ai, Aa. ..
An
for eoch from ton)
a7
a
A****
8) for ( each i from i to t-i)
4 Replate each produchion of the fvm Ai>Aj bj the
ProducHon PSYS- |8, shere
AS,|S2 -|Sk are all curTent Aj producions
5] 3
6 Eliminat the
the immediat lett Tetuisi on among the
Ar prductions
4]3
Let acering
transformation that fs
eft facBoring s a qrammar
producing a qranmat
Suitabll
for redickve o r
Useful for too alternatve
the chofce behoen
top-doon parsing. when
elear ioe may be able to rerî& the
A produckon is onot
the dectston until enough of the npat
Produchions to def refer
has been Seen that we Can make the mght choice
for Ea if oe have the produchion
stmt
Stmt Ff Erpr then Stmt etse
if exp then stmt
ohfch
Cannot immediatlay tett
OnSeing the n put tf
de
stmt. In general
Produchion to choose to Expand
Adp.4Pa
CaTe hoo A-produchons and the input begtrns oith a mon-emphy
ghna der'ved from a
Page No L
LEFT RECURSION PROBLEMS
Elmnate left recurston fhom the
follootng grarmar
(a EE+T/T
(b TTPF
(c F-EDd
Solution
To Eltminate left recuYsion have
oe a rule,
AAP
Thes mule Can be Converted în to
ABA
(aconsider the grammar,
EEtTT
Map this qTammar oith the onule AAcP
AAd|P
EE+TJIT
ABA
ETE
E'+TE'le
ottl be
T h e qramma» wolthout left e cuTston
E TE
E+TE'le
Page No &
b Cons tder the qrammar
T T* FF
Map thes qrammas oth the mul A A«B
AAdB
TTAEIEB
A A
T FT
T*FT|E
h e qrammas without left reursion í s
TET
T*FT
C) Consider the
qrammas,
F E td
There fs mo left recuTsion in the above qramm as,
Page No -3
Constder the followfng qramma4,
A ABd| Aala
BBeb
Remore left recurston
Solutfon
RewRE the {ollooEng qTammar as folloc
AABdla
Ahala
BBelb
oe have the ou
To eliminau left re urston
AAo|B
Converted En o
This u l e Can be
AB
Con sider the qTammoAY,
A-Bd|a
thts qrammar
oith the mule AAd|P d
Map
AAd B
9Bdla,
AaA
BdA' |E
Consider the qramma,
ABala
this qrammay ofth the r u l e A Ad
Map
fala
A aA
A'aAle
Page No-H
Consider the qrammar
BBelb
Map thts qrammar olth the rule A>Ad P
AAdP
B Belb
A
B- bB
The qrammar otthout left reurston will be
Aan
A-Bd'|an'|e
BbB
B'eee
3Elminat left recurston rom the follootng 9Tammai,
SaBlac |sd|se
BbBe1f
C
Soluion
Revorfb thes qrammar as folloas
SsdlaB
S- Selae
B bBc|f
C surn
Can be eltminated ustng the
The qrammas wrth lejt gecuTsion
ru. AAdP
Thrs Can be Converted into
ABA
Page No9
Conside1 the gvammas,
S Sd|aB
map thfs qrammar i h the rule A Ad|P
AAa PB
SsdlaB
SaBs
s'dsle
Consider the qTammar,
SSelac
this ofth the u l e AAdB
Map qtamma
SselaG
S0es'
ses'le
ConsideT the qrammas
B- bBclf
There is mo left reurSion n the above qramma
Consider the qrammar,
There s
C
mo left reuTSion {n tha above qrammar.
The qrammar oithout left gecuTBfon is,
S aBs'|acs'
s'de|es'|e
8 beclf
c
Page-No 6
H Elmênati the lest recuTston from the foltouwtng qramma
AAcA ad bdc
SDluion
ReonG the qammaA as foltows,
A A c bd
A ade
oe have a rule
secursfon
To Elmina b left
Con ted înto
Thie u l e can be ver
A BA
Consider the qrammar.
A Ac|bd
Map thes qrammar otth the rule, A Ad| PB
AAdP
Ac l bd
Ad
ACA'|e
Consider the qrammaT,
AAad|c
oith the rule A>Ad
Map thic qrammar
AAdP
AAad
A
IS
ACA
Aadle
The qramma hithout lest ecurston iC
A b d a|CA'
A' CA'ladA'| e |
age N -¥
5 Eltminai left reursion from the folloofna qramman
LL,s|s
Sa|CL)
Solution
To eleminat left r ecuTston e have the rule
rule
AAd P
This ted en td
ule Can be Conver
Consider the qrammar
L Ls|s
f th t h e rul
map th ts qrammar
AAo
A
LsL
Consider the q rammaT S a]CL)
There S mo left oreuTSion in the above qrammas
The qrammai O thout lept recuTsion ee,
LsL'
L,stle
S alL)
Paqe No-
Eliminalk Left re cursi on from the fo touong qamman
S a t | T)
T T,s|s
Soluion:
reur6ton oe have the mule
o elimtnai left
AAd|P
This mule Can be converted tnto
A BA
A'd e
Consider the qrammas,
T T,ss
ith the Tule AAAP
Aa|P
map this gramma»
AAd|P
TTS
T ST
T STle
Con sider the qrammaY,
Salt|LTD
qramma
reussion tn t h e folto voYng
There s no Left
wthout left recuston S T
T h e qrammas
Salt| TD
T ST
T STe
PoqeNo-9
LEFT FACTORING PRoBLEMS
othe left fachoring for the follooing qrammas
S Ets Etses|a
E b
Solution e have the rule
To elfmninat the left faetoring
d
FB.Pa
Consider the qrammar,
S Ets ietses|a
1his using he uu, AAP1|d Pal dPn
map qramma
S ietss'
seseE
Cconsiter ths aramma
Eb
Trie qTammar doesnel Contain left facfornq
.The qrammas with lesft facforirng fs
SiEtss'|a
sesle
Eb
Page-No -10
Do the left facorfng In the follootna qramma
AaB|aAla
BbBb
Soluion-
e have the rule,
For Lett fatforing
nPPa
Consider the qramm aT,
AaAB aA|a
o t h the
rule A 7dp|< PL
map thês qrammas
APPa
AB|e
Consider the qrammas,
B-bBb
map thts qrammas oith t h oru A P, | p2
P
BbB
BBe
.The qrammas oith left facví ng ts
aA
A AB|Ale
B- bB
B Ble