CS323 Written Assignment 4 - SLR, CLR, and LALR Parsing
Exercise 1: Consider the following grammar G, which you have seen in the previous assignment:
1 | E -> TX |
1. Please construct the SLR parsing table for G. Is the grammar SLR(1)?
| (Non-Terminal) | FIRST Set | FOLLOW Set |
|---|---|---|
| E | {(,a, b} | {$, )} |
| T | {(,a, b} | {$, ), +} |
| X | {ε, +} | {$, )} |
| F | {(,a, b} | {$, (,), +, a, b} |
| Y | {ε, (,a, b} | {$, ), +} |
| P | {(,a, b} | {$, (,), *, +, a, b} |
| Z | {ε, *} | {$, (,), +, a, b} |
$$ \begin{aligned} (1) & \quad E \to TX \\ (2) & \quad X \to +E \\ (3) & \quad X \to \varepsilon \\ (4) & \quad T \to FY \\ (5) & \quad Y \to T \\ (6) & \quad Y \to \varepsilon \\ (7) & \quad F \to PZ \\ (8) & \quad Z \to *Z \\ (9) & \quad Z \to \varepsilon \\ (10) & \quad P \to (E) \\ (11) & \quad P \to a \\ (12) & \quad P \to b \end{aligned} $$
Augmented grammar: Introduce a new symbol E’ and add a new production
E' -> E
1 | I0 = closure({[E'->•E]}) |
The SLR parsing table
| State | ACTION | GOTO | ||||||||||||
| a | b | + | * | ( | ) | $ | E | T | X | F | Y | P | Z | |
| 0 | s6 | s7 | s5 | 1 | 2 | 3 | 4 | |||||||
| 1 | acc | |||||||||||||
| 2 | s9 | s5 | r3 | r3 | 8 | |||||||||
| 3 | s6 | s7 | r6 | r6 | r6 | 11 | 3 | 10 | 4 | |||||
| 4 | r9 | r9 | r9 | s13 | r9 | r9 | r9 | 12 | ||||||
| 5 | s6 | s7 | s5 | 14 | 2 | 3 | 4 | |||||||
| 6 | r11 | r11 | r11 | r11 | r11 | r11 | r11 | |||||||
| 7 | r12 | r12 | r12 | r12 | r12 | r12 | r12 | |||||||
| 8 | r1 | r1 | ||||||||||||
| 9 | s6 | s7 | s5 | 15 | 2 | 3 | 4 | |||||||
| 10 | r4 | r4 | r4 | |||||||||||
| 11 | r5 | r5 | r5 | |||||||||||
| 12 | r7 | r7 | r7 | r7 | r7 | r7 | ||||||||
| 13 | r9 | r9 | r9 | s13 | r9 | r9 | r9 | 16 | ||||||
| 14 | s17 | |||||||||||||
| 15 | r2 | r2 | ||||||||||||
| 16 | r8 | r8 | r8 | r8 | r8 | r8 | ||||||||
| 17 | r10 | r10 | r10 | r10 | r10 | r10 | r10 | |||||||
The grammar is SLR(1), because there are no conflict during the parsing table construction.
2. Give the parsing steps for the input string (a∗+b)+b. If there are conflicts in the parsing table, please address them reasonably.
| LINE | STACK | SYMBOLS | INPUT | ACTION |
|---|---|---|---|---|
| (1) | 0 | $ | (a*+b)+b$ | shift to 5 |
| (2) | 0 5 | $ ( | a*+b)+b$ | shift to 6 |
| (3) | 0 5 6 | $ ( a | *+b)+b$ | reduce by P->a |
| (4) | 0 5 4 | $ ( P | *+b)+b$ | shift to 13 |
| (5) | 0 5 4 13 | $ ( P * | +b)+b$ | reduce by Z->ε |
| (6) | 0 5 4 13 16 | $ ( P * Z | +b)+b$ | reduce by Z->*Z |
| (7) | 0 5 4 12 | $ ( P Z | +b)+b$ | reduce by F->PZ |
| (8) | 0 5 3 | $ ( F | +b)+b$ | reduce by Y->ε |
| (9) | 0 5 3 10 | $ ( F Y | +b)+b$ | reduce by T->FY |
| (10) | 0 5 2 | $ ( T | +b)+b$ | shift to 9 |
| (11) | 0 5 2 9 | $ ( T + | b)+b$ | shift to 7 |
| (12) | 0 5 2 9 7 | $ ( T + b | )+b$ | reduce by P->b |
| (13) | 0 5 2 9 4 | $ ( T + P | )+b$ | reduce by Z->ε |
| (14) | 0 5 2 9 4 12 | $ ( T + PZ | )+b$ | reduce by F->PZ |
| (15) | 0 5 2 9 3 | $ ( T + F | )+b$ | reduce by Y->ε |
| (16) | 0 5 2 9 3 10 | $ ( T + FY | )+b$ | reduce by T->FY |
| (17) | 0 5 2 9 2 | $ ( T + T | )+b$ | reduce by X->ε |
| (18) | 0 5 2 9 2 8 | $ ( T + TX | )+b$ | reduce by E->TX |
| (19) | 0 5 2 9 15 | $ ( T + E | )+b$ | reduce by X->+E |
| (20) | 0 5 2 8 | $ ( T X | )+b$ | reduce by E->TX |
| (21) | 0 5 14 | $ ( E | )+b$ | shift to 17 |
| (22) | 0 5 14 17 | $ ( E ) | +b$ | reduce by P->(E) |
| (23) | 0 4 | $ P | +b$ | reduce by Z->ε |
| (24) | 0 4 12 | $ P Z | +b$ | reduce by F->PZ |
| (25) | 0 3 | $ F | +b$ | reduce by Y->ε |
| (26) | 0 3 10 | $ FY | +b$ | reduce by T->FY |
| (27) | 0 2 | $ T | +b$ | shift to 9 |
| (28) | 0 2 9 | $ T + | b$ | shift to 7 |
| (29) | 0 2 9 7 | $ T + b | $ | reduce by P->b |
| (30) | 0 2 9 4 | $ T + P | $ | reduce by Z->ε |
| (31) | 0 2 9 4 12 | $ T + PZ | $ | reduce by F->PZ |
| (32) | 0 2 9 3 | $ T + F | $ | reduce by Y->ε |
| (33) | 0 2 9 3 10 | $ T + F Y | $ | reduce by T->FY |
| (34) | 0 2 9 2 | $ T + T | $ | reduce by X->ε |
| (35) | 0 2 9 2 8 | $ T + TX | $ | reduce by E->TX |
| (36) | 0 2 9 15 | $ T + E | $ | reduce by X->+E |
| (37) | 0 2 8 | $ T X | $ | reduce by E->TX |
| (38) | 0 1 | $ E | $ | accept |
Exercise 2: Consider the following grammar G:
1 | S -> 0A |
1. Please construct the shift-reduce parsing table for the above grammar G using each of the following algorithms: (1) SLR, (2) CLR, and (3) LALR.
- SLR
FIRST(S) = {0} FOLLOW(S) = {1, $}
FIRST(A) = {ε, 0} FOLLOW(A) = {1, $}
1 | (1) S -> 0A |
Augmented grammar: Introduce a new symbol S’ and add a new production
S' -> S
1 | I0 = closure({[S'->•S]}) |
the shift-reduce parsing table using SLR
| State | ACTION | GOTO | |||
| 0 | 1 | $ | S | A | |
| 0 | s2 | 1 | |||
| 1 | acc | ||||
| 2 | s2 | r3 | r3 | 3 | 4 |
| 3 | s5 | ||||
| 4 | r1 | r1 | |||
| 5 | s2 | r3 | r3 | 3 | 6 |
| 6 | r2 | r2 | |||
- CLR
FIRST(S) = {0} FOLLOW(S) = {1, $}
FIRST(A) = {ε, 0} FOLLOW(A) = {1, $}
1 | (1) S -> 0A |
Augmented grammar: Introduce a new symbol S’ and add a new production
S' -> S
FIRST($) = {$}
FIRST(1A$) = {1}
1 | I0 = closure({[S'->•S, $]}) |
the shift-reduce parsing table using CLR
| State | ACTION | GOTO | |||
| 0 | 1 | $ | S | A | |
| 0 | s2 | 1 | |||
| 1 | acc | ||||
| 2 | s5 | r3 | 3 | 4 | |
| 3 | s6 | ||||
| 4 | r1 | ||||
| 5 | s5 | r3 | 7 | 8 | |
| 6 | s5 | r3 | 3 | 9 | |
| 7 | s10 | ||||
| 8 | r1 | ||||
| 9 | r2 | ||||
| 10 | s5 | r3 | 7 | 11 | |
| 11 | r2 | ||||
- LALR
1 | I25 = I2 U I5 = {[S->0•A, 1/$], [A->•S1A, 1/$], [S->•0A, 1], [A->•, 1/$]} |
1 | State 0: I0 |
| State | ACTION | GOTO | |||
| 0 | 1 | $ | S | A | |
| 0 | s2 | 1 | |||
| 1 | acc | ||||
| 2 | s2 | r3 | r3 | 3 | 4 |
| 3 | s5 | ||||
| 4 | r1 | r1 | |||
| 5 | s2 | r3 | r3 | 3 | 6 |
| 6 | r2 | r2 | |||
2. Is the grammar SLR(1)? Is it LR(1)? Is it LALR(1)?
Yes, this grammar is SLR(1), LR(1), and LALR(1). The SLR(1) parsing table with 7 states has no conflicts despite using coarser FOLLOW sets, the LR(1) (CLR) table with 12 states is conflict-free using precise lookaheads, and the LALR(1) table successfully merges core-equivalent LR(1) states back to 7 states without introducing any new conflicts.
3. Can an LALR(1) parser accept the string 000001111? If yes, please list the parsing steps; otherwise, please state the reason. Before parsing, please address conflicts in the parsing table if any.
LALR(1) parser can accept the string 000001111
| LINE | STACK | SYMBOLS | INPUT | ACTION |
|---|---|---|---|---|
| (1) | 0 | $ | 000001111$ | shift to 2 |
| (2) | 0 2 | $ 0 | 00001111$ | shift to 2 |
| (3) | 0 2 2 | $ 0 0 | 0001111$ | shift to 2 |
| (4) | 0 2 2 2 | $ 0 0 0 | 001111$ | shift to 2 |
| (5) | 0 2 2 2 2 | $ 0 0 0 0 | 01111$ | shift to 2 |
| (6) | 0 2 2 2 2 2 | $ 0 0 0 0 0 | 1111$ | reduce by A->ε |
| (7) | 0 2 2 2 2 2 4 | $ 0 0 0 0 0 A | 1111$ | reduce by S->0A |
| (8) | 0 2 2 2 2 3 | $ 0 0 0 0 S | 1111$ | shift to 5 |
| (9) | 0 2 2 2 2 3 5 | $ 0 0 0 0 S 1 | 111$ | reduce by A->ε |
| (10) | 0 2 2 2 2 3 5 6 | $ 0 0 0 0 S 1 A | 111$ | reduce by A->S1A |
| (11) | 0 2 2 2 2 4 | $ 0 0 0 0 A | 111$ | reduce by S->0A |
| (12) | 0 2 2 2 3 | $ 0 0 0 S | 111$ | shift to 5 |
| (13) | 0 2 2 2 3 5 | $ 0 0 0 S 1 | 11$ | reduce by A->ε |
| (14) | 0 2 2 2 3 5 6 | $ 0 0 0 S 1 A | 11$ | reduce by A->S1A |
| (15) | 0 2 2 2 4 | $ 0 0 0 A | 11$ | reduce by S->0A |
| (16) | 0 2 2 3 | $ 0 0 S | 11$ | shift to 5 |
| (17) | 0 2 2 3 5 | $ 0 0 S 1 | 1$ | reduce by A->ε |
| (18) | 0 2 2 3 5 6 | $ 0 0 S 1 A | 1$ | reduce by A->S1A |
| (19) | 0 2 2 4 | $ 0 0 A | 1$ | reduce by S->0A |
| (20) | 0 2 3 | $ 0 S | 1$ | shift to 5 |
| (21) | 0 2 3 5 | $ 0 S 1 | $ | reduce by A->ε |
| (22) | 0 2 3 5 6 | $ 0 S 1 A | $ | reduce A->S1A |
| (23) | 0 2 4 | $ 0 A | $ | reduce by S->0A |
| (24) | 0 1 | $ S | $ | accept |