-2

S → aS | aSbS | (empty) where the alphabet is {a,b}

in other words, the set of strings where any prefix has at least as many 'a's as 'b's.

Raphael
  • 73,212
  • 30
  • 182
  • 400
hollow7
  • 527
  • 3
  • 7

1 Answers1

1

A grammar that can do this unambiguously is:

$S \to aS \mid A S \mid \epsilon$
$A \to a AAb \mid \epsilon$

Every b is associated with an a in front of it, and anything between these is also associated in the same way so there is always balance.

Raphael
  • 73,212
  • 30
  • 182
  • 400
lPlant
  • 1,622
  • 9
  • 19