编译原理第二版习题3.9

正则表达式与DFA转换详解
本文深入探讨了正则表达式的概念,并详细解释了如何使用算法将正则表达式直接转换为确定有限自动机(DFA)。通过具体实例,展示了不同正则表达式的语法树构建,firstpos与lastpos节点的计算,以及如何根据这些计算结果构建DFA。此外,还讨论了如何证明两个正则表达式的等价性,以及最小状态DFA的构造方法。

Exercises for Section 3.9

3.9.1

Extend the table of Fig. 3.58 to include the operators

Answer
node nnullable(n)firstpos(n)
n = c_1 ?truefirstpos(c_1)
n = c_1 +nullable(c_1)firstpos(c_1)

3.9.2

Use Algorithm 3.36 to convert the regular expressions of
Ex­ercise 3.7.3 directly to deterministic finite automata.

Answer
  1. (a|b)*

    • Syntax tree

      3 9 2-1-1

    • firstpos and lastpos for nodes in the syntax tree

      3 9 2-1-2

    • The function followpos

      node nfollowpos(n)
      1{1, 2, 3}
      2{1, 2, 3}
      3
    • Steps

      The value of firstpos for the root of the tree is {1, 2, 3}, so this set is the start state of D. Call this set of states A. We compute Dtran[A, a] and Dtran[A, b]. Among the positions of A, 1 correspond to a, while 2 correspond to b. Thus Dtran[A, a] = followpos(1) = {1, 2, 3}, Dtran[A, b] = followpos(2) = {1, 2, 3}. Both the results are set A, so dose not have new state, end the computation.

    • DFA

      3 9 2-1-dfa

  2. (a*|b*)*

  3. ((ε|a)|b*)*

  4. (a|b)*abb(a|b)*

3.9.3 !

We can prove that two regular expressions are equivalent by
showing that their minimum-state DFA’s are the same up to renaming of states.
Show in this way that the following regular expressions: (a|b)*, (a*|b*)*, and
((ε|a)b*)* are all equivalent. Note: You may have constructed the DFA’s for
these expressions in response to Exercise 3.7.3.

Answer

Refer to the answers of 3.7.3 and 3.9.2-1

3.9.4 !

Construct the minimum-state DFA’s for the following regular expressions:

  1. (a|b)*a(a|b)
  2. (a|b)*a(a|b)(a|b)
  3. (a|b)*a(a|b)(a|b)(a|b)

Do you see a pattern?

3.9.5 !!

To make formal the informal claim of Example 3.25, show
that any deterministic finite automaton for the regular expression

(a|b)*a(a|b)…(a|b)

where (a|b) appears n - 1 times at the end, must have at least 2n states. Hint:
Observe the pattern in Exercise 3.9.4. What condition regarding the history of
inputs does each state represent?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值