Unit-II (Part-B) Bottom Up Parsing Techniques
Unit-II (Part-B) Bottom Up Parsing Techniques
id + * $
id .> .> .>
+ <. .> <. .>
* <. .> .> .>
$ <. <. <. .>
Resulting graph:
gid fid
f* g*
g+ f+
f$ g$
From the previous graph we extract the
following precedence functions:
id + * $
f 4 2 4 0
g 5 1 3 0
Handles
• A handle of a string is a substring that matches
the right side of a production and whose
reduction to the non-terminal on the left side of
the production represents the previous right
sentential form.
$ w $
– Note:
• GOTO identifies groups of ‘Kernel items’ i.e., those
whose dots are not at extreme left.
• CLOSURE adds ‘Non-kernel items’ i.e., those whose
dots are at extreme left.
Construction of Canonical Collection
of Set of Items
• A canonical collection of LR(0) items for an
augmented grammar can be constructed using
CLOSURE and GOTO functions.
• A collection of set of items which corresponds
to the states of a DFA recognizing viable
prefixes is called as canonical collection.
• Therefore construction of a DFA involves
finding canonical collection of set of items.
Construction of Canonical Collection
of LR(0) items
• An LR (0) item is a production G with dot at
some position on the right side of the
production.
• LR(0) items is useful to indicate that how
much of the input has been scanned up to a
given point in the process of parsing.
• In the LR (0), we place the reduce node in the
entire row.
Construction of Canonical Collection
of LR(0) items
Given grammar:
S → AA
A → aA | b
Add Augment Production and insert '•'
symbol at the first position for every
production in G
S` →•S
S → •AA
A → •aA
A → •b
Construction of Canonical Collection
of LR(0) items
I0 State:
Add Augment production to the I0 State and
Compute the Closure
I0 = Closure (S` → •S)
Add all productions starting with S in to I0
State because "•" is followed by the non-
terminal. So, the I0 State becomes
I0 = S` → •S
S → •AA
Construction of Canonical Collection
of LR(0) items
Add all productions starting with "A" in
modified I0 State because "•" is followed by
the non-terminal. So, the I0 State becomes.
I0= S` → •S
S → •AA
A → •aA
A → •b
Construction of Canonical Collection
of LR(0) items
I1= Go to (I0, S) = closure (S` → S•) = S` → S•
Here, the Production is reduced so close the
State.
I1= S` → S•
I2= Go to (I0, A) = closure (S → A•A)
Add all productions starting with A in to I2
State because "•" is followed by the non-
terminal. So, the I2 State becomes
Construction of Canonical Collection
of LR(0) items
I2 =S→A•A
A → •aA
A → •b
Go to (I2,a) = Closure (A → a•A) = (same as I3)
Go to (I2, b) = Closure (A → b•) = (same as I4)
I3= Go to (I0,a) = Closure (A → a•A)
Add productions starting with A in I3.
A → a•A
A → •aA
A → •b
Construction of Canonical Collection
of LR(0) items
Go to (I3, a) = Closure (A → a•A) = (same as I3)
Go to (I3, b) = Closure (A → b•) = (same as I4)
I4= Go to (I0, b) = closure (A → b•) = A → b•
I5= Go to (I2, A) = Closure (S → AA•) = SA → A•
I6= Go to (I3, A) = Closure (A → aA•) = A → aA•
Construction of Canonical Collection
of LR(0) items
Drawing DFA:
The DFA contains the 7 states I0 to I6.
Construction of LR(0) Table
LR(0) Table
• If a state is going to some other state on a
terminal then it correspond to a Shift move.
• If a state is going to some other state on a
variable then it correspond to GO TO move.
• If a state contain the final item in the
particular row then write the reduce node
completely.
Construction of LR(0) Table
I2 S4 S5 r2 r2 7
I3 r4 r4 r4 S9 r4
I4 r6 r6 r6 r6 r6
I5 r7 r7 r7 r7 r7
I6 S4 S5 8 3
I7 r3 r3 r3 S9 r3
I8 S4 S5 r1 r1 7
I9 r5 r5 r5 r5 r5
Constructing SLR Parsing tables
Example 3:
Construct a SLR or LR(0) parsing table for the
following grammar:
S → cA | ccB
A → cA | a
B → ccB | b
Constructing SLR Parsing tables
Solution:
The augmented grammar G’ is
S’ → S
S → cA | ccB
A → cA | a
B → ccB | b
The transition diagram for the given grammar is
as follows:
Constructing SLR Parsing tables
Constructing SLR Parsing tables
Now we prepare a FOLLOW set for all non-terminals.
FOLLOW(S) = {$}
FOLLOW(A) = {$}
FOLLOW(B) = {$}
The productions are numbered as
(1) S → cA
(2) S → ccB
(3) A → cA
(4) A→a
(5) B → ccB
(6) B→b
Constructing SLR Parsing tables
So final SLR table is as follows:
ACTION table GOTO table
State a b c $ S A B
I0 S3 S4 S2 1
I1 Accept
I2 S3 S6 5
I3 r4
I4 r6
I5 r1 /r3
I6 S3 S4 S9 8 7
I7 r2 /r5 From table, there
I8 r3 occurs reduce-
reduce conflict. So,
I9 S3 S10 8
the given grammar
I10 S3 S4 S9 8 11 is not LR(0).
I11 r5
Canonical LR Parser (CLR)
• Every SLR grammar is unambiguous but there are many
unambiguous grammars that are not SLR.
• We define CLR (Canonical LR) parser to accommodate
extra information in the form of a terminal symbol, as a
second component in each item of state.
• The general form of item becomes [A → α.β, a], where
A → α.β is a production and ‘a’ is terminal symbol or
right end marker $. We call such an item as LR(1)
items.