GATE / CS ARCHIVE

GATE CSE 2023 / QUESTION 39

GATE CSE 2023 Q39

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

GATE 2023, Q39 2 marks · MCQ
Report issue

Context-Free Grammars & Languages

Question 39, GATE 2023: For grammar S→aSb|X and X→aX|Xb|a|b with start S, identify the language: A (a+b)*; B a*(a+b)b*; C a*b*(a+b); D a nonregular language.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

X generates every nonempty string of the form aⁱbʲ: its alternatives prepend a or append b and terminate at a or b. These operations never introduce a b before an a. Wrapping with S→aSb preserves that form and adds no strings beyond those already generated by X. Thus L(G)=a*b* minus epsilon, equivalently a*(a+b)b*, option B. The grammar may be context-free while its generated language is regular.

Teaching detail. For completeness, any aⁱbʲ with i+j≥1 can be derived from X. If i≥1, terminate with a and use i−1 prepending steps plus j appending steps. If i=0 and j≥1, terminate with b and append j−1 further b's. Conversely every X-production preserves the a-before-b ordering and cannot create ε. The S wrapping rule also preserves this ordering and nonemptiness. Hence it neither removes any X-generated word nor introduces a word outside the claimed regular language.

Original paper, page 40 · Answer key