How can I construct a context-free grammar for the following language?
The language is: $L = \{{(0 + 1)^* | \text{#}0 = 3p, \text{#}1 = 5q, p, q \geq 0} \}$. I can construct a CFG for L if the zeroes and ones are grouped, such as strings of the form 000…11111…000…11111…. However, I’m struggling to create a grammar that works when zeroes and ones are mixed together, for example, 00101111.
My approach so far:
- I define 2 production rules S → $A_0$ | $A_1$
$A_0$ is the start of generating zeroes. $A_1$ is the start of generating ones. - For zeroes I use:
$A_0$ → $0B_0A_1$|ϵ
$B_0$ → $0C_0A_1$
$C_0$ → $0A_1$ - For ones I use:
$A_1$ → $1B_1A_0$|ϵ
$B_1$ → $1C_1A_0$
$C_1$ → $1D_1A_0$
$D_1$ → $1E_1A_0$
$E_1$ → $1A_0$
This ensures that groups of three zeroes and five ones are generated, but there’s a problem. If I switch from generating zeroes to generating ones (or vice versa), I must immediately generate all three zeroes or five ones before switching back. This forces strings like 00011111 but doesn’t allow mixed strings like 00101111. How do I break the problem down so I can sort of keep count even If I mix the symbols?