Esempio n. 1
0
        public DFA NFAtoDFA(NFA_ nfa)
        {
            List <List <int> > combinations = new List <List <int> >();
            List <int[]>       TT           = new List <int[]>();



            int s1 = nfa.IS;


            combinations.Add(new List <int>()
            {
                s1
            });
            int n        = 1;
            int addIndex = 0;

            while (n <= combinations.Count)
            {
                int[] ttArray = new int[domain.Length];
                int   i       = 0;

                foreach (char item in domain)
                {
                    List <int> tempComb = combinations[n - 1];
                    List <int> s2       = new List <int>();
                    foreach (int state in tempComb)
                    {
                        if (state != -1)
                        {
                            List <int> tempstate = nfa.getTransitoins(state, item);

                            foreach (var trans in tempstate)
                            {
                                s2.Add(trans);
                            }
                        }
                        else
                        {
                            s2.Add(-1);
                        }
                    }
                    s2.Sort();
                    s2 = s2.Distinct().ToList();

                    int index = 0;
                    if (s2.Count > 1)
                    {
                        s2.Remove(-1);
                    }
                    if (combinationExists(combinations, s2))
                    {
                        // not add
                        index        = getCombinationIndex(combinations, s2);
                        ttArray[i++] = index;
                    }
                    else
                    {
                        combinations.Add
                            (s2);
                        ttArray[i] = ++addIndex;
                        i++;
                    }
                }
                n++;
                TT.Add(ttArray);
            }


            //------------------------------Convert List to Array----------------------------------//
            int[,] finalTT = new int[TT.Count, nfa.domain.Length];
            for (int i = 0; i < finalTT.GetLength(0); i++)
            {
                for (int j = 0; j < finalTT.GetLength(1); j++)
                {
                    finalTT[i, j] = TT[i][j];
                }
            }

            //// ------------ Mark final states of new Dfa --------------------------//

            List <int> tempFs = new List <int>();

            for (int r = 0; r < combinations.Count; r++)
            {
                List <int> tempComb = combinations[r];

                foreach (var item in nfa.FS)
                {
                    if (tempComb.Contains(item))
                    {
                        tempFs.Add(r);
                    }
                }
            }
            tempFs = tempFs.Distinct().ToList();
            tempFs.Sort();

            DFA dfa3 = new DFA(0, tempFs.ToArray(), nfa.domain, finalTT);

            return(dfa3);
        }