I am trying to formulate a CF grammar for a language of alphabet { a , b, c} with a condition that the number of characters a standing anywhere in the word before this given c larger by 3 than the double of the number of characters b standing anywhere in the word after the given c. for example aababcaacaaacb is in L (the second c is OK), babaabca ∈ L, as well, however aabcabaacbbab , aac and ε are not elements of L. This was a question that i found in a pdf when practicing but could not get to a solution.
-
Could you please provide a link to the pdf? It would help in better understanding the context – sai-kartik May 09 '20 at 17:15
-
The verbal description of $L$ is a bit involved; it appears that in fairly standard notation $$L={ucv\in{a,b,c}^*:|u|_a=2|v|_b+3};.$$ – Brian M. Scott May 09 '20 at 17:17
-
Do you know how to design a push-down automaton for this? And how to convert from PDA to CFG? – Aravind May 09 '20 at 17:20
2 Answers
The following context-free grammar appears to generate $L$:
$$\begin{align*} S&\to AASB\mid AAAc\\ A&\to bA\mid cA\mid Ab\mid Ac\mid a\\ B&\to aB\mid cB\mid Ba\mid Bc\mid b \end{align*}$$
$A$ and $B$ generate the regular languages corresponding to the regular expressions $(b+c)^*a(b+c)^*$ and $(a+c)^*b(a+c)^*$, respectively. Any derivation is going to begin
$$S\Rightarrow^n A^{2n}SB^n\Rightarrow A^{2n+3}cB^n\;;$$
each $A$ will generate a single $a$, possibly surrounded by a ‘halo’ of $b$s and $c$s, and each $B$ will generate a single $b$, possibly surrounded by a ‘halo’ of $a$s and $c$s.
- 616,228
-
If I read the (admittedly hard to read) question correctly I think it's $S \to AASB | AAAc$ for the first rule. – Daniel McLaury May 09 '20 at 17:31
-
-
The solution is perfect thank you! i just tested it using the CF grammar generator it is also a good tool if you guys need to test your CF grammar for some inputs. Just i realized that this works for every n except for n=0 cases similar to (aaac , aabac , babaabca) and so on. – MaJou May 10 '20 at 11:38
-
If I understand correctly, the language consists of words which contain a 'c' for which the number $n_1$ of 'a's before this 'c' and the number $n_2$ of 'b's after this 'c' satisfy
$n_1 = 2n_2+3$.
Okay so we want to do two things:
- Handle the case where you have three a's (and an arbitrary number of b's and c's) before a 'c', followed by a string containing no b's
- Handle adding two a's to the front and a b to the back.
For the first, we can do
$X = [bc]^*a[bc]^*a[bc]^*a[bc]^*c[ac]^*$
For the second,
$Y = [bc]^*a[bc]^*a[bc]^*X[ac]^*b[ac]^*$
assuming I didn't make any mistakes, anyway.
- 24,656