如何将 NFA 转换为正则表达式
我知道将正则表达式转换为 NFA 是有一个算法的。
但我想知道是否有一种算法可以将 NFA 转换为正则表达式。 如果有,那是什么?
如果没有,我也想知道是否所有 NFA 都可以转换为正则表达式。 是否存在正则表达式无法表示的NFA?
谢谢你! :D
I knew that converting a regular expression to a NFA, there is a algorithm.
But I was wondering if there is a algorithm to convert a NFA to regular expression.
If there is, what is it?
And if there isn't, I am also wondering if all NFA can convert to a regular expression.
Is there a NFA that a regular expression that cannot represent?
Thank you! :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个算法,其中每个转换都逐渐用正则表达式替换,直到只有初始状态和最终状态: https://courses.engr.illinois.edu/cs373/sp2009/lectures/lect_08.pdf [PDF]
Here is an algorithm where each transition is incrementally replaced with a regex, until there is only an initial and final state: https://courses.engr.illinois.edu/cs373/sp2009/lectures/lect_08.pdf [PDF]
NFA 可以写成一个不等式系统(通过 Kleene 代数),每个状态有一个变量,每个最终状态 q 有一个不等式 q ≥ 1,对于 x 从状态 q 到状态的每次转换有一个不等式 q ≥ xr河这是一个 Kleene 代数上的右仿射定点系统,对于任何 q,其最小定点解为您提供由以 q 作为起始状态的 NFA 识别的正则表达式。该系统可以进行整理,以便对于每个给定的 q,所有不等式 q ≥ A、q ≥ B、...、q ≥ Z 都可以合并为 q ≥ A + B + ... Z。结果是 a矩阵系统
An NFA can be written as a system of inequalities (over a Kleene algebra), with one variable for each state, one inequality q ≥ 1 for each final state q and one inequality q ≥ x r for each transition on x from state q to state r. This is a right-affine fixed point system, over a Kleene algebra, whose least fixed point solution gives you, for any q, the regular expression recognized by the NFA that has q as the start state. The system can be collated, so that all the inequalities q ≥ A, q ≥ B, ..., q ≥ Z, for each given q, are combined into q ≥ A + B + ... Z. The result is a matrix system ???? ≥ ???? + H ????, where ???? is the vector of all the variables, ???? the vector of the affine coefficients - 0's for non-final states, 1's for final states, but those details are not important for what follows; and H is the matrix of all the linear coefficients.
To solve a right-affine system, do so one variable at a time. In Kleene algebra, the least fixed point solution to x ≥ a + bx is x = b* a. This applies both singly and matrix-wise, so that the least fixed point solutuion to ???? ≥ ???? + H ????, in matrix form is ???? = H* ????.
Matrices over Kleene algebras form a Kleene algebras, with matrix addition and matrix multiplication, respectively, for the sum and product operators and the matrix star for the Kleene star. Finding the matrix star is one and the same process as solving the corresponding system ???? ≥ ???? + H ????.
A Generic Example:
Consider the NFA with states q, r, s, with q the start state and s the only final state, and with transitions:
Let (x, y, z) = (0, 0, 1) denote the corresponding affine coefficients. Then, the corresponding right-affine system is:
Solve for s, first, to obtain
Substitute in the other inequalities to get:
Rewrite this as
where
Solve for r to get
Substitute into the inequality for q to get
and rewrite this as
where
Finally, solve this for q to get
This is also the general form for that embodies the generic fail-safe solution for NFA's with 3 states.
Since q is the start state, then a"* x" is the regular expression sought for, with a", x", a', b', d', e', x', y', x, y and z as indicated above. If you try to in-line substitute them all, the expression will blow up to a size that is exponential in the number of states and will be large in size even for three states.
An Optimized Example:
Consider the system for the NFA whose states are q, r, s, with q the start state, s the final state, and the transitions
The corresponding right-affine system is
Solve for s first:
Substitute into the inequality for r and solve to find the least fixed point:
Finally, substitute into the inequality for q and solve to find the least fixed point:
The resulting regular expression is (a + b)* a b (a + b)*. So, with chess-playing strategizing, simpler and optimal forms for the solution can be found.