public void AddDFA(string[] input, WordType type) { RegularGrammer rg = new RegularGrammer(input); NFA nfa = new NFA(rg); DFA dfa = new DFA(nfa); if (!DFAs.ContainsKey(type)) { DFAs.Add(type, dfa); } else { DFAs[type] = dfa; } }
public DFA(NFA nfa) { StatusMap = new Dictionary <int, DFAStatus>(); Map = new Dictionary <int, HashSet <int> >(); _Map = new Dictionary <HashSet <int>, int>(); HashSet <int> tmp = new HashSet <int>(); tmp.Add(nfa.Start); HashSet <int> set = nfa.Closure(tmp); if (nfa.IsEnd(set)) { StatusMap.Add(0, new DFAStatus(0, 1)); } else { StatusMap.Add(0, new DFAStatus(0, 0)); } Map.Add(0, set); _Map.Add(set, 0); for (int i = 0; i < Map.Count; ++i) { HashSet <int> t = Map.ElementAt(i).Value; int nowId = MapKey(t); HashSet <int> result = new HashSet <int>(); //HashSet<int> newnext = new HashSet<int>(); foreach (char ch in nfa.Vt) { HashSet <int> newnext = new HashSet <int>(); if (ch == System.Configuration.ConfigurationManager.AppSettings["Empty"][0]) { continue; } foreach (int nfaId in t) { if (nfa.StatusMap[nfaId].Next.ContainsKey(ch)) { foreach (int ii in nfa.StatusMap[nfaId].Next[ch]) { newnext.Add(ii); } } } if (newnext.Count == 0) { continue; } HashSet <int> closenew = nfa.Closure(newnext); if (closenew.Count > 0 && !InMap(closenew)) { int newId = Map.Count, endf = 0; Map.Add(newId, closenew); _Map.Add(closenew, newId); if (nfa.IsEnd(closenew)) { endf = 1; } DFAStatus dfastate = new DFAStatus(newId, endf); StatusMap.Add(newId, dfastate); } if (closenew.Count > 0) { if (!StatusMap[nowId].Next.ContainsKey(ch)) { StatusMap[nowId].Next.Add(ch, MapKey(closenew)); } } } } }
public DFA(NFA nfa) { StatusMap = new Dictionary<int, DFAStatus>(); Map = new Dictionary<int, HashSet<int>>(); _Map = new Dictionary<HashSet<int>, int>(); HashSet<int> tmp = new HashSet<int>(); tmp.Add(nfa.Start); HashSet<int> set = nfa.Closure(tmp); if (nfa.IsEnd(set)) { StatusMap.Add(0, new DFAStatus(0, 1)); } else { StatusMap.Add(0, new DFAStatus(0, 0)); } Map.Add(0, set); _Map.Add(set, 0); for (int i = 0; i < Map.Count; ++i) { HashSet<int> t = Map.ElementAt(i).Value; int nowId = MapKey(t); HashSet<int> result = new HashSet<int>(); //HashSet<int> newnext = new HashSet<int>(); foreach (char ch in nfa.Vt) { HashSet<int> newnext = new HashSet<int>(); if(ch==System.Configuration.ConfigurationManager.AppSettings["Empty"][0]) { continue; } foreach (int nfaId in t) { if(nfa.StatusMap[nfaId].Next.ContainsKey(ch)) { foreach (int ii in nfa.StatusMap[nfaId].Next[ch]) { newnext.Add(ii); } } } if (newnext.Count == 0) { continue; } HashSet<int> closenew = nfa.Closure(newnext); if (closenew.Count > 0 && !InMap(closenew)) { int newId = Map.Count,endf=0; Map.Add(newId, closenew); _Map.Add(closenew, newId); if(nfa.IsEnd(closenew)) { endf=1; } DFAStatus dfastate = new DFAStatus(newId,endf); StatusMap.Add(newId,dfastate); } if (closenew.Count > 0) { if (!StatusMap[nowId].Next.ContainsKey(ch)) { StatusMap[nowId].Next.Add(ch, MapKey(closenew)); } } } } }