GATE CS 2024 Set 1 — Question 38

MCQ+2 / -0.67MediumFirst & Follow SetsSyntax AnalysisCompiler Design

Compiler Design → Syntax Analysis → First & Follow Sets

Last updated

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).SdaT(1)S \rightarrow daT \mid \underline{\hspace{1cm}} (1)TaSbT(2)T \rightarrow aS \mid bT \mid \underline{\hspace{1cm}} (2)R(3)ϵR \rightarrow \underline{\hspace{1cm}} (3) \mid \epsilonThe 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\}, \text{FIRST}(T) = \{a, b, \epsilon\}, \text{FIRST}(R) = \{c, \epsilon\}FOLLOW(S)=FOLLOW(T)={c,f,$},FOLLOW(R)={f}\text{FOLLOW}(S) = \text{FOLLOW}(T) = \{c, f, \$\}, \text{FOLLOW}(R) = \{f\}.
Which one of the following options CORRECTLY fills in the incomplete productions?
A.
(1) SRfS \rightarrow Rf (2) TϵT \rightarrow \epsilon (3) RcTRR \rightarrow cTR
B.
(1) SfRS \rightarrow fR (2) TϵT \rightarrow \epsilon (3) RcTRR \rightarrow cTR
C.
(1) SfRS \rightarrow fR (2) TcTT \rightarrow cT (3) RcRR \rightarrow cR
D.
(1) SRfS \rightarrow Rf (2) TcTT \rightarrow cT (3) RcRR \rightarrow cR

Correct answer

(A) (1) S arrow Rf (2) T arrow ε (3) R arrow cTR

Solution

We are given the FIRST sets for the non-terminals:
FIRST(S)={c,d,f}\text{FIRST}(S) = \{c, d, f\}
FIRST(T)={a,b,ϵ}\text{FIRST}(T) = \{a, b, \epsilon\}
FIRST(R)={c,ϵ}\text{FIRST}(R) = \{c, \epsilon\}Let's analyze the incomplete productions based on these sets.
1. Analyze Production (1) for SS:
SdaTS \rightarrow daT \mid \dots
The production SdaTS \rightarrow daT contributes dd to FIRST(S)\text{FIRST}(S).
The missing part (1) must contribute the remaining elements of FIRST(S)\text{FIRST}(S), which are {c,f}\{c, f\}.
  • Option (B) & (C): SfRS \rightarrow fR. This starts with the terminal ff. Thus, it adds ff to FIRST(S)\text{FIRST}(S), but it cannot add cc. So, (B) and (C) are incorrect.
  • Option (A) & (D): SRfS \rightarrow Rf. Since FIRST(R)={c,ϵ}\text{FIRST}(R) = \{c, \epsilon\}, this production adds cc (from RcR \rightarrow c\dots) and, because RR is nullable (ϵFIRST(R)\epsilon \in \text{FIRST}(R)), it also adds FIRST(f)={f}\text{FIRST}(f) = \{f\}. Thus, SRfS \rightarrow Rf contributes both cc and ff. This matches the requirement.
2. Analyze Production (2) for TT:
TaSbTT \rightarrow aS \mid bT \mid \dots
The existing productions contribute aa and bb to FIRST(T)\text{FIRST}(T).
The missing part (2) must contribute ϵ\epsilon to match FIRST(T)={a,b,ϵ}\text{FIRST}(T) = \{a, b, \epsilon\}.
  • Option (A): TϵT \rightarrow \epsilon. This correctly adds ϵ\epsilon to FIRST(T)\text{FIRST}(T).
  • Option (D): TcTT \rightarrow cT. This would add cc to FIRST(T)\text{FIRST}(T). However, cc is not in the given FIRST(T)\text{FIRST}(T). Thus, (D) is incorrect.
3. Analyze Production (3) for RR:
RϵR \rightarrow \dots \mid \epsilon
The existing production contributes ϵ\epsilon.
The missing part (3) must contribute cc to match FIRST(R)={c,ϵ}\text{FIRST}(R) = \{c, \epsilon\}.
  • Option (A): RcTRR \rightarrow cTR. This starts with cc, satisfying the condition.
Therefore, Option (A) is the only one consistent with all the given FIRST sets.

More questions on Syntax Analysis

Practice GATE CS PYQs with adaptive difficulty

Timed practice, skill tracking, and AI explanations — free to start.

Start practicing free