Esempio n. 1
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;
            }
        }