Question

Consider two grammars G1G_1 and G2G_2 with the production rules given below:

G1:  S→if E then S∣if E then S else S∣aG_1: \; S \rightarrow \textit{if } E \textit{ then } S \mid \textit{if } E \textit{ then } S \textit{ else } S \mid a
G1:  E→b\phantom{G_1: \; } E \rightarrow b

G2:  S→if E then S∣MG_2: \; S \rightarrow \textit{if } E \textit{ then } S \mid M
G2:  M→if E then M else S∣c\phantom{G_2: \; } M \rightarrow \textit{if } E \textit{ then } M \textit{ else } S \mid c
G2:  E→b\phantom{G_2: \; } E \rightarrow b

where if,then,else,a,b,c\textit{if}, \textit{then}, \textit{else}, a, b, c are the terminals.

Which of the following option(s) is/are CORRECT?

Choose every correct option
Official answer and solution
Official answer
C and D
From
Official GATE 2025 answer key

Solution

Check both grammars for FIRST/FIRST conflicts on terminal if and for dangling-else ambiguity (noting that in G2G_2, M→if E then M else SM \rightarrow \textit{if } E \textit{ then } M \textit{ else } S has SS instead of MM after else).

  1. Neither G1G_1 nor G2G_2 is LL(1) (Option C is TRUE):
    • In G1G_1, the two SS-productions if E then S\textit{if } E \textit{ then } S and if E then S else S\textit{if } E \textit{ then } S \textit{ else } S both start with terminal if\textit{if} (FIRST/FIRST conflict).
    • In G2G_2, for non-terminal SS, FIRST(if E then S)∩FIRST(M)={if}≠∅\text{FIRST}(\textit{if } E \textit{ then } S) \cap \text{FIRST}(M) = \{\textit{if}\} \neq \emptyset, causing a FIRST/FIRST conflict in the LL(1) parsing table.
  2. Both G1G_1 and G2G_2 are ambiguous (Option D is TRUE):
    • G1G_1 is the classic ambiguous dangling-else grammar.
    • In the standard unambiguous dangling-else grammar, a matched statement MM requires both branches of if-then-else to be matched (M→if E then M else M∣cM \rightarrow \textit{if } E \textit{ then } M \textit{ else } M \mid c). However, G2G_2 has M→if E then M else SM \rightarrow \textit{if } E \textit{ then } M \textit{ else } S, which allows MM to end with an unmatched if inside its else branch! Consequently, the string:
      w=if b then if b then c else if b then c else cw = \textit{if } b \textit{ then if } b \textit{ then } c \textit{ else if } b \textit{ then } c \textit{ else } c
      has two distinct parse trees in G2G_2:
      • Tree 1: S⇒M⇒if E then M1 else S1S \Rightarrow M \Rightarrow \textit{if } E \textit{ then } M_1 \textit{ else } S_1, where M1⇒if b then c else (if b then c)M_1 \Rightarrow \textit{if } b \textit{ then } c \textit{ else } (\textit{if } b \textit{ then } c) and S1⇒M⇒cS_1 \Rightarrow M \Rightarrow c.
      • Tree 2: S⇒if E then S1S \Rightarrow \textit{if } E \textit{ then } S_1, where S1⇒M1⇒if b then c else (if b then c else c)S_1 \Rightarrow M_1 \Rightarrow \textit{if } b \textit{ then } c \textit{ else } (\textit{if } b \textit{ then } c \textit{ else } c).
    • Since G2G_2 is ambiguous, this also immediately confirms that G2G_2 cannot be LL(1).
Final answer

Options (C), (D)

Common trap

Mistaking G2G_2 for the textbook unambiguous grammar without noticing SS (instead of MM) at the end of M→if E then M else SM \rightarrow \textit{if } E \textit{ then } M \textit{ else } S.

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

Source

Paper
GATE 2025 Computer Science and Information Technology - Session 2 (CS2)
Question
51 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.