Show that the CFG with productions S→ a | Sa | bSS | SSb | SbS is ambiguous?

4.2k Views Asked by At

How does one know which string to choose when nothing is given in the question?

2

There are 2 best solutions below

2
On

HINT: You can use $abaa$. Try it yourself; if you get stuck, I’ve left two leftmost derivations in the spoiler-protected block below.

$S\Rightarrow Sa\Rightarrow SbSa\Rightarrow abSa\Rightarrow abaa$ and $S\Rightarrow SbS\Rightarrow abS\Rightarrow abSa\Rightarrow abaa$

0
On

I generally start by looking for a single production with repeated nonterminals that easily generate the same sequence twice. In your case I 'hit jackpot' with the last: S -> SbS. This generates the sequence SbSbS two ways: (SbS)bS and Sb(SbS). I don't think the same thing happens with S->bSS or S->SSb.

Failing that, I would try to combine two or more productions to achieve the same effect. This works with the productions S->bSS and S->SSb, which combine to produce the string bSSSb two ways: (bSS)Sb or bS(SSb).

Failing that, look for ways to produce the same terminal(s) at different heights of the parse tree. That leads to the approach implied by @Brian M. Scott.