Unit - 2 - Left Recursion and Factoring
Unit - 2 - Left Recursion and Factoring
it haS nonterminal A.
A gramY ts left recuTsive if a
Coutd be
Teplaced by the
mon-left recuTsive produchons
ABA
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]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
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
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
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
B'eee
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
There s
C
mo left reuTSion {n tha above qrammar.
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
A BA
A Ac|bd
Map thes qrammar otth the rule, A Ad| PB
AAdP
Ac l bd
Ad
ACA'|e
Consider the qrammaT,
AAad|c
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
AAo
A
LsL
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:
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
S Ets Etses|a
E b
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-
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