//tested public RegularExpression Concat(RegularExpression right) { NFAModel model = this.nfa; model.tailState.AddEdge(right.nfa.entryEdge); model.tailState = right.nfa.tailState; return(this); }
public RegularExpression KleeneStar() { NFAModel model = new NFAModel(); model.tailState = new NFAState(); model.entryEdge = new NFAEdge(model.tailState, 0); nfa.tailState.AddEdgeTo(model.tailState); model.tailState.AddEdge(nfa.entryEdge); nfa = model; return(this); }
//basisOp //tested public RegularExpression DefineLiteral(string input) { NFAModel model = new NFAModel(); NFAState s = new NFAState(0); model.entryEdge = new NFAEdge(s, 0); for (int i = 0; i < input.Length; i++) { NFAState state = new NFAState(i + 1); s.AddEdge(new NFAEdge(state, input[i])); s = state; } model.tailState = new NFAState(s.id + 1); s.AddEdgeTo(model.tailState); nfa = model; return(this); }
//tested public RegularExpression Union(RegularExpression right) { var left = this; NFAModel model = new NFAModel(); NFAState s = new NFAState(0); model.entryEdge = new NFAEdge(s, 0); s.AddEdge(left.nfa.entryEdge); s.AddEdge(right.nfa.entryEdge); model.tailState = new NFAState(); left.nfa.tailState.AddEdgeTo(model.tailState); right.nfa.tailState.AddEdgeTo(model.tailState); nfa = model; return(this); }
//Todo: need faster public void CreateDFAModel() { var finalNFA = new NFAModel(); var tmpState = new NFAState(0); finalNFA.entryEdge = new NFAEdge(tmpState); var index = 1; foreach (var regex in regexCollection) { var x = regex.NFAModel; tmpState.AddEdge(x.entryEdge); index = x.RenameStates(index); } //make first dfaset DFAState first = new DFAState(tmpState); int id = 0; first.Id = id++; DFAList.Add(first); for (int i = 0; i < DFAList.Count; i++) { var list = new List <int>(); //possible move foreach (var x in DFAList[i].Edges) { if (!list.Contains(x.statement)) { list.Add(x.statement); } } foreach (var x in list) { var lead = DFAList[i].E_closure(x); var have = false; foreach (var u in DFAList) { if (u.isEqual(lead)) { DFAList[i].LeadTo(u, x); have = true; break; } } if (have) { continue; } lead.Id = id++; DFAList[i].LeadTo(lead, x); DFAList.Add(lead); } } //change priority here for (int i = regexCollection.Count - 1; i >= 0; i--) { var op = regexCollection[i]; var nfa = op.NFAModel; foreach (var x in DFAList) { if (x.Contains(nfa.tailState)) { x.isEndState = true; x.describtion = op.Describtion; x.index = op.Index; } } } }