3

So in preparation for an interview, I have been revising and studying by solving some CFGs, and here is a question, which I solved, but I feel like I haven't solved it right, given its boundary requirements.

Give a CFG for the set of all strings in the form $O^i 1^j 2^k 3^l$ where $0 \leq j \leq 2i$ and $k=l$.

My approach: When $j=2$, $i=2$, and $k=3$ I started solving this (do share some of your tips and hints in solving CFGs, it would help me alot and I would appreciate your expertise tips) I produced a string

$$011222333$$

And given that, after analyzing it, I came up with this grammar

$$S \to OA$$ $$A \to 1A1B|\epsilon$$ $$B \to 2BC|\epsilon$$ $$C \to 3c|\epsilon$$

I was able to produce that string from the above grammar, but I feel like I was intentionally selecting and choosing when to choose epsilon, almost forcing my grammar to work, it felt weird.

Your assistance, plus tips will greatly help!

TMM
  • 9,976
Gaak
  • 221

2 Answers2

1

First, split your language in two, that is, $L_1 = \{\mathtt{0}^i\mathtt{1}^j \mid 0 \leq j \leq 2i\}$ and $L_2 = \{\mathtt{2}^k\mathtt{3}^k \mid k \in \mathbb{N}\}$,

$$S \to L_1L_2.$$

Now, $L_2$ is simply

$$L_2 \to \mathtt{2}\,L_2\mathtt{3} \mid \varepsilon$$

while the first one can be done using this simple trick:

\begin{align} L_1 &\to \mathtt{0}\,L_1JJ \mid \varepsilon \\ J &\to \mathtt{1} \mid \varepsilon \end{align}

I hope this helps ;-)

dtldarek
  • 37,381
  • you are the man! That was such a frickin neat explanation. However, I feel like any input you ask me to produced, I can produce with my approach, why is mine considered 'wrong'? – Gaak Apr 05 '13 at 18:18
  • is the language 01 in this language, or is it only 011 since i must be 2*i in the output? – Gaak Apr 05 '13 at 18:55
  • String $01$ can be constructed like this: $(0(\varepsilon)(1)(\varepsilon))(\varepsilon)$. – dtldarek Apr 05 '13 at 19:01
  • but is that accepted? I guess the boundary is confusing me. When it says, j<=2i, does that mean if I choose i =1, then there must be two 0s, or since the original i was 1, then there must be one. – Gaak Apr 05 '13 at 19:04
  • the above question is really bothering me. An explanation as to how these boundaries work, mean and affect the output – Gaak Apr 05 '13 at 19:14
  • $0 \leq j \leq 2i$ means that when you pick $i=1$, then $j \in {0,1,2}$, i.e. all strings 0, 01 and 011 are in the language. When you pick $i = 2$ then you get 00, 001, 0011, 00111 and 001111. Similarly for bigger $i$-s. – dtldarek Apr 05 '13 at 19:40
0

Your grammar does not construct the language intended. $\newcommand{\Ze}{\mathtt{0}} \newcommand{\On}{\mathtt{1}} \newcommand{\Tw}{\mathtt{2}} \newcommand{\Th}{\mathtt{3}}$ For example, it can construct the following string: $$\begin{align} \color{red}{S} &\Rightarrow \color{blue}{ \Ze A } = \Ze \color{red}{A} \\ &\Rightarrow \Ze \color{blue}{ \On A \On B } = \Ze \On \color{red}{A} \On B \\ &\Rightarrow \Ze \On \color{blue}{ \On A \On B } \On B = \Ze \On \On \color{red}{A} \On B \On B \\ &\Rightarrow \Ze \On \On \color{blue}{\varepsilon} \On B \On B = \Ze \On \On \On \color{red}{B} \On B \\ &\Rightarrow \Ze \On \On \On \color{blue}{ \Tw B C } \On B = \Ze \On \On \On \Tw \color{red}{B} C \On B \\ &\Rightarrow \Ze \On \On \On \Tw \color{blue}{\varepsilon} C \On B = \Ze \On \On \On \Tw \color{red}{C} \On B \\ &\Rightarrow \Ze \On \On \On \Tw \color{blue}{\varepsilon} \On B = \Ze \On \On \On \Tw \On \color{red}{B} \\ &\Rightarrow \Ze \On \On \On \Tw \On \color{blue}{\varepsilon} = \Ze \On \On \On \Tw \On \end{align}$$ which is not in the desired language.

In fact, you can easily show that every string your grammar constructs has only one $\Ze$. Working a bit more, you can show that the language your CFG constructs is (abusing notation somewhat) $$ \{ \Ze \On^n ( \On \Tw^* \Th^* )^n : n \geq 0 \}.$$

(dtldarek's answer provides a CFG for the grammar in question.)

user642796
  • 52,188
  • as long as I can produce strings which are outputs of that language, arent I in compliance? – Gaak Apr 05 '13 at 18:16
  • @Gaak: No. To be a CFG for a given language the CFG must construct all and only strings from the given language. – user642796 Apr 05 '13 at 18:23
  • 1
    @Gaak: If just being able to construct every string in the language were sufficient, every language would be context-free (indeed regular), using the grammar with productions $S\to xS$ (for all $x$ in the alphabet) and $S\to \epsilon$. – Tara B Apr 05 '13 at 18:24
  • @TaraB exactly, and that is what bothers me. I dont know when to stop and call it a day, or how to impose requirements – Gaak Apr 05 '13 at 18:25
  • @Gaak: I don't really understand your comment. As Arthur said, you just need to find a grammar which generates exactly your language. – Tara B Apr 05 '13 at 18:26
  • @TaraB My comment is, if you look at dtldareks post, he says L1 -->0L1JJ I don't seem to get that, or know when to apply such requirements – Gaak Apr 05 '13 at 18:29
  • @Gaak: In dtldarek's solution the rule $L_1 \Rightarrow \mathtt{0} L_1 JJ$ (along with the rules for $J$) means that for every $\mathtt{0}$ in the string there are at most two $\mathtt{1}$s, which will imply that the number of $\mathtt{1}$s is no more than twice the number of $\mathtt{0}$s. The only advice anyone can give you for getting this is eerily similar to advice for getting to Carnegie Hall: practice, practice, practice! – user642796 Apr 05 '13 at 19:31