[英]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.
我们通过自下而上构建语法来开始论证。
A_1 := a
to get any strings.A_1 := a
来获取任何字符串。A_2 := A_1 A_1
to get any string with length greater than 1.A_2 := A_1 A_1
才能得到长度大于 1 的任何字符串。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
A_3 := A_2 A_1
.A_3 := A_2 A_1
。A_21 := XY
.A_21 := XY
形式A_21 := XY
。 So we can add up to two more.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_6
和A_12
- 我们也不能按要求产生A_21
(我们最多可以产生A_18 := A_6 A_12
。所以添加A_3
不能得到我们生成我们语言的语法在六个制作中。 Case 2: A_4
案例 2:
A_4
A_4 := A_2 A_2
.A_4 := A_2 A_2
。A_21 := XY
.A_21 := XY
形式A_21 := XY
。 So we can add up to two more.A_5
, A_6
and A_8
.A_5
、 A_6
和A_8
。 A_5
and A_6
will fail for the same reason we stated for Case 1 above. A_5
和A_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
。A_20, A_19, A_17
or A_13
.A_20, A_19, A_17
或A_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.