Question

Consider the following grammar GG, with SS as the start symbol. The grammar GG has three incomplete productions denoted by (1), (2), and (3).

S→daT∣(1)T→aS∣bT∣(2)R→(3)∣ϵ\begin{aligned} S &\rightarrow daT \mid (1) \\ T &\rightarrow aS \mid bT \mid (2) \\ R &\rightarrow (3) \mid \epsilon \end{aligned}

The set of terminals is {a,b,c,d,f}\{a, b, c, d, f\}. The FIRST and FOLLOW sets of the different non-terminals are as follows.

FIRST(S)={c,d,f},FIRST(T)={a,b,ϵ},FIRST(R)={c,ϵ}\text{FIRST}(S) = \{c, d, f\},\quad \text{FIRST}(T) = \{a, b, \epsilon\},\quad \text{FIRST}(R) = \{c, \epsilon\}
FOLLOW(S)=FOLLOW(T)={c,f,$},FOLLOW(R)={f}\text{FOLLOW}(S) = \text{FOLLOW}(T) = \{c, f, \$\},\quad \text{FOLLOW}(R) = \{f\}

Which one of the following options CORRECTLY fills in the incomplete productions?

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2024 answer key

Solution

Use the given FIRST and FOLLOW sets to deduce each missing production one by one.

  1. Deduce (2) from FIRST(T)={a,b,ϵ}\text{FIRST}(T) = \{a, b, \epsilon\}:
    Since ϵ∈FIRST(T)\epsilon \in \text{FIRST}(T), TT must have the production T→ϵT \rightarrow \epsilon. This eliminates options (C) and (D).
  2. Deduce (1) from FIRST(S)={c,d,f}\text{FIRST}(S) = \{c, d, f\} and FOLLOW(R)={f}\text{FOLLOW}(R) = \{f\}:
    The production S→daTS \rightarrow daT only contributes dd to FIRST(S)\text{FIRST}(S), so production (1) must contribute both cc and ff to FIRST(S)\text{FIRST}(S). With S→RfS \rightarrow Rf, since FIRST(R)={c,ϵ}\text{FIRST}(R) = \{c, \epsilon\}, we get:
    FIRST(Rf)=(FIRST(R)∖{ϵ})∪FIRST(f)={c,f}\text{FIRST}(Rf) = (\text{FIRST}(R) \setminus \{\epsilon\}) \cup \text{FIRST}(f) = \{c, f\}
    which yields FIRST(S)={c,d,f}\text{FIRST}(S) = \{c, d, f\} and also places f∈FOLLOW(R)f \in \text{FOLLOW}(R) (whereas S→fRS \rightarrow fR would only give FIRST(S)={d,f}\text{FIRST}(S) = \{d, f\} and would place $∈FOLLOW(R)\$ \in \text{FOLLOW}(R)).
  3. Verify (3) R→cTRR \rightarrow cTR:
    This gives FIRST(R)={c,ϵ}\text{FIRST}(R) = \{c, \epsilon\} and adds (FIRST(R)∖{ϵ})∪FOLLOW(R)={c,f}(\text{FIRST}(R) \setminus \{\epsilon\}) \cup \text{FOLLOW}(R) = \{c, f\} to FOLLOW(T)\text{FOLLOW}(T), matching FOLLOW(T)=FOLLOW(S)={c,f,$}\text{FOLLOW}(T) = \text{FOLLOW}(S) = \{c, f, \$\}.

Thus Option (A) is correct.

Final answer

Option (A)

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2024 Computer Science and Information Technology - Set 1 (CS1)
Question
38 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.