All mock tests are FREE till 31 December — register to save your progress

Consider the following Context Free Grammar (G):

S → A B
B → + A B | ε
A → C D
D → * C D | ε
C → [ S ] | x

Variable = { S, A, B, C, D }

Terminals = { +, *, [, ], x }

What will be the FIRST(S) & FIRST(D)?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: C. FIRST (S) = { [, x }; FIRST (D) = { *, ε }

Explanation

Since S → AB and A → CD with C → [S] | x (neither alternative is ε), A can never derive ε, so FIRST(S) = FIRST(A) = FIRST(C) = {[, x} (the first symbols of C's two alternatives). For D → *CD | ε, the first alternative starts with the terminal '*' and the second is ε, so FIRST(D) = {*, ε}. Hence FIRST(S) = {[, x}, FIRST(D) = {*, ε}.

More Theory of Computation and Compilers questions