GATE / CS ARCHIVE

GATE CSE 2024 SET 1 / QUESTION 38

Recover productions from FIRST and FOLLOW sets

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 2024 Set 1 paper

GATE 2024 Set 1, Q38 2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 38, GATE 2024: A grammar has S→daT | (1), T→aS | bT | (2), R→(3) | ε. FIRST(S)={c,d,f}, FIRST(T)={a,b,ε}, FIRST(R)={c,ε}; FOLLOW(S)=FOLLOW(T)={c,f,$}, FOLLOW(R)={f}. Complete (1),(2),(3). A Rf, ε, cTR; B fR, ε, cTR; C fR, cT, cR; D Rf, cT, cR.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: Use FIRST to eliminate incompatible productions, then verify the surviving option with FOLLOW rather than stopping after a partial match.
1. T must derive ε, and its FIRST set excludes c. Since aS and bT cannot produce ε, the missing alternative is T→ε, not T→cT. This eliminates C and D.
2. FIRST(S) contains c. The existing production daT starts with d. An alternative fR would start only with f, whereas Rf starts with c when R→cTR and with f when R→ε. Thus only A has the required FIRST(S).
3. In A, S→Rf places f immediately after R, giving FOLLOW(R)={f}. R→cTR places a nullable R after T, contributing c and FOLLOW(R)'s f to FOLLOW(T).
4. The trailing T in S→daT inherits FOLLOW(S), while the trailing S in T→aS inherits FOLLOW(T). Because S is the start symbol, $ propagates to both sets. The result is exactly {c,f,$} for each.
Answer: A. FIRST describes how derived strings begin; FOLLOW describes terminals that can occur immediately after a nonterminal in a sentential form.

Original paper, page 24 · Answer key