编译原理第二版习题3.6

3.6 Exercises for Section 3.6

3.6.1 !

Figure 3.19 in the exercises of Section 3.4 computes the failure
function for the KMP algorithm. Show how, given that failure function, we
can construct, from a keyword b1b2…bn an n + 1-state DFA that recognizes
.*b1b2…bn, where the dot stands for “any character.” Moreover, this DFA can
be constructed in O(n) time.

Answer

Take the string “abbaabb” in exercise 3.4.3-3 as example, the failure function is:

  • n : 1, 2, 3, 4, 5, 6, 7
  • f(n): 0, 0, 0, 1, 1, 2, 3

The DFA is:

3 6 1

Pseudocode of building the DFA:

for (i = 0; i< n; i ++) {
  move[s[i], c] = {
    if ( c == b1b2…bn[i] ) {
      goto s[i+1]
    } else {
      goto s[f(i)]
    }
  }
}

It is obviously that with the known f(n), this DFA can be constructed in O(n) time.

3.6.2

Design finite automata (deterministic or nondeterministic)
for each of the languages of Exercise 3.3.5.

3.6.3

For the NFA of Fig. 3.29, indicate all the paths labeled aabb.
Does the NFA accept aabb?

Answer
  • (0) -a-> (1) -a-> (2) -b-> (2) -b-> ((3))
  • (0) -a-> (0) -a-> (0) -b-> (0) -b-> (0)
  • (0) -a-> (0) -a-> (1) -b-> (1) -b-> (1)
  • (0) -a-> (1) -a-> (1) -b-> (1) -b-> (1)
  • (0) -a-> (1) -a-> (2) -b-> (2) -b-> (2)
  • (0) -a-> (1) -a-> (2) -b-> (2) -ε-> (0) -b-> (0)
  • (0) -a-> (1) -a-> (2) -ε-> (0) -b-> (0) -b-> (0)

This NFA accepts “aabb”

3.6.4

Repeat Exercise 3.6.3 for the NFA of Fig. 3.30.

3.6.5

Give the transition tables for the NFA of:

  1. Exercise 3.6.3.
  2. Exercise 3.6.4.
  3. Figure 3.26.
Answer

Table 1

stateabε
0{0,1}{0}
1{1,2}{1}
2{2}{2,3}{0}
3

Table 2

stateabε
0{1}{3}
1{2}{0}
2{3}{1}
3{0}{2}

Table 3

stateabε
0{1,2}
1{2}
2{2}
3{4}
4{4}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值