Search code examples
context-free-grammarformal-languages

constructing CFG


How can I construct a Context-Free Grammer for the language x^a y^b z^2(a+b) where a>=0, b>=0. Thanks for helps...


Solution

  • Think of it this way

    x^a y^b z^2(a+b) = x^a y^b z^2a z^2b = x^a y^b (z^2)^b (z^2)^a 
    

    Therefore

    S -> xSzz | S1
    S1 -> yS1zz | e