简体   繁体   English

{a*b*c*} 的上下文无关文法 - {a^nb^nc^n | n >=0}

[英]Context free grammar for {a*b*c*} - {a^n b^n c^n | n >=0}

I'm having trouble with this problem.我遇到了这个问题。 I believe its telling me that no string can be generated that has even # of a's b's and c's.我相信它告诉我,不能生成甚至包含 # 个 a 的 b 和 c 的字符串。 This is due to the subtraction of the second set.这是由于减去了第二组。

A good string from a newly formed CFG should be something like aaabbc or abbbcc and so on.来自新形成的 CFG 的好字符串应该是类似 aaabbc 或 abbbcc 之类的东西。

So I tried breaking the problem into three parts...所以我试着把问题分成三个部分......

  1. Single states单一状态

    a.) S(1) -> aS(1) | a | ^ b.) S(2) -> bS(2) | b | ^ c.) S(3) -> cS(2) | b | ^
  2. Two States两个国家

    a.) S(4) -> aS(4)b | S(1) | S(2) b.) S(5) -> bS(5)c | S(2) c.) S(6) -> aS(6)c | S(3) | S(1)
  3. States w/AB states带 AB 状态的状态

    a.) S(7) -> S(1) | S(4)S(6) b.) S(8) -> S(2) | S(5)S(6) c.) S(9) -> S(3) | S(6)S(3)

    with an orginal start state of ...原始开始状态为...

     S -> S(7) | S(8) | S(9)

However I'm having problems building strings like aaaabbbcc ...但是我在构建像 aaaabbbcc 这样的字符串时遇到了问题......

Am I forming CFGs incorrectly?我是否错误地形成了 CFG? I felt like I was on the right track but now I'm quite lost.我觉得我在正确的轨道上,但现在我很迷茫。

Context Free Grammer for the language of a^nb^nc^n can be constructed using given production.可以使用给定的产生式构造 a^nb^nc^n 语言的上下文无关语法。

S -> aSc | X
X -> BX
X -> null

Using the above production you can generate a^nb^nc^n.使用上面的产生式你可以生成一个^nb^nc^n。 Suppose we're generating a^2 b^2 c^2.假设我们正在生成 a^2 b^2 c^2。 we will start from S.我们将从 S 开始。

= aSc
= aaScc ; aSc
= aaXcc ; S -> X
= aabXcc ; X -> bX
= aabbXcc ; X -> bX
= aabb(null)cc ; X -> null
= aabbcc

A simpler expression for this language is:这种语言的一个更简单的表达是:

{a n b m c * | {a n b m c * | nm } ∪ {a * b n c m | nm } ∪ {a * b n c m | nm } nm }

(Using the Kleene star above is probably an abuse of notation. Originally, I wrote it with a third integer variable. But I think the star is clearer.) (使用上面的Kleene星可能是滥用符号。最初,我用第三个整数变量来写它。但我认为星更清晰。)

Now, {a n b m |现在,{a n b m | nm } is simply a n (a + |b + )b n nm } 只是一个n (a + |b + )b n

So the full expression could be written as a n (a + |b + )b n c * |所以完整的表达式可以写成n (a + |b + )b n c * | a * b n (b + |c + )c n a * b n (b + |c + )c n

Putting all that together into a CFG is a little tedious so I left it for the reader.将所有这些放在一个 CFG 中有点乏味,所以我把它留给了读者。 But it's totally mechanical.但这完全是机械的。

Making a DFA (or more properly Deterministic Pushdown Automaton) out of that might be trickier, since the CFG is ambiguous.从中制作 DFA(或更恰当地说确定性下推自动机)可能会更棘手,因为 CFG 是模棱两可的。 In fact, every CFG for that language is ambiguous.事实上,该语言的每个CFG 都是模棱两可的。 But there's no problem making an NDPA.但是制定 NDPA 没有问题。

So I split the problem into 2 sub problems所以我把问题分成2个子问题

The number of a's is different from the number of b's. a 的数量与 b 的数量不同。 Therefore the a's and c's or b's and c's can be the same.因此,a 和 c 或 b 和 c 可以相同。

The number of b's is different from the number of c's. b 的数量与 c 的数量不同。 Therefore the a's and b's or a's and c's can be the same.因此,a 和 b 或 a 和 c 可以相同。

Edit : More formally as stated by Rici {a n b m c * |编辑:更正式的如 Rici {a n b m c * | 所述nm } ∪ {a * b n c m | nm } ∪ {a * b n c m | nm } nm }

//a and b is different
S-->XR
//b and c is different
S-->DY

//Genarates a string where a is more than b
X--> aXb | A
A --> aA  | a

//Genarates a string where b is more than a
X--> bXa | B
B --> bB  | b

//Genarates a string where b is more than c
Y--> bYc | B
B --> bB  | b

//Genarates a string where c is more than b
Y--> cYb | C
C --> cC  | c

R-->Rc|c|^
D-->Da|a|^

The answer above by Murdock is very close but fails some cases. Murdock 上面的答案非常接近,但在某些情况下失败了。 For example it can generate a, b and aabcc as it should, but cannot generate c, abbcc or bc even though it should be able to.例如,它可以生成 a、b 和 aabcc,但不能生成 c、abbcc 或 bc,即使它应该能够生成。 (JFlap is a great software I use for quickly testing different strings on grammers I am constructing, it is also helpful for a lot of other concepts in formal languages and computational theory.) Using Murdock's CFG it is also possible for the letters to appear in different order, but the question statement forces all a's to come first, followed by all b's and finally all c's. (JFlap 是一个很棒的软件,我用来在我构建的语法上快速测试不同的字符串,它对形式语言和计算理论中的许多其他概念也很有帮助。)使用 Murdock 的 CFG,字母也可以出现在不同的顺序,但问题陈述强制所有 a 先出现,然后是所有 b,最后是 c。

Danish Ahmed seems to have misunderstood the question and tried to construct a CFG for the language where a's, b's and c's are all in equal amounts.丹麦人艾哈迈德似乎误解了这个问题,并试图为 a、b 和 c 的数量相等的语言构建一个 CFG。 Off the top of their head, my lecturer has said it is impossible to construct a CFG for this.我的讲师突然说不可能为此构建 CFG。 Danish's CFG is incorrect for a = b = c because it can generate a 'b' by itself through S -> X;对于 a = b = c,丹麦语的 CFG 是不正确的,因为它可以通过 S -> X 自行生成一个 'b'; X -> BX; X -> BX; X -> null (also assuming the capital B was meant to be a terminal b.) X -> null(还假设大写 B 是终端 b。)

Here is my answer, which adds to and adjusts some components of Murdock's CFG so that it is valid for the question.这是我的答案,它添加并调整了 Murdock 的 CFG 的一些组件,使其对问题有效。

S=XR|DY|WR|DZ
X=aXb|A
A=aA|a
W=aWb|B
B=bB|b
Y=bYc|B
Z=bZc|C
C=cC|c
R=Rc|c|epsilon
D=Da|a|epsilon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM