简体   繁体   English

如果给出了语言定义,如何生成无限制语法

[英]How to generate an Unrestricted grammar if a language defination is given

How to construct unrestricted grammar that generates L = {a^ib^jc^k^d^l: i not equal to k AND j not equal to l}.如何构造生成 L = {a^ib^jc^k^d^l: i not equal to k AND j not equal to l} 的无限制文法。

a is to the power of i, 'b' raised to the power of j, 'c' raised to the power of k, 'd' rasied to the power l. a 是 i 的幂,'b' 是 j 的幂,'c' 是 k 的幂,'d' 是 l 的幂。

I was good with regular and CFG but as unrestricted grammers can contains more than one non-terminal in the left side of production, I'm confused and unable to come to with a solution.我对常规和 CFG 很好,但由于不受限制的语法可以在生产的左侧包含多个非终端,我很困惑并且无法找到解决方案。

I will not try for the most efficient or most elegant grammar, but shoot for an easy grammar - one we can quickly see must be correct.我不会尝试最有效或最优雅的语法,而是尝试一种简单的语法——我们可以很快看到的语法一定是正确的。

Your language L = {a^ib^jc^k^d^l: i not equal to k AND j not equal to l} can be written as the union of four languages L1, L2, L3 and L4:你的语言 L = {a^ib^jc^k^d^l: i not equal to k AND j not equal to l} 可以写成四种语言 L1、L2、L3 和 L4 的并集:

  • L1 = {a^ib^jc^k^d^l: i < k AND j < l} L1 = {a^ib^jc^k^d^l: i < k AND j < l}
  • L2 = {a^ib^jc^k^d^l: i < k AND j > l} L2 = {a^ib^jc^k^d^l: i < k AND j > l}
  • L3 = {a^ib^jc^k^d^l: i > k AND j < l} L3 = {a^ib^jc^k^d^l: i > k AND j < l}
  • L4 = {a^ib^jc^k^d^l: i > k AND j > l} L4 = {a^ib^jc^k^d^l: i > k AND j > l}

There is some unrestricted grammar corresponding to each of these cases, call the grammars G1, G2, G3, G4.每一种情况都有一些不受限制的文法,称为文法 G1、G2、G3、G4。 Then, our grammar G can have a start nonterminal that produces the start nonterminals of each of these four grammars, and it will generate the union of those grammars' languages.然后,我们的文法 G 可以有一个起始非终结符,它产生这四个文法中每一个的起始非终结符,并且它将生成这些文法的语言的并集。 Thus, if we can find G1 - G4, our problem is solved.因此,如果我们能找到 G1 - G4,我们的问题就解决了。 It should be easy to see that each of these grammars will be pretty similar, only differing in which symbols are to be less frequent than which others.很容易看出,这些文法中的每一个都非常相似,不同之处仅在于哪些符号的出现频率低于其他符号。 We will focus on trying to find G1 and leave G2, G3 and G4 as exercises.我们将专注于尝试找到 G1,并将 G2、G3 和 G4 留作练习。

Our strategy for G1 will be the following: write grammars for a^ic^k where i < k, and b^jd^l where j < l, separately;我们对 G1 的策略如下:分别为 i < k 的 a^ic^k 和 j < l 的 b^jd^l 编写语法; then, combine them and reorder the b's and c's so that everything is in the right order.然后,合并它们并对 b 和 c 重新排序,以便所有内容都按正确的顺序排列。 So, for the first part:所以,对于第一部分:

S1 := X1 Y1
X1 := A1 X1 C1 | X1 C1 | C1
Y1 := B1 Y1 D1 | Y1 D1 | D1
A1 := a
B1 := b
C1 := c
D1 := d

This gives us strings like a^ic^kb^jd^l where i < k and j < l.这给了我们像 a^ic^kb^jd^l 这样的字符串,其中 i < k 和 j < l。 This is pretty close to what we wanted, but we need the c's and b's swapped.这非常接近我们想要的,但我们需要交换 c 和 b。 To do this, we can use a special new nonterminal whose only function is to allow A1, B1, C1 and D1 to resolve to a, b, c and d, but only when they are in the right order.为此,我们可以使用一个特殊的新非终结符,它的唯一功能是允许 A1、B1、C1 和 D1 解析为 a、b、c 和 d,但前提是它们的顺序正确。 To that we will add a production C1 B1:= B1 C1 which will allow the grammar to bubble the B's and C's to the right positions.为此,我们将添加一个产生式 C1 B1:= B1 C1,这将允许语法将 B 和 C 冒泡到正确的位置。 We can call our special marker M1:我们可以调用我们的特殊标记 M1:

// ensure i < k and j < l
S1 := X1 Y1 M1'
X1 := A1 X1 C1 | X1 C1 | C1
Y1 := B1 Y1 D1 | Y1 D1 | D1

// allow b's to come before c's
C1 B1 := B1 C1

// require d's come last, c's come before d's,
// b's come before c's and d's, and a's come
// before everything
D1 M1' := M1 d
D1 M1 d := M1 dd
C1 M1 d := M1 cd
C1 M1 c := M1 cc
B1 M1 d := M1 bd
B1 M1 c := M1 bc
B1 M1 b := M1 bb
A1 M1 d := M1 ad
A1 M1 c := M1 ac
A1 M1 b := M1 ab
A1 M1 a := M1 aa

// allow the derivation to terminate nondeterministically
// only by terminating after removing all nonterminals
// will the resulting derivation be in the grammar's language
M1 := (empty)

Let's do a few simple examples to show that abccdd is in the language whereas aabccdd and accbdd are not:让我们做几个简单的例子来说明 abccdd 在语言中,而 aabccdd 和 accbdd 不是:

S1 := X1 Y1 M1'
   := A1 X1 C1 Y1 M1'
   := A1 C1 C1 Y1 M1'
   := A1 C1 C1 B1 Y1 D1 M1'
   := A1 C1 C1 B1 D1 D1 M1'
   := A1 C1 B1 C1 D1 D1 M1'
   := A1 B1 C1 C1 D1 D1 M1'
   := A1 B1 C1 C1 D1 M1 d
   := A1 B1 C1 C1 M1 dd
   := A1 B1 C1 M1 cdd
   := A1 B1 M1 ccdd
   := A1 M1 bccdd
   := M1 abccdd
   := abccdd

S1 := X1 Y1 M1'
   := A1 X1 C1 Y1 M1'
   := A1 A1 X1 C1 C1 Y1 M1'
   ... unable to proceed, cannot get rid of X1 without adding more c's

S1 := X1 Y1 M1'
   := A1 X1 C1 Y1 M1'
   := A1 C1 C1 Y1 M1'
   := A1 C1 C1 B1 Y1 D1 M1'
   := A1 C1 C1 B1 D1 D1 M1'
   := A1 C1 C1 B1 D1 M1 d
   := A1 C1 C1 B1 M1 dd
   := A1 C1 C1 M1 bdd
   ... no production matches C1 M1 b, no way to clear C1 now

Notice that I introduce special marker M' for the very first move, not sure that's really necessary, but you can play with it.请注意,我在第一步中引入了特殊标记 M',不确定是否真的有必要,但您可以玩一下。

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

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