I know how to construct the context-free grammars for strings that have same counts of a
and b
, or have the same counts of c
and d
:
S → ε S → ε
S → SASBS S → SCSDS
S → SBSAS S → SDSCS
A → a C → c
B → b D → d
But, I don't know how to create a grammar for strings where the counts of a
and b
are the same, and the counts of c
and d
are the same. My attempts fail for strings like cadbdabc
where the pairings are between each other (ex: c, <a>, d, <b>
). So, I would appreciate if someone could help out on this.