public DFA(NFA nfa) { this.nfa = nfa; allStateNodes = new List <DFAStateNode>(); endStateNodes = new List <DFAStateNode>(); minDFAAllStateNodes = new List <DFAStateNode>(); minDFAEndStateNodes = new List <DFAStateNode>(); CreateDFA(nfa); }
/// <summary> /// 用NFA构造DFA /// </summary> /// <param name="nfa"></param> private void CreateDFA(NFA nfa) { Stack <DFAStateNode> stack = new Stack <DFAStateNode>(); startStateNode = new DFAStateNode(); startStateNode.AddNFAStateNodeList(NullClosure(nfa.StartStateNode)); allStateNodes.Add(startStateNode); stack.Push(startStateNode); while (stack.Count != 0) { DFAStateNode curNode = stack.Pop(); foreach (char c in nfa.Alphabet) { List <NFAStateNode> destNodes = NullClosure(MoveTo(curNode, c)); // 是否需要产生新的DFA状态节点 bool needNew = true; foreach (DFAStateNode existedNode in allStateNodes) { if (existedNode.Contains(destNodes)) { curNode.Connect(existedNode, c); needNew = false; break; } } if (needNew && destNodes.Count != 0) { DFAStateNode newNode = new DFAStateNode(); newNode.AddNFAStateNodeList(destNodes); curNode.Connect(newNode, c); stack.Push(newNode); allStateNodes.Add(newNode); } } } FillEndStateNodes(); }