LALR (1) Parsing
LALR (1) Parsing:
- LALR refers to the lookahead LR. To construct the LALR (1) parsing table, we use the canonical collection of LR (1) items.
- In the LALR (1) parsing, the LR (1) items which have same productions but different look ahead are combined to form a single set of items
- LALR (1) parsing is same as the CLR (1) parsing, only difference in the parsing table.
Example
LALR ( 1 ) Grammar
S β AA
A β aA
A β b
Add Augment Production, insert 'β’' symbol at the first position for every production in G and also add the look ahead.
S` β β’S, $
S β β’AA, $
A β β’aA, a/b
A β β’b, a/b
I0 State:
Add Augment production to the I0 State and Compute the ClosureL
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, $
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
A β β’b, a/b
I1= Go to (I0, S) = closure (S` β Sβ’, $) = S` β Sβ’, $
I2= Go to (I0, A) = closure ( S β Aβ’A, $ )
Add all productions starting with A in I2 State because "β’" is followed by the non-terminal. So, the I2 State becomes
I2= S β Aβ’A, $
A β β’aA, $
A β β’b, $
I3= Go to (I0, a) = Closure ( A β aβ’A, a/b )
Add all productions starting with A in I3 State because "β’" is followed by the non-terminal. So, the I3 State becomes
I3= A β aβ’A, a/b
A β β’aA, a/b
A β β’b, a/b
Go to (I3, a) = Closure (A β aβ’A, a/b) = (same as I3)
Go to (I3, b) = Closure (A β bβ’, a/b) = (same as I4)
I4= Go to (I0, b) = closure ( A β bβ’, a/b) = A β bβ’, a/b
I5= Go to (I2, A) = Closure (S β AAβ’, $) =S β AAβ’, $
I6= Go to (I2, a) = Closure (A β aβ’A, $)
Add all productions starting with A in I6 State because "β’" is followed by the non-terminal. So, the I6 State becomes
I6 = A β aβ’A, $
A β β’aA, $
A β β’b, $
Go to (I6, a) = Closure (A β aβ’A, $) = (same as I6)
Go to (I6, b) = Closure (A β bβ’, $) = (same as I7)
I7= Go to (I2, b) = Closure (A β bβ’, $) = A β bβ’, $
I8= Go to (I3, A) = Closure (A β aAβ’, a/b) = A β aAβ’, a/b
I9= Go to (I6, A) = Closure (A β aAβ’, $) A β aAβ’, $
If we analyze then LR (0) items of I3 and I6 are same but they differ only in their lookahead.
I3 = { A β aβ’A, a/b
A β β’aA, a/b
A β β’b, a/b
}
I6= { A β aβ’A, $
A β β’aA, $
A β β’b, $
}
Clearly I3 and I6 are same in their LR (0) items but differ in their lookahead, so we can combine them and called as I36.
I36 = { A β aβ’A, a/b/$
A β β’aA, a/b/$
A β β’b, a/b/$
}
The I4 and I7 are same but they differ only in their look ahead, so we can combine them and called as I47.
I47 = {A β bβ’, a/b/$}
The I8 and I9 are same but they differ only in their look ahead, so we can combine them and called as I89.
I89 = {A β aAβ’, a/b/$}
Drawing DFA:

LALR (1) Parsing table:

Labels: CD Unit-2, Compiler Design
Discussion & Queries (<$I18NNumComments$>):
<$CommentPager$>
- <$I18NAtCommentTimeWithPermalink$>, <$I18NCommentAuthorSaid$>
<$CommentPager$>
<$BlogCommentBody$>
<$BlogCommentDeleteIcon$>