LR parsing is an efficient bottom-up parsing technique that can parse large classes of context-free grammars. It works by scanning the input from left to right, constructing the rightmost derivation in reverse using k lookahead symbols. The key steps are:
1. Constructing LR(0) items and closure sets
2. Building the LR parsing table from the LR(0) items
3. Using the table to parse input strings based on lookahead symbols
The main differences between LR parsing techniques are the way lookahead is handled, with CLR using exact lookahead, SLR using limited lookahead, and LALR approximating lookahead.
1. Parsing Methods
Parsing
Top down parsing Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
Parsing)
LR parsing
Operator precedence
LALR
CLR
SLR
Recursive
descent
LL(1)
2. LR parser
• LR parsing is most efficient method of bottom up parsing which can be used to
parse large class of context free grammar.
• The technique is called LR(k) parsing:
1. The “L” is for left to right scanning of input symbol,
2. The “R” for constructing right most derivation in reverse,
3. The “k” for the number of input symbols of look ahead that are used in
making parsing decision.
LR parsing
program
INPUT
OUTPUT
a + b $
X
Y
Z
$
Parsing Table
Action Goto
3. Parsing Methods
Parsing
Top down parsing Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
parsing)
LR parsing
Operator precedence
LALR
CLR
SLR
Recursive
descent
LL(1)
5. Steps to construct SLR parser
1. Construct Canonical set of LR(0) items
2. Construct SLR parsing table
3. Parse the input string
6. Example: SLR(1)- simple LR
S AA
A aA | b
S’.S
S. AA
A. aA
A. b
S’ S.
S A . A
A. aA
A. b
A. aA
A. b
A a . A
A b.
A. aA
A. b
A a . A
A b.
A. aA
A. b
A a . A
A b.
S AA .
A aA .
𝑰𝟎
𝑰𝟏
𝑰𝟐
𝑰3 𝑰6
𝑰5
𝑰3
𝑰3
𝑰4
𝑰4
𝑰4
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎)
Augmented
grammar
LR(0) item set
7. Rules to construct SLR parsing table
1. Construct 𝐶 = { 𝐼0, 𝐼1, … … . 𝐼𝑛}, the collection of sets of LR(0) items for 𝐺’.
2. State 𝑖 is constructed from 𝐼𝑖. The parsing actions for state 𝑖 are determined as
follow :
a) If [ 𝐴 → 𝛼. 𝑎𝛽 ] is in 𝐼𝑖 and GOTO (𝐼𝑖 , 𝑎) = 𝐼𝑗 , then set 𝐴𝐶𝑇𝐼𝑂𝑁[𝑖, 𝑎] to “shift j”.
Here a must be terminal.
b) If [𝐴 → 𝛼. ] is in 𝐼𝑖, then set 𝐴𝐶𝑇𝐼𝑂𝑁[𝑖, 𝑎] to “reduce A 𝛼” for all a in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴);
here A may not be S’.
c) If [𝑆 → 𝑆. ] is in 𝐼𝑖, then set action [𝑖, $] to “accept”.
3. The goto transitions for state i are constructed for all non terminals A using
the𝑖𝑓 𝐺𝑂𝑇𝑂( 𝐼𝑖 , 𝐴 ) = 𝐼𝑗 𝑡ℎ𝑒𝑛 𝐺𝑂𝑇𝑂 [𝑖, 𝐴] = 𝑗.
4. All entries not defined by rules 2 and 3 are made error.
8. Example: SLR(1)- simple LR
Action Go to
Item
set
a b $ S A
0 S3 S4 1 2
1 Accept
2 S3 S4 5
3 S3 S4 6
4 R3 R3 R3
5 R1
6 R2 R2 R2
S’. S
S. AA
A. aA
A. b
S’ S.
S A . A
A. aA
A. b
A. aA
A. b
A a . A
A b.
A. aA
A. b
A a . A
A b.
A. aA
A. b
A a . A
A b.
S AA .
A aA .
𝑰𝟎
𝑰𝟏
𝑰𝟐
𝑰3 𝑰6
𝑰5
𝑰3
𝑰3
𝑰4
𝑰4
𝑰4
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎)
S AA
A aA | b
𝐹𝑜𝑙𝑙𝑜𝑤(𝑆) = {$}
𝐹𝑜𝑙𝑙𝑜𝑤(𝐴) = {𝑎, 𝑏, $}
9. Parsing Methods
Parsing
Top down parsing Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
Parsing)
LR parsing
Operator precedence
LALR
CLR
SLR
Recursive
descent
LL(1)
10. How to calculate look ahead?
How to calculate look ahead?
SCC
C cC | d
Closure(I)
S’.S,$
S.CC,
C.cC,
C.d,
S’ . S , $
A 𝜶 . X 𝜷 , 𝒂
Lookahead = First 𝜷𝒂
First $
= $
$
S . C C , $
A 𝜶 . X 𝜷 , 𝒂
Lookahead = First 𝜷𝒂
First 𝐶$
= 𝒄, 𝒅
c|d
c|d
13. Parsing Methods
Parsing
Top down parsing Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
Parsing)
LR parsing
Operator precedence
LALR
CLR
SLR
Recursive
descent
LL(1)