top downandbotttomup
TRANSCRIPT
-
8/3/2019 Top Downandbotttomup
1/23
Top-Down and Bottom-Up Parsing
-
8/3/2019 Top Downandbotttomup
2/23
p p g
TopDownParsing
BottomUpParsing
-
8/3/2019 Top Downandbotttomup
3/23
Thingstoknow:Topdownparsingisconstructingaparsetreefortheinputstartingfromtherootand
createnodesoftheparsetreeinpreorder(depthfirst).
Ageneralformoftopdownparsingistherecursivedescentparsing.
Arecursivedescentparsingisatopdownparsingtechniquethatexecuteasetof
recursiveprocedurestoprocesstheinput,thatinvolvesbacktracking(meansscanningtheinputrepeatedly).
Backtrackingistimeconsumingandtherefore,inefficient.Thatswhyaspecialcaseoftopdownparsingwasdeveloped,calledpredictiveparsing,wherenobacktrackingisrequired.
Adilemmacanoccurifthereisaleftrecursivegrammar.Evenwithbacktracking,youcanfindtheparsertogointoaninfiniteloop.
Therearetwotypesofrecursion,leftrecursiveandrightrecursive,basedonitsname,aleftrecursivegrammarbuildtreesthatgrowsdowntotheleft,whilerightrecursiveisviceversa.
TopDownParsing
-
8/3/2019 Top Downandbotttomup
4/23
Top-downParsetreeofGrammarG(Whereinput=id):G=E->TE E->+TE|
T->FT
T->*FT|
F->(E)|id
Anexampleofasimpleproductionwithleftrecursivegrammar
Considerthegrammar:expr -> expr + termThisisanexampleofaleftrecursivegrammar.Wheneverwecallexpr,thesameprocedureiscalledout,andtheparserwillloopforever.
Bycarefullywritingagrammar,onecaneliminateleftrecursionfromit.
expr -> expr + term, canbewrittenasexpr -> expr + term | termAfter obtaining a grammar that needs no backtracking, we can use thePREDICTIVE PARSER
E E
T E
E
T E
F T
E
T E
F T
id
-
8/3/2019 Top Downandbotttomup
5/23
TopDownParsingTechniques
Recursive-DescentParsing
PredictiveParsing
-
8/3/2019 Top Downandbotttomup
6/23
Recursive-DescentParsing
Arecursive-descentparsingprogramconsistsofasetofprocedures,oneforeachnonterminal.Executionbeginswiththeprocedureforthestartsymbol,whichhalts
andannouncessuccessifitsprocedurebodyscanstheentireinputstring.Generalrecursive-descentmayrequirebacktracking;thatis,itmayrequirerepeated
scansovertheinput.
Considerthegrammarwithinputstringcad:
S->cAd
A->ab|a
S SS
c ccA AA dd d
a ab
c a d
Back
Recursive-DescentParsing
-
8/3/2019 Top Downandbotttomup
7/23
PredictiveParsing-aparsingtechniquethatusesalookaheadsymboltodetermineifthecurrentinputargumentsmatchesthelookaheadsymbol.
FirstandFollow
ConstructionofPredictive
ParsingTables
LL(1)
Grammars ErrorRecovery
-
8/3/2019 Top Downandbotttomup
8/23
FirstandFollowaidstheconstructionofapredictiveparser.
Theyallowustofillintheentriesofapredictiveparsingtable.
aisanystringofterminals,thenFirst(a)isthesetofterminalsthatbeginthestringsderivedfroma.Ifaisanemptystring(),thenisalsoinFirst(a).
Follow(A),foranonterminalA,tobethesetofterminalsathatcanappearimmediatelytotherightofAinasententialform.
Firstand
Follow
-
8/3/2019 Top Downandbotttomup
9/23
RulesincomputingFIRST(X)whereXcanbeaterminalornonterminal,oreven(emptystring).
1) IfXisaterminal,thenFIRST(X)=X.
2) IfXis,thenFIRST(X)=.3) IfXisanonterminalandYandZarenonterminals,withaproductionof
X->Y
Y->Za
Z->b;thenFIRST(X)=b;whereFIRST(nonterminal1)->FIRST(nonterminal2)oruntilyoureachthefirstterminaloftheproduction.Inthatcase
(FIRST(nonterminaln)=FIRST(nonterminaln+1))
4)IfXisanonterminalandcontainstwoproductions.EX:
X->a|b;thenFIRST(X)={a,b}
Firstand
Follow
-
8/3/2019 Top Downandbotttomup
10/23
ConsideragaingrammarG:
1)E->TE
E->+TE|
T->FT
T->*FT|
F->(E)|id
2)S->iEtSS|a
S->eS| E->b
ANSWERS(FIRST):
1)FIRST(E)=FIRST(T)=FIRST(F)={(,id}
FIRST(E)={+,}
FIRST(T)={*,}
2)FIRST(S)={i,a}
FIRST(S)={e,} FIRST(E)={b}
FirstandFollow
-
8/3/2019 Top Downandbotttomup
11/23
RulesincomputingFOLLOW(X)whereXisanonterminal
1) IfXisapartofaproductionandissucceededbyaterminal,forexample:A->Xa;thenFollow(X)={a}
2) IfXisthestartsymbolforagrammar,forex:
X->AB A->a
B->b;thenadd$toFOLLOW(X);FOLLOW(X)={$}
3) IfXisapartofaproductionandfollowedbyanothernonterminal,gettheFIRSTofthatsucceedingnonterminal.
ex:A->XD
D->aB;thenFOLLOW(X)=FIRST(D)={a};andifFIRST(D)contains
(ex:D->aB|),theneverythinginFOLLOW(D)isinFOLLOW(X).
4) IfXisthelastsymbolofaproduction,ex:S->abX,then
FOLLOW(X)=FOLLOW(S)
FirstandFollow
-
8/3/2019 Top Downandbotttomup
12/23
ConsideragaingrammarG:
1)E->TE
E->+TE|
T->FT T->*FT|
F->(E)|id
2)S->iEtSS|a
S->eS|
E->b
ANSWERS(FIRST):1)FIRST(E)=FIRST(T)=FIRST(F)={(,id}
FIRST(E)={+,}
FIRST(T)={*,}
2) FIRST(S)={i,a};FIRST(S)={e, };FIRST(E)={b}
ANSWERS(FOLLOW):
ANSWERSFORFOLLOW:
1) FOLLOW(E)=FOLLOW(E)={),$}
FOLLOW(T)=FOLLOW(T)={+,),$}
FOLLOW(F)={+,*,),$}
2) FOLLOW(S)=FOLLOW(S)={e,$}
FOLLOW(E)={t}
FirstandFollow
-
8/3/2019 Top Downandbotttomup
13/23
ThegeneralideaistousetheFIRSTANDFOLLOWtoconstructtheparsingtables.
EachFIRSTofeveryproductionislabeledinthetablewhenevertheinputmatcheswithit.
WhenaFIRSTofaproductioncontains,thenwegettheFollowoftheproduction
ConstructionofPredictive
ParsingTables
-
8/3/2019 Top Downandbotttomup
14/23
ConsideragaingrammarG:E->TEE->+TE|T->FT
T-->*FT|F->(E)|idandtheirFirstandFollow
FIRST(E)=FIRST(T)=FIRST(F)={(,id} FOLLOW(E)=FOLLOW(E)={),$}
FIRST(E)={+,} FOLLOW(T)=FOLLOW(T)={+,),$}
FIRST(T)={*,} FOLLOW(F)={+,*,),$}
ConstructionofPredictive
ParsingTables
Nonterminals Id + * ( ) $EE
TTF
E->TE
T->FT
F->id
E->+TE
T->
T->*FT
E->TE
T-FT
F->(E)
E->
T->
E->
T->
-
8/3/2019 Top Downandbotttomup
15/23
Back
STACK INPUT ACTION$E$ET$ETF
$ETid$ET$E$ET+$ET$ETF
$ETid$ET$ETF*$ETF$ETid$ET$E
$
id+id*id$id+id*id$id+id*id$
id+id*id$+id*id$+id*id$+id*id$id*id$id*id$
id*id$*id$*id$id$id$$$
$
E->TET->FT
F->id
T->E->+TE
T->FT
F->idT->*FT
F->id
T->
E->
Nonterminals Id + * ( ) $EET
TF
E->TE
T->FT
F->id
E->+TE
T->
T->*FT
E->TE
T->FT
F->(E)
E->
T->
E->
T->
-
8/3/2019 Top Downandbotttomup
16/23
WhatdoesLL(1)mean?
ThefirstLinLL(1)standsforscanningtheinputfromlefttoright,thesecondL
isforproducingaleftmostderivation,andthe1forusingoneinputsymboloflookaheadateachsteptomakeparsingactiondecisions.
NoambiguousorleftrecursivegrammarisLL(1).
LL(1)
Grammars
-
8/3/2019 Top Downandbotttomup
17/23
Thereremainsaquestionofwhatshouldbedonewhenaparsingtablehasmultiple-definedentries.
Onesolutionistotransformthegrammarbyeliminatingallleftrecursionandthenleftfactoringwhenpossible,butnotallgrammarscanyieldanLL(1)grammaratall.
Themaindifficultyinusingapredictiveparsingisinwritingagrammarforthesourcelanguagesuchthatapredictiveparsercanbeconstructedfromthegrammar.
Toalleviatesomeofthedifficulty,onecanuseaoperatorprecedence,orevenbettertheLRparser,thatprovidesboththebenefitsofpredictiveparsingandoperatorprecedenceautomatically.
BACK
LL(1)
Grammars
-
8/3/2019 Top Downandbotttomup
18/23
Whendoesanerrorpossiblyoccur?
-AnerrorisdetectedwhentheterminalonthetopofthestackdoesnotmatchthenextinputsymbolorwhenthenonterminalAisonthetopofthestack,aisthenextinputsymbol,andtheparsingtableentryM[A,a]isempty.
Howcanwedealwitherrors?
Panic-modeerrorrecoveryisbasedontheideaofskippingsymbolsontheinputuntilatokeninaselectedsetofsynchtokensappears.
ErrorRecovery
-
8/3/2019 Top Downandbotttomup
19/23
Howdoesitwork?
Usingfollowandfirstsymbolsassynchronizingtokensworkswell.TheparsingtablewillbefilledwithsynchtokensobtainedfromtheFOLLOWsetofthenonterminal.
WhenaparserlooksupentryM[A,a]andfindsitblank,thenaisskipped.Iftheentryissynch,thenthenonterminalispoppedinanattempttoresumeparsing.
ErrorRecovery
-
8/3/2019 Top Downandbotttomup
20/23
Back
Nonterminals Id + * ( ) $EET
TF
E->TE
T->FT
F->id
E->+TEsynch
T->synch
T->*FTsynch
E->TE
T->FT
F->(E)
synch
E->synch
T->synch
synch
E->synch
T->synchTACK INPUT ACTION
$E$E$ET$ETF
$ETid$ET$ETF*$ETF$ET$E$ET+$ET$ETF$ETid$ET$E$
)id*+id$id*+id$
id*+id$id*+id$
id*+id$*+id$*+id$+id$+id$+id$+id$id$id$id$$$$
Error,skip)IdisinFIRST(E)
Error,M[F,+1=synchFhasbeenpopped
-
8/3/2019 Top Downandbotttomup
21/23
AnothererrorrecoveryprocedureisthePhrase-levelRecovery.Thisisimplementedbyfillingintheblankentries
intheparsingtablewithpointerstoerrorroutines.Theseroutinescanalsopopsymbolsfromthestack,change,insertordeletesymbolsontheinput,andissueappropriateerrormessages.Thealterationofstack
symbolsisveryquestionableandrisky.
BACK
ErrorRecovery
-
8/3/2019 Top Downandbotttomup
22/23
Ageneralstyleofbottomupparsingwillbeintroduced,itistheshift-reduceparsing.
Shiftreduceparsingworksbasedonitsname,ShiftandReduce,sowheneverthestackholdssymbolsthat
cannotbereducedanymore,weshiftanotherinput,andwhenitmatches,wereduce.
BottomUpParsing
-
8/3/2019 Top Downandbotttomup
23/23
STACK INPUT ACTION1) $2) $id13) $E
4) $E+5) $E+id26) $E+E7) $E+E*8) $E+E*id39) $E+E*E10)$E+E11)$E
id1+id2*id3$+id2*id3$+id2*id3$
id2*id3$*id3$*id3$id3$
$$$$
ShiftReducebyE->id
ShiftShiftReducebyE->idShiftShiftReducebyE->idReducebyE->E*EReducebyE->E+E
ACCEPT
BottomUpParsing