0% found this document useful (0 votes)
18 views38 pages

M2 - P4 LR Parser

Module 2 covers syntax analysis with a focus on bottom-up parsing techniques, particularly LR parsing, which efficiently handles a wide range of context-free grammars. It explains the construction of parsing tables, the computation of closure and goto functions, and provides examples of Simple LR (SLR) parsing. Additionally, it includes exercises for constructing SLR parsing tables for various grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views38 pages

M2 - P4 LR Parser

Module 2 covers syntax analysis with a focus on bottom-up parsing techniques, particularly LR parsing, which efficiently handles a wide range of context-free grammars. It explains the construction of parsing tables, the computation of closure and goto functions, and provides examples of Simple LR (SLR) parsing. Additionally, it includes exercises for constructing SLR parsing tables for various grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Module 2 – Syntax Analysis

Bottom Up Parsing
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. a + b $ INPUT

X
Y LR parsing
program OUTPUT
Z
$

Parsing Table
Action Goto
Parsing Methods
Parsin
g

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive
parsing) SLR
LL(1)
CLR
Recursi
ve LALR

descen
t
Computation of closure & goto function
Closure(I):
• If ‘I’ is a set of items for a grammar, G then closure of I (closure (I)) is
set of items constructed from I by applying following 3 rules:
1. Construct augmented grammar.
2. Initially, every item in I is added to closure(I).
3. If A → α • B β is in closure (I) and B ⟶ ४ is a production, then add the item B
⟶ •४ to I, if it is not already in existence, we apply this rule until no more new
items can be added to closure(I).

Example:
E E+T | T
T T*F | F
F (E)| id
Computation of closure & goto function
Goto(I,X)
• The function goto can be defined as,
• If there is a production A → α • B β then,
goto (A → α • B β, B) = A → α B • β.
• Simply shifting of “•” one position ahead over grammar symbol.
Example:
E E+T | T
T T*F | F
F id | (E)
Steps to construct Simple LR (SLR)
parser
1. Construct Canonical set of LR(0) items
2. Construct SLR parsing table
3. Parse the input string
Example: SLR(1)- simple LR
S-->AA
A-->aA | S AA . 5
S’ S. 𝑰𝟏 A a .
b 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A
𝑰𝟐
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) S A . ,2 𝑎 )
A. aA 3

, 𝐴 𝐼
𝐼( 0 AA. aA 𝑜(
A. b
S’S S’.S
𝑜 𝑡
SAA S. AA 𝑡𝑜 𝐺
𝑜
A. aA 𝐺
A. b A b.
AaA
𝐺𝑜 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) 4

,3 𝐴
Ab A. b
𝑡𝑜 (𝑜 𝐼 A aA .
LR(0) item
(𝐼 𝑡
3 6
Augmented set
Grammar 0 , A a . 𝐺𝑜
𝑎) A
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A. aA 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎) A a.
A
A b. 4 A. b 𝐺𝑜 A. aA 3

𝑡𝑜 A. b
(𝐼
3,
𝑏)
A b. 4
Rules to construct SLR parsing table
1. Construct , the collection of sets of LR(0) items for
2. Stateis 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 a 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.
Example: SLR(1)- simple LR
S AA . 5
Action Go to
S’ S. 𝑰𝟏 A a .
𝑰𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A Item a b $ S A
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) S A . ,2 𝑎 )
A. aA 3 set
, 𝐴 𝐼 0 S3 S4 1 2
S’. S 𝐼( 0 AA. aA 𝑡𝑜 ( A. b

S. AA 𝑡𝑜 𝐺𝑜 1 Accept
𝑜
A. aA 𝐺
A. b A b. 2 S3 S4 5
𝐺𝑜 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) 4

,3 𝐴 3 S3 S4 6
A. b
𝑡𝑜 (𝑜 𝐼 A aA . 4 R3 R3 R3
(𝐼 𝑡
3 6

0 , A a . 𝐺𝑜 5 R1
𝑎) A
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A. aA 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎) A a . 6 R2 R2 R2
A
A b. 4 A. b 𝐺𝑜 A. aA 3

𝑡𝑜 A. b S-->S’ -------(0)


(𝐼
3, S-->AA -------(1)
𝑏) A-->aA --------(2)
A b. 4
A-->b ---------(3)
Parse the string aabb using SLR parser Parsing
Table
Stack Input Action
0 aabb$ Shift I0->a,
S3
0a3 abb$ Shift I3 ->a,
S3
0a3a3 bb$ I3->b, S4
0a3a3b b$ I4->b, R3 Pop 4,
4 goto(I3,A)=6
0a3a3A b$ I6->b, R2 S-->S’ -------(0)
6 S-->AA -------(1)
A-->aA --------(2)
0a3A6 b$ I6->b, R2 A-->b ---------(3)
0A2 b$ I2->b, s4
Example:2
Construct SLR parsing table for the following Step 2: Closure operation
grammar:
S-->Aa | bAc| Bc | bBa
S’-->.S
A-->d S-->.Aa
B-->d
S-->.bAc
Solution:
Step 1: Grammar Augmentation S-->.Bc
S’-->S … rule 0 S-->.bBa
S-->Aa … rule 1
A-->.d
S-->bAc … rule 2
S-->Bc …. rule 3 B-->.d
S-->bBa ….rule 4
A-->d …. rule 5
Cont…
Cont… Canonical Form
Cont… Parsing table
Exercises
1. Construct SLR parsing table for the following grammar:
E→E+T|T
T→T*F|F
F → id
2. Construct SLR parsing table for the following grammar and parse
the input string a * b + a:
E → E + T|T
T → TF|F
F → F*| a | b
3. Construct SLR parsing table for the following grammar
S→L=R|R
L → * R | id
R→L
Parsing Methods
Parsin
g

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursi
ve LALR

descen
How to calculate look ahead?
How to calculate look ahead?
SCC
CcC | d
Closure(I)
S’.S,$ S’  . S , $
S.CC, $ A  . X ,
C.cC, c|d
C.d, c|d
S  . C C , $
A  . X ,
Example: CLR(1)- Canonical LR –
LR(0)+LOOKAHEAD
5 S AA. , 6
, 𝐴 ) A aA.,$ 9
S’ S.,
𝑰𝟏 $ A a.A,$
𝑡𝑜 ( 𝐼6 6

𝑎)
$
𝑰 𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴)
A. aA, 𝐺𝑜 𝐼 6 , 𝑎 ) A a.A,$
𝑜 𝑡𝑜 (

𝐼2,
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) 𝐴) S A.A, $A. b,
𝐺𝑜 𝑡𝑜 ( 𝐼 𝐺 A. aA,
$A. b,

𝑡𝑜 (
S’.S, 0𝐼 , $A.aA, $ 6 , 𝑏) A $
$ ( $ b. ,S
𝐺𝑜
S.AA,$ 𝑡𝑜 A. b, A b. ,
7 7

A.aA, a| 𝑜 )
𝐺 $ 𝐺𝑜𝑡𝑜(𝐼2,𝑏) $
b
A.b, a| ,3 𝐴 8

b ( 𝐼 A aA.,a| LR(1) item


𝑡𝑜
𝐺𝑜

Augment 3
b set
ed Aa.A, a| 𝐺𝑜
𝑡𝑜 (

3
grammar b
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b
𝐼0,

A. b, a| A.aA , a|
𝐺 𝑜
𝑎)

A b., a|b 4 b b
𝑡𝑜 A.b , a|b
S-->AA ( 𝐼3
A-->aA | , 𝑏 A b., a|
)
b 4 b
Example: CLR(1)- canonical LR
5 S AA. , 6
, 𝐴 ) A aA.,$ 9
S’ S.,
𝑰𝟏 $ A a.A,$
𝑡𝑜 ( 𝐼6 6

𝑎)
$
𝑰 𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴)
A. aA, 𝐺𝑜 𝐼 6 , 𝑎 ) A a.A,$
𝑜 𝑡𝑜 (

𝐼2,
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) 𝐴) S A.A, $A. b,
𝐺𝑜 𝑡𝑜 ( 𝐼 𝐺 A. aA,
$A. b,

𝑡𝑜 (
S’.S, 0𝐼 , $A.aA, $ 6 , 𝑏) A $
$ ( $ b. ,S
𝐺𝑜
S.AA,$ 𝑡𝑜 A. b, A b. ,
7 7

A.aA, a| 𝑜 ) Item Action Go to


𝐺 $ 𝐺𝑜𝑡𝑜(𝐼2,𝑏) $
b
A.b, a| ,3 𝐴 8
set
a b $ S A
b (𝑜 𝐼 A aA.,a|
𝑡
𝐺𝑜

3
b 0 S3 S4 1 2
Aa.A, a| 𝐺𝑜 1 Accept
𝑡𝑜 (

3
b 2 S6 S7 5
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b
𝐼0,

3 S3 S4 8
A. b, a| A.aA , a|
𝐺 𝑜 4 R3 R3
𝑎)

A b., a|b 4 b b
𝑡𝑜 A.b , a|b 5 R1
S-->AA ( 𝐼3 6 S6 S7 9
A-->aA | , 𝑏 A b., a| 7 R3
) 8 R2 R2
b 4 b 9 R2
Example 2
Construct CLR parsing table for the following grammar:
SCC
CcC | d
Exercise
Construct CLR parsing table for the following grammar:
S  aAd | bBd | aBe | bAe
A c
Bc
Solution
Cont…
Parsing Methods
Parsin
g

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursi
ve LALR

descen
t
Example: LALR(1)- look ahead LR
, 𝐴)
5 S AA. , 6 A aA.,$ 9
S’ S.,
𝑰𝟏 $ A a.A,$ ( 𝐼 6
𝐺𝑜 𝑡𝑜
6

𝑎)
$
𝑰𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A. aA,
( 𝐼 6 , 𝑎 ) A a.A,$
𝑡 𝑜

𝐼2,
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) $A. b, 𝐺𝑜 A. aA,
,0 𝐴 S A.A, 𝐺𝑜 𝑡𝑜 ( 𝐼 $A. b,

𝑡𝑜 (
S’.S, $
$ 6 , 𝑏) A b. , $
$ ( 𝐼 A.aA, $

𝐺𝑜
S.AA,$ 𝑡𝑜 $A. b, A b. ,
7 7

A.aA, a| 𝑜 )
𝐺 $ 𝐺𝑜𝑡𝑜(𝐼2,𝑏) $
b
A.b, a| ,3 𝐴 8
36 CLR
b (𝑜 𝐼 A aA.,a|
Aa.A, a|b|$
𝑡
𝐺𝑜

3
b
Aa.A, a| 𝐺𝑜 A.aA , a|b|$
𝑡𝑜 (

3
b A. b, a|b|$
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b
𝐼0,

A. b, a| A.aA , a|
𝐺 𝑜
𝑎)

47
A b., a|b 4 b b
𝑡𝑜 A.b , a|b A b., a|b|$
S-->AA (𝐼
3,
A-->aA | 𝑏) A b., a| 89
A aA.,a|b|
b 4 b $
Example: LALR(1)- look ahead LR

Item Action Go to
set
a b $ S A
Item Action Go to
0 S3 S4 1 2 set a b $ S A
1 Accept
2 S6 S7 5 0 S36 S47 1 2
3 S3 S4 8 1 Accept
4 R3 R3 2 S36 S47 5
36 S36 S47 89
5 R1 47 R3 R3 R3
6 S6 S7 9 5 R1
7 R3 89 R2 R2 R2
8 R2 R2
9 R2
CLR Parsing Table LALR Parsing Table
Construct LALR (1) Parsing table for the grammar.
S → A a|b A c|dc|bda
A→d
Parse input string "bdc" using LALR Parsing Table.

Step1− Construct Augmented Grammar


(0) S′ → S
(1) S → A a
(2) S → b A c
(3) S → d c
(4) S → b d a
(5) A → d

Step2−Find the canonical set of


LR (1) items for the Grammar
Cont…
Step3 − LALR Parsing Step4 − Parsing of String "bdc"
Table Stack Input String Action
$0 bdc $ Shift 3
$0b3 dc $ Shift 7
$0b3d7 c$ Reduce by A → d
$0b3A6 c$ Shift 9
$0b3A6c $ Reduce by S → b Ac
9
$0s1 $ accept
First/First conflict
in predictive parser
first/follow conflicts in predictive
parser
• Unambiguous grammars can also have first/follow conflicts. Here's an
example:

• S → Ab

• A→b|ε

• This grammar can produce two strings, bc and bbc, and it's
unambiguous. However, there's a FIRST/FOLLOW conflict on
production A → b | ε, because b ∈ FIRST(A) and b ∈ FOLLOW(A) as well.
• So the intersection of FIRST and FOLLOW of NT(A) is not null set and
this leads to FIRST/FOLLOW conflict
Points to be noted - SLR
• In case of two reductions, if the
follow of both the reduced
productions have something
common then it will result in
multiple entries in table hence not
SLR.
• In case of one shift and one
reduction, if there is a GOTO
operation from that state on a
terminal which is the follow of the
reduced production than it will
result in multiple entries hence
not SLR.
How do you identify whether a grammar is LL(1), LR(0), or SLR(1)?
X → Yz | a
Y → bZ | ε
Z→ε
FIR(X)={a,z}, FIR(Y)={b, Ԑ}, FIR(Z)={Ԑ}, FOL(X)={$}, FOL(Y}={z}, FOL(Z)={z}
To check if a grammar is LL(1), one option is to construct the LL(1) parsing table and check for
any First / First or First / Follow conflicts.
To check if a
grammar a b z $
is LR(0) or
X a Yz Yz
SLR(1),
we begin by Y bZ ε
building
up all of the Z ε
LR(0)
From this, we can see that the grammar is not LR(0) because there is a
configurating
shift/reduce
sets conflicts in state (1). Specifically, because we have the shift item
Yfor
→ .bZ
the and Y → . (r4), we can't tell whether to shift b or reduce the empty
string
grammar.
Points to be noted - CLR

Consider the following grammar


S -> AaAb | BbBa
A -> ε
B -> ε

Augmented grammar –
S’ -> S
S -> AaAb
S -> BbBa
A -> ε
B -> ε
GOTO graph for this grammar will be -
Cont…
• If a state has two reductions and both have same lookahead then
it will in multiple entries in parsing table thus a conflict.
• If a state has one reduction and there is a shift from that state
on a terminal same as the lookahead of the reduction then it will
lead to multiple entries in parsing table thus a conflict.

1. Even though CLR parser does not


have RR conflict but LALR may contain
RR conflict.
2. If number of states LR(0) = n1,
number of states SLR = n2, number of
states LALR = n3, number of states
CLR = n4 then, n1 = n2 = n3 <= n4

You might also like