1

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:

  1. 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.
  2. For zeroes I use:
    $A_0$$0B_0A_1$
    $B_0$$0C_0A_1$
    $C_0$$0A_1$
  3. 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?

brodar
  • 177

2 Answers2

1

Hint 1: This language is actually regular. Do you know a theorem about grammars of regular languages?

Hint 2: have 15 nonterminals named $A_{0,0}\ A_{0,1}\ A_{0,2}\ A_{1,0}\ \dots\ A_{4,2}$ and use the nonterminals to sort of keep count of both symbols at the same time.

MJD
  • 67,568
  • 43
  • 308
  • 617
  • Which theorem? I know that regular languages are contained in CFG and that for any regular language, I could construct a DFA, so maybe a conversion from DFA to CFG? Is there any shorter way than 15 nonterminals? – brodar Nov 19 '24 at 10:46
  • Also, did you reason that it is regular because from the CFG, it is apparent we could construct a DFA, so it's regular? Or what logic did you use to determine it was regular? – brodar Nov 19 '24 at 10:52
  • The theorem I was thinking of is that a regular language can always be represented by a right-regular grammar. If you don't know this theorem, that's OK, it's not important. But since you asked, the reason I knew this language was regular is because it's easy to imagine it recognized by a finite machine. The machine only needs to count how many more zeroes and ones it needs to see to get to the next multiple of 3 or 5, and that's a finite amount of information. And the grammar you suggested below captures this intuition. – MJD Nov 19 '24 at 14:57
0

@MJD so something along these lines:

S → 0$A_{01}$ | 1$A_{10}$
$A_{01}$ → 0$A_{11}$ | $1A_{02}$
...
$A_{24}$ → 0$A_{04}$ | $1A_{20}$

So 3x5 = 15 states and the start state which is equivalent to $A_{00}$. When I add a zero the left part of the subscript increases and for a one I increase the right part of the subscript.

brodar
  • 177
  • I would $S\to A_{00}$, because then it's more uniform, but sure. Don't forget you need an $A_{??}\to\epsilon$ production somewhere. – MJD Nov 19 '24 at 14:38
  • 1
    Oh, and when you write this up, don't forget that $L$ includes the empty string! Your grammar above doesn't generate $\epsilon$. – MJD Nov 19 '24 at 14:58