简体   繁体   English

为乔姆斯基范式的语言构建上下文无关文法

[英]Construct a context free grammar for a language in Chomsky Normal Form

I'm trying to construct a CFG in Chomsky Normal Form with as few productions as possible that accepts the language containing the only string a^21.我正在尝试以 Chomsky Normal Form 构建一个 CFG,并尽可能少地接受包含唯一字符串 a^21 的语言。

I understand that I could just convert我知道我可以转换

S -> AAAAAAAAAAAAAAAAAAAAA A -> a S -> AAAAAAAAAAAAAAAAAAAAAA -> a

but is there any other way to shorten that language then convert it into Chomsky Normal Form?但是有没有其他方法可以缩短该语言然后将其转换为乔姆斯基范式?

We can pretty easily show that we need at least six symbols to hope to generate a CFG in CNF for this language by recognizing we can at best double the generated string length with each production and we must start with 2^0:我们可以很容易地证明,我们至少需要六个符号才能为这种语言生成 CNF 中的 CFG,方法是认识到我们最多可以在每次产生时将生成的字符串长度加倍,而且我们必须从 2^0 开始:

A_21 := ...
A_16 := A_16 A_16
A_8  := A_4  A_4
A_4  := A_2  A_2
A_2  := A_1  A_1
A_1  := a

We can then show there is no grammar in CNF with six productions that generates our target language.然后我们可以证明 CNF 中没有语法,有六个产生式来生成我们的目标语言。 We start the argument by building the grammar from the bottom up.我们通过自下而上构建语法来开始论证。

  1. We must have A_1 := a to get any strings.我们必须有A_1 := a来获取任何字符串。
  2. We must have A_2 := A_1 A_1 to get any string with length greater than 1.我们必须有A_2 := A_1 A_1才能得到长度大于 1 的任何字符串。
  3. We can now generate either A_3 or skip that and generate A_4 , or both.我们现在可以生成A_3或跳过它并生成A_4 ,或两者兼而有之。 We consider each of these cases below.我们将在下面考虑这些情况。

Case 1: A_3案例 1: A_3

  1. We add A_3 := A_2 A_1 .我们添加A_3 := A_2 A_1
  2. We already have 3 productions, and know we need one of the form A_21 := XY .我们已经有 3 个作品,并且知道我们需要A_21 := XY形式A_21 := XY So we can add up to two more.所以我们最多可以添加两个。 Even if we add the biggest productions that are now possible - A_6 and A_12 - we can't produce A_21 as required (we can produce at most A_18 := A_6 A_12 . So adding A_3 can't get us a grammar that generates our language in six productions.即使我们添加了现在可能的最大产生式 - A_6A_12 - 我们也不能按要求产生A_21 (我们最多可以产生A_18 := A_6 A_12 。所以添加A_3不能得到我们生成我们语言的语法在六个制作中。

Case 2: A_4案例 2: A_4

  1. We add A_4 := A_2 A_2 .我们添加A_4 := A_2 A_2
  2. We already have 3 productions, and know we need one of the form A_21 := XY .我们已经有 3 个作品,并且知道我们需要A_21 := XY形式A_21 := XY So we can add up to two more.所以我们最多可以添加两个。 Our options currently are A_5 , A_6 and A_8 .我们目前的选项是A_5A_6A_8 A_5 and A_6 will fail for the same reason we stated for Case 1 above. A_5A_6将失败,原因与我们针对上述案例 1 所述的原因相同。 A_8 , however, does not fail by that reasoning, so we add A_8 := A_4 A_4 .然而, A_8不会因为这个推理而失败,所以我们添加A_8 := A_4 A_4
  3. We now have only one production and need it to be either A_20, A_19, A_17 or A_13 .我们现在只有一个产品,需要它是A_20, A_19, A_17A_13 We cannot generate any of these using our existing productions.我们无法使用我们现有的作品生成任何这些。

We have thus ruled out a grammar with 6 productions.因此,我们排除了具有 6 个产生式的语法。 If you try to find a grammar with 7 productions using the above reasoning, you will find several.如果您尝试使用上述推理找到具有 7 个产生式的语法,您会找到几个。 Since we know there are grammars in CNF with 7 productions and none with 6, you're done.既然我们知道 CNF 中有 7 个产生式的语法,而没有 6 个产生式,那么您就完成了。 Here are a few of the 7-production grammars:以下是 7 个产生式语法中的一些:

S    := A_18 A_3
A_18 := A_9  A_9
A_9  := A_6  A_3
A_6  := A_3  A_3
A_3  := A_2  A_1
A_2  := A_1  A_1
A_1  := a

S    := A_17 A_4
A_17 := A_9  A_8
A_9  := A_8  A_1
A_8  := A_4  A_4
A_4  := A_2  A_2
A_2  := A_1  A_1
A_1  := a

S    := A_16 A_5
A_16 := A_8  A_8
A_8  := A_4  A_4
A_5  := A_4  A_1
A_4  := A_2  A_2
A_2  := A_1  A_1
A_1  := a

S    := A_15 A_6
A_15 := A_9  A_6
A_9  := A_6  A_3
A_6  := A_3  A_3
A_3  := A_2  A_1
A_2  := A_1  A_1
A_1  := a

S    := A_14 A_7
A_14 := A_7  A_7
A_7  := A_4  A_3
A_4  := A_3  A_1
A_3  := A_2  A_1
A_2  := A_1  A_1
A_1  := a

S    := A_13 A_8
A_13 := A_8  A_5
A_8  := A_5  A_3
A_5  := A_3  A_2
A_3  := A_2  A_1
A_2  := A_1  A_1
A_1  := a

S    := A_12 A_9
A_12 := A_9  A_3
A_9  := A_6  A_3
A_6  := A_3  A_3
A_3  := A_2  A_1
A_2  := A_1  A_1
A_1  := a

S    := A_11 A_10
A_11 := A_10 A_1
A_10 := A_8  A_2
A_8  := A_4  A_4
A_4  := A_2  A_2
A_2  := A_1  A_1
A_1  := a

And there are lots more.还有更多。 The hard part was showing there weren't any with 6 productions.困难的部分是显示没有任何 6 个作品。

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

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