Uploaded by itscris7ron

15-LALR Parsing-06-06-2023

advertisement
Module 2 – Syntax Analysis
Bottom Up Parsing
Parsing Methods
Parsing
Top down parsing
Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
Parsing)
Operator precedence
LR parsing
SLR
LL(1)
CLR
Recursive
descent
LALR
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 𝜷 , 𝒂
Lookahead = First 𝜷𝒂
C.cC, c|d
First $
C.d, c|d
=$
S 
. C C , $
A  𝜶
. X 𝜷 , 𝒂
Lookahead = First 𝜷𝒂
First 𝐶$
= 𝒄, 𝒅
Example: CLR(1)- Canonical LR – LR(0)+LOOKAHEAD
𝑰5
S’ S., $ 𝑰𝟏
𝑰𝟎
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
S’.S,$
S.AA,$
A.aA, a|b
A.b, a|b
Augmented
grammar
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
A b., a|b 𝑰4
𝑰6
S AA. ,$
𝑰𝟐
A a.A,$
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
A. aA,$
A.aA, $
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝑰3
Aa.A, a|b
𝑰7
𝑰7
A b. ,$
A b. ,S
A. b, $
𝑰8
A aA.,a|b
𝑰3
A.aA , a|b
A.b , a|b
S-->AA
A-->aA | b
𝑰6
A. aA,$
A.aA ,a|b 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A a.A , a|b
A. b, a|b
𝑰9
A a.A,$
A. b, $
S A.A,$
A. b, $
A aA.,$
𝑰4 A b., a|b
LR(1) item set
Example: CLR(1)- canonical LR
𝑰5
S’ S., $ 𝑰𝟏
𝑰𝟎
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
S’.S,$
S.AA,$
A.aA, a|b
A.b, a|b
𝑰6
S AA. ,$
𝑰𝟐
A a.A,$
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝑰3
Aa.A, a|b
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
A b., a|b 𝑰4
A. aA,$
A.aA, $
𝑰7
𝑰7
A b. , $
𝑰8
A aA.,a|b
𝑰3
A.aA , a|b
A.b , a|b
S-->AA
A-->aA | b
𝑰6
A. aA,$
A.aA ,a|b 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A a.A , a|b
A. b, a|b
𝑰9
A a.A,$
A. b, $
S A.A,$
A. b, $
A aA.,$
𝑰4 A b., a|b
A b. ,S
Item
set
0
1
2
3
4
5
6
7
8
9
A. b, $
Action
a
S3
b
S4
Go to
$
S
1
A
2
Accept
S6
S3
R3
S7
S4
R3
5
8
R1
S6
S7
9
R3
R2
R2
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
Parsing
Top down parsing
Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
Parsing)
Operator precedence
LR parsing
SLR
LL(1)
CLR
Recursive
descent
LALR
Example: LALR(1)- look ahead LR
𝑰5
S’ S., $ 𝑰𝟏
𝑰𝟎
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
S’.S,$
S.AA,$
A.aA, a|b
A.b, a|b
𝑰6
S AA. ,$
𝑰𝟐
A a.A,$
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
A.aA, $
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝑰3
Aa.A, a|b
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
A b., a|b 𝑰4
𝑰7
𝑰7
A b. ,$
𝑰8
A aA.,a|b
𝑰3
A.aA , a|b
A.b , a|b
S-->AA
A-->aA | b
𝑰6
A. aA,$
A.aA ,a|b 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A a.A , a|b
A. b, a|b
𝑰9
A a.A,$
A. aA,$
A. b, $
S A.A,$
A. b, $
A aA.,$
𝑰4 A b., a|b
A b. ,$
𝑰36
A. b, $
CLR
Aa.A, a|b|$
A.aA , a|b|$
A. b, a|b|$
𝑰47
A b., a|b|$
𝑰89
A aA.,a|b|$
Example: LALR(1)- look ahead LR
Item
set
0
1
2
3
4
5
6
7
8
9
Action
a
S3
b
S4
Go to
$
S
1
A
2
Accept
S6
S3
R3
S7
S4
R3
5
8
R1
S6
S7
9
R3
R2
R2
Item
set
0
1
2
36
47
5
89
Action
a
b
$
S36 S47
Accept
S36 S47
S36 S47
R3 R3
R2
R2
5
89
R3
R1
R2
R2
CLR Parsing Table
Go to
S A
1 2
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…
Step4 − Parsing of String "bdc"
Step3 − LALR Parsing Table
Stack
Input String
Action
$0
bdc $
Shift 3
$0b3
dc $
Shift 7
$0b3d7
c$
Reduce by A → d
$0b3A6
c$
Shift 9
$0b3A6c9
$
Reduce by S → b Ac
$0s1
$
accept
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→ε
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.
b begin byzbuilding up
$ all of
To check if a grammar is LR(0) oraSLR(1), we
the LR(0)
X
a
Yz
Yz
configurating sets for the grammar.
Y
bZ
ε
Z
ε
From this, we can see that the grammar is not LR(0) because there is a shift/reduce
conflicts in state (1). Specifically, because we have the shift item X → .a and Y → ., we
can't tell whether to shift the a or reduce the empty string
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
Download