Question:hard

Consider the following grammar where 𝑆 is the start symbol, and π‘Ž and 𝑏 are
terminal symbols.
𝑆 β†’π‘Žπ‘†π‘π‘† ∣ 𝑏𝑆 ∣ Ο΅
Which of the following statements is/are true?

Show Hint

Since every 'a'-led derivation starts with aSbS, count how many ways the literal 'b' in that production can be matched against the b's in the target string to test ambiguity, then recall that all context-free languages are decidable.
Updated On: Aug 3, 2026
  • The grammar is ambiguous
  • The string π‘Žπ‘π‘ has two distinct derivations in this grammar
  • The string π‘Žπ‘π‘Žπ‘ has only one rightmost derivation
  • The language generated by the grammar is undecidable
Show Solution

The Correct Option is A, B, C

Solution and Explanation

A useful way to understand this grammar is to notice what language it generates: \(S \to aSbS \mid bS \mid \epsilon\) generates strings over a and b where every 'a' is eventually matched with a later 'b' introduced by its own production, while extra unmatched b's can appear anywhere via the \(bS\) rule. This is a classic textbook example of an ambiguous grammar.

Step 1: Verify ambiguity using the string 'abb'.

Think of 'a' as needing a matching 'b' somewhere after it. In 'abb' there are two b's after the single 'a', so either of the two b's could be treated as the matching one used inside the \(aSbS\) production, with the other b generated separately by the trailing S factor. This gives two genuinely different parse trees for the same string 'abb', so the grammar is ambiguous, confirming option A, and directly confirming option B since 'abb' indeed has two distinct derivations.

Step 2: Check 'abab' for uniqueness using the same matching idea.

Here the single 'a' needs a matching 'b' drawn from the two available b's in 'bab' (the part after the leading a). If we try to match the 'a' with the second b (the last character), the leftover before it would be 'ba', which would have to be generated entirely by the S factor before the matched b. But 'ba' can never be produced by S, since S never derives a string ending in an unmatched trailing 'a'. So this matching choice is impossible. Only matching the 'a' with the first b works, forcing a single unique tree, so 'abab' has just one rightmost derivation, confirming option C.

Step 3: Address option D about decidability.

Any language defined by a context-free grammar is decidable in principle (parsing algorithms like CYK or Earley always terminate and correctly answer membership), so calling this language 'undecidable' is incorrect regardless of the grammar's ambiguity. Ambiguity is a property of the grammar, not a barrier to decidability of the language. So option D is false.

Step 4: Final selection.

The true statements are A, B, and C.

Was this answer helpful?
0


Questions Asked in GATE CS exam