该语言的上下文无关语法

发布于 2024-10-19 04:52:20 字数 309 浏览 1 评论 0原文

我正在研究一些考试准备材料并陷入这个问题。

显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2024-10-26 04:52:20

由于每个“a”后面必须有一个“b”,并且得到的单词必须是回文(如果从末尾读到开头也是一样),这意味着每个“a”前面也必须有一个“b”。

我们从中间开始构建这个词,向两个方向发展。规则是,当中间部分以“a”(这是非终结符 A)结束(并因此开始)时,则其后面(和前面)必须是“b”。另一方面,当中间部分被“b”包围时(这是非终结符 B),它可以用单个“a”(前一种情况)或任意数量的“b”来扩展。中间偶数个“b”的特殊情况也得到处理。起始符号S只允许由“b”限制的单词,因此所有规则都匹配。

S -> B | [empty]
B -> bAb | bBb | bb | b
A -> aBa | a

编辑:我之前的解决方案不正确,我希望现在可以工作。

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.

S -> B | [empty]
B -> bAb | bBb | bb | b
A -> aBa | a

Edit: My previous solution was incorrect, I hope this works now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文