GATE / CS ARCHIVE

GATE CSE 2025 SET 1 / QUESTION 19

Deriving a grammar’s two alternatives

Theory of Computation · Context-Free Grammars & Languages · Full GATE CSE 2025 Set 1 paper

GATE 2025 Set 1, Q19 1 mark · MCQ
Report issue

Context-Free Grammars & Languages

Question 19, GATE 2025: A grammar has start symbol S and productions S→aaB | Abb, A→a | aA, B→b | bB. Choose its language: A {a²b^n:n≥1}∪{a^nb²:n≥1}; B {a^nb^(2n):n≥1}∪{a^(2n)b^n:n≥1}; C {a^nb^n:n≥1}; D {a^(2n)b^(2n):n≥1}.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: First identify what each helper nonterminal can generate, then substitute into each start production.
A→a is the base case. Each application of A→aA adds one a before eventually taking the base case, so A generates exactly an for n≥1. Similarly B generates exactly bn for n≥1. Neither can generate the empty string.
Taking S→aaB fixes the number of a’s at two and allows any positive number of b’s. Taking S→Abb allows any positive number of a’s and fixes the number of b’s at two. The language is the union of these alternatives, which is A.
For example aabbb is generated by the first branch, and ab b (the word abb) by the second. The grammar does not synchronize the two counts, so the equality and ratio conditions in B–D are unjustified. The common word aabb belongs to both branches but is counted just once in the language.

Original paper, page 19 · Answer key