private void ExecuteOperation(char operation) { switch (operation) { case '*': { Automat automat = automats.Pop(); automat = op.Interation(automat); index++; automats.Push(automat); } break; case '|': { Automat automat1 = automats.Pop(); Automat automat2 = automats.Pop(); automat1 = op.Union(automat1, automat2); automats.Push(automat1); } break; case '$': { Automat automat2 = automats.Pop(); Automat automat1 = automats.Pop(); automat1 = op.Concat(automat1, automat2); automats.Push(automat1); } break; } }
public Automat Union(Automat automat1, Automat automat2) { Automat resAutomat = new Automat(); resAutomat.name = automat1.name; resAutomat.priority = automat1.priority; automat1.setStates.UnionWith(automat2.setStates); resAutomat.setStates = automat1.setStates; automat1.alphabet.UnionWith(automat2.alphabet); resAutomat.alphabet = automat1.alphabet; automat1.startStates.UnionWith(automat2.startStates); resAutomat.startStates = automat1.startStates; automat1.finishStates.UnionWith(automat2.finishStates); resAutomat.finishStates = automat1.finishStates; resAutomat.Table = automat1.Table; foreach (var i in automat2.Table.Keys) { Dictionary <string, HashSet <int> > dic = new Dictionary <string, HashSet <int> >(); foreach (var pair in automat2.Table[i]) { dic.Add(pair.Key, pair.Value); } resAutomat.Table.Add(i, dic); } return(resAutomat); }
public Automat(Automat automat) { name = automat.name; priority = automat.priority; alphabet = new HashSet <string>(automat.alphabet); setStates = new HashSet <int>(automat.setStates); startStates = new HashSet <int>(automat.startStates); finishStates = new HashSet <int>(automat.finishStates); Table = automat.Table; }
public Result MaxString(Automat automate, string str, int k) { HashSet <int> currentSet = new HashSet <int>(automate.startStates); Result result = new Result(false, 0, ""); if (automate.finishStates.Intersect(currentSet).Count() != 0) { result.res = true; } HashSet <int> temp; for (int i = k; i < str.Length; i++) { temp = new HashSet <int>(); if (automate.alphabet.Contains(str[i].ToString())) { foreach (var currentState in currentSet) { foreach (var pair in automate.Table[currentState]) { if (pair.Key == str[i].ToString()) { foreach (var p in pair.Value) { if (p != -1 || pair.Value.Count > 1) { temp.Add(p); } } } } if (automate.finishStates.Intersect(temp).Count() != 0) { result.res = true; result.m = i - k + 1; result.substring = str.Substring(k, result.m); break; } } if (temp.Count == 0) { return(result); } temp.Remove(-1); currentSet = temp; } else { break; } } return(result); }
public Automat CreateSimpleAutomat(string name, int priority, HashSet <string> alphabet, HashSet <string> newChar, ref int index) { Automat automat = new Automat(); //автомат для 1 символа automat.name = name; automat.priority = priority; automat.startStates = new HashSet <int>(); automat.setStates = new HashSet <int>(); automat.finishStates = new HashSet <int>(); automat.alphabet = new HashSet <string>(alphabet); automat.Table = new Dictionary <int, Dictionary <string, HashSet <int> > >(); automat.startStates.Add(index); automat.setStates.UnionWith(automat.startStates); index++; automat.finishStates.Add(index); index++; automat.setStates.UnionWith(automat.finishStates); Dictionary <string, HashSet <int> > list = new Dictionary <string, HashSet <int> >(); Dictionary <string, HashSet <int> > listEnd = new Dictionary <string, HashSet <int> >(); for (int j = 0; j < automat.alphabet.Count; j++) { list.Add(automat.alphabet.ElementAt(j), new HashSet <int> { -1 }); listEnd.Add(automat.alphabet.ElementAt(j), new HashSet <int> { -1 }); } Dictionary <string, HashSet <int> > listCopy = new Dictionary <string, HashSet <int> >(list); for (int j = 0; j < newChar.Count; j++) { foreach (var pair in list) { if (newChar.ElementAt(j).Contains(pair.Key)) { listCopy[pair.Key] = new HashSet <int>(automat.finishStates); } } } automat.Table.Add(automat.startStates.ElementAt(0), listCopy); automat.Table.Add(automat.finishStates.ElementAt(0), listEnd); return(automat); }
public Automat Interation(Automat automat) { //Из всех заключительных состояний организуем переходы туда же, куда есть переходы из нач состояний Automat copyAutomat = new Automat(automat); foreach (var i in automat.finishStates) { foreach (var j in copyAutomat.startStates) { foreach (var pair in copyAutomat.Table[j]) { if (copyAutomat.Table[i].ContainsKey(pair.Key)) { copyAutomat.Table[i][pair.Key] = pair.Value; } else { copyAutomat.Table[i].Add(pair.Key, pair.Value); } } } } //Добавляем новое состояние и делаем его заключительным и начальным int index = automat.Table.Keys.Max(); index++; copyAutomat.startStates.Add(index); copyAutomat.finishStates.Add(index); copyAutomat.setStates.Add(index); Dictionary <string, HashSet <int> > dictionary = new Dictionary <string, HashSet <int> >(); foreach (var item in automat.alphabet) { dictionary.Add(item, new HashSet <int> { -1 }); } copyAutomat.Table.Add(index, dictionary); return(copyAutomat); }
public Automat Create(Automat automat) { HashSet <string> alph = new HashSet <string> { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m" }; HashSet <string> digits = new HashSet <string> { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; HashSet <string> allSymbols = new HashSet <string> { "!", "@", "\"", "#", "№", "$", ";", ":", "%", "^", "&", "?", "*", "(", ")", "-", "+", "=", ",", ".", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; automats = new Stack <Automat>(); operators = new Stack <char>(); index = 0; StringBuilder regex = automat.regex; for (int i = 0; i < regex.Length; i++) { switch (regex[i]) { case '(': operators.Push('('); break; case ')': while (operators.Peek() != '(') { ExecuteOperation(operators.Pop()); } operators.Pop(); break; case '*': case '$': case '|': char currentOperator = regex[i]; while (operators.Count != 0 && automats.Count != 0 && GetPriorityOperation(operators.Peek()) >= GetPriorityOperation(currentOperator)) { ExecuteOperation(operators.Pop()); } operators.Push(currentOperator); break; case '\\': i++; switch (regex[i]) { case ('w'): automat.alphabet.UnionWith(alph); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, alph, ref index)); break; case ('d'): automat.alphabet.UnionWith(digits); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, digits, ref index)); break; case ('t'): automat.alphabet.UnionWith(new HashSet <string> { "\t" }); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, new HashSet <string> { "\t" }, ref index)); break; case ('r'): automat.alphabet.UnionWith(new HashSet <string> { "\r" }); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, new HashSet <string> { "\r" }, ref index)); break; case ('n'): automat.alphabet.UnionWith(new HashSet <string> { "\n" }); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, new HashSet <string> { "\n" }, ref index)); break; case ('s'): automat.alphabet.UnionWith(new HashSet <string> { "\\s" }); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, new HashSet <string> { "\\s" }, ref index)); break; case (' '): automat.alphabet.UnionWith(new HashSet <string> { "\\s" }); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, new HashSet <string> { "\\s" }, ref index)); break; case ('@'): automat.alphabet.UnionWith(new HashSet <string> { "" }); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, new HashSet <string> { "" }, ref index)); break; case ('.'): automat.alphabet.UnionWith(allSymbols); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, allSymbols, ref index)); break; default: automat.alphabet.UnionWith(new HashSet <string> { regex[i].ToString() }); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, new HashSet <string> { regex[i].ToString() }, ref index)); break; } break; default: if (regex[i] == '-') { int er = 0; } automat.alphabet.UnionWith(new HashSet <string> { regex[i].ToString() }); automats.Push(CreateSimpleAutomat(automat.name, automat.priority, automat.alphabet, new HashSet <string> { regex[i].ToString() }, ref index)); break; } } while (operators.Count() != 0) { ExecuteOperation(operators.Pop()); } if (automats.Count() == 1) { return(automats.Pop()); } return(null); }
public Automat Concat(Automat automat1, Automat automat2) { if (automat1.alphabet != null) { Automat resAutomat = new Automat(); resAutomat.name = automat1.name; resAutomat.priority = automat1.priority; automat1.alphabet.UnionWith(automat2.alphabet); resAutomat.alphabet = automat1.alphabet; resAutomat.startStates = automat1.startStates; resAutomat.finishStates = automat2.finishStates; resAutomat.setStates = automat1.setStates; resAutomat.setStates.UnionWith(automat2.setStates); resAutomat.Table = new Dictionary <int, Dictionary <string, HashSet <int> > >(); foreach (var i in automat1.Table.Keys) { resAutomat.Table.Add(i, automat1.Table[i]); } foreach (var i in automat2.Table.Keys) { resAutomat.Table.Add(i, automat2.Table[i]); } foreach (var i in automat1.finishStates) { foreach (var j in automat2.startStates) { foreach (var pair in automat2.Table[j]) { if (pair.Key == ".") { int uer = 0; } if (resAutomat.Table[i].ContainsKey(pair.Key)) { //resAutomat.Table[i][pair.Key].Remove(-1); HashSet <int> values = new HashSet <int>(); foreach (var vl in resAutomat.Table[i][pair.Key]) { values.Add(vl); } foreach (var value in pair.Value) { values.Add(value); } resAutomat.Table[i][pair.Key] = values; } else { resAutomat.Table[i].Add(pair.Key, pair.Value); } } } } if (automat2.finishStates.Intersect(automat2.startStates).Count() != 0) { //если хотя бы одно из нач. сост было заключ, то объявляем заключ. все заключ. сост 1го автомата resAutomat.finishStates.UnionWith(automat1.finishStates); } return(resAutomat); } else { return(automat2); } }