上下文无关语法和反转

发布于 2024-10-08 20:02:31 字数 307 浏览 0 评论 0原文

我正在设计一个上下文无关语法来生成这种语言:

{ w in {a,b}* | w is of the form uvu^R, where u and v are any strings in {a,b}* }

我将前两个字符串定义为:

U -> aU | bU | _
V -> aV | bV | _

然后将它们组合起来:

S -> UV

但是如何将反转表达为上下文无关语法?

I'm designing a context-free grammar to generate this language:

{ w in {a,b}* | w is of the form uvu^R, where u and v are any strings in {a,b}* }

I would define the first two strings as:

U -> aU | bU | _
V -> aV | bV | _

And then combine those:

S -> UV

But how do I express the reversal as context-free grammar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半枫 2024-10-15 20:02:31

您需要利用语法的上下文无关性(到目前为止您所呈现的只是常规语法):

U-> aUa | bUb | a | b | _

将匹配“ababa”和“aabaa”等内容,但不匹配“aabba”。

我将让您根据需要进行更改 - 但请记住,您指定的语言有可能 u 为空字符串,因此它会生成 {a 中的所有字符串,b}*

You need to make use of the context-free-ness of the grammar (what you're presenting so far is just a regular grammar):

U-> aUa | bUb | a | b | _

Will match things like "ababa" and "aabaa", but not "aabba".

I'll leave it to you to alter this to your needs - but keep in mind that your specified language has the possibility of u being the empty string, hence it generates all strings in {a,b}*.

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