Example #1
0
        static public Automata <String> getExampleSlide8Lesson2()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <String> automata = new Automata <string>(alphabet);

            automata.AddTransition(new Transition <String>("q0", "q1", 'a'));
            automata.AddTransition(new Transition <String>("q0", "q4", 'b'));

            automata.AddTransition(new Transition <String>("q1", "q4", 'a'));
            automata.AddTransition(new Transition <String>("q1", "q2", 'b'));

            automata.AddTransition(new Transition <String>("q2", "q3", 'a'));
            automata.AddTransition(new Transition <String>("q2", "q4", 'b'));
            automata.AddTransition(new Transition <String>("q3", "q1", 'a'));
            automata.AddTransition(new Transition <String>("q3", "q2", 'b'));

            // the error state, loops for a and b:
            automata.AddTransition(new Transition <String>("q4", 'a'));
            automata.AddTransition(new Transition <String>("q4", 'b'));

            // only on start state in a dfa:
            automata.DefineAsStartState("q0");

            // two final states:
            automata.DefineAsFinalState("q2");
            automata.DefineAsFinalState("q3");

            return(automata);
        }
Example #2
0
        static public Automata <String> dfaMutationTestL4()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <String> automata = new Automata <String>(alphabet);

            automata.AddTransition(new Transition <string>("A", "B", 'a'));
            automata.AddTransition(new Transition <string>("B", "D", 'b'));
            automata.AddTransition(new Transition <string>("D", "E", 'b'));
            automata.AddTransition(new Transition <string>("B", 'a'));
            automata.AddTransition(new Transition <string>("D", "B", 'a'));

            automata.AddTransition(new Transition <string>("A", "C", 'b'));
            automata.AddTransition(new Transition <string>("C", "F", 'b'));
            automata.AddTransition(new Transition <string>("F", "G", 'a'));
            automata.AddTransition(new Transition <string>("C", "B", 'a'));
            automata.AddTransition(new Transition <string>("F", "C", 'b'));

            automata.AddTransition(new Transition <string>("E", 'a'));
            automata.AddTransition(new Transition <string>("E", 'b'));
            automata.AddTransition(new Transition <string>("G", 'a'));
            automata.AddTransition(new Transition <string>("G", 'b'));

            automata.DefineAsStartState("A");
            automata.DefineAsFinalState("E");
            automata.DefineAsFinalState("G");

            return(automata);
        }
Example #3
0
        static public Automata <String> getExampleSlide14Lesson2()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <String> automata = new Automata <String>(alphabet);

            automata.AddTransition(new Transition <String>("A", "C", 'a'));
            automata.AddTransition(new Transition <String>("A", "B", 'b'));
            automata.AddTransition(new Transition <String>("A", "C", 'b'));

            automata.AddTransition(new Transition <String>("B", "C", 'b'));
            automata.AddTransition(new Transition <String>("B", "C"));

            automata.AddTransition(new Transition <String>("C", "D", 'a'));
            automata.AddTransition(new Transition <String>("C", "E", 'a'));
            automata.AddTransition(new Transition <String>("C", "D", 'b'));

            automata.AddTransition(new Transition <String>("D", "B", 'a'));
            automata.AddTransition(new Transition <String>("D", "C", 'a'));

            automata.AddTransition(new Transition <String>("E", 'a'));
            automata.AddTransition(new Transition <String>("E", "D"));

            // only on start state in a dfa:
            automata.DefineAsStartState("A");

            // two final states:
            automata.DefineAsFinalState("C");
            automata.DefineAsFinalState("E");

            return(automata);
        }
Example #4
0
        /// <summary>
        /// Turns a <see cref="RegExp"/> into a <see cref="Automata{string}"/>, which is a NDFA.
        /// </summary>
        /// <param name="expressionToTranslate">The <see cref="RegExp"/> to translate.</param>
        /// <returns>A <see cref="Automata{string}"/>, which is a NDFA or null if there was a problem</returns>
        public Automata <string> GenerateNDFA(RegExp expressionToTranslate)
        {
            HashSet <char> alphabet      = new HashSet <char>();
            String         regexAsString = expressionToTranslate.ToString();

            foreach (char c in regexAsString)
            {
                if (IsUsableCharacter(c))
                {
                    alphabet.Add(c);
                }
            }
            ;

            ThompsonPart completeNdfaAsThompson = GenerateThompsonPart(regexAsString);

            if (completeNdfaAsThompson.Equals(new ThompsonPart()))
            {
                return(null);
            }

            Automata <string> NDFA = new Automata <string>(alphabet.ToArray());

            foreach (Transition <string> thompsonTransition in completeNdfaAsThompson.transitions)
            {
                NDFA.AddTransition(thompsonTransition);
            }
            NDFA.DefineAsStartState(completeNdfaAsThompson.startState);
            NDFA.DefineAsFinalState(completeNdfaAsThompson.finalState);

            return(NDFA);
        }
Example #5
0
        static public Automata <String> dfaMutationTestL1()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <String> automata = new Automata <String>(alphabet);

            automata.AddTransition(new Transition <string>("A", "B", 'b'));
            automata.AddTransition(new Transition <string>("B", "C", 'a'));
            automata.AddTransition(new Transition <string>("C", "D", 'b'));
            automata.AddTransition(new Transition <string>("D", "E", 'a'));
            automata.AddTransition(new Transition <string>("E", "F", 'a'));

            automata.AddTransition(new Transition <string>("A", "G", 'a'));
            automata.AddTransition(new Transition <string>("B", "G", 'b'));
            automata.AddTransition(new Transition <string>("C", "G", 'a'));
            automata.AddTransition(new Transition <string>("D", "G", 'b'));
            automata.AddTransition(new Transition <string>("E", "G", 'b'));

            //End point shenenigans so testing goes a bit smoother
            automata.AddTransition(new Transition <string>("F", 'a'));
            automata.AddTransition(new Transition <string>("F", 'b'));
            automata.AddTransition(new Transition <string>("G", 'a'));
            automata.AddTransition(new Transition <string>("G", 'b'));

            automata.DefineAsStartState("A");
            automata.DefineAsFinalState("F");

            return(automata);
        }
Example #6
0
        /// <summary>
        /// This makes sure only a single - will end up between states.
        /// </summary>
        /// <param name="dfaToRemap">The dfa to remap the states for</param>
        /// <returns>A re mapped version of the given automata</returns>
        private Automata <T> RemapStates(Automata <T> dfaToRemap)
        {
            Dictionary <T, T> stateMap = new Dictionary <T, T>();

            for (int i = 0; i < dfaToRemap.States.Count; i++)
            {
                string newState  = "q" + i;
                T      newStateT = (T)Convert.ChangeType(newState, typeof(T));
                stateMap.Add(dfaToRemap.States.ElementAt(i), newStateT);
            }

            Console.WriteLine("Now printing the remap dictionary:");
            foreach (KeyValuePair <T, T> map in stateMap)
            {
                Console.WriteLine("Mapped {0} as {1}", map.Key, map.Value);
            }
            Console.WriteLine("-----------------------------------------");

            Automata <T> remappedDfa = new Automata <T>(dfaToRemap.Symbols);

            foreach (Transition <T> transition in dfaToRemap.Transitions)
            {
                remappedDfa.AddTransition(new Transition <T>(stateMap[transition.FromState], stateMap[transition.ToState], transition.Identifier));
            }
            foreach (T startState in dfaToRemap.StartStates)
            {
                remappedDfa.DefineAsStartState(stateMap[startState]);
            }
            foreach (T finalState in dfaToRemap.FinalStates)
            {
                remappedDfa.DefineAsFinalState(stateMap[finalState]);
            }

            return(remappedDfa);
        }
        /// <summary>
        /// Turns a dfa into a form in which the original is not accepted.
        /// </summary>
        /// <param name="originalDfa">The dfa to make a reverse of</param>
        /// <returns>A dfa that is a reverse of the original dfa</returns>
        public Automata <T> ReverseDfa(Automata <T> originalDfa)
        {
            if (!originalDfa.IsDfa())
            {
                throw new ArgumentException("Given automata is not a DFA.");
            }

            Automata <T> reversedDfa = new Automata <T>(originalDfa.Symbols);

            //Every transition is swapped as follows: new transition(original end state, symbol, original start state)
            foreach (Transition <T> transition in originalDfa.Transitions)
            {
                reversedDfa.AddTransition(new Transition <T>(transition.ToState, transition.FromState, transition.Identifier));
            }
            //All start states become end states
            foreach (T startState in originalDfa.StartStates)
            {
                reversedDfa.DefineAsFinalState(startState);
            }
            //All end states become start states
            foreach (T endState in originalDfa.FinalStates)
            {
                reversedDfa.DefineAsStartState(endState);
            }

            return(reversedDfa);
        }
        /// <summary>
        /// Turns a dfa into a form in which the original is not accepted.
        /// </summary>
        /// <param name="originalDfa">The dfa to "invert"</param>
        /// <returns>A dfa that is !the original dfa</returns>
        public Automata <T> NotDfa(Automata <T> originalDfa)
        {
            if (!originalDfa.IsDfa())
            {
                throw new ArgumentException("Given automata is not a DFA.");
            }

            Automata <T> notDfa = new Automata <T>(originalDfa.Symbols);
            //Alles wat geen end state is word een end, en omgekeerd.
            //Filter all end states from all the states
            IEnumerable <T> newEndStates = originalDfa.States.Except(originalDfa.FinalStates);

            //Set everthing in the new dfa
            foreach (Transition <T> transition in originalDfa.Transitions)
            {
                notDfa.AddTransition(transition);
            }
            foreach (T startState in originalDfa.StartStates)
            {
                notDfa.DefineAsStartState(startState);
            }
            foreach (T finalState in newEndStates)
            {
                notDfa.DefineAsFinalState(finalState);
            }

            return(notDfa);
        }
        /// <summary>
        /// Combines two <see cref="Automata{T}"/> into a single automata if they are both dfa's
        /// </summary>
        /// <param name="firstDfa">The first dfa to use</param>
        /// <param name="secondDfa">The other dfa to use</param>
        /// <returns>A dfa that consists that takes the conditions for both dfa's into account</returns>
        public Automata <T> CombineAutomataAnd(Automata <T> firstDfa, Automata <T> secondDfa)
        {
            if (!firstDfa.IsDfa() || !secondDfa.IsDfa())
            {
                throw new ArgumentException("One of the given automata is not a dfa");
            }

            IEnumerable <char> combinedAlphabet = firstDfa.Symbols.Union(secondDfa.Symbols);
            Automata <T>       combinedDfa      = new Automata <T>(combinedAlphabet.ToArray());

            startStatesForDfa.Clear();
            combinedTransitionsMap.Clear();

            //Read the starting states
            SetStartStatesForAndOrOr(firstDfa, secondDfa);

            //Gather all states that follow
            MapReachableStatesForGivenDFA(firstDfa, secondDfa);

            //Generate the dfa, starting with the start states (no really sherlock)
            GenerateCombinedDfa(combinedDfa, startStatesForDfa);

            //Set start states
            foreach (T startState in startStatesForDfa)
            {
                combinedDfa.DefineAsStartState(startState);
            }

            //And end state, for the and it is a combination. So if is is a F and E, G then the endstates should contain F and either E or G.
            //So generate the end state by combining them in the same way as the start states, and then check if the dfa has the state.
            List <T> endStates = new List <T>();

            foreach (T firstEnd in firstDfa.FinalStates)
            {
                string firstAsString = firstEnd.ToString();
                foreach (T secondEnd in secondDfa.FinalStates)
                {
                    string secondAsString = secondEnd.ToString();
                    string newState       = firstAsString + "-" + secondAsString;
                    T      newStateT      = (T)Convert.ChangeType(newState, typeof(T));
                    endStates.Add(newStateT);
                }
            }

            foreach (T endState in endStates)
            {
                foreach (T state in combinedDfa.States)
                {
                    if (state.Equals(endState))
                    {
                        combinedDfa.DefineAsFinalState(endState);
                    }
                }
            }

            return(combinedDfa);
        }
        /// <summary>
        /// Combines two <see cref="Automata{T}"/> into a single automata if they are both dfa's
        /// </summary>
        /// <param name="firstDfa">The first dfa to use</param>
        /// <param name="secondDfa">The other dfa to use</param>
        /// <returns>A dfa that consists that takes the conditions for both dfa's into account</returns>
        public Automata <T> CombinaAutomataOr(Automata <T> firstDfa, Automata <T> secondDfa)
        {
            if (!firstDfa.IsDfa() || !secondDfa.IsDfa())
            {
                throw new ArgumentException("One of the given automata is not a dfa");
            }

            IEnumerable <char> combinedAlphabet = firstDfa.Symbols.Union(secondDfa.Symbols);
            Automata <T>       combinedDfa      = new Automata <T>(combinedAlphabet.ToArray());

            startStatesForDfa.Clear();
            combinedTransitionsMap.Clear();

            //Read the starting states
            SetStartStatesForAndOrOr(firstDfa, secondDfa);

            //Gather all states that follow
            MapReachableStatesForGivenDFA(firstDfa, secondDfa);

            //Generate the dfa, starting with the start states (no really sherlock)
            GenerateCombinedDfa(combinedDfa, startStatesForDfa);

            //Set start states
            foreach (T startState in startStatesForDfa)
            {
                combinedDfa.DefineAsStartState(startState);
            }

            //And end state, for the or it should contain one. So if is is a F and E, G then any state with a E, F or G is an end state.
            List <T> endStates = new List <T>();

            endStates.AddRange(firstDfa.FinalStates);
            foreach (T state in secondDfa.FinalStates)
            {
                foreach (T endState in endStates)
                {
                    if (!StatesAreEqual(state, endState))
                    {
                        endStates.Add(state); break;
                    }
                }
            }

            foreach (T state in combinedDfa.States)
            {
                string[] splitStates     = state.ToString().Split('-');
                List <T> separateStatesT = new List <T>();
                foreach (string splitState in splitStates)
                {
                    separateStatesT.Add((T)Convert.ChangeType(splitState, typeof(T)));
                }

                //Now check if any of the seperates states are in endStates (so if intersect leaves atleast one element)
                int elementsLeft = separateStatesT.Intersect(endStates).ToList().Count;

                if (elementsLeft > 0)
                {
                    combinedDfa.DefineAsFinalState(state);
                }
            }

            return(combinedDfa);
        }