3

I was practicing for an exam of CFG and I'm struggling with this CFG:

$L =\{a^ib^jc^k | j \le i+k\}$

I tried this CFG but is ambiguous:

$$S\to AXC$$ $$A\to aA|\lambda$$ $$C\to cC|\lambda$$ $$X\to YZ$$ $$Y\to aYb|\lambda$$ $$Z\to bZc|\lambda$$

If anyone can help me with this expression to make it unambiguous I will really appreciate it.

Parcly Taxel
  • 103,344

1 Answers1

1

We distinguish between the $i\ge j$ and $i<j$ cases, making separate CFGs for each. In the first case a left nonterminal will generate $i-j$ excess $a$'s, a middle nonterminal will generate each remaining $a$ and $b$ in nested pairs and a right nonterminal will exclusively generate $c$'s: $$S\to AXC$$ $$A\to aA|\varepsilon$$ $$X\to aXb|\varepsilon$$ $$C\to cC|\varepsilon$$ In the second case a left nonterminal will generate $i$ nested pairs of $a$ and $b$, a middle nonterminal will generate $b$-$c$ nested pairs (at least one this time since there is at least one more $b$ and there are at least as many $c$'s as $b$'s in the part not covered by the left nonterminal) and a right nonterminal is as before: $$S\to XYC$$ $$Y\to bYc|bc$$ Thus the final unambiguous CFG is $$S\to AXC|XYC$$ $$A\to aA|\varepsilon$$ $$X\to aXb|\varepsilon$$ $$Y\to bYc|bc$$ $$C\to cC|\varepsilon$$

Parcly Taxel
  • 103,344