S -> aSb | A | B
A -> aS | a
B -> Sb | b
is this the language generated by this CFG? Or am I missing something?
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It only takes a minute to sign up.
Sign up to join this communityS -> aSb | A | B
A -> aS | a
B -> Sb | b
is this the language generated by this CFG? Or am I missing something?
A technique that can be useful here is to eliminate some of the variables. Since $A$ can only be replaced by $aS$ or $a$ and $B$ by $Sb$ or $b$ we have the equivalent grammar $$ S\rightarrow aSb\mid aS\mid a\mid Sb\mid b $$ Now notice that in any sentential form in a derivation we can only add $a$s to the left of $S$ and we can only add $b$s to the right, so any string of terminals derived from the start variable must have the form $a^mb^n$. Finishing off, we see that $S$ must eventually be replaced by one of $a$ or $b$, so we see that your proposed answer was almost correct: $$ L(G) = \{a^mb^n\mid m,n\ge 0\text{ and }m,n\text{ not both }0\} $$ or, if you prefer a slightly nonstandard regular expression-ish version $$ a^*b^*\setminus\epsilon $$