//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); }
//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); }
//can only contain value //97-122,65-90,48-57 //a-z,A-Z,1-9 public static RegularExpression DefineRange(char x, char y) { if (x > y) { var t = x; x = y; y = x; } RegularExpression r = new RegularExpression(); var model = r.nfa = new NFAModel(); NFAState s = new NFAState(0); model.tailState = new NFAState(1); model.entryEdge = new NFAEdge(s); for (int i = x; i <= y; i++) { s.AddEdge(new NFAEdge(model.tailState, i)); } return(r); }
//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; } } } }