Context-Free Grammars & Languages

Answer & solution
Official answer: A, B, C
Givens and goal
For the grammar S→aSbS | bS | ε, decide: A the grammar is ambiguous; B abb has two distinct derivations; C abab has only one rightmost derivation; D the generated language is undecidable.
Approach
Exhibit different parse structures for the ambiguity claim, prove uniqueness for the specific other word, and use the decidability of context-free membership.
Step-by-step solution
Work with the grammar S → aSbS | bS | ε. Here ε denotes the empty string.
1. The string abb admits these two different leftmost derivations:
- S ⇒ aSbS ⇒ abS ⇒ abbS ⇒ abb. - S ⇒ aSbS ⇒ abSbS ⇒ abbS ⇒ abb. In the first derivation, the extra b comes from the final S of the root production. In the second, it comes from the first S. Thus they represent different parse trees, not just different orders of expanding one tree. Both ambiguity (A) and the existence of two distinct derivations (B) follow.
2. For abab, the root must use aSbS because the word starts with a. Its explicit b must be the b in position 2 or position 4. Choosing position 4 would require the first S to generate ba. That is impossible: every nonempty word generated by S ends in b. To prove this invariant, bS ends in b if its child is empty or ends in b if its child does; aSbS has the same property through its explicit b and final child.
3. Consequently the explicit root b is in position 2, its first child is ε, and its final child generates ab. The latter has only the construction aSbS with both children empty. Therefore abab has exactly one parse tree and one rightmost derivation, establishing C. One rightmost derivation is S ⇒ aSbS ⇒ aSbaSbS ⇒ aSbaSb ⇒ aSbab ⇒ abab.
4. This is a context-free grammar. Membership in a context-free language is decidable, for example by converting to a suitable normal form and applying a finite parsing algorithm. Therefore the undecidability claim D is false.
Common error: Two unrestricted derivations can differ solely in expansion order. Ambiguity requires different parse trees, equivalently different leftmost derivations or different rightmost derivations; the witness above meets that stronger test.
Parse-tree distinction for abb can be made explicit by writing the root’s first and last S expansions as a pair. One tree has root a S b S with child words (ε,b); the other has child words (b,ε). Both concatenate to abb, but they attach the additional b to different children. This makes the ambiguity evidence structural, not just a consequence of expanding independent children in different orders.