Context free grammar for language

148 Views Asked by At

I'm learning how to generate context-free grammar for a language.

$L=\{{a}^i {b}^j {c}^k\, |\,i=j\lor j=k$

Here is how I tried to solve it. First I take a quick look at what words I can generate with such a language.

Same number as than b or c but not both. Meaning that if i = 0 then i = j = 0 but k != 0.

{c, b, ab, ac, aabb, aacc, abbc}

So I need a rule for generate either a number of cs or empty

${C\rightarrow cC\,|\,\epsilon}$

Same for b

${B\rightarrow bB\,|\,\epsilon}$

And then a rule that either generate the same number of as than bs and any number cs

${S\rightarrow abC\,|\,acB}$

And all together

$${S\rightarrow abC\,|\,acB}$$ $${B\rightarrow bB\,|\,\epsilon}$$ $${C\rightarrow cC\,|\,\epsilon}$$

Is my approach right?

1

There are 1 best solutions below

0
On

The language $L$ has strings $a^ib^jc^k$, where $i=j$ or $j=k$.

Your approach isn't quite right. The rule ${S\rightarrow abC\,|\,acB}$ allows you to make only one $a$ and in the other option the $b$'s go to wrong place.

Think it like this: when you want to generate a word, you must first choose if there are equal number of $a$'s and $b$'s or equal number of $b$'s and $c$'s.

Have you tried to do easier exercises like the languge $\{ a^nb^n \mid n\geq 0\}$?