CFG对于A = B和C = D(长度)

发布于 2025-01-20 10:02:22 字数 477 浏览 3 评论 0 原文

我知道如何为具有相同计数的 ab 或具有相同计数的 c 和 的字符串构建上下文无关语法d

S → ε             S → ε
S → SASBS         S → SCSDS
S → SBSAS         S → SDSCS
A → a             C → c
B → b             D → d

但是,我不知道如何为 ab 的计数相同的字符串创建语法,并且cd 的计数为 相同。对于诸如 cadbdabc 之类的字符串,我的尝试会失败,其中配对彼此之间(例如:c、、d、)。因此,如果有人可以提供帮助,我将不胜感激。

I know how to construct the context-free grammars for strings that have same counts of a and b, or have the same counts of c and d:

S → ε             S → ε
S → SASBS         S → SCSDS
S → SBSAS         S → SDSCS
A → a             C → c
B → b             D → d

But, I don't know how to create a grammar for strings where the counts of a and b are the same, and the counts of c and d are the same. My attempts fail for strings like cadbdabc where the pairings are between each other (ex: c, <a>, d, <b>). So, I would appreciate if someone could help out on this.

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

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

发布评论

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

评论(1

不即不离 2025-01-27 10:02:22

您试图识别的语言不是不含上下文的,因此您将无法为其构建无上下文的语法。

要看一下,请记住,语言 a n c m b n n d m 不是无上下文。 (您可以轻松地证明使用泵送引理;实际上,这是经典的例子之一。请以 m n 均大于泵送常数< m p ; u 和 x 带有空字符串 - 泵送0重复 - 必须成为一个平等性的一个。)

现在,令 l 是您的语言,并考虑 l a*c*c*b*d*的交叉点。无上下文语言和普通语言的交集是保证是上下文free < /a>, a*c*b*d*显然是规律的。因此,如果 l 是无上下文的,那么 l a*c*c*b*d*将是上下文-自由的。但是 l ∩a*c*b*d*恰恰是语言 a n c c m b n d m ,这不是上下文;矛盾。因此, l 不能无上下文。

The language you are trying to recognise is not context-free, so you're not going to be able to construct a context-free grammar for it.

To see that, remember that the language ancmbndm is not context-free. (You can easily prove that using the pumping lemma; in fact, it's one of the classic examples. Take a string where both m and n are greater than the pumping constant p; any substring uvx whose length is no greater than p can contain at most two different symbols with two different repetition counts; so replacing u and x with the empty string -- pumping 0 repetitions -- must make one of the equalities false.)

Now, let L be your language, and consider the intersection L ∩ a*c*b*d*. The intersection of a context-free language and a regular language is guaranteed to be context-free, and a*c*b*d* is obviously regular. So if L were context-free, then L ∩ a*c*b*d* would be context-free. But L ∩ a*c*b*d* is precisely the language ancmbndm, which is not context-free; a contradiction. Thus, L cannot be context-free.

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