Context Free Grammar (CFG) for Even String ((a+b)(a+b))* | CFG for Even String ((a+b)(a+b))*

 Context Free Grammar (CFG) for Even String ((a+b)(a+b))* |  CFG for Even String ((a+b)(a+b))* 

×

×

×

L= { ε,  aa , bb , ab , ba,  ………………………..}

OR
To construct a context-free grammar (CFG) for the language {ww is an even-length string over {a,b}}{w mid w text{ is an even-length string over } {a, b}}, we need a CFG that generates strings where the total length of the string is even. The language described can be expressed as (a+b)(a+b)(a+b)(a+b)^*, which represents strings of even length over the alphabet {a,b}{a, b}.
CFG for Even-Length Strings

Here’s a CFG that generates all strings of even length over the alphabet {a,b}{a, b}:
1.Start Symbol: S
2.Production Rules:
  • SAAS rightarrow AA
  • AabA rightarrow a mid b
Explanation of the Rules:

1. :
  • This rule ensures that the string generated has an even length by generating two symbols at a time. The non-terminal AA generates a single character (either aa or bb), and SS generates two such characters, ensuring that the total length is always even.
2. :
  • These rules generate individual characters aa or bb, which are used by SS to create strings of even length.

Example Derivations:


For the string abab:

  • SAAS Rightarrow AA
  • For AA:
    • AaA Rightarrow a
    • AbA Rightarrow b
  • Combining these, we get: aba , b.

For the string aabbaabb:

  • SAAS Rightarrow AA
  • Applying AaA rightarrow a and AbA rightarrow b:
    • First pair: AaA rightarrow a and AaA rightarrow a gives aa.
    • Second pair: AbA rightarrow band AbA rightarrow b gives bbbb.
  • Combining these, we get: aabbaabb.

For the string abababab:

  • SAAS Rightarrow AA
  • First pair: AaA rightarrow a and AbA rightarrow b gives abab.
  • Second pair: AaA rightarrow a and AbA rightarrow b gives abab.
  • Combining these, we get: abababab.
This CFG ensures that all generated strings are of even length by using the production SAAS rightarrow AA to pair up characters, guaranteeing that the total length of the string is always even.

Leave a Comment