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

Generating CFG for a Language


Consider the language {anbmcp | n <= p OR m <= p} , create a CFG for this language.
I have started this with S -> aA | aB, but am unsure how I should go about defining A or B. The "OR" seems quite difficult to incorporate into the language's definition as it doesn't seem necessary to track both n and m and compare them against p, yet I don't know which one I want to track


Solution

  • In order to maintain that constraint, for every 'a' you need to add a 'c'. Similarly for every 'b' you should add a 'c'.

    A -> aAC | aC | B

    B -> bB | bC

    C -> cC | c

    I could be wrong here. But that's how you should think while creating a CFG.