设计上下文无关语法 [HW]

发布于 2024-10-20 17:35:14 字数 953 浏览 2 评论 0原文

我已经为此花费了大约 5 个小时的家庭作业,我希望你们中的一些人能够提供帮助,因为 CFG 是 CS 的重要组成部分。

我真正的麻烦是 C 部分。

为以下每种语言设计一个 CFG:

A. {(a^i)(b^j)(c^k)} WHERE (i != j) AND i,j,k >= 0)

我想出了:

Start-> aAB | AbB
A-> epsilon | aA
B-> epsilon | C | bB
C-> epsilon | cC

这似乎适用于 bccc、abbcc、aabbb、cc,所以我认为我在这里很好。

B. {(a^i)(b^j)(c^k)} WHERE (i != k) AND i,j,k >= 0)

Start-> aABC | ABcC
A-> epsilon | aA
B-> epsilon | bB
C-> epsilon | cC

这适用于 bc, bbcc, ab, abb, aac 所以当 i!=k

C. {(a^i)(b^j)(c^k)} WHERE (i != j OR i != k) AND i,j,k >= 0 时,一切看起来都很好)

Start-> aABC | AbB | ABcC
A-> epsilon | aA
B-> epsilon | bB | C
C-> epsilon | cC

我不认为C部分是正确的,但我相信A和B部分是正确的。谁能告诉我我是对/错还是提供帮助?我相信,在最后一种情况下,我只是做一个 OR,并且因为我的 A、B、C 变量几乎相同,所以我可以通过组合来逃脱。它似乎像 bc 、 acc 和其他许多东西一样工作,但我感觉我不应该简单地组合开始状态。

有人知道我是否正确或接近或有任何提示吗?

I've been working on this for about 5 hours for homework and I was hoping some of you guys might be able to help since CFG's are a huge part of CS.

My real trouble is with part C.

Design a CFG for each of the following languages:

A. {(a^i)(b^j)(c^k)} WHERE (i != j) AND i,j,k >= 0)

I came up with:

Start-> aAB | AbB
A-> epsilon | aA
B-> epsilon | C | bB
C-> epsilon | cC

This seems to work for bccc, abbcc, aabbb,cc so I think I am good here.

B. {(a^i)(b^j)(c^k)} WHERE (i != k) AND i,j,k >= 0)

Start-> aABC | ABcC
A-> epsilon | aA
B-> epsilon | bB
C-> epsilon | cC

This works for bc, bbcc, ab, abb, aac so all looks well for when i!=k

C. {(a^i)(b^j)(c^k)} WHERE (i != j OR i != k) AND i,j,k >= 0)

Start-> aABC | AbB | ABcC
A-> epsilon | aA
B-> epsilon | bB | C
C-> epsilon | cC

I do not think part C is correct but I believe A and B are correct. Can anyone tell me if I am right/wrong or help in anyway? I believe since in the last case I am just doing a OR, and because my A,B,C variables are pretty much the same I can get away with combining. It seems to work as bc works, acc works and many others but I get the feeling I should not simply be combining start states.

Anyone know if I am right or close or have any tips?

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

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

发布评论

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

评论(2

萌化 2024-10-27 17:35:14

请记住,当您测试语法时,请确保还使用应被拒绝的字符串进行测试(有关当前对您而言失败的一些字符串,请参阅 @Paulo 的答案)。要解决前两个问题,请编写一个表示 A^i B^j 的语法,其中 i != j;将任意数量的 c 的语法添加到第 1 部分的末尾,并将该语法添加到第 2 部分的中间。对于第 3 部分,请记住两个语法的并集可以很容易写成语法。

Remember that when you are testing grammars, be sure to also test with strings that should be rejected (see @Paulo's answer for some that fail for you currently). To solve the first two, write a grammar that represents A^i B^j where i != j; add a grammar for an arbitrary number of c's onto the end of that for part 1, and add that grammar into the middle for part 2. For part 3, remember that the union of two grammars can be easily written as a grammar.

躲猫猫 2024-10-27 17:35:14

您的第一个语法与 abc 匹配(通过 Start -> AbB -> abB -> abC -> abc),因此它不是正确的语法。

因此,您需要确保以相同的数量创建 ab(然后为其中一个创建更多数量)。

同样的 abc 也与您的第二条规则相匹配,但它也不应该被允许。

Your first grammar matches abc (by Start -> AbB -> abB -> abC -> abc), so it is not a right grammar.

So, you need to make sure the as and bs are created in the same quantity (and then some more for one of them).

The same abc is also matched by your second rule, where it shouldn't be permitted, too.

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