Search code examples
context-free-grammarformal-languagescontext-free-language

Construction of a context free grammar


So, I have this language L={a^i b^2j+1 / i<>j} and I have to generate a context free grammar based on it, can you please help me in illustrating the steps in doing that.

So far I have this:

    S-->aS/aBbb
    B-->bB/b/e(empty)

but I am not sure if it is correct, please help me understand it.


Solution

  • For languages with a "not-equal" restriction, the easiest approach is usually to first find a grammar that corresponds to the language with an "equal" restriction instead, and then change it to require more of one of the things.

    In this case we have a number of a tokens followed by an odd number of b tokens where the constraint is on the number of each. For the equal case, that becomes just

    S → aSbb | b

    a single b with the same number of as and pairs of bs wrapped around it.

    To make it not-equal, we need to add either extra as or extra pairs of Bs, but not both:

    S → AS' | S'B
    S' → aS'bb | b
    A → Aa | a
    B → Bbb | bb