To determine whether the given grammar can be parsed using SLR(1), LALR(1), and LR(1) parsing techniques, we'll systematically construct the corresponding automata. The grammar in question is:
S' → S
S → G = D
S → D
G → * D
G → c
D → G
Overview of Parsing Techniques
- SLR(1) (Simple LR): Uses LR(0) items augmented with FOLLOW sets to resolve parsing actions.
- LALR(1) (Look-Ahead LR): Merges states with identical LR(0) cores but maintains separate lookaheads.
- LR(1): Uses full LR(1) items, maintaining distinct lookaheads for each item.
Step-by-Step Construction
1. Augmented Grammar
First, augment the grammar to facilitate parsing:
S' → S
S → G = D
S → D
G → * D
G → c
D → G
2. Compute FIRST and FOLLOW Sets
FIRST Sets:
FIRST(S) = FIRST(G = D) ∪ FIRST(D) = { *, c }
FIRST(G) = { *, c }
FIRST(D) = FIRST(G) = { *, c }
FOLLOW Sets:
FOLLOW(S') = { $ }
FOLLOW(S) = FOLLOW(S') = { $ }
FOLLOW(G) = { =, $ }
FOLLOW(D) = FOLLOW(S) = { $ }
3. Construct LR(0) Automaton
We'll build the canonical collection of LR(0) items (states):
-
State I0 (Initial State):
S' → • S
S → • G = D
S → • D
D → • G
G → • * D
G → • c
Transitions from I0:
- On
S: Goto I1
- On
G: Goto I2
- On
*: Shift to I4
- On
c: Shift to I5
- On
D: Goto I6
-
State I1:
S' → S •
Action: Accept (final state)
-
State I2:
S → G • = D
D → G •
Transitions from I2:
- On
=: Shift to I3
- Note: Since
D → G • is a reducible item, in SLR(1), it will reduce based on FOLLOW(D).
-
State I3:
S → G = • D
D → • G
G → • * D
G → • c
Transitions from I3:
- On
D: Shift to I9
- On
G: Goto I10
- On
*: Shift to I4
- On
c: Shift to I5
-
State I4:
G → * • D
D → • G
G → • * D
G → • c
Transitions from I4:
- On
D: Goto I7
- On
*: Shift to I4
- On
c: Shift to I5
-
State I5:
G → c •
Action: Reduce using G → c
-
State I6:
S → D •
Action: Reduce using S → D
-
State I7:
G → * D •
Action: Reduce using G → * D
-
State I9:
S → G = D •
Action: Reduce using S → G = D
-
State I10:
D → G •
Action: Reduce using D → G
4. Resolving Conflicts in SLR(1)
In State I2, we have:
S → G • = D
D → G •
- Action for
=:
- Shift: According to
S → G • = D, shift =.
- Reduce: According to
D → G •, reduce by D → G if = is in FOLLOW(D).
But, = is not in FOLLOW(D) (FOLLOW(D) = { $ }). Therefore, there's no shift/reduce conflict in SLR(1) parsing because the reduce action for D → G only occurs on $, while = triggers a shift.
5. LALR(1) and LR(1) Automata
Since the grammar is SLR(1), it naturally qualifies for LALR(1) and LR(1) parsing as well. LALR(1) merges states with identical LR(0) cores but could have more precise lookaheads. LR(1) maintains distinct lookaheads for each item, providing even greater precision.
Conclusion
All three parsing techniques—SLR(1), LALR(1), and LR(1)—can successfully parse the given grammar without any conflicts.
Here’s a summary of the states for clarity:
-
I0:
S' → • S
S → • G = D
S → • D
D → • G
G → • * D
G → • c
-
I1:
S' → S •
(Accept)
-
I2:
S → G • = D
D → G •
-
I3:
S → G = • D
D → • G
G → • * D
G → • c
-
I4:
G → * • D
D → • G
G → • * D
G → • c
-
I5:
G → c •
(Reduce G → c)
-
I6:
S → D •
(Reduce S → D)
-
I7:
G → * D •
(Reduce G → * D)
-
I9:
S → G = D •
(Reduce S → G = D)
-
I10:
D → G •
(Reduce D → G)
By following this systematic construction, we ensure that the grammar is free of ambiguities and suitable for all three parsing strategies.