Skip to content
Official PYQ · 2017

GATE CS 2017 Set 1 Q34 — Context-free Grammars and Push-down Automata

Explain with AI

If GG is a grammar with productions

S→SaS∣aSb∣bSa∣SS∣ϵS\rightarrow SaS\mid aSb\mid bSa\mid SS\mid\epsilon

where SS is the start variable, then which one of the following strings is not generated by GG?

Choose one option
View worked solution

Answer: D

To determine which string cannot be generated by the grammar GG, we can analyze the properties of the strings generated by its production rules:

S→SaS∣aSb∣bSa∣SS∣ϵS \rightarrow SaS \mid aSb \mid bSa \mid SS \mid \epsilon

1. Finding the Invariant of the Grammar

Let Na(w)N_a(w) and Nb(w)N_b(w) denote the number of occurrences of the symbols aa and bb in a string ww, respectively. Let's analyze how each production affects these counts:

  • S→ϵS \rightarrow \epsilon: Na=0N_a = 0 and Nb=0N_b = 0. Here, Na≥NbN_a \ge N_b.
  • S→aSbS \rightarrow aSb: Adds exactly one aa and one bb. If the inner SS satisfies Na≥NbN_a \ge N_b, then the resulting string also satisfies Na≥NbN_a \ge N_b.
  • S→bSaS \rightarrow bSa: Adds exactly one bb and one aa. If the inner SS satisfies Na≥NbN_a \ge N_b, then the resulting string also satisfies Na≥NbN_a \ge N_b.
  • S→SaSS \rightarrow SaS: Adds one aa and no bb. This strictly increases the count of aa relative to bb, preserving Na≥NbN_a \ge N_b.
  • S→SSS \rightarrow SS: Concatenates two strings. If both satisfy Na≥NbN_a \ge N_b, their concatenation also satisfies Na≥NbN_a \ge N_b.

By induction, every string ww generated by GG must satisfy the invariant:

Na(w)≥Nb(w)N_a(w) \ge N_b(w)

2. Testing the Options

  • Option A (abababab): Na=2N_a = 2, Nb=2N_b = 2. Since 2≥22 \ge 2, it satisfies the invariant.
    Derivation: S⇒SS⇒aSbS⇒abS⇒abaSb⇒ababS \Rightarrow SS \Rightarrow aSb S \Rightarrow ab S \Rightarrow ab aSb \Rightarrow abab
  • Option B (aaabaaab): Na=3N_a = 3, Nb=1N_b = 1. Since 3≥13 \ge 1, it satisfies the invariant.
    Derivation: S⇒SaS⇒aSaS⇒aaSbS⇒aaabS \Rightarrow SaS \Rightarrow a SaS \Rightarrow a aSb S \Rightarrow aaab
  • Option C (abbaaabbaa): Na=3N_a = 3, Nb=2N_b = 2. Since 3≥23 \ge 2, it satisfies the invariant.
    Derivation: S⇒SaS⇒aSbaS⇒abbaS⇒abbaSaS⇒abbaaS \Rightarrow SaS \Rightarrow aSb a S \Rightarrow abba S \Rightarrow abba SaS \Rightarrow abbaa
  • Option D (babbababba): Na=2N_a = 2, Nb=3N_b = 3. Since 2<32 < 3, this string violates the invariant Na(w)≥Nb(w)N_a(w) \ge N_b(w) and therefore cannot be generated by GG.

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

Published
Report a correction
Keep exploringMore Context-free Grammars and Push-down Automata questions