This is an exercise of compiler. We are asked if it’s possible to match the following patterns with regular expression or context free grammar:
- n ‘a’ followed by n ‘b’, like ‘aabb’
- palindrome, like ‘abbccbba’
- n ‘a’, then n ‘b’, then n ‘c’, like ‘aabbcc’
Note that n could be any positive integer. (Otherwise it’s too simple)
Only 3 character ‘abc’ could appear in the text to parse.
I’m confused because as far as I can see, non of these patterns can be described by regular expression and context free grammar.
The critical question is: how much and what kind of memory do you need?
In the case of problem 1, you need to somehow keep track of the number of
aterminals as you are parsing thebterminals. Since you know you need one for one, a stack is clearly sufficient (you can put theaon the stack and pop one off with everyb). Since a pushdown automaton is equivalent to a CFG in expressive power, you can create a CFG for problem 1.In the case of problem 2, the technique that a PDA uses in problem 1 should be suggestive of a technique you could use for problem 2. PDAs can build a stack of the first half of the input, then pop it off as its reverse comes in.
In the case of problem 3, if you use the stack technique for counting the number of
aterminals andbterminals, that’s all well and good, but what happened to your stack memory? Was it sufficient? No, you would have needed to store the number ofas somewhere else, so a CFG cannot express this grammar.