3

So I was trying to solve the CFG,

$$\{w \in (0,1)^* \mid w \text{ contains at least three 1's}\}$$

My approach:

I decided that a string can begin with a $0$, end with a $0$, it may begin with a $1$, ended with a $1$, begin with a 0 end with a $1$, or begin with a $1$ and end with a $0$.

This culminates to:

$S \to 0S0 \mid 1S0 \mid 0S1 \mid 1S1 \mid 111$

vonbrand
  • 27,812
Gaak
  • 221
  • Your proposed grammar seems to produce the set of all strings of odd length that contain at least three consecutive 1's in the center of the string. – Andreas Blass Apr 06 '13 at 15:45
  • @AndreasBlass how could I tweek it to make it work for all length? – Gaak Apr 06 '13 at 16:02
  • 1
    Did you mean $w \in {0,1}^$? If so, even a regular expression is enough, e.g. $\Sigma^(1\Sigma^*)^3$ for $\Sigma = {0,1}$. – dtldarek Apr 06 '13 at 16:49

2 Answers2

3

That is regular, so a grammar is simple to cook up starting from a DFA. Let $A$ stand for no 1 yet, $B$ for one 1, $C$ for two 1, and $D$ for three (or more) 1. Then: $$ \begin{align*} A &\rightarrow 0 A \mid 1 B \\ B &\rightarrow 0 B \mid 1 C \\ C &\rightarrow 0 C \mid 1 D \mid 1 \\ D &\rightarrow 0 D \mid 1 D \mid 0 \mid 1 \end{align*} $$

vonbrand
  • 27,812
0

I hope that this would work:

$$ S \implies S_1aS_1aS_1aS_1 $$ $$ S_1 \implies aS_1 | bS_1 | \epsilon$$

How does this look?

gxyd
  • 303