patternMinor
A context-free grammar for all strings that end in b and have an even number of bs
Viewed 0 times
numberallfreeandgrammarforthatendcontexteven
Problem
I'm trying to find CFG's that generate a regular language over the alphabet {a b}
I believe I got this one right: All strings that end in b and have an even number of b's in total:
$\qquad S \to SS \\
\qquad S \to YbYb \mid \varepsilon \\
\qquad Y \to aY \mid \varepsilon$
However, Im not sure how to accomplish this with an odd number of b's.
So for example, how could I find a CFG that generates all strings that end in b and have an odd number of b's in total: So far I have this,
$\qquad S \to SS \\
\qquad S \to YYb \mid \varepsilon \\
\qquad Y \to abY \mid baY \mid \varepsilon$
But this can generate abababb so it's incorrect and Im stumped at this point.
I believe I got this one right: All strings that end in b and have an even number of b's in total:
$\qquad S \to SS \\
\qquad S \to YbYb \mid \varepsilon \\
\qquad Y \to aY \mid \varepsilon$
However, Im not sure how to accomplish this with an odd number of b's.
So for example, how could I find a CFG that generates all strings that end in b and have an odd number of b's in total: So far I have this,
$\qquad S \to SS \\
\qquad S \to YYb \mid \varepsilon \\
\qquad Y \to abY \mid baY \mid \varepsilon$
But this can generate abababb so it's incorrect and Im stumped at this point.
Solution
There is an easy and very natural way how to derive a context-free grammar $G=(V,\Sigma,R,S)$ from a DEA $M=(Q,\Sigma,\delta,q_0,F)$. We set
It's an easy exercise to show that $L(G)=L(M)$. With this technique you can now build your automaton first (which is an easy two-state example in your case), and then apply this construction.
- $V=Q$
- $S= q_0$
- $R$ consists of the following rules:
- for all $X\in Q$ and $a\in \Sigma$ with $Y=\delta(X,a)$ we add the rule $X \to aY$
- for all $X\in F$ we add the rule $X \to \varepsilon$
- there are no other rules in $R$
It's an easy exercise to show that $L(G)=L(M)$. With this technique you can now build your automaton first (which is an easy two-state example in your case), and then apply this construction.
Context
StackExchange Computer Science Q#6163, answer score: 4
Revisions (0)
No revisions yet.