If \(G\) is a grammar with productions \(S\rightarrow SaS\mid aSb\mid bSa\mid…
2017
If \(G\) is a grammar with productions
\(S\rightarrow SaS\mid aSb\mid bSa\mid SS\mid\epsilon\)
where \(S\) is the start variable, then which one of the following strings is not generated by \(G\) ?
- A.
\(abab\) - B.
\(aaab\) - C.
\(abbaa\) - D.
\(babba\)
Attempted by 113 students.
Show answer & explanation
Correct answer: D
Key insight: every derivation preserves the property that the number of a's is at least the number of b's.
Reason:
ε has #a − #b = 0.
If x and y are derivable with differences dx and dy, then S → S S gives difference dx + dy (addition preserves nonnegativity).
S → S a S adds one a, increasing the difference by 1.
S → a S b and S → b S a add one a and one b, so they do not change the difference.
Thus every string generated satisfies #a − #b ≥ 0. The string babba has 2 a's and 3 b's (difference −1), so it cannot be generated by the grammar. The other listed strings have at least as many a's as b's and can be constructed (see individual option feedback for short derivations).
A video solution is available for this question — log in and enroll to watch it.