GATE / CS ARCHIVE

GATE CSE 2007 / QUESTION 78

String generated by equal-count grammar

Theory of Computation · Context-Free Grammars & Languages · Full GATE CSE 2007 paper

GATE 2007, Q78 2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 78, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. Which string is generated?
Options:
(A) aaaabb
(B) aabbbb
(C) aabbab
(D) abbbba
Question 78, GATE 2007: A context-free grammar has nonterminals {S,A,B}, terminals {a,b}, start S, and productions S→aB | bA; B→b | bS | aBB; A→a | aS | bAA. Which string is generated?
Options:
(A) aaaabb
(B) aabbbb
(C) aabbab
(D) abbbba
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step through it (4 derivations)

1. Approach. Track the invariant #a−#b for strings derived from each nonterminal: it is 0 from S, +1 from A and −1 from B. Reject options violating the invariant, then construct a derivation for the survivor.

2. Derivation and interpretation. Every S-derived string has equal numbers of a and b; A-derived strings have one more a, and B-derived strings one more b. These invariants follow directly from all eight productions. Only aabbab among the four options has equal counts. It is generated by S => aB => aaBB => aabB => aabbS => aabbaB => aabbab. Thus C.

Original paper, page 20