该语言的上下文无关语法
我正在研究一些考试准备材料并陷入这个问题。
显示 L = {we {a,b}* 的上下文无关语法:w = wR 并且每个 a 后面紧跟着 ab}。
wR 是 w 的倒转。因此,在英语中,回文是每个“a”后跟一个“b”,使用任意数量的 a 和 b。
到目前为止,我得到了相反的部分,但我不知道如何合并每个 a 后面跟着 ab 部分,同时确保回文属性仍然成立。
S -> bSb | b | [the empty string]
非常感谢任何帮助!
I'm working on some test preparation material and stuck on this problem.
Show a context free grammar for L = {w e {a,b}*: w = wR and every a is immediately followed by a b}.
wR is w in reverse. So, in english, a palindrome with every "a" being followed by a "b", using any number of a's and b's.
So far, I got this for the reverse portion, but I can't figure out how to incorporate the every a is followed by a b part while ensuring the palindrome property will still hold.
S -> bSb | b | [the empty string]
Any help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于每个“a”后面必须有一个“b”,并且得到的单词必须是回文(如果从末尾读到开头也是一样),这意味着每个“a”前面也必须有一个“b”。
我们从中间开始构建这个词,向两个方向发展。规则是,当中间部分以“a”(这是非终结符 A)结束(并因此开始)时,则其后面(和前面)必须是“b”。另一方面,当中间部分被“b”包围时(这是非终结符 B),它可以用单个“a”(前一种情况)或任意数量的“b”来扩展。中间偶数个“b”的特殊情况也得到处理。起始符号S只允许由“b”限制的单词,因此所有规则都匹配。
编辑:我之前的解决方案不正确,我希望现在可以工作。
Since every "a" must be followed by a "b", and the resulting word must be palindrome (it is the same if being read from the end to the beginning), this implies that every "a" must also be preceeded by a "b".
We start building the word from the middle, growing in both directions. The rule is that when the middle section ends (and therefore begins) with an "a" (this is the nonterminal A), then it must be followed (and preceeded) by a "b". On the other end, when the middle section is sorrounded by "b"s (this is nonterminal B), it can expand with a single "a" (the previous case), or any number of "b"s. The special case of evenly numbered "b"s in the middle are also handled. The start symbol S only allows words limited by "b"s, therefore all rules are matched.
Edit: My previous solution was incorrect, I hope this works now.