Lecture 9
Lecture 9
Bottom – Up parsers
Lecture 9
Bottom-Up Parsing
A bottom-up parse corresponds to the construction of
a parse tree for an input string beginning at the leaves
(the bottom) and working up towards the root (the
top).
E -> E + ( E ) | int
E E
E E
E E E
A rightmost derivation E E E
in reverse
int + ( int ) + ( int )
Each step is a
reduction.
BITS Pilani, Hyderabad Campus
Terminologies
Right-Sentential Form
Sentential Form that has Been Derived in a Rightmost Derivation
Grammar:
E -> E + (E) | int
S aABe
aAde
aAbcde
abbcde
BITS Pilani, Hyderabad Campus
Handle Pruning
What bottom-up really means...
abbcde aAbcde
aAbcde aAde
aAde aABe
aABe S
A
What Does
α Signify? α β w What Does
What Does
w Contain?
Input Processed β Represent?
Still on Parsing Stack Candidate Handle to Input yet to be
be Reduced Consumed
BITS Pilani, Hyderabad Campus
General Processing of BUP
Basic mechanisms
“Shift”
“Reduce”
Accept
Error
Basic data-structure
A stack of grammar symbols (Terminals and Non-Terminals).
Basic idea
Shift input symbols on the stack until it has the entire handle of the last
rightmost reduction.
When the body of the last RM reduction is on Stack, reduce it by
replacing the body by the right-hand-side of the Production Rule.
When only start symbol is left (entire input is processed).
We are done.
BITS Pilani, Hyderabad Campus
Stack Input
Example
Rule to Reduce
with
Handle
Handle
$aAd e$ Reduce
Rule to
Reduce $aAB e$ Shift
with
$aABe $ Reduce
$S $ Accept
BITS Pilani, Hyderabad Campus
Stack Input
Example
$ abbcde$ Shift
$a bbcde$ Shift
$ab bcde$ Reduce
$aA bcde$ Shift
$aAb cde$ Shift
$aAbc de$ Reduce
$aA de$ Shift
$aAd e$ Reduce
$aAB e$ Shift
$aABe $ Reduce
$S $ BITS Accept
Pilani, Hyderabad Campus
Stack Input
Example
$ abbcde$ Shift
$a bbcde$ Shift
$ab bcde$ Reduce
$aA bcde$ Shift
$aAb cde$ Shift
$aAbc de$ Reduce
$aA de$ Shift
$aAd e$ Reduce
$aAB e$ Shift
$aABe $ Reduce
$S $ Accept
BITS Pilani, Hyderabad Campus
What are Possible Grammar
Conflicts?
• Shift-Reduce (S/R) Conflict:
– Content of Stack and Reading Current Input
– More than One Option of What to do Next
Grammar
LR(0)
automata