0% found this document useful (0 votes)
41 views12 pages

Compiler: Syntax Analysis Bottom Up Parser

A bottom-up parser begins by constructing the leaves of the parse tree first before moving up to the root. It follows a rightmost derivation in reverse order, using grammar rules to replace the right-hand side of productions with their left-hand side. The parser uses shift and reduce actions, shifting input symbols onto a stack and reducing handles found on the stack by applying grammar rules.

Uploaded by

Nick Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views12 pages

Compiler: Syntax Analysis Bottom Up Parser

A bottom-up parser begins by constructing the leaves of the parse tree first before moving up to the root. It follows a rightmost derivation in reverse order, using grammar rules to replace the right-hand side of productions with their left-hand side. The parser uses shift and reduce actions, shifting input symbols onto a stack and reducing handles found on the stack by applying grammar rules.

Uploaded by

Nick Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

COMPILER

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

2nd step: T*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.

You might also like