简体   繁体   中英

Could these patterns be matched by regular expression or context free grammar?

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:

  1. n 'a' followed by n 'b', like 'aabb'
  2. palindrome, like 'abbccbba'
  3. 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 a terminals as you are parsing the b terminals. Since you know you need one for one, a stack is clearly sufficient (you can put the a on the stack and pop one off with every b ). 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 a terminals and b terminals, 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 of a s somewhere else, so a CFG cannot express this grammar.

Here's an attempt at a simple CFG for problem 2 (it validates an empty input, but you'll get the idea):

S -> a S a
S -> b S b
S -> c S c
S -> ɛ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM