Professional Documents
Culture Documents
LL Parser: Rules
LL Parser: Rules
LL Parser: Rules
ToformanLLparser,weshouldhaveknowledgeoftwoterms,
1.FIRST
2.FOLLOW
1) FOLLOW:
LetAbeanygrammarsymbolofagrammarG.Thenweassociatewith
eachgrammarsymbolAthesetFIRST(A).Theimplicationofthissetisthat
thegrammarsymbolAcaninsomestepsoftransitionproducetheelements
ofthesetFIRST(A)asthefirstterminalorepsilon(e).
Inotherwords,
If'a'isanystringofgrammarsymbols,letFIRST(e)bethesetof
terminals
thatbeginthestringderivedfrom.Ifa=*=>ethenaddetoFIRST(a).
Rules:
1) IfXisaterminal,thenFIRST(X)is{X}
2) IfX>ethenaddetoFIRST(X)
3) IfXisanonterminalandX>Y1Y2Y3...Yn,thenput'a'in
FIRST(X)ifforsomei,aisinFIRST(Yi)andeisinallof
FIRST(Y1),...FIRST(Yi1);
Inotherwords,Y1...Yi1=*=>e.
IfeisinallofFIRST(Yj)forallj=1,2....n,thenaddeto
FIRST(X).ThisissobecauseallYi'sproducee,soXdefinitely
producese.
2) FOLLOW:
FOLLOWisdefinedonlyfornonterminalsofthegrammarG.
ItcanbedefinedasthesetofterminalsofgrammarG,whichcan
immediatelyfollowthenonterminalinaproductionrulefromstart
symbol.NB:wedontincludeepsiloninFOLLOW
Inotherwords,ifAisanonterminal,thenFOLLOW(A)issetof
terminals'a'thatcancome/appearimmediatelytotherightofAinsome
sententialform,i.e.Thesetofterminals'a'suchthatthereexistsa
derivationoftheformS=*=>aAazforsomea and z(whichcanbe
strings,orempty).
Rules:
1) IfSisthestartsymbol,thenadd$totheFOLLOW(S).
2) IfthereisaproductionruleA>aBztheneverythinginFIRST(z)
exceptforeisplacedinFOLLOW(B).
3) IfthereisaproductionA>aB,orapoductionA>aBzwhere
FIRST(z)containse theneverythinginFOLLOW(A)isin
FOLLOW(B).
/**************************************************/
Example:Considerthefollowinggrammar
S>aABe
A>Abc|b
B>d
FindtheFIRSTandFOLLOWforeachnonterminalofthegrammar.
Solution:Seenextpage
Steps:
1) Findforeverynonterminalifitisnullable.
2) FindFIRSTforeverynonterminalusingrulesdescribed
earlier.
3) Usingtheinformationgotfromcalculationsinsteps1and2
onecouldcalculateFOLLOWforeverynonterminalby
rulesdescribedearlier.
NeitherA,norBisnullable./*Check*/
TocalculateFIRSTs
a)TocalculateFIRST(S):
FromruleS>aABe,weget'a'belongstoFIRST(S)
NootherrulewillhelpgiveanelementinFIRST(S).
So,FIRST(S)={a}
b)TocalculateFIRST(A):
FromruleA>Abc,wecan'taddanyelement
FromruleA>b,weknowthat'b'belongstoFIRST(A)
NootherrulewillhelpgiveanelementinFIRST(A).
So,FIRST(A)={b}
c)TocalculateFIRST(B)
FromruleB>d,weadd'd'toFIRST(B)
NootherrulewillhelpgiveanelementinFIRST(B).
So,FIRST(B)={d}
TocalculateFOLLOWs
a)TocalculateFOLLOW(S)
SinceSisstartsymbol,add$toFOLLOW(S)
FromruleS>aABe,wedontgetanycontributiontothe
FOLLOW(S)/*Seerules2and3forFOLLOW*/
FromruleA>Abc,sincenowhereweseeanysymbolS,so
nocontributiontoFOLLOW(A)isfoundinthisrule.
FromruleA>b,againnocontribution.
FromruleB>d,againnocontribution.
HenceFOLLOW(S)={$}
b)TocalcuateFOLLOW(A):
FromrulesA>b,B>dwegetnocontribution.
FromruleS>aABeweexpecttogetsomecontribution.
How?
Seerule2offormingFOLLOW(onpage2)
Asperthat,wecanaddeverythinginFIRST(Be)inFOLLOW(A)
exceptforepsilon.
FIRST(Be)=FIRST(B)/*sinceBisnotnulable*/
={d}
Soadd'd'toFOLLOW(A).
SinceBeisnotnullable,sowecan'tusetherule3(Seepage2)
ForruleA>Abc,wedogetsomecontributionstraightaway.
Fromrule2(page2),'b'belongstoFOLLOW(A)
NootherrulewillhelpgiveanelementinFOLLOW(A).
HenceFOLLOW(A)={d,b}
TocalculateFOLLOW(B)
OnlyruleS>aABecontributes.
Byrule2,'e'belongstoFOLLOW(B).
HenceFOLLOW(B)={e}
/***********************************************/
NowontoLLparser.
WhatdoesLLsignify?
ThefirstLmeansthatthescanningtakesplacefromLefttoright.
ThesecondLmeansthattheLeftderivationisproducedfirst.
Theprimerequirementsare:
1) Stack
2) ParsingTable
3) Inputbuffer
4) Parsingprogram.
Inputbuffercontainsthestringtobeparsed,followedby$,
asymbolusedtoindicateendoftheinputstring.Thestackindicatesa
sequenceofgrammarsymbolswith$onthebottom,indicatingbottomof
thestack.Initially,thestackcontainsthestartsymbolofthegrammar
onthetopof$.Theparsingtableisa2DarrayM[A,a]whereA
isanonterminal,andaisaterminalorthesymbol$.
Howtocontroltheparser?
1. IfX=a=$,parserhalts,stringaccepted.
2. IfX=a!=$,parserpopsX,andadvancestheinputpointertopointto
nextinputsymbol.
3. IfXisanonterminal,theprogramconsultsentryM[X,a]oftheparsing
tableM.Replacethetopofstack(X)withproductionrule
cossespondingtoentryintable.Ifentry=ERROR,callerrorrecovery
routine.
Constructionofpredictiveparsingtable:
Toconstructparsingtable,thefollowingalgomustbeused:
Algorithmtoconstructpredictiveparsingtable:
1. ForeachproductionA>aofgrammarG,dosteps2and3
2. Foreachterminal'a'inFIRST(a),addA>ainM[A,a].
3. IfeisinFIRST(a),addA>atoM{A,b]foreachterminalbin
FOLLOW(A).IfeisinFIRST(a),and$isinFOLLOW(A),then
addA>atoM{A,$].
4. MakeeachundefinedentryasERROR,i.e.Anerrorentry.
/********************************************************/
Example:
ConsiderthegrammarG:
E>TE'
E'>+TE'|e
T>FT'
T'>*FT'|e
F>(E)|id
CalculatetheFIRSTandFOLLOW.Thefollowingtableisobtained.
Nullable?
FIRST
FOLLOW
No
Id,(
$,)
E'
Yes
$,)
No
Id,(
+,$,)
T'
Yes
+,$,)
No
Id,(
+,*,$,)
Usingthealgotoobtaintheparsingtable,wefindthefolowing
table.
id
+
*
(
)
$
E
E
>TE'
E'
T
E'
>+TE'
T>FT
'
T'
F
E>TE'
E'>e
E>e
T'>e
T'>e
T>FT'
T'>e T'
>*FT'
F
>id
F
>(E)
Fromthistable,anyinputbelongingtothisgrammarGcanbeparsed.
Letid+id*idbetheinput.
Thenparsingproceedsasgiveninnextpage.
stack
Inputbuffer
$E
id+id*id$
$E'T'
Id+id*id$
$E'T'F
Id+id*id$
$E'T'id
Id+id*id$
$E'T'
+id*id$
$E'
+id*id$
$E'T+
+id*id$
$E'T
id*id$
$E'T'F
id*id$
$E'T'id
id*id$
$E'T'
*id$
$E'T'F*
*id$
$E'T'F
id$
$E'T'id
id$
$E'T'
$E'
$
Theinputisaccepted
Thus,wecaneasilyconstructanLLparserwith1lookahead.
Sinceonelookaheadisinvolved,wealsocallitanLL(1)parser.
/********************************************************/
RequirementtoformanLL(1)parser:
1) Noambiguityingramar
2) Noleftfactoringingrammar
3) IfA>a|bisaproductioninthegrammar,thenFIRST(a)and
FIRST(b),wherea,bisanystring,shouldbemutuallyexclusive,
i.e.,theyshouldhavenothingincommon.
4) IfA>a|b,andFIRST(A)containsepsilon,thenFOLLOW(A)and
FIRST(b)shouldnotcoincide.Herea,bareanystringofgrammar
symbols.
5) IfA>a|b|c,thenatmostoneofa,b,cshouldbenulable.
Inthisway,iftheprecautionsaretaken,onecouldmakeagrammarfor
whichanLLparsercouldbedeveloped.
HowevernotallgrammarscouldbeparsedbyLLparsers.
NexttopicisLRparsers,whicharemorepowerfulthanLL.
Hereisanotherexample
/*********************************************************/
Example:S>iEtSS|a
S>eS|
E>b
Non
Term
S
S
E
Syma
Symb
Syme
S>a
Symi
Symt
Sym$
S>iEtSS
S>
S>eS
S>
E>b
Thegrammarisambiguousanditisevidentbythefactthatwehavetwo
entriescorrespondingtoM[S,e]containingS>andS>eS.This
ambiguitycanberesolvedifwechooseS>eSi.eassociatingthetheelses
withtheclosestpreviousthen.
LL(1)grammarshavedistinctproperties.Noambiguousgrammarorleft
recusivegrammarcanbeLL(1).AgrammarisLL(1)ifandonlyifwhenever
aproductionA>C|Dthefollowingconditionshold:
1)FornoterminalabothCandDderivestringsbeginningwitha.Thus
First(C)!=First(D)
2)AtmostoneofCorDcanderive
3)IfC*thenDdoesnotderiveanystringbeginningwithterminal
Follow(A).
/*********************************************************/