I’m trying to write a CFG over the alphabet Σ = {a,b} for all words starting and ending with the same number of a‘s, with at least one b in the middle.
Now I understand the basic concept of CFG, variables, production rules, etc. Unfortunately I’ve run out of ideas for writing the aforementioned CFG. All I’ve got so far is
S → aYXYa X → XbX | b | λ Y → ???
I think that the production rules S and X will give me a string with two **a**s on both sides with as many **b**s in the middle as I’d like. However, I’m not sure how I can also put as many **a**s on both sides of the **b**s while making sure there are exactly the same number of **a**s on each side.
Any suggestions, solutions would be much appreciated. Thanks.
This should be the CFG you’re looking for. Whenever you’re dealing with the same thing on the beginning and end, remember that you can’t guarantee that the same var will be filled the same way. As such, you have to make those explicit.