Question

If GG is a grammar with productions

S→SaS∣aSb∣bSa∣SS∣ϵS \to 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
Official answer and solution
Official answer
D
From
Official GATE 2017 answer key

Solution

Find an invariant property relating the counts of terminals 'a' and 'b' across all production rules in grammar G.

Examine the production rules of the grammar:

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

Let na(w)n_a(w) and nb(w)n_b(w) denote the number of occurrences of 'a' and 'b' in string ww:

  • S→SaSS \to SaS introduces one 'a' and zero 'b's: Δ(na−nb)=+1\Delta(n_a - n_b) = +1.
  • S→aSbS \to aSb introduces one 'a' and one 'b': Δ(na−nb)=0\Delta(n_a - n_b) = 0.
  • S→bSaS \to bSa introduces one 'a' and one 'b': Δ(na−nb)=0\Delta(n_a - n_b) = 0.
  • S→SSS \to SS preserves the sum of differences from each subderivation.
  • S→ϵS \to \epsilon produces the empty string with na=nb=0n_a = n_b = 0.

Because every production rule either increases nan_a by 1 without increasing nbn_b, or increases both nan_a and nbn_b by 1 equally, every string generated by GG must satisfy the invariant:

na(w)≥nb(w)n_a(w) \ge n_b(w)

Check each candidate string:

  • (A) abab: na=2,nb=2  ⟹  na≥nbn_a = 2, n_b = 2 \implies n_a \ge n_b. Generated by S  ⟹  SS  ⟹  aSb aSb  ⟹  ababS \implies SS \implies aSb \, aSb \implies abab.
  • (B) aaab: na=3,nb=1  ⟹  na≥nbn_a = 3, n_b = 1 \implies n_a \ge n_b. Generated by S  ⟹  SaS  ⟹  …S \implies SaS \implies \dots
  • (C) abbaa: na=3,nb=2  ⟹  na≥nbn_a = 3, n_b = 2 \implies n_a \ge n_b.
  • (D) babba: na=2,nb=3  ⟹  na<nbn_a = 2, n_b = 3 \implies n_a < n_b. This violates the invariant and can never be generated by GG.
Final answer

D

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

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 1
Question
34 of 65, printed as CS Q.34
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.