Similar questions 1 more →
Context-Free Grammars & Languages

Answer & solution
Official answer: B, D
Givens and goal
The grammar is S→abaABA bba (spaces only separate symbols), A→aaBBA b | bB abaa, and B→aBb | ab. For a generated string s let n₁ count a’s and n₂ count b’s. Assess A: some n₁<n₂; B: always n₁≥n₂; C: some n₁>2n₂; D: always n₁≤2n₂.
Approach
Track the pair of terminal counts through productions and prove invariant inequalities by induction rather than enumerate all words.
Step-by-step solution
Write a(w) and b(w) for the numbers of the two terminals in a generated word w.
1. Every expansion of B produces ak bk for some k≥1. Hence its two counts are equal.
2. For A's terminating alternative, a B expansion with k of each terminal gives counts a=k+3 and b=k+2. Thus b≤a≤2b.
3. Suppose a word derived from the recursive A child has counts x,y satisfying y≤x≤2y. The surrounding recursive production contributes two a terminals and one b terminal, plus two B words. If those B words together contribute K of each terminal, the new counts are x+K+2 and y+K+1. Their difference is (x−y)+1≥0. Also twice the new b count minus the new a count is (2y−x)+K≥0. Thus both inequalities are invariant under recursion.
4. The start production combines two A words, one B word and an outer string with three a terminals and three b terminals. Adding the counts preserves b≤a≤2b. Consequently every complete word satisfies the two bounds in B and D. The existential violations in A and C are impossible.
Common error: Counting only the explicitly written terminals of a production omits arbitrarily long balanced B expansions. The invariant accounts for them without enumerating strings.
Terminal-count bookkeeping
Production component Added a's Added b's
A→bB abaa, excluding B 3 2
A→aaBBA b, excluding B,B,A 2 1
S→aba A B A bba, excluding A,B,A 3 3For example, choosing B→ab and the terminating A alternative gives each A counts (4,3). The shortest choices in S therefore yield totals (3,3)+(4,3)+(1,1)+(4,3)=(12,10), consistent with both bounds. This example illustrates the invariant but the induction, not the example alone, proves it for arbitrarily deep recursion.