Consider the following grammar:
S -> FR
F -> i
R -> +S | epsilon
In the predictive parser table M of the grammar, the entries M[S, id] and M[R, +] respectively are
A. {S -> FR} and {R -> +S}
B. {S -> FR} and {R -> epsilon}
C. {S -> FR} and {R -> +S}
D. {R -> id} and {R -> +i}
GATE 2006 · Compiler Design · Parsing · medium
Answer: M[S, id] = {S -> FR} and M[R, +] = {R -> +S}, which is option A.
Compute FIRST sets: FIRST(F) = {i} (only production F -> i). FIRST(R) = {+, epsilon} (from R -> +S and R -> epsilon). FIRST(S) = FIRST(FR) = FIRST(F) = {i}.
Compute FOLLOW sets: S is the start symbol so $ is in FOLLOW(S). R appears only at the end of S -> FR, so FOLLOW(R) = FOLLOW(S) = {$}. In R -> +S, S is at the end, so FOLLOW(S) also includes FOLLOW(R) = {$} (consistent).
Fill the parse table entries: M[S, id]: FIRST(FR) = {i/id} => M[S, id] = {S -> FR}.
M[R, +]: FIRST(+S) = {+} => M[R, +] = {R -> +S}.
M[R, $]: epsilon in FIRST(epsilon) and $ in FOLLOW(R) => M[R, $] = {R -> epsilon}.