Esempio n. 1
0
        private static Automaat <int> GetStartAbbOrEndBaab()
        {
            char[]         alphabet = { 'a', 'b' };
            Automaat <int> m        = new Automaat <int>(alphabet);

            m.AddTransition(new Transition <int>(1, 'a', 2));
            m.AddTransition(new Transition <int>(1, 'b', 5));
            m.AddTransition(new Transition <int>(2, 'a', 9));
            m.AddTransition(new Transition <int>(2, 'b', 3));
            m.AddTransition(new Transition <int>(3, 'a', 6));
            m.AddTransition(new Transition <int>(3, 'b', 4));
            m.AddTransition(new Transition <int>(4, 'a', 4));
            m.AddTransition(new Transition <int>(4, 'b', 4));
            m.AddTransition(new Transition <int>(5, 'a', 6));
            m.AddTransition(new Transition <int>(5, 'b', 5));
            m.AddTransition(new Transition <int>(6, 'a', 7));
            m.AddTransition(new Transition <int>(6, 'b', 5));
            m.AddTransition(new Transition <int>(7, 'a', 9));
            m.AddTransition(new Transition <int>(7, 'b', 8));
            m.AddTransition(new Transition <int>(8, 'a', 6));
            m.AddTransition(new Transition <int>(8, 'b', 5));
            m.AddTransition(new Transition <int>(9, 'a', 9));
            m.AddTransition(new Transition <int>(9, 'b', 5));
            m.DefineAsStartState(1);
            m.DefineAsFinalState(4);
            m.DefineAsFinalState(8);
            return(m);
        }
Esempio n. 2
0
        private static void GramToNdfaAndReverse()
        {
            char[] alphabet = { 'a', 'b' };
            var    ndfa     = new Automaat <int>(alphabet);

            ndfa.AddTransition(new Transition <int>(0, alphabet[0], 0));
            ndfa.AddTransition(new Transition <int>(0, alphabet[0], 1));
            ndfa.AddTransition(new Transition <int>(0, alphabet[1], 0));
            ndfa.AddTransition(new Transition <int>(0, alphabet[1], 3));

            ndfa.AddTransition(new Transition <int>(1, alphabet[0], 2));

            ndfa.AddTransition(new Transition <int>(2, alphabet[0]));
            ndfa.AddTransition(new Transition <int>(2, alphabet[1]));

            ndfa.AddTransition(new Transition <int>(3, alphabet[1], 4));

            ndfa.AddTransition(new Transition <int>(4, alphabet[0]));
            ndfa.AddTransition(new Transition <int>(4, alphabet[1]));

            ndfa.DefineAsStartState(0);

            ndfa.DefineAsFinalState(2);
            ndfa.DefineAsFinalState(4);

            Graphviz.PrintGraph(ndfa, "ndfaToGram");

            var gram = RegGram <int> .NdfaToRegGram(ndfa);

            Console.WriteLine(gram.ToString());
        }
Esempio n. 3
0
        static void testEpsilonNDFA()
        {
            char[] alphabet = { 'a', 'b' };
            var    m        = new Automaat <string>(alphabet);

            m.AddTransition(new Transition <string>("1", alphabet[0], "1"));
            m.AddTransition(new Transition <string>("1", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("1", Transition <string> .Epsilon, "3"));

            m.AddTransition(new Transition <string>("2", alphabet[1], "4"));
            m.AddTransition(new Transition <string>("3", alphabet[1], "5"));

            m.DefineAsStartState("1");
            m.DefineAsFinalState("5");

            //woord bevat 1 b en eindigt op 1 b
            var testWords = new List <Tuple <string, bool> >();

            testWords.Add(new Tuple <string, bool>("b", true));
            testWords.Add(new Tuple <string, bool>("ab", true));
            testWords.Add(new Tuple <string, bool>("a", false));
            testWords.Add(new Tuple <string, bool>("bba", false));
            testWords.Add(new Tuple <string, bool>("aaab", true));
            //TestingAutomaat("Testing for epsilon transitions", m, testWords);

            var dfa = NdfatoDfa.MakeDfa(m);

            dfa.PrintTransitions();
            TestingAutomaat("Testing for epsilon transitions", dfa, testWords);
        }
Esempio n. 4
0
        private static Automaat <int> GetEvenBOrUnevenA()
        {
            char[]         alphabet = { 'a', 'b' };
            Automaat <int> m        = new Automaat <int>(alphabet);

            m.AddTransition(new Transition <int>(1, alphabet[0], 2));
            m.AddTransition(new Transition <int>(1, alphabet[1], 3));

            m.AddTransition(new Transition <int>(2, alphabet[0], 1));
            m.AddTransition(new Transition <int>(2, alphabet[1], 5));

            m.AddTransition(new Transition <int>(3, alphabet[0], 2));
            m.AddTransition(new Transition <int>(3, alphabet[1], 4));

            m.AddTransition(new Transition <int>(4, alphabet[0], 2));
            m.AddTransition(new Transition <int>(4, alphabet[1], 4));

            m.AddTransition(new Transition <int>(5, alphabet[0], 1));
            m.AddTransition(new Transition <int>(5, alphabet[1], 4));

            // only on start state in a dfa:
            m.DefineAsStartState(1);

            // two final states:
            m.DefineAsFinalState(2);
            m.DefineAsFinalState(4);
            m.DefineAsFinalState(5);
            return(m);
        }
Esempio n. 5
0
            public Automaat <int> ToAutomaat()
            {
                var automaat = new Automaat <int>(Alphabet);

                foreach (var block in Blocks)
                {
                    foreach (char c in Alphabet)
                    {
                        if (block.Rows.Count > 0)
                        {
                            automaat.AddTransition(new Transition <int>(block.Identifier, c, block.Rows[0].FindDestination(c).Identifier));
                        }
                    }
                    Automaat._startStates.ToList().ForEach(startstate =>
                    {
                        block.States.ToList().ForEach(state =>
                        {
                            if (state.Equals(startstate))
                            {
                                automaat.DefineAsStartState(block.Identifier);
                            }
                        });
                    });
                    if (block.isFinalState)
                    {
                        automaat.DefineAsFinalState(block.Identifier);
                    }
                }

                return(automaat);
            }
Esempio n. 6
0
        static void Ndfa()
        {
            char[]            alphabet = { 'a', 'b' };
            Automaat <string> m        = new Automaat <string>(alphabet);

            m.AddTransition(new Transition <string>("1", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("1", alphabet[0], "3"));
            m.AddTransition(new Transition <string>("1", alphabet[1], "1"));

            m.AddTransition(new Transition <string>("2", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("2", alphabet[1], "2"));

            m.AddTransition(new Transition <string>("3", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("3", alphabet[1], "4"));

            m.AddTransition(new Transition <string>("4", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("4", alphabet[1], "2"));

            // only on start state in a dfa:
            m.DefineAsStartState("1");

            // two final states:
            m.DefineAsFinalState("4");

            var testWords = new List <Tuple <string, bool> >();

            testWords.Add(new Tuple <string, bool>("ab", true));
            testWords.Add(new Tuple <string, bool>("ba", false));
            testWords.Add(new Tuple <string, bool>("aa", false));
            testWords.Add(new Tuple <string, bool>("bbab", true));

            TestingAutomaat("testing code for ndfa", m, testWords);
        }
Esempio n. 7
0
        public Automaat <T> Reverse()
        {
            var reversedAutomaat = new Automaat <T>(_symbols);

            _startStates.ToList().ForEach(state => reversedAutomaat.DefineAsFinalState(state));
            _finalStates.ToList().ForEach(state => reversedAutomaat.DefineAsStartState(state));
            _transitions.ToList().ForEach(trans => reversedAutomaat.AddTransition(trans.Reverse()));

            return(reversedAutomaat);
        }
Esempio n. 8
0
        private static Automaat <int> EindigtOp(string rulesymbols, char[] alphabet)
        {
            var a            = new Automaat <int>(alphabet);
            var stateCounter = 0;
            var startState   = ++stateCounter;

            var fromState = startState;

            a.DefineAsStartState(fromState);

            var word = "";

            foreach (var s in rulesymbols.ToCharArray())
            {
                stateCounter++;
                word += s;
                foreach (var letter in a.GetAlphabet())
                {
                    if (s == letter)
                    {
                        a.AddTransition(new Transition <int>(fromState, s, stateCounter));
                    }
                    else
                    {
                        int i;
                        for (i = 0; i < word.Length; i++)
                        {
                            if (word[i] != letter)
                            {
                                break;
                            }
                        }
                        a.AddTransition(new Transition <int>(fromState, letter, i + startState));
                    }
                }
                fromState = stateCounter;
            }

            foreach (var letter in a.GetAlphabet())
            {
                int i;
                for (i = 0; i < rulesymbols.Length; i++)
                {
                    if (word[i] != letter)
                    {
                        break;
                    }
                }
                a.AddTransition(new Transition <int>(fromState, letter, i + startState));
            }

            a.DefineAsFinalState(stateCounter);

            return(a);
        }
Esempio n. 9
0
        static void PracL1ERepresentatie1()
        {
            /*begint met abb of eindigt op baab*/
            char[]            alphabet = { 'a', 'b' };
            Automaat <string> m        = new Automaat <string>(alphabet);

            m.AddTransition(new Transition <string>("1", 'a', "2"));
            m.AddTransition(new Transition <string>("1", 'b', "5"));

            m.AddTransition(new Transition <string>("2", 'a', "9"));
            m.AddTransition(new Transition <string>("2", 'b', "3"));

            m.AddTransition(new Transition <string>("3", 'a', "6"));
            m.AddTransition(new Transition <string>("3", 'b', "4"));

            m.AddTransition(new Transition <string>("4", 'a', "4"));
            m.AddTransition(new Transition <string>("4", 'b', "4"));

            m.AddTransition(new Transition <string>("5", 'a', "6"));
            m.AddTransition(new Transition <string>("5", 'b', "5"));

            m.AddTransition(new Transition <string>("6", 'a', "7"));
            m.AddTransition(new Transition <string>("6", 'b', "5"));

            m.AddTransition(new Transition <string>("7", 'a', "9"));
            m.AddTransition(new Transition <string>("7", 'b', "8"));

            m.AddTransition(new Transition <string>("8", 'a', "6"));
            m.AddTransition(new Transition <string>("8", 'b', "5"));

            m.AddTransition(new Transition <string>("9", 'a', "9"));
            m.AddTransition(new Transition <string>("9", 'b', "5"));


            // only on start state in a dfa:
            m.DefineAsStartState("1");

            // two final states:
            m.DefineAsFinalState("4");
            m.DefineAsFinalState("8");

            List <Tuple <string, bool> > testWords = new List <Tuple <string, bool> >();

            testWords.Add(new Tuple <string, bool>("abb", true));
            testWords.Add(new Tuple <string, bool>("baab", true));
            testWords.Add(new Tuple <string, bool>("aabb", false));
            testWords.Add(new Tuple <string, bool>("ab", false));
            testWords.Add(new Tuple <string, bool>("baa", false));
            testWords.Add(new Tuple <string, bool>("aabbabaaabaab", true));
            testWords.Add(new Tuple <string, bool>("ababaabaa", false));
            testWords.Add(new Tuple <string, bool>("abba", true));
            TestingAutomaat("Begint met abb of eindigt op baab", m, testWords);
        }
Esempio n. 10
0
        public static Automaat <int> CreateAutomaat(RegExp reg)
        {
            var automaat = new Automaat <int>();
            int stateCounter = 1, leftState = 0, rightState = 1;

            automaat.DefineAsStartState(leftState);
            automaat.DefineAsFinalState(rightState);

            ModifyAutomaat(reg, ref automaat, ref stateCounter, leftState, rightState);

            return(automaat);
        }
Esempio n. 11
0
        private static Automaat <int> GetStartWithABB()
        {
            var a1 = new Automaat <int>(alphabet);

            a1.AddTransition(new Transition <int>(0, alphabet[0], 1));
            a1.AddTransition(new Transition <int>(1, alphabet[1], 2));
            a1.AddTransition(new Transition <int>(2, alphabet[1], 3));
            a1.AddTransition(new Transition <int>(3, alphabet[0], 3));
            a1.AddTransition(new Transition <int>(3, alphabet[1], 3));
            a1.DefineAsStartState(0);
            a1.DefineAsFinalState(3);
            return(a1);
        }
Esempio n. 12
0
        static void PracL1Representatie2()
        {
            char[]            alphabet = { 'a', 'b' };
            Automaat <string> m        = new Automaat <string>(alphabet);

            m.AddTransition(new Transition <string>("1", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("1", alphabet[1], "3"));

            m.AddTransition(new Transition <string>("2", alphabet[0], "1"));
            m.AddTransition(new Transition <string>("2", alphabet[1], "5"));

            m.AddTransition(new Transition <string>("3", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("3", alphabet[1], "4"));

            m.AddTransition(new Transition <string>("4", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("4", alphabet[1], "4"));

            m.AddTransition(new Transition <string>("5", alphabet[0], "1"));
            m.AddTransition(new Transition <string>("5", alphabet[1], "4"));

            // only on start state in a dfa:
            m.DefineAsStartState("1");

            // two final states:
            m.DefineAsFinalState("2");
            m.DefineAsFinalState("4");
            m.DefineAsFinalState("5");

            List <Tuple <string, bool> > testWords = new List <Tuple <string, bool> >();

            testWords.Add(new Tuple <string, bool>("a", true));
            testWords.Add(new Tuple <string, bool>("bb", true));
            testWords.Add(new Tuple <string, bool>("abb", true));
            testWords.Add(new Tuple <string, bool>("aabb", true));
            testWords.Add(new Tuple <string, bool>("baa", false));
            testWords.Add(new Tuple <string, bool>("aabbabaaabab", true));
            testWords.Add(new Tuple <string, bool>("ababaabaa", false));
            testWords.Add(new Tuple <string, bool>("abba", true));
            testWords.Add(new Tuple <string, bool>("babab", false));
            testWords.Add(new Tuple <string, bool>("ababa", true));
            TestingAutomaat("Bevat een even aantal b’s of bevat een oneven aantal a’s", m, testWords);
            var a = m.GeefTaal(3);

            Console.WriteLine("geaccepteerde woorden:");
            a.ForEach(word => Console.WriteLine("\t" + word));
            var b = m.GeefNietTaal(3);

            Console.WriteLine("niew geaccepteerde woorden:");
            b.ForEach(word => Console.WriteLine("\t" + word));
        }
Esempio n. 13
0
        static void TestSamenvoegen()
        {
            char[] alphabet = { 'a', 'b' };
            var    dfa1     = new Automaat <int>(alphabet);

            dfa1.AddTransition(new Transition <int>(1, alphabet[0], 2));
            dfa1.AddTransition(new Transition <int>(1, alphabet[1], 1));

            dfa1.AddTransition(new Transition <int>(2, alphabet[0], 1));
            dfa1.AddTransition(new Transition <int>(2, alphabet[1], 2));

            dfa1.DefineAsStartState(1);
            dfa1.DefineAsFinalState(1);

            var dfa2 = new Automaat <int>(alphabet);

            dfa2.AddTransition(new Transition <int>(1, alphabet[0], 1));
            dfa2.AddTransition(new Transition <int>(1, alphabet[1], 2));

            dfa2.AddTransition(new Transition <int>(2, alphabet[0], 1));
            dfa2.AddTransition(new Transition <int>(2, alphabet[1], 3));

            dfa2.AddTransition(new Transition <int>(3, alphabet[0], 1));
            dfa2.AddTransition(new Transition <int>(3, alphabet[1], 4));

            dfa2.AddTransition(new Transition <int>(4, alphabet[0], 4));
            dfa2.AddTransition(new Transition <int>(4, alphabet[1], 4));

            dfa2.DefineAsStartState(1);

            dfa2.DefineAsFinalState(1);
            dfa2.DefineAsFinalState(2);

            var notDfa1 = !dfa1;
            var notDfa2 = !dfa2;

            var and = dfa1 & dfa2;
            var or  = dfa1 | dfa2;

            Graphviz.PrintGraph(and, "and");

            var testWords = new List <Tuple <string, bool> >();

            dfa1.GeefTaal(4).ForEach(s => testWords.Add(new Tuple <string, bool>(s, true)));
            dfa2.GeefTaal(4).ForEach(s => testWords.Add(new Tuple <string, bool>(s, true)));

            TestingAutomaat("And dfa", and, testWords);
            TestingAutomaat("Or dfa", or, testWords);
        }
Esempio n. 14
0
        private static void TestNdfaGraphviz()
        {
            var ndfa = new Automaat <string>(Alphabet);

            ndfa.AddTransition(new Transition <string>("0", Alphabet[0], "0"));
            ndfa.AddTransition(new Transition <string>("0", Alphabet[0], "1"));
            ndfa.AddTransition(new Transition <string>("0", Alphabet[1], "3"));

            ndfa.AddTransition(new Transition <string>("1", Alphabet[0], "1"));
            ndfa.AddTransition(new Transition <string>("1", Alphabet[1], "2"));

            ndfa.AddTransition(new Transition <string>("3", Alphabet[0]));
            ndfa.AddTransition(new Transition <string>("3", Alphabet[1]));

            ndfa.AddTransition(new Transition <string>("4", Alphabet[0], "1"));
            ndfa.AddTransition(new Transition <string>("4", Alphabet[1]));

            ndfa.DefineAsStartState("0");
            ndfa.DefineAsStartState("4");

            ndfa.DefineAsFinalState("2");

            Graphviz.PrintGraph(ndfa, "ndfaTest");
        }
Esempio n. 15
0
        private static Automaat <int> GetEndWithABB()
        {
            var a1 = new Automaat <int>(alphabet);

            a1.AddTransition(new Transition <int>(0, Transition <int> .Epsilon, 1)); a1.AddTransition(new Transition <int>(0, Transition <int> .Epsilon, 7));
            a1.AddTransition(new Transition <int>(1, Transition <int> .Epsilon, 2)); a1.AddTransition(new Transition <int>(1, Transition <int> .Epsilon, 4));
            a1.AddTransition(new Transition <int>(2, alphabet[0], 3)); a1.AddTransition(new Transition <int>(3, Transition <int> .Epsilon, 6));
            a1.AddTransition(new Transition <int>(4, alphabet[1], 5)); a1.AddTransition(new Transition <int>(5, Transition <int> .Epsilon, 6));
            a1.AddTransition(new Transition <int>(6, Transition <int> .Epsilon, 7)); a1.AddTransition(new Transition <int>(7, alphabet[0], 8));
            a1.AddTransition(new Transition <int>(6, Transition <int> .Epsilon, 1)); a1.AddTransition(new Transition <int>(8, alphabet[1], 9));
            a1.AddTransition(new Transition <int>(9, alphabet[1], 10));
            a1.DefineAsStartState(0);
            a1.DefineAsFinalState(10);
            return(a1);
        }
Esempio n. 16
0
        private static Automaat <State> BetweenAutomaat(Automaat <T> a1, Automaat <T> a2)
        {
            var alphabet = new SortedSet <char>(a1.GetAlphabet());

            a2.GetAlphabet().ToList().ForEach(c => alphabet.Add(c));
            var newA = new Automaat <State>(alphabet);

            var stateCounter = 0;
            var beginState   = new State(stateCounter, a1._startStates.First(), a2._startStates.First());

            newA.DefineAsStartState(beginState);

            FindNextState(ref newA, ref stateCounter, a1, a2, beginState);

            return(newA);
        }
Esempio n. 17
0
        static void TestOptimizingDfa()
        {
            char[] alphabet = { 'a', 'b' };
            var    m        = new Automaat <string>(alphabet);

            m.AddTransition(new Transition <string>("0", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("0", alphabet[1], "3"));

            m.AddTransition(new Transition <string>("1", alphabet[0], "3"));
            m.AddTransition(new Transition <string>("1", alphabet[1], "2"));

            m.AddTransition(new Transition <string>("2", alphabet[0], "0"));
            m.AddTransition(new Transition <string>("2", alphabet[1], "4"));

            m.AddTransition(new Transition <string>("3", alphabet[0], "1"));
            m.AddTransition(new Transition <string>("3", alphabet[1], "5"));

            m.AddTransition(new Transition <string>("4", alphabet[0], "6"));
            m.AddTransition(new Transition <string>("4", alphabet[1], "5"));

            m.AddTransition(new Transition <string>("5", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("5", alphabet[1], "0"));

            m.AddTransition(new Transition <string>("6", alphabet[0], "4"));
            m.AddTransition(new Transition <string>("6", alphabet[1], "0"));

            m.DefineAsStartState("0");

            m.DefineAsFinalState("1");
            m.DefineAsFinalState("3");
            m.DefineAsFinalState("4");
            m.DefineAsFinalState("6");

            var testWords = new List <Tuple <string, bool> >();

            testWords.Add(new Tuple <string, bool>("b", true));
            testWords.Add(new Tuple <string, bool>("ba", true));
            testWords.Add(new Tuple <string, bool>("bab", false));
            testWords.Add(new Tuple <string, bool>("bba", false));
            testWords.Add(new Tuple <string, bool>("babb", true));

            TestingAutomaat("Testing not optimized automaat", m, testWords);
        }
Esempio n. 18
0
        private static Automaat <int> CombineAutomaat(Automaat <T> a1, Automaat <T> a2,
                                                      Func <bool, bool, bool> finalstateDefine)
        {
            var combinedAutomaat = BetweenAutomaat(a1, a2);
            var newA             = new Automaat <int>(combinedAutomaat.GetAlphabet());

            combinedAutomaat._startStates.ToList().ForEach(state => newA.DefineAsStartState(state.Id));

            combinedAutomaat._transitions.ToList().ForEach(trans => newA.AddTransition(new Transition <int>(trans.FromState.Id, trans.Symbol, trans.ToState.Id)));

            foreach (var state in combinedAutomaat._states)
            {
                if (finalstateDefine(a1.IsFinalState(state.StateAutomaat1), a2.IsFinalState(state.StateAutomaat2)))
                {
                    newA.DefineAsFinalState(state.Id);
                }
            }

            return(newA);
        }
Esempio n. 19
0
        static void DfaToDfa()
        {
            //eindigd op 'aa'
            char[] alphabet = { 'a', 'b' };
            var    m        = new Automaat <int>(alphabet);

            m.AddTransition(new Transition <int>(1, alphabet[0], 2));
            m.AddTransition(new Transition <int>(1, alphabet[1], 1));

            m.AddTransition(new Transition <int>(2, alphabet[0], 3));
            m.AddTransition(new Transition <int>(2, alphabet[1], 1));

            m.AddTransition(new Transition <int>(3, alphabet[0], 3));
            m.AddTransition(new Transition <int>(3, alphabet[1], 1));

            m.DefineAsStartState(1);
            m.DefineAsFinalState(3);

            NdfatoDfa.MakeDfa(m);
        }
Esempio n. 20
0
        public static Automaat <int> MakeDfa <T>(Automaat <T> ndfa) where T : IComparable
        {
            var betweenDfa = new Automaat <State <T> >(ndfa.GetAlphabet());
            var table      = MakeTable(ndfa);
            // PrintTable(table);
            var stateCounter = 1;

            // Define start state
            var startStates = new SortedSet <T>(ndfa._startStates);

            ndfa._startStates.ToList().ForEach(sState => GetToStatesByEpsilon(ref startStates, ndfa, sState));
            var startState = new State <T>(stateCounter)
            {
                States = startStates
            };

            betweenDfa.DefineAsStartState(startState);

            BuildBetweenDfa(ref betweenDfa, table, ref stateCounter, startState);

            // Define final state
            var finalStates = new SortedSet <State <T> >();

            foreach (var state in betweenDfa._states)
            {
                foreach (var stateInAState in state.States)
                {
                    if (ndfa._finalStates.Contains(stateInAState))
                    {
                        finalStates.Add(state);
                    }
                }
            }
            foreach (var finalState in finalStates)
            {
                betweenDfa.DefineAsFinalState(finalState);
            }


            return(BuildDfa(betweenDfa));
        }
Esempio n. 21
0
        static void TestMinimize()
        {
            char[] alphabet = { 'a', 'b' };
            var    m        = new Automaat <string>(alphabet);

            m.AddTransition(new Transition <string>("0", alphabet[0], "0"));
            m.AddTransition(new Transition <string>("0", alphabet[1], "1"));

            m.AddTransition(new Transition <string>("1", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("1", alphabet[1], "1"));

            m.AddTransition(new Transition <string>("2", alphabet[0], "0"));
            m.AddTransition(new Transition <string>("2", alphabet[1], "3"));

            m.AddTransition(new Transition <string>("3", alphabet[0], "4"));
            m.AddTransition(new Transition <string>("3", alphabet[1], "1"));

            m.AddTransition(new Transition <string>("4", alphabet[0], "5"));
            m.AddTransition(new Transition <string>("4", alphabet[1], "3"));

            m.AddTransition(new Transition <string>("5", alphabet[0], "0"));
            m.AddTransition(new Transition <string>("5", alphabet[1], "3"));

            m.DefineAsStartState("0");
            m.DefineAsFinalState("2");
            m.DefineAsFinalState("4");
            Console.WriteLine("first automate:");
            m.PrintTransitions();
            var minimized = m.MinimizeHopCroft(true);

            Console.WriteLine("minimized automate:");
            minimized.PrintTransitions();

            var otherMinimized = m.MinimizeReverse();

            Console.WriteLine("other minimization:");
            otherMinimized.PrintTransitions();
        }
Esempio n. 22
0
        static void PracL1Representatie3()
        {
            char[]            alphabet = { 'a', 'b' };
            Automaat <string> m        = new Automaat <string>(alphabet);

            m.AddTransition(new Transition <string>("1", alphabet[0], "3"));
            m.AddTransition(new Transition <string>("1", alphabet[1], "2"));

            m.AddTransition(new Transition <string>("2", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("2", alphabet[1], "1"));

            m.AddTransition(new Transition <string>("3", alphabet[0], "4"));
            m.AddTransition(new Transition <string>("3", alphabet[1], "2"));

            m.AddTransition(new Transition <string>("4", alphabet[0], "3"));
            m.AddTransition(new Transition <string>("4", alphabet[1], "5"));

            m.AddTransition(new Transition <string>("5", alphabet[0], "2"));
            m.AddTransition(new Transition <string>("5", alphabet[1], "1"));

            // only on start state in a dfa:
            m.DefineAsStartState("1");

            // two final states:
            m.DefineAsFinalState("5");

            List <Tuple <string, bool> > testWords = new List <Tuple <string, bool> >();

            testWords.Add(new Tuple <string, bool>("aab", true));
            testWords.Add(new Tuple <string, bool>("bbaab", true));
            testWords.Add(new Tuple <string, bool>("bbbaab", false));
            testWords.Add(new Tuple <string, bool>("aabaabaab", true));
            testWords.Add(new Tuple <string, bool>("bbaabb", false));
            testWords.Add(new Tuple <string, bool>("aabaab", false));
            testWords.Add(new Tuple <string, bool>("ababaabaa", false));
            TestingAutomaat("Bevat een even aantal b’s en eindigt op aab", m, testWords);
        }
Esempio n. 23
0
        private static Automaat <int> BegintMet(string symbols, char[] alphabet)
        {
            var a            = new Automaat <int>(alphabet);
            var stateCounter = 0;

            var wrongState = stateCounter++;
            var fromState  = stateCounter;

            a.DefineAsStartState(fromState);
            foreach (var s in symbols.ToCharArray())
            {
                stateCounter++;
                a.AddTransition(new Transition <int>(fromState, s, stateCounter));

                foreach (var letter in a.GetAlphabet())
                {
                    if (s == letter)
                    {
                        continue;
                    }

                    a.AddTransition(new Transition <int>(fromState, letter, wrongState));
                }

                fromState = stateCounter;
            }

            // on this point, the statecounter is the final state
            foreach (var s in a.GetAlphabet())
            {
                a.AddTransition(new Transition <int>(wrongState, s));
                a.AddTransition(new Transition <int>(stateCounter, s));
            }
            a.DefineAsFinalState(stateCounter);

            return(a);
        }
Esempio n. 24
0
        static void NdfaToDfa()
        {
            char[] alphabet = { 'a', 'b' };
            var    m        = new Automaat <int>(alphabet);

            m.AddTransition(new Transition <int>(0, Transition <int> .Epsilon, 1));
            m.AddTransition(new Transition <int>(0, Transition <int> .Epsilon, 7));

            m.AddTransition(new Transition <int>(1, Transition <int> .Epsilon, 2));
            m.AddTransition(new Transition <int>(1, Transition <int> .Epsilon, 4));

            m.AddTransition(new Transition <int>(2, alphabet[0], 3));

            m.AddTransition(new Transition <int>(3, Transition <int> .Epsilon, 6));

            m.AddTransition(new Transition <int>(4, alphabet[1], 5));

            m.AddTransition(new Transition <int>(5, Transition <int> .Epsilon, 6));

            m.AddTransition(new Transition <int>(6, Transition <int> .Epsilon, 7));
            m.AddTransition(new Transition <int>(6, Transition <int> .Epsilon, 1));

            m.AddTransition(new Transition <int>(7, alphabet[0], 8));

            m.AddTransition(new Transition <int>(8, alphabet[1], 9));

            m.AddTransition(new Transition <int>(9, alphabet[1], 10));

            m.DefineAsStartState(0);
            m.DefineAsFinalState(10);

            var dfa = NdfatoDfa.MakeDfa(m);

            Graphviz.PrintGraph(m, "test");
            dfa.PrintTransitions();
        }
Esempio n. 25
0
        private static Automaat <int> BuildDfa <T>(Automaat <State <T> > stateAutomaat) where T : IComparable
        {
            var dfa = new Automaat <int>(stateAutomaat.GetAlphabet());

            foreach (var startState in stateAutomaat._startStates)
            {
                dfa.DefineAsStartState(startState.Id);
            }
            foreach (var finalState in stateAutomaat._finalStates)
            {
                dfa.DefineAsFinalState(finalState.Id);
            }

            foreach (var trans in stateAutomaat._transitions)
            {
                dfa.AddTransition(new Transition <int>(trans.FromState.Id, trans.Symbol, trans.ToState.Id));
            }

            if (!dfa.IsDfa())
            {
                throw new Exception("Created dfa is not a dfa :(");
            }
            return(dfa);
        }