a) Compute First and Follow sets for all non-terminals of grammar G:
Non-terminals: S, W, Z, X, Y
Terminals: a, b, c, d, x
First Sets:
-
FIRST(Z):
Z → a | d
FIRST(Z) = { a, d }
-
FIRST(X):
X → x b | ε
FIRST(X) = { x, ε }
-
FIRST(Y):
Y → c Y | ε
FIRST(Y) = { c, ε }
-
FIRST(W):
W → Z X Y | X Y
- FIRST(Z X Y): Since FIRST(Z) = { a, d } and Z doesn't derive ε,
FIRST(Z X Y) = FIRST(Z) = { a, d }
- FIRST(X Y):
- Since FIRST(X) = { x, ε } and ε ∈ FIRST(X),
FIRST(X Y) = (FIRST(X) - { ε }) ∪ FIRST(Y) = { x } ∪ { c, ε } = { x, c, ε }
- Therefore, FIRST(W) = FIRST(Z X Y) ∪ FIRST(X Y) = { a, d, x, c, ε }
-
FIRST(S):
S → W
FIRST(S) = FIRST(W) = { a, d, x, c, ε }
Follow Sets:
-
FOLLOW(S):
Since S is the start symbol, FOLLOW(S) = { $ }
-
FOLLOW(W):
From S → W, FOLLOW(W) includes FOLLOW(S)
FOLLOW(W) = { $ }
-
FOLLOW(Z):
From W → Z X Y:
- Since FIRST(X Y) = { x, c, ε } and ε ∈ FIRST(X Y),
FOLLOW(Z) = FIRST(X Y) - { ε } ∪ FOLLOW(W) = { x, c } ∪ { } = { x, c, }
-
FOLLOW(X):
From W → Z X Y and W → X Y:
- Since FIRST(Y) = { c, ε } and ε ∈ FIRST(Y),
FOLLOW(X) = FIRST(Y) - { ε } ∪ FOLLOW(W) = { c } ∪ { } = { c, }
-
FOLLOW(Y):
From W → Z X Y and W → X Y:
FOLLOW(Y) = FOLLOW(W) = { $ }
b) Construct the LL(1) parsing table and check if the grammar is LL(1):
Parsing Table:
| Non-Terminals | a | b | c | d | x | $ |
|---|
| S | S → W | | S → W | S → W | S → W | S → W |
| W | W→ZXY | | W→XY | W→ZXY | W→XY | W→XY |
| Z | Z → a | | | Z → d | | |
| X | | | X → ε | | X→x b | X → ε |
| Y | | | Y→c Y | | | Y → ε |
Explanation:
-
For S → W:
- FIRST(W) = { a, d, x, c, ε }
- Since ε ∈ FIRST(W), we also include FOLLOW(S) = { $ }
- Entries for S → W are placed under columns a, c, d, x, $
-
For W → Z X Y:
- FIRST(Z X Y) = { a, d }
- Entries for W → Z X Y are placed under columns a, d
-
For W → X Y:
- FIRST(X Y) = { x, c, ε }
- Since ε ∈ FIRST(X Y), we include FOLLOW(W) = { $ }
- Entries for W → X Y are placed under columns x, c, $
-
For Z → a and Z → d:
- Entries for Z → a under column a
- Entries for Z → d under column d
-
For X → x b and X → ε:
- FIRST(x b) = { x }
- Entry for X → x b under column x
- Since ε ∈ FIRST(X), entries for X → ε under FOLLOW(X) = { c, $ }
- Entries for X → ε under columns c, $
-
For Y → c Y and Y → ε:
- Entry for Y → c Y under column c
- Since ε ∈ FIRST(Y), entry for Y → ε under FOLLOW(Y) = { $ }
- Entry for Y → ε under column $
Conclusion:
- The parsing table has no multiple entries in any cell, indicating no conflicts.
- Therefore, the given grammar is LL(1).
Final First and Follow Sets:
-
FIRST(S) = { a, d, x, c, ε }
-
FIRST(W) = { a, d, x, c, ε }
-
FIRST(Z) = { a, d }
-
FIRST(X) = { x, ε }
-
FIRST(Y) = { c, ε }
-
FOLLOW(S) = { $ }
-
FOLLOW(W) = { $ }
-
FOLLOW(Z) = { x, c, $ }
-
FOLLOW(X) = { c, $ }
-
FOLLOW(Y) = { $ }