Example #1
0
        /// <summary>
        /// Создание автомата по таблице
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private DeterminedFiniteAutomaton TableToDFA(List <List <string> > table)
        {
            DeterminedFiniteAutomaton dfa = new DeterminedFiniteAutomaton(startStates[0]);

            states.Remove(startStates[0]);
            foreach (var sequence in symbolsSequence)
            {
                foreach (var symbol in sequence)
                {
                    if (!dfa.IsSymbolExist(symbol))
                    {
                        dfa.AddSymbol(symbol);
                    }
                }
            }
            for (int i = 0; i < states.Count; i++)
            {
                dfa.AddState(states[i]);
            }
            dfa.SetFinalStates(new HashSet <string>(finalStates));
            for (int j = 0; j < transitions.Count; j++)
            {
                dfa.AddTransition(transitions[j].Source, transitions[j].Destinations[0], transitions[j].Symbol);
            }
            return(dfa);
        }
Example #2
0
        /// <summary>
        /// Пример работы с автоматами
        /// </summary>
        static void AutomataSample()
        {
            DeterminedFiniteAutomaton dfa = new DeterminedFiniteAutomaton("q0");

            dfa.AddSymbol("a");
            dfa.AddToFinalStates("q0");
            dfa.AddTransition("q0", "q0", "a");
            dfa.AddSymbol("b");
            dfa.AddState("q1");
            dfa.AddState("q2");
            dfa.AddTransition("q0", "q2", "b");
            dfa.AddTransition("q2", "q1", "b");
            dfa.AddToFinalStates("q1");
            string fromQ0ByB = dfa.GetTransitionDestination("q0", "b");

            Console.WriteLine("Переход по символу b из состояния q0 приведёт в состояние: {0}", fromQ0ByB);
        }
Example #3
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);
        }