You are on page 1of 31

BITS Pilani

BITS Pilani CS F363 Compiler Construction


Hyderabad Campus
BITS Pilani
Hyderabad Campus

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).

➢ Also called LR parsing


➢ L means that tokens are read left to right
➢ R means that it constructs a rightmost derivation !

BITS Pilani, Hyderabad Campus


Bottom-Up Parsing

➢ LR parsers don’t need left-factored grammars and can


also handle left-recursive grammars

➢ Consider the following grammar:

➢ E -> E + ( E ) | int

➢ Consider the string: int + ( int ) + ( int )

BITS Pilani, Hyderabad Campus


Bottom-Up Parsing

➢ LR parsing reduces a string to the start symbol by


inverting productions:

str = w input string


repeat
– Identify  in str such that A →  is a production
(i.e., str =   )
– Replace  by A in str (i.e., str w =  A )
until str = S (the start symbol) OR all possibilities are
exhausted

BITS Pilani, Hyderabad Campus


Bottom-Up Parsing
E -> E + (E) | int

int + (int) + (int)

int + ( int ) + ( int )

BITS Pilani, Hyderabad Campus


Bottom-Up Parsing
E -> E + (E) | int

int + (int) + (int)


E + (int) + (int)

int + ( int ) + ( int )

BITS Pilani, Hyderabad Campus


Bottom-Up Parsing
E -> E + (E) | int

int + (int) + (int)


E + (int) + (int)
E + (E) + (int)

E E

int + ( int ) + ( int )

BITS Pilani, Hyderabad Campus


Bottom-Up Parsing
E -> E + (E) | int

int + (int) + (int)


E + (int) + (int)
E + (E) + (int)
E + (int) E

E E

int + ( int ) + ( int )

BITS Pilani, Hyderabad Campus


Bottom-Up Parsing
E -> E + (E) | int

int + (int) + (int)


E + (int) + (int)
E + (E) + (int)
E + (int)
E
E + (E)

E E E

int + ( int ) + ( int )

BITS Pilani, Hyderabad Campus


Bottom-Up Parsing

int + (int) + (int) E


E + (int) + (int)
E + (E) + (int)
E + (int)
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

➢ Handle: substring of a Right Sentential Form that


➢ Appears on Right Hand Side of Production Rule and
➢ Whose reduction represents one step along the reverse of
a rightmost derivation.

BITS Pilani, Hyderabad Campus


Handle Pruning
➢ A rightmost derivation can be obtained by handle
pruning.
➢ Consider the following grammar:
S→aABe
A → Abc | b
B→d

❑ S  aABe
 aAde
 aAbcde
 abbcde
BITS Pilani, Hyderabad Campus
Handle Pruning
 What bottom-up really means...

abbcde aAbcde

BITS Pilani, Hyderabad Campus


Handle Pruning

aAbcde aAde

BITS Pilani, Hyderabad Campus


Handle Pruning

aAde aABe

BITS Pilani, Hyderabad Campus


Handle Pruning

aABe S

BITS Pilani, Hyderabad Campus


What’s Going on in Parse
Tree?

Consider Right Sentential Form: αβw and Rule A → β


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

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
Handle Rule to Reduce with $aAd e$ Reduce
$aAB e$ Shift
$aABe $ Reduce
$S $ Accept
BITS Pilani, Hyderabad Campus
Example Stack Input
$ abbcde$ Shift
$a bbcde$ Shift
$ab bcde$ Reduce
$aA bcde$ Shift
$aAb cde$ Shift
$aAbc de$ Reduce
$aA de$ Shift

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

stmt → if expr then stmt


| if expr then stmt else stmt
| other

Consider Stack as below with input of token else


$ …. if expr then stmt

– Do we Reduce if expr then stmt -> stmt


– Do we Shift “else” onto Stack?
BITS Pilani, Hyderabad Campus
What are Possible Grammar
Conflicts?
• Reduce-Reduce (R/R) Conflict:
stmt → id ( parameter_list )
parameter_list → parameter_list, parameter
parameter → id
expr → id ( expression_list )
| id
expression_list → expression_list, expr
| expr
• Consider Stack as below with input of token
$ …. id (id, … , id) ….
– Do we Reduce to stmt?
– Do we Reduce to expr?

BITS Pilani, Hyderabad Campus


Key Question: To Shift or to
Reduce?

BITS Pilani, Hyderabad Campus


LR(0) item

BITS Pilani, Hyderabad Campus


LR(0) item

BITS Pilani, Hyderabad Campus


LR(0) automaton example

Grammar

LR(0)
automata

These sets of LR(0)


items computation
needs two functions:
Closure
GoTo
BITS Pilani, Hyderabad Campus
Thank you

BITS Pilani, Hyderabad Campus

You might also like