Skip to content
Official PYQ · 2007

GATE CS 2007 Q52 — Parsing (LL, LR)

Explain with AI

Consider the grammar with non-terminals N={S,C,S1}N=\left\{S,C,S_1\right\}, terminals T={a,b,i,t,e}T=\left\{a, b, i, t, e\right\}, with SS as the start symbol, and the following set of rules:

S→iCtSS1∣aS \rightarrow iCtSS_1 \mid a

S1→eS∣ϵS_1 \rightarrow eS \mid \epsilon

C→bC \rightarrow b

The grammar is NOT LL(1) because:

Choose one option
View worked solution

Answer: C

The given grammar represents the classic dangling-else problem in programming languages, where:

  • ii stands for if
  • CC stands for condition
  • tt stands for then
  • ee stands for else

An LL(1) grammar must be unambiguous. Let's analyze why this grammar is not LL(1):

1. Ambiguity Analysis

Consider the string: w=ibtibtaeaw = ibtibtaea (which represents if b then if b then a else a).

This string has two distinct leftmost derivations (and thus two different parse trees):

Derivation 1 (associating the else with the inner if):

S⇒iCtSS1⇒ibtSS1⇒ibt(iCtSS1)S1⇒ibtibtaS1S1⇒ibtibtaeSS1(using S1→eS)⇒ibtibtaeaS1⇒ibtibtaea(using S1→ϵ)\begin{aligned} S &\Rightarrow iCtSS_1 \\ &\Rightarrow ibtSS_1 \\ &\Rightarrow ibt(iCtSS_1)S_1 \\ &\Rightarrow ibtibtaS_1S_1 \\ &\Rightarrow ibtibtaeSS_1 \quad (\text{using } S_1 \rightarrow eS) \\ &\Rightarrow ibtibtae a S_1 \\ &\Rightarrow ibtibtaea \quad (\text{using } S_1 \rightarrow \epsilon) \end{aligned}

Derivation 2 (associating the else with the outer if):

S⇒iCtSS1⇒ibtSS1⇒ibtS(eS)(using S1→eS)⇒ibt(iCtSS1)eS⇒ibtibtaS1eS⇒ibtibtaeS(using S1→ϵ)⇒ibtibtaea\begin{aligned} S &\Rightarrow iCtSS_1 \\ &\Rightarrow ibtSS_1 \\ &\Rightarrow ibtS(eS) \quad (\text{using } S_1 \rightarrow eS) \\ &\Rightarrow ibt(iCtSS_1)eS \\ &\Rightarrow ibtibtaS_1eS \\ &\Rightarrow ibtibtaeS \quad (\text{using } S_1 \rightarrow \epsilon) \\ &\Rightarrow ibtibtaea \end{aligned}

Since the grammar is ambiguous, it cannot be parsed by any LL(1) parser.

2. Elimination of Other Options

  • Option A: There is no left-recursion in the grammar (no production of the form A→AαA \rightarrow A\alpha).
  • Option B: Right-recursion (e.g., S1→eSS_1 \rightarrow eS) is perfectly fine and does not prevent a grammar from being LL(1).
  • Option D: The grammar is context-free because every production has a single non-terminal on its left-hand side.

Hence, the correct option is C.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Parsing (LL, LR) questions