Context-Free Grammars & Languages


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.