【形式语言与自动机】【《形式语言与自动机理论(第4版)》笔记】第四章:正则表达式
前导
【形式语言与自动机】【《形式语言与自动机理论(第4版)》笔记】第一章:绪论
【形式语言与自动机】【《形式语言与自动机理论(第4版)》笔记】第二章:文法
【形式语言与自动机】【《形式语言与自动机理论(第4版)》笔记】第三章:有穷状态自动机
4.1|启示
4.2|正则表达式的形式定义
正则表达式性质
- L ( ( r + ε ) ? ) = L ( r ? ) L((r + \varepsilon)^{*}) = L(r^{*}) L((r+ε)?)=L(r?)
- L ( ( r ? s ? ) ? ) = L ( ( r + s ) ? ) L((r^{*} s^{*})^{*}) = L((r + s)^{*}) L((r?s?)?)=L((r+s)?)
4.3|正则表达式与 F A FA FA等价
正则表达式表示的语言是正则语言
- 施归纳于正则表达式中所含运算符的个数 n n n,证明对于字母表 Σ \Sigma Σ上的任意正则表达式 x x x,存在 F A ? M FA \ M FA?M,使得 L ( M ) = L ( x ) L(M) = L(x) L(M)=L(x),并且 M M M恰有一个终止状态,而且 M M M在终止状态下不做任何移动
n = 0 n = 0 n=0时
r = ε r = \varepsilon r=ε
r = ? r = \emptyset r=?
? a ∈ Σ \forall a \in \Sigma ?a∈Σ, r = a r = a r=a
n = k + 1 n = k + 1 n=k+1时
r = r 1 + r 2 r = r_{1} + r_{2} r=r1?+r2?
- 此时 r 1 r_{1} r1?, r 2 r_{2} r2?种运算符的个数不会大于 k k k,由归纳假设,存在满足定理要求的 ε ? N F A \varepsilon-NFA ε?NFA, M 1 = ( Q 1 , Σ , δ 1 , q 01 , { ? f 1 ? } ) M_{1} = (Q_{1} , \Sigma , \delta_{1} , q_{01} , \set{f_{1}}) M1?=(Q1?,Σ,δ1?,q01?,{f1?}), M 2 = ( Q 2 , Σ , δ 2 , q 02 , { ? f 2 ? } ) M_{2} = (Q_{2} , \Sigma , \delta_{2} , q_{02} , \set{f_{2}}) M2?=(Q2?,Σ,δ2?,q02?,{f2?}),使得 L ( M 1 ) = L ( r 1 ) L(M_{1}) = L(r_{1}) L(M1?)=L(r1?), L ( M 2 ) = L ( r 2 ) L(M_{2}) = L(r_{2}) L(M2?)=L(r2?)
- 不妨设
Q
1
∪
Q
2
=
?
Q_{1} \cup Q_{2} = \emptyset
Q1?∪Q2?=?,取
q
0
q_{0}
q0?,
f
?
Q
1
∪
Q
2
f \notin Q_{1} \cup Q_{2}
f∈/Q1?∪Q2?,令
M
=
(
Q
1
∪
Q
2
∪
{
?
(
?
}
q
0
,
f
)
,
Σ
,
δ
,
q
0
,
{
?
f
?
}
)
M = (Q_{1} \cup Q_{2} \cup \set(q_{0} , f) , \Sigma , \delta , q_{0} , \set{f})
M=(Q1?∪Q2?∪{(}q0?,f),Σ,δ,q0?,{f}),其中
δ
\delta
δ的定义为
- δ ( q 0 , ε ) = { ? q 01 , q 02 ? } \delta(q_{0} , \varepsilon) = \set{q_{01} , q_{02}} δ(q0?,ε)={q01?,q02?}
- 对 ? q ∈ Q 1 ? { ? f 1 ? } \forall q \in Q_{1} - \set{f_{1}} ?q∈Q1??{f1?}, a ∈ Σ ∪ { ? ε ? } a \in \Sigma \cup \set{\varepsilon} a∈Σ∪{ε}, δ ( q , a ) = δ 1 ( q , a ) \delta(q , a) = \delta_{1}(q , a) δ(q,a)=δ1?(q,a),对 ? q ∈ Q 2 ? { ? f 1 ? } \forall q \in Q_{2} - \set{f_{1}} ?q∈Q2??{f1?}, a ∈ Σ ∪ { ? ε ? } a \in \Sigma \cup \set{\varepsilon} a∈Σ∪{ε}, δ ( q , a ) = δ 2 ( q , a ) \delta(q , a) = \delta_{2}(q , a) δ(q,a)=δ2?(q,a)
- δ ( f 1 , ε ) = { ? f ? } \delta(f_{1} , \varepsilon) = \set{f} δ(f1?,ε)={f}
- δ ( f 2 , ε ) = { ? f ? } \delta(f_{2} , \varepsilon) = \set{f} δ(f2?,ε)={f}
- 往证
L
(
r
1
+
r
2
)
=
L
(
M
)
L(r_{1} + r_{2}) = L(M)
L(r1?+r2?)=L(M)
- 由归纳假设, L ( r 1 ) = L ( M 1 ) L(r_{1}) = L(M_{1}) L(r1?)=L(M1?), L ( r 2 ) = L ( M 2 ) L(r_{2}) = L(M_{2}) L(r2?)=L(M2?),根据正则表达式的定义 L ( r 1 + r 2 ) = L ( r 1 ) ∪ L ( r 2 ) L(r_{1} + r_{2}) = L(r_{1}) \cup L(r_{2}) L(r1?+r2?)=L(r1?)∪L(r2?), L ( r 1 + r 2 ) = L ( M 1 ) ∪ L ( M 2 ) L(r_{1} + r_{2}) = L(M_{1}) \cup L(M_{2}) L(r1?+r2?)=L(M1?)∪L(M2?),因此,只需要证明 L ( M ) = L ( M 1 ) ∪ L ( M 2 ) L(M) = L(M_{1}) \cup L(M_{2}) L(M)=L(M1?)∪L(M2?)
- 先证
L
(
M
1
)
∪
L
(
M
2
)
?
L
(
M
)
L(M_{1}) \cup L(M_{2}) \subseteq L(M)
L(M1?)∪L(M2?)?L(M)
- 设 x ∈ L ( M 1 ) ∪ L ( M 2 ) x \in L(M_{1}) \cup L(M_{2}) x∈L(M1?)∪L(M2?),从而有 x ∈ L ( M 1 ) x \in L(M_{1}) x∈L(M1?),或者 x ∈ L ( M 2 ) x \in L(M_{2}) x∈L(M2?)
- 当 x ∈ L ( M 1 ) x \in L(M_{1}) x∈L(M1?)时,有 δ 1 ( q 01 , x ) = { ? f 1 ? } \delta_{1}(q_{01} , x) = \set{f_{1}} δ1?(q01?,x)={f1?}
- 由 M M M的定义可得 δ ( q 0 , x ) = δ ( q 0 , ε x ε ) = δ ( δ ( q 0 , ε ) , x ε ) = δ ( { ? q 01 , q 02 ? } , x ε ) = δ ( q 01 , x ε ) ∪ δ ( q 02 , x ε ) = δ ( δ ( q 01 , x ) , ε ) ∪ δ ( δ ( q 02 , x ) , ε ) = δ ( δ 1 ( q 01 , x ) , ε ) ∪ δ ( δ 2 ( q 02 , x ) , ε ) = { ? f ? } ∪ δ ( δ 2 ( q 02 , x ) , ε ) \begin{aligned} \delta(q_{0} , x) &= \delta(q_{0} , \varepsilon x \varepsilon) \\ &= \delta(\delta(q_{0} , \varepsilon) , x \varepsilon) \\ &= \delta(\set{q_{01} , q_{02}} , x \varepsilon) \\ &= \delta(q_{01} , x \varepsilon) \cup \delta(q_{02} , x \varepsilon) \\ &= \delta(\delta(q_{01} , x) , \varepsilon) \cup \delta(\delta(q_{02} , x) , \varepsilon) \\ &= \delta(\delta_{1}(q_{01} , x) , \varepsilon) \cup \delta(\delta_{2}(q_{02} , x) , \varepsilon) \\ &= \set{f} \cup \delta(\delta_{2}(q_{02} , x) , \varepsilon) \end{aligned} δ(q0?,x)?=δ(q0?,εxε)=δ(δ(q0?,ε),xε)=δ({q01?,q02?},xε)=δ(q01?,xε)∪δ(q02?,xε)=δ(δ(q01?,x),ε)∪δ(δ(q02?,x),ε)=δ(δ1?(q01?,x),ε)∪δ(δ2?(q02?,x),ε)={f}∪δ(δ2?(q02?,x),ε)?
- 即 x ∈ L ( M ) x \in L(M) x∈L(M)
- 同理可证,当 x ∈ L ( M 2 ) x \in L(M_{2}) x∈L(M2?)时, x ∈ L ( M ) x \in L(M) x∈L(M)
- 再证
L
(
M
)
?
L
(
M
1
)
∪
L
(
M
2
)
L(M) \subseteq L(M_{1}) \cup L(M_{2})
L(M)?L(M1?)∪L(M2?)
- 设 x ∈ L ( M ) x \in L(M) x∈L(M), f ∈ δ ( q 0 , x ) f \in \delta(q_{0} , x) f∈δ(q0?,x)
- 按照 M M M的定义, δ ( q 0 , x ) = δ ( q 0 , ε x ε ) = δ ( δ ( q 0 , ε ) , x ε ) = δ ( { ? q 01 , q 02 ? } , x ε ) = δ ( q 01 , x ε ) ∪ δ ( q 02 , x ε ) = δ ( δ ( q 01 , x ) , ε ) ∪ δ ( δ ( q 02 , x ) , ε ) = δ ( δ 1 ( q 01 , x ) , ε ) ∪ δ ( δ 2 ( q 02 , x ) , ε ) \begin{aligned} \delta(q_{0} , x) &= \delta(q_{0} , \varepsilon x \varepsilon) \\ &= \delta(\delta(q_{0} , \varepsilon) , x \varepsilon) \\ &= \delta(\set{q_{01} , q_{02}} , x \varepsilon) \\ &= \delta(q_{01} , x \varepsilon) \cup \delta(q_{02} , x \varepsilon) \\ &= \delta(\delta(q_{01} , x) , \varepsilon) \cup \delta(\delta(q_{02} , x) , \varepsilon) \\ &= \delta(\delta_{1}(q_{01} , x) , \varepsilon) \cup \delta(\delta_{2}(q_{02} , x) , \varepsilon) \end{aligned} δ(q0?,x)?=δ(q0?,εxε)=δ(δ(q0?,ε),xε)=δ({q01?,q02?},xε)=δ(q01?,xε)∪δ(q02?,xε)=δ(δ(q01?,x),ε)∪δ(δ(q02?,x),ε)=δ(δ1?(q01?,x),ε)∪δ(δ2?(q02?,x),ε)?
-
f
∈
δ
(
q
0
,
x
)
f \in \delta(q_{0} , x)
f∈δ(q0?,x),并且此时
M
M
M的最后一次移动必是根据
δ
(
f
1
,
ε
)
=
{
?
f
?
}
\delta(f_{1} , \varepsilon) = \set{f}
δ(f1?,ε)={f}或
δ
(
f
2
,
ε
)
=
{
?
f
?
}
\delta(f_{2} , \varepsilon) = \set{f}
δ(f2?,ε)={f}之一进行的移动
- 如果是根据定义式 δ ( f 1 , ε ) = { ? f ? } \delta(f_{1} , \varepsilon) = \set{f} δ(f1?,ε)={f}进行的最后一次移动,此时必有 δ 1 ( q 01 , x ) = { ? f 1 ? } \delta_{1}(q_{01} , x) = \set{f_{1}} δ1?(q01?,x)={f1?}, x ∈ L ( M 1 ) x \in L(M_{1}) x∈L(M1?)
- 如果是根据定义式 δ ( f 2 , ε ) = { ? f ? } \delta(f_{2} , \varepsilon) = \set{f} δ(f2?,ε)={f}进行的最后一次移动,此时必有 δ 2 ( q 02 , x ) = { ? f 2 ? } \delta_{2}(q_{02} , x) = \set{f_{2}} δ2?(q02?,x)={f2?}, x ∈ L ( M 2 ) x \in L(M_{2}) x∈L(M2?)
- 无论是哪一种情况,都有 x ∈ L ( M 1 ) ∪ L ( M 2 ) x \in L(M_{1}) \cup L(M_{2}) x∈L(M1?)∪L(M2?)
r = r 1 r 2 r = r_{1} r_{2} r=r1?r2?
- 此时 r 1 r_{1} r1?、 r 2 r_{2} r2?中运算符的个数不会大于 k k k,由归纳假设,存在满足定理要求的 ε ? N F A \varepsilon-NFA ε?NFA, M 1 = ( Q 1 , Σ , δ 1 , q 01 , { ? f 1 ? } ) M_{1} = (Q_{1} , \Sigma , \delta_{1} , q_{01} , \set{f_{1}}) M1?=(Q1?,Σ,δ1?,q01?,{f1?}), M 2 = ( Q 2 , Σ , δ 2 , q 02 , { ? f 2 ? } ) M_{2} = (Q_{2} , \Sigma , \delta_{2} , q_{02} , \set{f_{2}}) M2?=(Q2?,Σ,δ2?,q02?,{f2?}),使得 L ( M 1 ) = L ( r 1 ) L(M_{1}) = L(r_{1}) L(M1?)=L(r1?), L ( M 2 ) = L ( r 2 ) L(M_{2}) = L(r_{2}) L(M2?)=L(r2?),而且 Q 1 ∩ Q 2 = ? Q_{1} \cap Q_{2} = \emptyset Q1?∩Q2?=?
- 取
M
=
(
Q
1
∪
Q
2
,
Σ
,
δ
,
q
01
,
{
?
f
2
?
}
)
M = (Q_{1} \cup Q_{2} , \Sigma , \delta , q_{01} , \set{f_{2}})
M=(Q1?∪Q2?,Σ,δ,q01?,{f2?}),其中
δ
\delta
δ的定义为
- 对 ? q ∈ Q 1 ? { ? f 1 ? } \forall q \in Q_{1} - \set{f_{1}} ?q∈Q1??{f1?}, a ∈ Σ ∪ { ? ε ? } a \in \Sigma \cup \set{\varepsilon} a∈Σ∪{ε}, δ ( q , a ) = δ 1 ( q , a ) \delta(q , a) = \delta_{1}(q , a) δ(q,a)=δ1?(q,a)
- 对 ? q ∈ Q 2 \forall q \in Q_{2} ?q∈Q2?, a ∈ Σ ∪ { ? ε ? } a \in \Sigma \cup \set{\varepsilon} a∈Σ∪{ε}, δ ( q , a ) = δ 2 ( q , a ) \delta(q , a) = \delta_{2}(q , a) δ(q,a)=δ2?(q,a)
- δ ( f 1 , ε ) = { ? q 02 ? } \delta(f_{1} , \varepsilon) = \set{q_{02}} δ(f1?,ε)={q02?}
- 往证
L
(
r
1
r
2
)
=
L
(
M
)
L(r_{1} r_{2}) = L(M)
L(r1?r2?)=L(M)
- 由归纳假设, L ( r 1 ) = L ( M 1 ) L(r_{1}) = L(M_{1}) L(r1?)=L(M1?), L ( r 2 ) = L ( M 2 ) L(r_{2}) = L(M_{2}) L(r2?)=L(M2?),根据正则表达式的定义 L ( r 1 r 2 ) = L ( r 1 ) L ( r 2 ) L(r_{1} r_{2}) = L(r_{1}) L(r_{2}) L(r1?r2?)=L(r1?)L(r2?), L ( r 1 r 2 ) = L ( M 1 ) L ( M 2 ) L(r_{1} r_{2}) = L(M_{1}) L(M_{2}) L(r1?r2?)=L(M1?)L(M2?),因此,只需要证明 L ( M ) = L ( M 1 ) L ( M 2 ) L(M) = L(M_{1}) L(M_{2}) L(M)=L(M1?)L(M2?)
- 先证
L
(
M
1
)
L
(
M
2
)
?
L
(
M
)
L(M_{1}) L(M_{2}) \subseteq L(M)
L(M1?)L(M2?)?L(M)
- 设 x ∈ L ( M 1 ) L ( M 2 ) x \in L(M_{1}) L(M_{2}) x∈L(M1?)L(M2?),从而有 x 1 ∈ L ( M 1 ) x_{1} \in L(M_{1}) x1?∈L(M1?)并且 x 2 ∈ L ( M 2 ) x_{2} \in L(M_{2}) x2?∈L(M2?),使得 x = x 1 x 2 x = x_{1} x_{2} x=x1?x2?
- δ ( q 01 , x 1 ) = δ 1 ( q 01 , x 1 ) = { ? f 1 ? } \delta(q_{01} , x_{1}) = \delta_{1}(q_{01} , x_{1}) = \set{f_{1}} δ(q01?,x1?)=δ1?(q01?,x1?)={f1?}, δ ( q 02 , x 2 ) = δ 2 ( q 02 , x 2 ) = { ? f 2 ? } \delta(q_{02} , x_{2}) = \delta_{2}(q_{02} , x_{2}) = \set{f_{2}} δ(q02?,x2?)=δ2?(q02?,x2?)={f2?}
- δ ( q 01 , x ) = δ ( q 01 , x 1 x 2 ) = δ ( δ ( q 01 , x 1 ) , x 2 ) = δ ( δ 1 ( q 01 , x 1 ) , x 2 ) = δ ( f 1 , x 2 ) = δ ( f 1 , ε x 2 ) = δ ( δ ( f 1 , ε ) , x 2 ) = δ ( q 02 , x 2 ) = δ 2 ( q 02 , x 2 ) = { ? f 2 ? } \begin{aligned} \delta(q_{01} , x) &= \delta(q_{01} , x_{1} x_{2}) \\ &= \delta(\delta(q_{01} , x_{1}) , x_{2}) \\ &= \delta(\delta_{1}(q_{01} , x_{1}) , x_{2}) \\ &= \delta(f_{1} , x_{2}) \\ &= \delta(f_{1} , \varepsilon x_{2}) \\ &= \delta(\delta(f_{1} , \varepsilon) , x_{2}) \\ &= \delta(q_{02} , x_{2}) \\ &= \delta_{2}(q_{02} , x_{2}) \\ &= \set{f_{2}} \end{aligned} δ(q01?,x)?=δ(q01?,x1?x2?)=δ(δ(q01?,x1?),x2?)=δ(δ1?(q01?,x1?),x2?)=δ(f1?,x2?)=δ(f1?,εx2?)=δ(δ(f1?,ε),x2?)=δ(q02?,x2?)=δ2?(q02?,x2?)={f2?}?
- 即 x ∈ L ( M ) x \in L(M) x∈L(M)
- 再证
L
(
M
)
?
L
(
M
1
)
L
(
M
2
)
L(M) \subseteq L(M_{1}) L(M_{2})
L(M)?L(M1?)L(M2?)
- 设 x ∈ L ( M ) x \in L(M) x∈L(M), δ ( q 01 , x ) = { ? f 2 ? } \delta(q_{01} , x) = \set{f_{2}} δ(q01?,x)={f2?}
- 必存在 x x x的前缀 x 1 x_{1} x1?和后缀 x 2 x_{2} x2?,使得 x = x 1 x 2 x = x_{1} x_{2} x=x1?x2?,并且 x 1 x_{1} x1?将 M M M从状态 q 01 q_{01} q01?引导到状态 f 1 f_{1} f1?, x 2 x_{2} x2?将 M M M从状态 q 02 q_{02} q02?引导到状态 f 2 f_{2} f2?,即 δ ( q 01 , x ) = δ ( q 01 , x 1 x 2 ) = δ ( f 1 , x 2 ) = δ ( f 1 , ε x 2 ) = δ ( q 02 , x 2 ) = { ? f 2 ? } \begin{aligned} \delta(q_{01} , x) &= \delta(q_{01} , x_{1} x_{2}) \\ &= \delta(f_{1} , x_{2}) \\ &= \delta(f_{1} , \varepsilon x_{2}) \\ &= \delta(q_{02} , x_{2}) \\ &= \set{f_{2}} \end{aligned} δ(q01?,x)?=δ(q01?,x1?x2?)=δ(f1?,x2?)=δ(f1?,εx2?)=δ(q02?,x2?)={f2?}?
- 其中, δ ( q 01 , x 1 ) = { ? f 1 ? } \delta(q_{01} , x_{1}) = \set{f_{1}} δ(q01?,x1?)={f1?},说明 δ 1 ( q 01 , x 1 ) = { ? f 1 ? } \delta_{1}(q_{01} , x_{1}) = \set{f_{1}} δ1?(q01?,x1?)={f1?}, δ ( q 02 , x 2 ) = { ? f 2 ? } \delta(q_{02} , x_{2}) = \set{f_{2}} δ(q02?,x2?)={f2?},说明 δ 2 ( q 02 , x 2 ) = { ? f 2 ? } \delta_{2}(q_{02} , x_{2}) = \set{f_{2}} δ2?(q02?,x2?)={f2?},这表明 x 1 ∈ L ( M 1 ) x_{1} \in L(M_{1}) x1?∈L(M1?), x 2 ∈ L ( M 2 ) x_{2} \in L(M_{2}) x2?∈L(M2?)
- 从而 x = x 1 x 2 ∈ L ( M 1 ) L ( M 2 ) x = x_{1} x_{2} \in L(M_{1}) L(M_{2}) x=x1?x2?∈L(M1?)L(M2?)
r = r 1 ? r = r_{1}^{*} r=r1??
- 此时 r 1 r_{1} r1?中运算符的个数不会大于 k k k,由归纳假设,存在满足定理要求的 ε ? N F A \varepsilon-NFA ε?NFA, M 1 = ( Q 1 , Σ , δ 1 , q 01 , { ? f 1 ? } ) M_{1} = (Q_{1} , \Sigma , \delta_{1} , q_{01} , \set{f_{1}}) M1?=(Q1?,Σ,δ1?,q01?,{f1?}),使得 L ( M 1 ) = L ( r 1 ) L(M_{1}) = L(r_{1}) L(M1?)=L(r1?)
- 取
M
=
(
Q
1
∪
{
?
q
0
,
f
?
}
,
Σ
,
δ
,
q
0
,
{
?
f
?
}
)
M = (Q_{1} \cup \set{q_{0} , f} , \Sigma , \delta , q_{0} , \set{f})
M=(Q1?∪{q0?,f},Σ,δ,q0?,{f}),其中
q
0
q_{0}
q0?,
f
?
Q
1
f \notin Q_{1}
f∈/Q1?,
δ
\delta
δ的定义为
- 对 ? q ∈ Q 1 ? { ? f 1 ? } \forall q \in Q_{1} - \set{f_{1}} ?q∈Q1??{f1?}, a ∈ Σ a \in \Sigma a∈Σ, δ ( q , a ) = δ 1 ( q , a ) \delta(q , a) = \delta_{1}(q , a) δ(q,a)=δ1?(q,a)
- δ ( f 1 , ε ) = { ? q 01 , f ? } \delta(f_{1} , \varepsilon) = \set{q_{01} , f} δ(f1?,ε)={q01?,f}
- δ ( q 0 , ε ) = { ? q 01 , f ? } \delta(q_{0} , \varepsilon) = \set{q_{01} , f} δ(q0?,ε)={q01?,f}
-
往证 L ( r 1 ? ) = L ( M ) L(r_{1}^{*}) = L(M) L(r1??)=L(M)
-
由归纳假设, L ( M 1 ) = L ( r 1 ) L(M_{1}) = L(r_{1}) L(M1?)=L(r1?),根据正则表达式的定义 L ( r ) = ( L ( r 1 ) ) ? L(r) = (L(r_{1}))^{*} L(r)=(L(r1?))?,只需证明 L ( M ) = ( L ( M 1 ) ) ? L(M) = (L(M_{1}))^{*} L(M)=(L(M1?))?
-
先证 L ( M ) ? ( L ( M 1 ) ) ? L(M) \subseteq (L(M_{1}))^{*} L(M)?(L(M1?))?
-
设 x ∈ L ( M ) x \in L(M) x∈L(M),如果 x = ε x = \varepsilon x=ε,由克林闭包的定义,显然 x ∈ ( L ( M 1 ) ) ? x \in (L(M_{1}))^{*} x∈(L(M1?))?
-
如果 x ≠ ε x \neq \varepsilon x=ε,由 M M M的定义,必定存在 x 1 x_{1} x1?, x 2 x_{2} x2?, ? \cdots ?, x n x_{n} xn?, x = x 1 x 2 ? x n ( n ≥ 1 ) x = x_{1} x_{2} \cdots x_{n} (n \geq 1) x=x1?x2??xn?(n≥1)满足 δ ( q 0 , x ) = δ ( q 0 , x 1 x 2 ? x n ) = δ ( δ ( q 0 , ε ) , x 1 x 2 ? x n ) = δ ( q 01 , x 1 x 2 ? x n ) = δ ( δ 1 ( q 01 , x 1 ) , x 2 ? x n ) = δ ( f 1 , x 2 ? x n ) ? = δ ( f 1 , x n ) = δ ( δ ( f 1 , ε ) , x n ) = δ ( q 01 , x n ) = δ ( δ 1 ( q 01 , x n ) , ε ) = δ ( f 1 , ε ) = { ? f ? } \begin{aligned} \delta(q_{0} , x) &= \delta(q_{0} , x_{1} x_{2} \cdots x_{n}) \\ &= \delta(\delta(q_{0} , \varepsilon) , x_{1} x_{2} \cdots x_{n}) \\ &= \delta(q_{01} , x_{1} x_{2} \cdots x_{n}) \\ &= \delta(\delta_{1}(q_{01} , x_{1}) , x_{2} \cdots x_{n}) \\ &= \delta(f_{1} , x_{2} \cdots x_{n}) \\ &\cdots \\ &= \delta(f_{1} , x_{n}) \\ &= \delta(\delta(f_{1} , \varepsilon) , x_{n}) \\ &= \delta(q_{01} , x_{n}) \\ &= \delta(\delta_{1}(q_{01} , x_{n}) , \varepsilon) \\ &= \delta(f_{1} , \varepsilon) \\ &= \set{f} \end{aligned} δ(q0?,x)?=δ(q0?,x1?x2??xn?)=δ(δ(q0?,ε),x1?x2??xn?)=δ(q01?,x1?x2??xn?)=δ(δ1?(q01?,x1?),x2??xn?)=δ(f1?,x2??xn?)?=δ(f1?,xn?)=δ(δ(f1?,ε),xn?)=δ(q01?,xn?)=δ(δ1?(q01?,xn?),ε)=δ(f1?,ε)={f}?
-
这表明 x = x 1 x 2 ? x n ∈ ( L ( M 1 ) ) ? x = x_{1} x_{2} \cdots x_{n} \in (L(M_{1}))^{*} x=x1?x2??xn?∈(L(M1?))?
-
-
再证 ( L ( M 1 ) ) ? ? L ( M ) (L(M_{1}))^{*} \subseteq L(M) (L(M1?))??L(M)
- 类似地,不难证明 ( L ( M 1 ) ) ? ? L ( M ) (L(M_{1}))^{*} \subseteq L(M) (L(M1?))??L(M)
-
例题
问题
- 构造与正则表达式 ( 0 + 1 ) ? 0 + ( 00 ) ? (0 + 1)^{*} 0 + (00)^{*} (0+1)?0+(00)?等价的 F A FA FA
解答
正则语言可以用正则表达式表示
构造与 D F A DFA DFA等价的正则表达式
- 设 D F A ? M = ( { ? q 1 , q 2 , ? ? , q n ? } , Σ , δ , q 1 , F ) DFA \ M = (\set{q_{1} , q_{2} , \cdots , q_{n}} , \Sigma , \delta , q_{1} , F) DFA?M=({q1?,q2?,?,qn?},Σ,δ,q1?,F)
- 令 R i j k = { ? x ∣ δ ( q i , x ) = q j , 而且对于 x 的任意前缀 y ( y ≠ x , y ≠ ε ) , 如果 δ ( q i , y ) = q l , 则 l ≤ k ? } R_{ij}^{k} = \set{x \mid \delta(q_{i} , x) = q_{j} , 而且对于 x 的任意前缀 y (y \neq x , y \neq \varepsilon) , 如果 \delta(q_{i} , y) = q_{l} , 则 l \leq k} Rijk?={x∣δ(qi?,x)=qj?,而且对于x的任意前缀y(y=x,y=ε),如果δ(qi?,y)=ql?,则l≤k}
- 为了便于计算,将 R i j k R_{ij}^{k} Rijk?递归地定义为
R i j 0 = { { ? a ∣ δ ( q i , a ) = q j ? } , i ≠ j { ? a ∣ δ ( q i , a ) = q j ? } ∪ { ? ε ? } , i = j R_{ij}^{0} = \begin{cases} \set{a \mid \delta(q_{i} , a) = q_{j}} , & i \neq j \\ \set{a \mid \delta(q_{i} , a) = q_{j}} \cup \set{\varepsilon} , & i = j \end{cases} Rij0?={{a∣δ(qi?,a)=qj?},{a∣δ(qi?,a)=qj?}∪{ε},?i=ji=j?
R i j k = R i k k ? 1 ( R k k k ? 1 ) ? R k j k ? 1 ∪ R i j k ? 1 R_{ij}^{k} = R_{ik}^{k - 1} (R_{kk}^{k - 1})^{*} R_{kj}^{k - 1} \cup R_{ij}^{k - 1} Rijk?=Rikk?1?(Rkkk?1?)?Rkjk?1?∪Rijk?1?
- 显然, L ( M ) = ? q f ∈ F R 1 f n L(M) = \displaystyle\bigcup\limits_{q_{f} \in F}{R_{1f}^{n}} L(M)=qf?∈F??R1fn?
图上作业方法
- 对 D F A ? M = ( Q , Σ , δ , q 0 , F ) DFA \ M = (Q , \Sigma , \delta , q_{0} , F) DFA?M=(Q,Σ,δ,q0?,F)的状态转移图,操作步骤如下
预处理
- 在状态转移图中增加状态 X X X和 Y Y Y,从状态 X X X到 M M M的开始状态 q 0 q_{0} q0?引一条标记为 ε \varepsilon ε的弧,对 ? q ∈ F \forall q \in F ?q∈F,从状态 q q q到状态 Y Y Y分别引一条标记为 ε \varepsilon ε的弧
- 去掉所有的不可达状态
循环操作
- 重复如下操作,直到该图中不再包含除了
X
X
X和
Y
Y
Y的其他状态,并且这两个状态之间最多只有一条弧
- 并弧:对图中任意两个状态 q q q, p p p,如果图中包含有从 q q q到 p p p的标记为 r 1 r_{1} r1?, r 2 r_{2} r2?, ? \cdots ?, r g r_{g} rg?的并行弧,则用从 q q q到 p p p的、标记为 r 1 + r 2 + ? + r g r_{1} + r_{2} + \cdots + r_{g} r1?+r2?+?+rg?的弧取代这 g g g个并行弧,其中, q q q和 p p p可以是同一个状态
- 去状态 1 1 1:对图中任意 3 3 3个状态 q q q, p p p, t t t,如果从 q q q到 p p p有一条标记为 r 1 r_{1} r1?的弧,从 p p p到 t t t有一条标记为 r 2 r_{2} r2?的弧,并且不存在从状态 p p p到状态 p p p的弧,则将状态 p p p和与之关联的这两条弧去掉,增加一条从 q q q到 t t t的标记为 r 1 r 2 r_{1} r_{2} r1?r2?的弧
- 去状态 2 2 2:对图中任意 3 3 3个状态 q q q, p p p, t t t,如果从 q q q到 p p p有一条标记为 r 1 r_{1} r1?的弧,从 p p p到 t t t有一条标记为 r 2 r_{2} r2?的弧,并且存在一条从状态 p p p到状态 p p p标记为 r 3 r_{3} r3?的弧,则将状态 p p p和与之关联的这 3 3 3条弧去掉,增加一条从 q q q到 t t t的标记为 r 1 r 3 ? r 2 r_{1} r_{3}^{*} r_{2} r1?r3??r2?的弧
- 去状态 3 3 3:如果图中只有 3 3 3个状态,而且不存在从状态 X X X到 Y Y Y的路,则将除状态 X X X和 Y Y Y之外的第三个状态及其相关的弧全部删除
- 从状态 X X X到 Y Y Y的弧的标记为所求的正则表达式,如果该弧不存在,则所求的正则表达式为 ? \emptyset ?
4.4|正则语言等价模型的总结
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!