Академический Документы
Профессиональный Документы
Культура Документы
SYNTAXANALYSIS
BOTTOMUPPARSER
TopdownversusBottomupParsing
Topdown:
Recursivedescentparsing
LL(k)parsing
Toptodownandleftmostderivation
Expandingfromstartingsymbol(top)tograduallyderivethe
inputstring
Canuseaparsingtabletodecidewhichproductionto
usenext
Thepowerislimited
ManygrammarsarenotLL(k)
Leftrecursioneliminationandleftfactoringcanhelpmake
manygrammarsLL(k),butafterrewriting,thegrammarcanbe
veryhardtocomprehend
Spaceefficient
Easytobuildtheparsetree
TopdownversusBottomupParsing
Bottomup:
Alsoknownasshiftreduceparsing
LRfamily
Precedenceparsing
Shift:allowshiftinginputcharacterstothestack,waitingtill
amatchingproductioncanbedetermined
Reduce:onceamatchingproductionisdetermined,reduce
Followtherightmostderivation,inareversedway
Parsefrombottom(theleavesoftheparsetree)andworkupto
thestartingsymbol
Duetotheaddedshift
Morepowerful
Canhandleleftrecursivegrammarsandgrammarswithleft
factors
Lessspaceefficient
BasicIdea
ConstructLeavesfirstthenmoveuptotheroot.
Example:
LetGbeagrammarsuchthat
E E+T|T
T T*F|F
F id
Lettheinputstringbew=id*id
Thestepstoconstructthetreewillbeasfollows:
1st step:F*id
id
id
3rd step: T*F
Fid
id
4th step: T
T*F
Fid
id
PropertiesofBottomUpParsing
Abottomupparserbeginswithparsetreesleaves,
andmovestowarditsroot
Abottomupparsertracesarightmostderivationin
reverse
Abottomupparserusesagrammarruletoreplace
therulesRHSwithitsLHS
(Fig.4.5&Fig.4.6)
Reduction
Definition:Reverseprocessofderivation.
Ex. =
andifA
Then =A
Handle
Definition:Asubstringwithinastringwhichmatcheswiththebodyof
aproduction.Itshouldcomplywiththerightmostderivation.
Ex. S
A
Then A
Thus isavalidhandle.
HandlePruning
=0 1 S
Challenges:
1) Whichproductionstouse.
2) Whentoreduce.
ShiftReduceParser
InputString Handle Production
id*id id Fid
F*id F TF
T*id T FT
T*F F Fid
T T*F TT*F
E T ET
MajorActions
1) Shift
2) Reduce
Stack Partialinputstring
$ x1 x2 x3 $
a) Findahandlefromthestringthatuhaveinthestack
b) Reduce.