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 , we need a CFG that generates strings where the total length of the string is even. The language described can be expressed as , which represents strings of even length over the alphabet .
CFG for Even-Length Strings
Here’s a CFG that generates all strings of even length over the alphabet :
1.Start Symbol: S
2.Production Rules:
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 generates a single character (either or ), and generates two such characters, ensuring that the total length is always even.
2. :
- These rules generate individual characters or , which are used by to create strings of even length.
Example Derivations:
For the string :
- For :
- Combining these, we get: .
For the string :
- Applying and :
- First pair: and gives
- Second pair: and gives .
- Combining these, we get: .
For the string :
- First pair: and gives .
- Second pair: and gives .
- Combining these, we get: .
This CFG ensures that all generated strings are of even length by using the production to pair up characters, guaranteeing that the total length of the string is always even.