Esempio n. 1
0
        static public Automata <String> TestNDFA2()
        {
            char []           alphabet = { 'a', 'b', 'c', 'd' };
            Automata <String> m        = new Automata <String>(alphabet);

            m.addTransition(new Transition <String> ("0", '$', "1"));
            m.addTransition(new Transition <String> ("0", '$', "3"));

            m.addTransition(new Transition <String> ("1", 'b', "2"));
            m.addTransition(new Transition <String> ("3", 'a', "4"));

            m.addTransition(new Transition <String> ("2", '$', "5"));
            m.addTransition(new Transition <String> ("4", '$', "5"));

            m.addTransition(new Transition <String> ("5", '$', "0"));

            m.addTransition(new Transition <String> ("5", '$', "6"));

            m.addTransition(new Transition <String> ("6", 'b', "7"));

            m.addTransition(new Transition <String> ("7", 'c', "8"));

            m.addTransition(new Transition <String> ("8", 'd', "9"));

            // only on start state in a dfa:
            m.defineAsStartState("0");

            // one final state:
            m.defineAsFinalState("9");

            return(m);
        }
Esempio n. 2
0
        static public Automata <String> TestProcedure2()
        {
            char []           alphabet = { 'a', 'b' };
            Automata <String> m        = new Automata <String>(alphabet);

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

            m.addTransition(new Transition <String> ("B", 'b', "C"));
            m.addTransition(new Transition <String> ("B", "C"));

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

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

            m.addTransition(new Transition <String> ("E", 'a'));
            m.addTransition(new Transition <String> ("E", "D"));

            // only on start state in a dfa:
            m.defineAsStartState("A");

            // two final states:
            m.defineAsFinalState("C");
            m.defineAsFinalState("E");

            return(m);
        }
Esempio n. 3
0
        static public Automata <String> TestDFA()
        {
            char []           alphabet = { 'a', 'b' };
            Automata <String> m        = new Automata <String>(alphabet);

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

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

            m.addTransition(new Transition <String> ("q2", 'a', "q3"));
            m.addTransition(new Transition <String> ("q2", 'b', "q4"));

            m.addTransition(new Transition <String> ("q3", 'a', "q1"));
            m.addTransition(new Transition <String> ("q3", 'b', "q2"));

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

            // only on start state in a dfa:
            m.defineAsStartState("q0");

            // two final states:
            m.defineAsFinalState("q2");
            m.defineAsFinalState("q3");

            return(m);
        }
Esempio n. 4
0
        static public Automata <String> TestNDFA()
        {
            char []           alphabet = { 'a', 'b' };
            Automata <String> m        = new Automata <String>(alphabet);

            m.addTransition(new Transition <String> ("1", 'a', "2"));
            m.addTransition(new Transition <String> ("2", 'b', "2"));

            m.addTransition(new Transition <String> ("2", 'b', "4"));
            m.addTransition(new Transition <String> ("1", 'a', "3"));

            m.addTransition(new Transition <String> ("3", 'a', "2"));
            m.addTransition(new Transition <String> ("3", 'a', "4"));

            m.addTransition(new Transition <String> ("4", 'a', "3"));

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

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

            return(m);
        }
Esempio n. 5
0
        public Automata <String> renameStates(Automata <String> automaton)
        {
            Automata <String>           renamedAutomaton = new Automata <string>(automaton.getAlphabet());
            Dictionary <String, String> referencer       = new Dictionary <string, string>();
            int counter = 0;

            foreach (String state in automaton.getStates())
            {
                referencer.Add(state, "q" + counter++);
                Console.WriteLine("q" + counter);
            }
            if (automaton.getStates().Contains("Ø"))
            {
                referencer["Ø"] = "Ø";
            }
            foreach (Transition <String> transition in automaton.getTransitions())
            {
                renamedAutomaton.addTransition(new Transition <string>(referencer[transition.getFromState()], transition.getSymbol(), referencer[transition.getToState()]));
            }
            foreach (String state in automaton.getFinalStates())
            {
                renamedAutomaton.defineAsFinalState(referencer[state]);
            }
            foreach (String state in automaton.getStartStates())
            {
                renamedAutomaton.defineAsStartState(referencer[state]);
            }
            return(renamedAutomaton);
        }
Esempio n. 6
0
        public Automata <String> inverseAutomata(Automata <String> automaton)
        {
            Automata <String> invertedAutomaton = new Automata <string>(automaton.getAlphabet());

            foreach (String state in automaton.getFinalStates())
            {
                invertedAutomaton.defineAsStartState(state);
            }

            foreach (Transition <String> transition in automaton.getTransitions())
            {
                invertedAutomaton.addTransition(transition);
            }

            foreach (String state in automaton.getStates())
            {
                if (!automaton.getFinalStates().Contains(state))
                {
                    invertedAutomaton.defineAsFinalState(state);
                }
            }

            foreach (String state in automaton.getFinalStates())
            {
                if (state != invertedAutomaton.getStartStates().First())
                {
                    invertedAutomaton.addTransition(new Transition <string>(invertedAutomaton.getStartStates().First(), state));
                }
            }


            return(invertedAutomaton);
        }
Esempio n. 7
0
//		public Automata<String> reverseAutomata(Automata<String> automaton)
//		{
//			Automata<String> reversedAutomaton = new Automata<String>(automaton.getAlphabet());
//
//			foreach (Transition<String> transition in automaton.getTransitions())
//			{
//				transition.reverseFromToState();
//				reversedAutomaton.addTransition(transition);
//			}
//
//			foreach (String state in automaton.getStartStates()) {
//				reversedAutomaton.defineAsStartState(state);
//			}
//
//			foreach (String state in automaton.getFinalStates()) {
//				reversedAutomaton.defineAsFinalState(state);
//			}
//			reversedAutomaton.printTransitions();
//
//			reversedAutomaton = inverseAutomata(reversedAutomaton);
//
//			reversedAutomaton.printTransitions();
//			return reversedAutomaton;
//		}

        public Automata <String> reverseAutomata(Automata <String> automaton)
        {
            Automata <String> reversedAutomaton = new Automata <string>(automaton.getAlphabet());

            foreach (Transition <String> transition in automaton.getTransitions())
            {
                reversedAutomaton.addTransition(new Transition <String> (transition.getToState(), transition.getSymbol(), transition.getFromState()));
            }

            foreach (String state in automaton.getFinalStates())
            {
                reversedAutomaton.defineAsStartState(state);
            }

            foreach (String state in automaton.getStartStates())
            {
                reversedAutomaton.defineAsFinalState(state);
            }

            return(reversedAutomaton);
        }
Esempio n. 8
0
        static public Automata <String> TestDFA2()
        {
            char []           alphabet = { 'a', 'b' };
            Automata <String> m        = new Automata <String>(alphabet);

            m.addTransition(new Transition <String> ("q0", 'a', "q2"));
            m.addTransition(new Transition <String> ("q0", 'b', "q3"));

            m.addTransition(new Transition <String> ("q1", 'a', "q3"));
            m.addTransition(new Transition <String> ("q1", 'b', "q2"));

            m.addTransition(new Transition <String> ("q2", 'a', "q0"));
            m.addTransition(new Transition <String> ("q2", 'b', "q4"));

            m.addTransition(new Transition <String> ("q3", 'a', "q1"));
            m.addTransition(new Transition <String> ("q3", 'b', "q5"));

            m.addTransition(new Transition <String> ("q4", 'a', "q6"));
            m.addTransition(new Transition <String> ("q4", 'b', "q5"));

            m.addTransition(new Transition <String> ("q5", 'a', "q2"));
            m.addTransition(new Transition <String> ("q5", 'b', "q0"));

            m.addTransition(new Transition <String> ("q6", 'a', "q4"));
            m.addTransition(new Transition <String> ("q6", 'b', "q0"));

            // only on start state in a dfa:
            m.defineAsStartState("q0");

            // final states:
            m.defineAsFinalState("q1");
            m.defineAsFinalState("q3");
            m.defineAsFinalState("q4");
            m.defineAsFinalState("q6");
            return(m);
        }
Esempio n. 9
0
        public void regexToNDFA(ref int depth, ref Automata <String> automata, string prevstate = null, string nextstate = null)
        {
            //THOMPSON
            if (depth == 0)
            {
                prevstate = "1";
                automata.defineAsStartState(prevstate);
                depth += 2;
            }
            //depth += 1;
            bool nonext = false;

            switch (operate)
            {
            case Operator.OR:
                automata.addTransition(new Transition <string>(prevstate, depth.ToString()));
                String splitstate = depth.ToString();
                depth++;
                String lnewstate = splitstate;
                if (left.operate == Operator.ONE)
                {
                    foreach (char s in left.terminal)
                    {
                        automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString()));
                        automata.addToAlphabet(s);
                        lnewstate = depth.ToString();                                 //NOT PROPERLY IMPLEMENTED BEHAVIOR
                        depth++;
                    }
                }
                else
                {
                    automata.addTransition(new Transition <string>(lnewstate, depth.ToString()));                           //print = print + left.terminal;//left.regexToNDFA(depth, automata);
                    lnewstate = depth.ToString();
                    depth++;
                    String loldstate2 = depth.ToString();
                    depth++;
                    left.regexToNDFA(ref depth, ref automata, lnewstate, loldstate2);
                    lnewstate = loldstate2;
                    //depth++;
                }
                automata.addTransition(new Transition <string>(prevstate, depth.ToString()));
                splitstate = depth.ToString();
                depth++;
                depth++;
                String rnewstate = splitstate;
                if (right.operate == Operator.ONE)
                {
                    foreach (char s in right.terminal)
                    {
                        automata.addTransition(new Transition <string>(rnewstate, s, depth.ToString()));
                        automata.addToAlphabet(s);
                        rnewstate = depth.ToString();
                        depth++;
                    }
                }
                else
                {
                    automata.addTransition(new Transition <string>(rnewstate, depth.ToString())); //print = print + left.terminal;//left.regexToNDFA(depth, automata);
                    rnewstate = depth.ToString();                                                 //NOT PROPERLY IMPLEMENTED BEHAVIOR
                    depth++;
                    String roldstate = depth.ToString();
                    depth++;
                    right.regexToNDFA(ref depth, ref automata, rnewstate, roldstate);
                    rnewstate = roldstate;
                    //depth++;
                }
                if (nextstate == null)
                {
                    automata.addTransition(new Transition <string>(lnewstate, depth.ToString()));
                    automata.addTransition(new Transition <string>(rnewstate, depth.ToString()));
                    automata.defineAsFinalState(depth.ToString());
                    depth++;
                }
                else
                {
                    automata.addTransition(new Transition <string>(lnewstate, nextstate));                            //WRONG
                    automata.addTransition(new Transition <string>(rnewstate, nextstate));
                }

                break;

            case Operator.DOT:

                if (nextstate == null)
                {
                    nextstate = depth.ToString();
                    depth++;
                    nonext = true;
                }
                depth++;
                String tmpstate = depth.ToString();
                if (left.operate == Operator.ONE)
                {
                    lnewstate = prevstate;
                    depth++;
                    foreach (char s in left.terminal)
                    {
                        automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString()));
                        automata.addToAlphabet(s);
                        lnewstate = depth.ToString();
                        depth++;
                    }
                    automata.addTransition(new Transition <string>(lnewstate, tmpstate));
                }
                else
                {
                    left.regexToNDFA(ref depth, ref automata, prevstate, tmpstate);
                }
                if (right.operate == Operator.ONE)
                {
                    lnewstate = tmpstate;
                    depth++;
                    foreach (char s in right.terminal)
                    {
                        automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString()));
                        automata.addToAlphabet(s);
                        lnewstate = depth.ToString();
                        depth++;
                    }
                    automata.addTransition(new Transition <string>(lnewstate, nextstate));
                }
                else
                {
                    right.regexToNDFA(ref depth, ref automata, tmpstate, nextstate);
                }
                if (nonext)
                {
                    automata.defineAsFinalState(nextstate);
                }
                break;

            case Operator.ONE:                     //NOT REALLY IMPLEMENTED
                depth++;
                lnewstate = prevstate;
                foreach (char s in terminal)
                {
                    automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString()));
                    automata.addToAlphabet(s);
                    lnewstate = depth.ToString();
                    depth++;
                }
                if (nextstate == null)
                {
                    automata.defineAsFinalState((depth - 1).ToString());
                }
                break;

            case Operator.PLUS:
                if (nextstate == null)
                {
                    nextstate = depth.ToString();
                    depth++;
                    nonext = true;
                }
                depth++;
                automata.addTransition(new Transition <string>(prevstate, depth.ToString()));
                lnewstate = depth.ToString();
                String loldstate = depth.ToString();
                depth++;
                if (left.operate == Operator.ONE)
                {
                    foreach (char s in left.terminal)
                    {
                        automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString()));
                        automata.addToAlphabet(s);
                        lnewstate = depth.ToString();
                        depth++;
                    }
                    automata.addTransition(new Transition <string>(lnewstate, loldstate));
                    automata.addTransition(new Transition <string>(lnewstate, depth.ToString()));
                    rnewstate = depth.ToString();
                }
                else
                {
                    rnewstate = depth.ToString();
                    depth++;
                    left.regexToNDFA(ref depth, ref automata, lnewstate, rnewstate);                             //NOT CHECKED, PROBABLY WRONG
                    //automata.addTransition(new Transition<string>(loldstate, depth.ToString()));
                    automata.addTransition(new Transition <string>(rnewstate, lnewstate));
                    automata.addTransition(new Transition <string>(rnewstate, depth.ToString()));
                    rnewstate = depth.ToString();
                }
                if (!nonext)
                {
                    automata.addTransition(new Transition <string>(depth.ToString(), nextstate));
                }
                else
                {
                    depth++;
                    automata.addTransition(new Transition <string>(rnewstate, depth.ToString()));
                    automata.defineAsFinalState(depth.ToString());
                }
                depth++;
                break;

            case Operator.STAR:
                if (depth.ToString() == nextstate)
                {
                    depth++;
                }
                if (nextstate == null)
                {
                    nextstate = depth.ToString();
                    depth++;
                    nonext = true;
                }
                automata.addTransition(new Transition <string>(prevstate, depth.ToString()));
                lnewstate = depth.ToString();
                rnewstate = lnewstate;
                depth++;
                if (left.operate == Operator.ONE)
                {
                    foreach (char s in left.terminal)
                    {
                        automata.addTransition(new Transition <string>(lnewstate, s, depth.ToString()));
                        automata.addToAlphabet(s);
                        lnewstate = depth.ToString();                                 //NOT PROPERLY IMPLEMENTED BEHAVIOR
                        depth++;
                    }
                    automata.addTransition(new Transition <string>(prevstate, nextstate));
                    automata.addTransition(new Transition <string>(lnewstate, rnewstate));
                }
                else
                {
                    automata.addTransition(new Transition <string>(lnewstate, depth.ToString()));                           //print = print + left.terminal;//left.regexToNDFA(depth, automata);
                    loldstate = lnewstate;
                    lnewstate = depth.ToString();
                    depth++;
                    String futurestate = depth.ToString();
                    depth++;
                    left.regexToNDFA(ref depth, ref automata, lnewstate, futurestate);
                    depth++;
                    automata.addTransition(new Transition <string>(futurestate, depth.ToString()));
                    automata.addTransition(new Transition <string>(futurestate, lnewstate));
                    automata.addTransition(new Transition <string>(loldstate, depth.ToString()));
                    lnewstate = depth.ToString();
                    depth++;
                }
                if (!nonext)
                {
                    automata.addTransition(new Transition <string>(lnewstate, nextstate));
                }
                else
                {
                    automata.addTransition(new Transition <string>(lnewstate, depth.ToString()));
                    automata.defineAsFinalState(depth.ToString());
                }
                break;
            }
        }
Esempio n. 10
0
        public Automata <String> NDFAToDFA(Automata <String> Automaton)
        {
            SortedSet <char>  alphabet = Automaton.getAlphabet();
            Automata <String> dfa      = new Automata <String>(alphabet);
            Dictionary <Transition <String>, SortedSet <String> > transitionsDict = new Dictionary <Transition <String>, SortedSet <String> >();

            dfa.defineAsStartState(prettyPrint(findStartState(Automaton))); //Set startstates for dfa
            foreach (char letter in alphabet)                               //Check all states after the startstate with all the letters in the alphabet
            {
                if (findMultipleAccessible(Automaton, letter, findStartState(Automaton)).Count() == 0)
                {
                    dfa.addTransition(new Transition <string>(prettyPrint(findStartState(Automaton)), letter, "Ø"));
                }
                else
                {
                    SortedSet <String>  toStates      = findMultipleAccessible(Automaton, letter, findStartState(Automaton));
                    Transition <String> newTransition = new Transition <string>(prettyPrint(findStartState(Automaton)), letter, prettyPrint(toStates));
                    transitionsDict.Add(newTransition, toStates);
                    dfa.addTransition(newTransition);
                }
            }

            int tempTransitionsNumber = 0;
            Dictionary <Transition <String>, SortedSet <String> > tempTransitionsDict = new Dictionary <Transition <String>, SortedSet <String> >();

            while (dfa.getTransistionsNumber() != tempTransitionsNumber)
            {
                tempTransitionsNumber = dfa.getTransistionsNumber();
                foreach (KeyValuePair <Transition <String>, SortedSet <String> > pair in transitionsDict)
                {
                    foreach (char letter in alphabet)
                    {
                        if (findMultipleAccessible(Automaton, letter, pair.Value).Count() == 0)
                        {
                            if (dfa.getTransitions().Contains(new Transition <string>(pair.Key.getToState(), letter, "Ø")) == false)
                            {
                                dfa.addTransition(new Transition <string>(pair.Key.getToState(), letter, "Ø"));
                                tempTransitionsDict.Add(new Transition <string>(pair.Key.getToState(), letter, "Ø"), findMultipleAccessible(Automaton, letter, pair.Value));
                            }
                        }
                        else
                        {
                            SortedSet <String>  toStates      = findMultipleAccessible(Automaton, letter, pair.Value);
                            Transition <String> newTransition = new Transition <string>(pair.Key.getToState(), letter, prettyPrint(toStates));
                            if (dfa.getTransitions().Contains(new Transition <string>(pair.Key.getToState(), letter, prettyPrint(toStates))) == false)
                            {
                                tempTransitionsDict.Add(newTransition, toStates);
                                dfa.addTransition(newTransition);
                            }
                        }
                    }
                }
                transitionsDict = transitionsDict.Union(tempTransitionsDict).ToDictionary(k => k.Key, v => v.Value);
            }
            SortedSet <String> tempListFinal = new SortedSet <String>();

            foreach (String stat in dfa.getStates())
            {
                foreach (String endState in Automaton.getFinalStates())
                {
                    if (stat.Contains(endState))
                    {
                        tempListFinal.Add(stat);
                    }
                }
            }
            foreach (String finState in tempListFinal)
            {
                dfa.defineAsFinalState(finState);
            }
            return(dfa);
        }