GATE / CS ARCHIVE

SIMILAR QUESTIONS / THEORY OF COMPUTATION

Extract terminal-count invariants from productions

2 questions solved the same way, newest first. Back to Theory of Computation

How these are solved

Track the terminal contribution of each recursive production and the terminating production, then test claimed counts/order constraints. The grammars and options differ.

2 marks · MSQ
Report issue

Counting terminal-symbol invariants

Context-Free Grammars & Languages

Question 52, GATE 2026: 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₂.
Tap to enlarge
Select all that apply
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           3

For 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.

Original paper, page 40 · Answer key

2 marks · MSQ
Report issue

Count terminals by production applications

Context-Free Grammars & Languages

Question 52, GATE 2024: Grammar S→aS | aSbS | c generates w. Let na,nb,nc count the respective terminals in w. Which always hold? A na>nb; B na>nc−2; C nc=nb+1; D nc=2nb.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, C

Approach: Count how each production changes both the terminal totals and the outstanding nonterminals.

1. Derivation. Let u count applications of S→aS and v count applications of S→aSbS. Then na=u+v and nb=v. A derivation starts with one S; unary rules leave the number of outstanding S unchanged, binary rules increase it by one, and S→c decreases it by one. Thus nc=v+1, proving C. Also na≥v=nc−1>nc−2, proving B. A fails for w=c, where both a and b counts are 0; D also fails for c, whose c count is 1 and b count 0.

2. Explanation and checks. Each binary rule contributes one b and creates one additional S requiring eventual termination by a c-rule. Starting with one S therefore requires exactly one more terminating c-rule than binary rules. Unary aS rules can add any extra a’s without changing b or c counts. Consequently na−nb equals their nonnegative number; it may be zero, so A’s strict inequality is too strong. In B the weaker strict bound still holds because na≥nc−1 is an integer stronger than na>nc−2.

Original paper, page 36 · Answer key