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
R --> R | R
R --> RR
R --> R*
R --> (R)
R --> a
R --> b
S → A.a, $
S' → .S, $ S → bA.c, $ S → bAc., $
S → .Aa, $
S → .bAc, $ S → b.Ac, $
S → b.Ba, $ S → bB.a, $ S → bBa., $
S → .Bc, $
S → .bBa, $ A → .d, c
A → .d, a B → .d, a
A → d., c
B → .d, c B → d., a
S → B.c, $
S → Bc., $
A → d., a
B → d., c
No multiple actions in the same box so it is LR(1) but when we make the
LALR(1) table we will merge I5 and I9 and we will have a reduce-reduce
conflict as shown in red above.
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 --> RR. R --> .b
R --> .RR R → R. | R
R --> .R* R --> R.R
R' --> .R R --> .(R) R --> R.*
R → .R | R R --> .a R → .R | R
R --> .RR R --> .b R --> .RR
R --> .R*
R --> .R*
R --> .(R)
R --> .(R)
R --> .a R --> (.R)
R --> .a
R --> .b R → .R | R
R --> .b
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 --> b. R → .R | R
R --> .RR
R --> .R*
R --> .(R)
R --> .a
R --> .b
SLR Parsing Table
Action Go to
State
| * ( ) a b $ R
0 S2 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