GateAI
Parsing

GATE CSE 2025 Set 2 · Q51

MSQ2 marksMedium

Time

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 \to if\ E\ then\ S \mid if\ E\ then\ S\ else\ S \mid a E→bE \to b G2:S→if E then S∣MG_2: S \to if\ E\ then\ S \mid M M→if E then M else S∣cM \to if\ E\ then\ M\ else\ S \mid c E→bE \to b where if,then,else,a,b,cif, then, else, a, b, c are the terminals. Which of the following option(s) is/are CORRECT?

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

Official answer: C) G1 and G2 are not LL(1).; D) G1 and G2 are ambiguous.

Parsing

### Analysis of Grammar G1G_1:

  • G1G_1 is the classic "dangling-else" grammar.

  • For the string if b then if b then a else a, there are two distinct leftmost derivations / parse trees:

    1. The else associates with the inner if.

    2. The else associates with the outer if.

  • Since G1G_1 generates a string with more than one parse tree, G1G_1 is ambiguous [46].

  • Any ambiguous grammar cannot be LL(1)LL(1), so G1G_1 is not $LL(1)$.

### Analysis of Grammar G2G_2:

  • Consider the string if b then if b then c else if b then c:

    • Parse Tree 1:

      S  ⟹  if E then S  ⟹  if b then M  ⟹  if b then if E then M else S  ⟹  if b then if b then c else (if E then S)…S \implies if\ E\ then\ S \implies if\ b\ then\ M \implies if\ b\ then\ if\ E\ then\ M\ else\ S \implies if\ b\ then\ if\ b\ then\ c\ else\ (if\ E\ then\ S) \dots
    • Parse Tree 2:

      S  ⟹  if E then S  ⟹  if b then (if E then S)  ⟹  if b then if b then M  ⟹  if b then if b then if E then M else S…S \implies if\ E\ then\ S \implies if\ b\ then\ (if\ E\ then\ S) \implies if\ b\ then\ if\ b\ then\ M \implies if\ b\ then\ if\ b\ then\ if\ E\ then\ M\ else\ S \dots
  • Alternatively, consider the non-terminal S→if E then S∣MS \to if\ E\ then\ S \mid M. The non-terminal MM has production M→if E then M else S∣cM \to if\ E\ then\ M\ else\ S \mid c. Both productions from SS can generate sentences starting with if, causing First/First and Follow conflicts.

  • More directly, G2G_2 does not unambiguously restrict the dangling-else structure because the else-branch in MM produces SS, which allows un-matched if statements inside it to cause ambiguity in nested conditionals.

  • Hence, G2G_2 is also ambiguous and consequently not $LL(1)$ [46].

Thus:

  • Option (C) is CORRECT (G1G_1 and G2G_2 are not LL(1)LL(1)) [46].

  • Option (D) is CORRECT (G1G_1 and G2G_2 are ambiguous) [46].

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Parsing previous year question is Q51 of the GATE CSE 2025 Set 2 paper. Find more GATE CSE PYQs on Parsing in Compiler Design, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Parsing PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Parsing in GATE CSE

Parsing has 23 GATE CSE PYQs across 16 papers from 2012 to 2026. This is the only 2-mark MSQ among them, and GATE CSE 2025 Set 2 has 2.

How GATE tests Parsing

More from GATE CSE 2025 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2025 Set 2 · Q51 · MSQ

Consider two grammars G1 and G2 with the production rules given below: G1: S → if E then S if E then S else S a E → b G2: S → if E then S M M → if E then M else S c E → b where if, then, else, a, b, c are the terminals. Which of the following option(s) is/are CORRECT?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.