Esempio n. 1
0
        static void AutomataSample2()
        {
            NondeterminedFiniteAutomaton nfa = new NondeterminedFiniteAutomaton();

            nfa.AddState("q0");
            nfa.AddToStartStates("q0");
            nfa.AddSymbol("a");
            nfa.AddToFinalStates("q0");
            nfa.AddTransition("q0", "q0", "a");
            nfa.AddSymbol("b");
            nfa.AddState("q1");
            nfa.AddState("q2");
            nfa.AddTransition("q0", "q2", "b");
            nfa.AddTransition("q2", "q1", "b");
            nfa.AddToFinalStates("q1");
            var fromQ0ByB = nfa.GetTransitionDestinations("q0", "b");

            if (fromQ0ByB.Count() != 0)
            {
                Console.WriteLine("Переход по символу b из состояния q0 приведёт в состояния: ");
                foreach (var state in fromQ0ByB)
                {
                    Console.Write(state + " ");
                }
            }
            else
            {
                Console.WriteLine("По символу b из q0 нет переходов.");
            }
        }
Esempio n. 2
0
        static void AutomataSample3()
        {
            NondeterminedFiniteAutomaton nfa = new NondeterminedFiniteAutomaton();

            nfa.AddState("q0");
            nfa.AddState("q1");
            nfa.AddState("q2");
            nfa.AddToFinalStates("q2");
            nfa.AddToStartStates("q0");
            nfa.AddSymbol("А");
            nfa.AddSymbol("Б");
            nfa.AddTransition("q0", "q2", "А");
            nfa.
            AddTransitionBySymbolsSequence("q0", "q1", new List <string>()
            {
                "А", "Б", "А"
            });
            nfa.AddTransition("q1", "q2", "Б");
            var fromQ0ByA = nfa.GetTransitionDestinations("q0", "А");

            if (fromQ0ByA.Count() != 0)
            {
                Console.WriteLine("Переход по символу А из состояния q0 приведёт в состояния: ");
                foreach (var state in fromQ0ByA)
                {
                    Console.Write(state + " ");
                }
            }
            else
            {
                Console.WriteLine("По символу А из q0 нет переходов.");
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            AutomataConverterApplication app = new AutomataConverterApplication();
            NondeterminedFiniteAutomaton nfa = new NondeterminedFiniteAutomaton();

            nfa.AddSymbols(new string[] { "a", "b", "c" });
            nfa.AddStates(new string[] { "q0", "q1", "q2", "q3", "q4", "q5" });
            nfa.SetStartStates(new HashSet <string>()
            {
                "q0", "q1"
            });

            nfa.SetFinalStates(new HashSet <string>()
            {
                "q5"
            });
            /*nfa.AddTransitionBySymbolsSequence("q0", "q1", new List<int>() {"" })*/

            app.AddCommand(new HelpCommand(app));
            app.AddCommand(new ReadAvtomateCommand(app));
            app.AddCommand(new ConvertAutomateCommand(app));
            app.AddCommand(new WriteAutomateCommand(app));
            app.AddCommand(new ExitCommand(app));
            app.AddCommand(new ShowStorageCommand(app));
            app.AddCommand(new DeleteEpsilonTransitionsCommand(app));
            app.Run();
        }
Esempio n. 4
0
        static void AutomataSample4()
        {
            NondeterminedFiniteAutomaton nfa = new NondeterminedFiniteAutomaton();

            nfa.AddStates(new string[] { "q0", "q1", "q2", "q3", "q4" });
            nfa.AddSymbols(new string[] { "a", "b" });
            nfa.SetFinalStates(new HashSet <string>()
            {
                "q3", "q2"
            });
            nfa.SetStartStates(new HashSet <string>()
            {
                "q0"
            });
            nfa.AddTransition("q0", "q3", "b");
            nfa.AddTransition("q0", "q2", "eps");
            nfa.AddTransition("q0", "q1", "a");
            nfa.AddTransition("q1", "q4", "b");
            nfa.AddTransition("q2", "q1", "eps");
            nfa.AddTransition("q3", "q2", "b");
            nfa.AddTransition("q4", "q1", "a");
            nfa.AddTransition("q4", "q2", "b");
            nfa.AddTransition("q4", "q0", "b");
            NondeterminedFiniteAutomaton NoEpsNfa = nfa.GetEquivalentDeletedEpsilons();
        }
Esempio n. 5
0
        public static IEnumerable <string> GetAllDestinationsByEpsilons(NondeterminedFiniteAutomaton epsilonNfa, string startState)
        {
            Queue <int> front = new Queue <int>();

            int[] number = new int[epsilonNfa.NumberOfStates];
            int[] father = new int[epsilonNfa.NumberOfStates];
            for (int i = 0; i < number.Length; i++)
            {
                number[i] = 0;
                father[i] = 0;
            }
            int startStateIndex = epsilonNfa.GetStateIndex(startState);

            number[startStateIndex] = 1;
            front.Enqueue(startStateIndex);
            int counterNum = 1;

            while (front.Count != 0)
            {
                int frontHead        = front.Dequeue();
                var destinationsFrom = epsilonNfa.GetTransitionDestinations(frontHead, 0);
                foreach (var state in destinationsFrom)
                {
                    if (number[state] == 0)
                    {
                        front.Enqueue(state);
                        number[state] = ++counterNum;
                        father[state] = frontHead;
                    }
                }
            }
            return(epsilonNfa.GetStates().Where((name) => (number[epsilonNfa.GetStateIndex(name)] > 0)));
        }
Esempio n. 6
0
        public static NondeterminedFiniteAutomaton GetEquivalentDeletedEpsilons(this
                                                                                NondeterminedFiniteAutomaton epsilonNfa)
        {
            NondeterminedFiniteAutomaton noEpsilonsNfa = new NondeterminedFiniteAutomaton();
            // копируем состояния и алфавит
            var alphabet    = epsilonNfa.GetAlphabet();
            var states      = epsilonNfa.GetStates();
            var finalStates = new HashSet <string>(epsilonNfa.GetFinalStates());
            var startStates = new HashSet <string>(epsilonNfa.GetStartStates());

            noEpsilonsNfa.AddSymbols(alphabet);
            noEpsilonsNfa.AddStates(states);
            noEpsilonsNfa.SetFinalStates(finalStates);
            noEpsilonsNfa.SetStartStates(startStates);
            // добавляем необходимые переходы
            foreach (var state in states)
            {
                // состояния в которые мы можем перейти из состояния state, используя эпсилон переходы.
                var statesReachByEpsilons = GetAllDestinationsByEpsilons(epsilonNfa, state);
                foreach (var symbol in alphabet)
                {
                    // состояния в которые мы можем попасть используя сперва эпсилон переходы,
                    // а затем символ symbol единажды
                    var statesReachByEpsilonsAndSymbol = statesReachByEpsilons.SelectMany((reachedByEps)
                                                                                          => (epsilonNfa.GetTransitionDestinations(reachedByEps, symbol))).Distinct();
                    // состояния в которые мы можем перейти из состояния state, используя символ symbol единажды
                    // и эпсилон переходы в любом порядке
                    var statesOneSymbolAndEpsilons = statesReachByEpsilonsAndSymbol.SelectMany((stateFrom
                                                                                                => GetAllDestinationsByEpsilons(epsilonNfa, stateFrom))).Distinct();
                    foreach (var reachedState in statesOneSymbolAndEpsilons)
                    {
                        noEpsilonsNfa.AddTransition(state, reachedState, symbol);
                    }
                }
                if (startStates.Contains(state))
                {
                    foreach (var s in statesReachByEpsilons)
                    {
                        startStates.Add(s);
                        noEpsilonsNfa.AddToStartStates(s);
                    }
                }
                // Если это начальное состояние и из него достижимо заключительное по эпсилон
                // то сделаем его заключительным
                if (startStates.Contains(state) && statesReachByEpsilons.Any(finalStates.Contains))
                {
                    noEpsilonsNfa.AddToFinalStates(state);
                }
            }
            foreach (var state in finalStates)
            {
                noEpsilonsNfa.AddToFinalStates(state);
            }
            return(noEpsilonsNfa);
        }
Esempio n. 7
0
        public static DeterminedFiniteAutomaton ConvertNFAToDFA(NondeterminedFiniteAutomaton nfa)
        {
            //DeterminedFiniteAutomaton dfa
            nfa = nfa.GetEquivalentDeletedEpsilons();
            HashSet <string>         currentStates, accessibleStates;
            List <HashSet <string> > allSetsOfStates = new List <HashSet <string> >();

            allSetsOfStates.Add(new HashSet <string>(nfa.GetStartStates()));
            DeterminedFiniteAutomaton dfa = new DeterminedFiniteAutomaton("q0");

            List <string> names = new List <string>();

            dfa.AddSymbols(nfa.GetAlphabet());
            var    finalStates = nfa.GetFinalStates();
            string from, to;
            bool   f = true;

            for (int allSetsCounter = 0, currentSetNumber = 0, pos; currentSetNumber <= allSetsCounter; currentSetNumber++)
            {
                currentStates = allSetsOfStates[currentSetNumber];
                if (currentStates.Any(state => nfa.GetFinalStates().Contains(state)))
                {
                    dfa.AddToFinalStates(string.Format("q{0}", currentSetNumber));
                }
                foreach (var symbol in nfa.GetAlphabet())
                {
                    accessibleStates = GetAccessibleStates(nfa, currentStates, symbol);
                    if (accessibleStates.Count != 0)
                    {
                        pos  = CheckIsListContains(allSetsOfStates, accessibleStates);
                        from = string.Format("q{0}", currentSetNumber);
                        to   = string.Format("q{0}", pos);
                        if (pos == -1)
                        {
                            allSetsCounter++;
                            to = string.Format("q{0}", allSetsCounter);
                            dfa.AddState(to);
                            allSetsOfStates.Add(new HashSet <string>(accessibleStates));
                        }
                        dfa.AddTransition(from, to, symbol);
                    }
                }
            }
            return(dfa);
        }
Esempio n. 8
0
        private string[,] NFAToTable(NondeterminedFiniteAutomaton nfa)
        {
            List <string> alphabet = new List <string>(nfa.GetAlphabet());

            alphabet.Add(nfa.GetEpsilonName());
            return(StrangeFunc((NamedAutomaton)nfa, new Func <string, string, string>(
                                   (source, symbol) =>
            {
                List <string> tmp = nfa.GetTransitionDestinations(source, symbol).ToList();
                if (tmp == null)
                {
                    return "-";
                }
                else
                {
                    return string.Join(", ", tmp);
                }
            }), alphabet, nfa.GetStartStates()));
        }
Esempio n. 9
0
        /// <summary>
        /// Создание автомата по таблице
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private NondeterminedFiniteAutomaton TableToNFA(List <List <string> > table)
        {
            NondeterminedFiniteAutomaton nfa = new NondeterminedFiniteAutomaton();

            foreach (var sequence in symbolsSequence)
            {
                foreach (var symbol in sequence)
                {
                    if (!nfa.IsSymbolExist(symbol))
                    {
                        nfa.AddSymbol(symbol);
                    }
                }
            }

            for (int i = 0; i < states.Count; i++)
            {
                nfa.AddState(states[i]);
            }
            for (int j = 0; j < transitionsBySequence.Count; j++)
            {
                for (int i = 0; i < transitionsBySequence[j].Destinations.Count; i++)
                {
                    nfa.AddTransitionBySymbolsSequence(
                        transitionsBySequence[j].Source, transitionsBySequence[j].Destinations[i],
                        transitionsBySequence[j].Symbols);
                }
            }
            for (int j = 0; j < transitions.Count; j++)
            {
                for (int i = 0; i < transitions[j].Destinations.Count; i++)
                {
                    nfa.AddTransition(transitions[j].Source, transitions[j].Destinations[i],
                                      transitions[j].Symbol);
                }
            }
            nfa.SetStartStates(new HashSet <string>(startStates));
            nfa.SetFinalStates(new HashSet <string>(finalStates));
            return(nfa);
        }
Esempio n. 10
0
 private static HashSet <string> GetAccessibleStates(NondeterminedFiniteAutomaton nfa,
                                                     IEnumerable <string> states, string symbol)
 {
     return(new HashSet <string>(states.SelectMany(
                                     state => nfa.GetTransitionDestinations(state, symbol)).ToList()));
 }