GATE CS 2024 Set 2 — Question 40

MCQ+2 / -0.67MediumLL(1) ParsingSyntax AnalysisCompiler Design

Compiler Design → Syntax Analysis → LL(1) Parsing

Last updated

Question

Consider the following context-free grammar where the start symbol is SS and the set of terminals is {a,b,c,d}\{a,b,c,d\}.SAaAbBbBaS \rightarrow AaAb \mid BbBaAcSϵA \rightarrow cS \mid \epsilonBdSϵB \rightarrow dS \mid \epsilonThe following is a partially-filled LL(1) parsing table.
aabbccdd$\$
SSSAaAbS \rightarrow AaAbSBbBaS \rightarrow BbBa(1)(2)
AAAϵA \rightarrow \epsilon(3)AcSA \rightarrow cS
BB(4)BϵB \rightarrow \epsilonBdSB \rightarrow dS
Which one of the following options represents the CORRECT combination for the numbered cells in the parsing table?
Note: In the options, "blank" denotes that the corresponding cell is empty.
A.
(1) SAaAbS \rightarrow AaAb (2) SBbBaS \rightarrow BbBa (3) AϵA \rightarrow \epsilon (4) BϵB \rightarrow \epsilon
B.
(1) SBbBaS \rightarrow BbBa (2) SAaAbS \rightarrow AaAb (3) AϵA \rightarrow \epsilon (4) BϵB \rightarrow \epsilon
C.
(1) SAaAbS \rightarrow AaAb (2) SBbBaS \rightarrow BbBa (3) blank (4) blank
D.
(1) SBbBaS \rightarrow BbBa (2) SAaAbS \rightarrow AaAb (3) blank (4) blank

Correct answer

(A) (1) S arrow AaAb (2) S arrow BbBa (3) A arrow ε (4) B arrow ε

Solution

To determine the entries in the LL(1) parsing table, we first compute the First and Follow sets for the non-terminals.
1. Compute First Sets:
  • First(A)={c,ϵ}First(A) = \{c, \epsilon\} (from AcSϵA \rightarrow cS \mid \epsilon)
  • First(B)={d,ϵ}First(B) = \{d, \epsilon\} (from BdSϵB \rightarrow dS \mid \epsilon)
  • First(S)First(S):
  • For SAaAbS \rightarrow AaAb: First(AaAb)=(First(A){ϵ})First(aAb)={c}{a}={a,c}First(AaAb) = (First(A) \setminus \{\epsilon\}) \cup First(aAb) = \{c\} \cup \{a\} = \{a, c\}.
  • For SBbBaS \rightarrow BbBa: First(BbBa)=(First(B){ϵ})First(bBa)={d}{b}={b,d}First(BbBa) = (First(B) \setminus \{\epsilon\}) \cup First(bBa) = \{d\} \cup \{b\} = \{b, d\}.
  • Thus, First(S)={a,b,c,d}First(S) = \{a, b, c, d\}.
2. Compute Follow Sets:
  • Follow(S)Follow(S) includes $\$ (start symbol).
  • From SAaAbS \rightarrow AaAb:
  • The first AA is followed by aa, so aFollow(A)a \in Follow(A).
  • The second AA is followed by bb, so bFollow(A)b \in Follow(A).
  • Therefore, Follow(A)={a,b}Follow(A) = \{a, b\}.
  • From SBbBaS \rightarrow BbBa:
  • The first BB is followed by bb, so bFollow(B)b \in Follow(B).
  • The second BB is followed by aa, so aFollow(B)a \in Follow(B).
  • Therefore, Follow(B)={a,b}Follow(B) = \{a, b\}.
3. Fill the Parsing Table Cells:
  • Cell (1) for (S,c)(S, c): Since cFirst(AaAb)c \in First(AaAb), the entry is SAaAbS \rightarrow AaAb.
  • Cell (2) for (S,d)(S, d): Since dFirst(BbBa)d \in First(BbBa), the entry is SBbBaS \rightarrow BbBa.
  • Cell (3) for (A,b)(A, b): Since bFollow(A)b \in Follow(A) and AϵA \rightarrow \epsilon is a production, the entry is AϵA \rightarrow \epsilon.
  • Cell (4) for (B,a)(B, a): Since aFollow(B)a \in Follow(B) and BϵB \rightarrow \epsilon is a production, the entry is BϵB \rightarrow \epsilon.
Matching these results with the options:
(1) SAaAbS \rightarrow AaAb
(2) SBbBaS \rightarrow BbBa
(3) AϵA \rightarrow \epsilon
(4) BϵB \rightarrow \epsilonThis corresponds to Option (A).

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