Search code examples
context-free-grammarcontext-free-language

Finding a context-free grammar for the given language


I am trying to find a CFG for the language A below.

I have spent hours on this but still could not find the answer. I also came up with the idea that this may not a context-free language but there is actually a PDA that recognizes it.

I would very much appreciate it if anyone can help me with this.

  A={0^a 1^b 0^c 1^d| a+b < c+d, a,b,c,d>=1}

Solution

  • A good way to deal with inequalities like this is to start with a grammar for the equality, and then add one or more extra symbols.

    Here's a simpler example, which might get you started. Let's look at the language

    L = {0a1b0c | a+b < c, a,b,c ≥ 1}

    We start with the language where the relationship between the lengths is equality:

    L' = {0a1b0c' | a+b = c', a,b,c' ≥ 1}

    Now, it should be clear that

    L = {ω 0c"|ω ∈ L', c" ≥ 1}

    In other words, L consists of a string from L' followed by one or more zeros. The original c has been split into c' + c".

    It's straightforward to write a grammar for L given L':

    L ⇒ L' 0
    L ⇒ L 0
    

    Or, if you prefer:

    L ⇒ L' Z
    Z ⇒ 0  
    Z ⇒ Z 0  
    

    So now we just need to work on L'. Since we know that c' = a + b, we can replace 0c' with 0b0a, giving us:

    L' = {0a1b0b0a | a,b ≥ 1}

    That's revealed to be the combination of an inner language with the same number of 1s and 0s, surrounded by an equal number of leading and trailing 0s. In other words:

    L' ⇒ 0 M 0
    L' ⇒ 0 L' 0 
    M ⇒ 1 0
    M ⇒ 1 M 0
    

    That gets you about half-way. Good luck with the original question.