简体   繁体   English

从有限自动机构造正则表达式

[英]Constructing a Regular Expression from a Finite Automata

I'm trying to construct a regular expression from a Finite Automaton but found my self completely stuck with this one. 我试图从有限自动机构造一个正则表达式,但发现自己完全陷入了困境。 The regex to use is like this: 使用的正则表达式是这样的:

? = 0 or 1 = 0或1
* = 0 or more * = 0或更多
+= 1 or more + = 1或更多
| | = or =或
_ = empty string _ =空字符串
@ = empty set @ =空集
() = parentheses ()=括号

As I understand the strings must either be "b*" end with "a*" or end with "a+bb+" 据我了解,字符串必须以“ b *”结尾,以“ a *”结尾或以“ a + bb +”结尾
What i have now is ((b*(a+(bb))*)*) but that doesn't take into account a string ending with 'a'. 我现在拥有的是((b*(a+(bb))*)*)但这没有考虑以'a'结尾的字符串。

As said, I'm 100% stuck with this and just can't get my head around how I am supposed to work with this. 如前所述,我100%坚持使用此功能,只是无法理解应该如何使用此功能。

image: http://img593.imageshack.us/img593/2563/28438387.jpg 图片: http : //img593.imageshack.us/img593/2563/28438387.jpg

CODE: 码:
Type of the automaton 自动机的类型
FA F A

States 状态
q1 Q1
q2 Q2
q3 Q3
q4 Q4

Alphabet 字母
a 一种
b b

Initial state 初始状态
q3 Q3

Final states 最终状态
q3 Q3
q4 Q4

Transitions 转变
q1 a q2 q1一q2
q1 b q3 q1 b q3
q2 a q2 q2一q2
q2 b q2 q2 b q2
q3 a q4 第3季度
q3 b q3 第3季
q4 a q4 第4季度
q4 b q1 第4季度

Any solutions or tips appreciated! 任何解决方案或提示赞赏!

If you feed this to tools for automata (eg, Vcsn ), you'd get this: 如果将其提供给自动机工具(例如Vcsn ),则会得到以下信息:

In [1]: import vcsn

In [2]: %%automaton a
   ...: $  -> q3
   ...: q1 -> q2 a
   ...: q1 -> q3 b
   ...: q2 -> q2 a
   ...: q2 -> q2 b
   ...: q3 -> q4 a
   ...: q3 -> q3 b
   ...: q4 -> q4 a
   ...: q4 -> q1 b
   ...: q3 -> $
   ...: q4 -> $
   ...: 
mutable_automaton<letterset<char_letters(ab)>, b>

In [3]: a.expression()
Out[3]: (b+aa*bb)*(\e+aa*)

where \\e denotes the empty string. 其中\\e表示空字符串。 Then it's only a problem of syntax conversion. 然后,这只是语法转换的问题。

Graphically: 图形:

VCSN图形呈现

See this example live , and toy with it. 请参见本示例live ,并使用它作为玩具。

It isn't possible to get from q2 to a final state. 从q2到最终状态是不可能的。 Remove it and the resulting DFA should be easier to convert. 删除它,生成的DFA应该更容易转换。

As I understand the strings must either be "b*" end with "a*" or end with "a+bb+" What i have now is ((b*(a+(bb)) ) ) but that doesn't take into account a string ending with 'a'. 据我所知,字符串必须是“b *”以“a *”结尾或以“a + bb +”结尾我现在拥有的是((b *(a +(bb)) )但是这不是以“a”结尾的字符串。

Imagine q3 was not a final state, and q4 was the initial state. 想象q3不是最终状态,而q4是初始状态。 What would the regex look like then? 那么正则表达式会是什么样? Changing that into what you want shouldn't be too hard, just don't be afraid to have the same state and/or transitions described by more than one part of the regex. 将其更改为您想要的内容应该不会太困难,只是不要害怕让正则表达式的多个部分描述相同的状态和/或转换。

One more hint: I'm pretty sure you're going to need to use either ? 还有一个提示:我很确定你还需要使用它们? or | 或者| at least once. 至少一次。

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

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