Homework 4: Programming Languages and Compilers (Cos 305) S - Aa - Bac - DC - Bda A - A
Homework 4: Programming Languages and Compilers (Cos 305) S - Aa - Bac - DC - Bda A - A
Homework 4: Programming Languages and Compilers (Cos 305) S - Aa - Bac - DC - Bda A - A
Q1) Show that the following grammar is LALR(1) but not SLR(1).
S --> Aa | bAc | dc | bda
A --> a
Q2) Show that the following grammar is LR(1) but not LALR(1).
S --> Aa | bAc | Bc | bBa
A --> d
B --> d
8 S10
S → A.a, $
9 R6 R5
S' → .S, $ S → bA.c, $ S → bAc., $
S → .Aa, $ 10 R3
S → .bAc, $ S → b.Ac, $
11 R2 $
S → bB.a, S → bBa., $
S → .Bc, $ S → b.Ba, $
S → .bBa, $ 12 A → .d, c R4
answer to Q3)
R→R|.R R → R | R.
R → .R | R R→R.|R
R --> .RR R --> R.R
R --> .R* R --> R.*
R --> .(R) R → .R | R
R' --> R. R --> .a R --> .RR
R → R. | R R --> .b R --> .R*
R --> R.R R --> .(R)
R --> R.* R --> .a
R → .R | R R --> .b
R --> .RR R --> RR.
R --> .R* R → R. | R
R --> .(R) R --> R.R
R --> .a R --> R.*
R' --> .R
R --> .b R → .R | R
R → .R | R
R --> .RR
R --> .RR
R --> .R*
R --> .R*
R --> .(R)
R --> .(R)
R --> .a
R --> .a R --> (.R)
R --> .b
R --> .b R → .R | R
R --> .RR
R --> .R*
R --> .(R)
R --> R*.
R --> .a
R --> .b
R --> (R.)
R→R.|R
R --> a. R --> R.R
R --> R.*
R --> (R).
R → .R | R
R --> b. SLR Parsing Table
R --> .RR
R Action
--> .R* Go to
State
| * ( R -->
) .(R) a b $ R
R --> .a
0 S2 R --> .b S3 S4 1
1 S5 S7 S2 S3 S4 accept 6
2 S2 S3 S4 8
3 R5 R5 R5 R5 R5 R5 R5
4 R6 R6 R6 R6 R6 R6 R6
5 S2 S3 S4 9
6 R2 R2/S7 R2 R2 R2 R2 R2 6
7 R3 R3 R3 R3 R3 R3 R3
8 S5 S7 S2 S10 S3 S4 6
9 R1 R1/S7 R1/S2 R1 R1/S3 R1/S4 R1 6
10 R4 R4 R4 R4 R4 R4 R4
We can see many shift-reduce conflicts in states I6 and I9