简体   繁体   English

这些模式可以通过正则表达式或上下文无关语法进行匹配吗?

[英]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' n'a'后跟n'b',就像'aabb'
  2. palindrome, like 'abbccbba' 回文,像'abbccbba'
  3. n 'a', then n 'b', then n 'c', like 'aabbcc' n'a',然后n'b',然后n'c',就像'aabbcc'

Note that n could be any positive integer. 注意,n可以是任何正整数。 (Otherwise it's too simple) (否则太简单了)

Only 3 character 'abc' could appear in the text to parse. 在文本中只能出现3个字符'abc'来解析。

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. 在问题1的情况下,你需要以某种方式保存数轨迹a终端为你解析b端子。 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 ). 因为你知道你需要一对一,所以堆栈显然已经足够了(你可以将a放在堆栈上并用每个b弹出一个)。 Since a pushdown automaton is equivalent to a CFG in expressive power, you can create a CFG for problem 1. 由于下推自动机相当于表达能力的CFG,因此您可以为问题1创建CFG。

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. 在问题2的情况下,PDA在问题1中使用的技术应该提示您可以用于问题2的技术.PDA可以构建输入的前半部分的堆栈,然后在其反向出现时将其弹出在。

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? 在问题3的情况下,如果你使用的堆栈技术用于计数的数目a端子和b端子,这一切都很好,但什么发生在你的堆栈内存? Was it sufficient? 这够了吗? No, you would have needed to store the number of a s somewhere else, so a CFG cannot express this grammar. 不,你会需要存储号码的a s ^别的地方,所以CFG无法表达这种语法。

Here's an attempt at a simple CFG for problem 2 (it validates an empty input, but you'll get the idea): 这是针对问题2的一个简单CFG的尝试(它验证了一个空输入,但你会得到这个想法):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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