Beispiel #1
0
        private void btnDFADtoDFAM_Click(object sender, EventArgs e)
        {
            List <DFAD.DFA> list = new List <DFAD.DFA>();

            DFAD.DFA dfa = new DFAD.DFA();
            for (int i = 0; i < lvDFAD.Items.Count; i++)
            {
                dfa.From  = Convert.ToInt16(lvDFAD.Items[i].SubItems[0].Text);
                dfa.Varch = Convert.ToChar(lvDFAD.Items[i].SubItems[1].Text);
                dfa.To    = Convert.ToInt16(lvDFAD.Items[i].SubItems[2].Text);
                list.Add(dfa);
            }
            DFAD.DFA[] dfad = new DFAD.DFA[list.Count];
            list.CopyTo(dfad);
            List <DFAD.DFA> result = new List <DFAD.DFA>();
            DfaMinClass     dmc    = new DfaMinClass();

            result = dmc.DMC(dfad, lblStarts.Text, lblEnds.Text, strState);
            string[] aa = new string[3];
            for (int i = 0; i < result.Count; i++)
            {
                aa[0] = result[i].From.ToString();
                aa[1] = result[i].Varch.ToString();
                aa[2] = result[i].To.ToString();
                ListViewItem item = new ListViewItem(aa);
                lvDFAN.Items.Add(item);
            }
        }
        public List <DFAD.DFA> DMC(DFAD.DFA[] Dfas, string Starts, string Ends, string States)
        {
            bool isrepa = false;
            int  starts = Convert.ToInt16(Starts);

            int[]      ends  = new int[Ends.Length];
            string[]   State = States.Split(',');
            List <int> node3 = new List <int>(); //终结状态组
            List <int> node4 = new List <int>(); //非终结状态组

            int[] st = new int[State.Length - 1];
            for (int i = 0; i < State.Length - 1; i++)
            {
                st[i] = Convert.ToInt16(State[i]);
            }
            //得到所有的开始状态和结束状态

            for (int i = 0; i < Ends.Length; i++)
            {
                ends[i] = Convert.ToInt16(Ends[i]);
            }

            for (int i = 0; i < st.Length; i++)
            {
                if (Ends.IndexOf(st[i].ToString()) != -1)
                {
                    Group.Node node1 = new Group.Node(1, st[i], 0);
                    node.Add(node1);
                    node3.Add(node1.num);
                }
                else
                {
                    Group.Node node1 = new Group.Node(0, st[i], 1);
                    node.Add(node1);
                    node4.Add(node1.num);
                }
            }
            //将所有的分组放入堆栈中
            nodesStack.Push(node3);
            nodesStack.Push(node4);
            //到此为止堆栈中有两个元素,终态集合和非终态集合
            while (nodesStack.Count != 0)
            {
                List <int> NeedGroup = new List <int>();
                NeedGroup = nodesStack.Pop();
                if (NeedGroup.Count > 1)
                {
                    ComparePos(GetAllChar(Dfas), Dfas, NeedGroup);
                }
            }
            string txtTest = "";

            for (int i = 0; i < node.Count; i++)
            {
                txtTest += "组号:" + node[i].pos.ToString() + "\t\t状态号:" + node[i].num.ToString() + "\t是否为非终结状态:" + node[i].accept.ToString() + "\r\n";
            }
            System.IO.File.WriteAllText(@"c:\txtTest.test", txtTest);
            txtTest = "";
            List <DFAD.DFA> newdfas = new List <DFAD.DFA>();

            foreach (DFAD.DFA dfa in Del(Dfas, ChangeDFA(Dfas)))
            {
                DFAD.DFA newdfa = new DFAD.DFA(SearchPos(dfa.From), SearchPos(dfa.To), dfa.Varch);
                for (int i = 0; i < newdfas.Count; i++)
                {
                    if (newdfa.From == newdfas[i].From && newdfa.To == newdfas[i].To && newdfa.Varch == newdfas[i].Varch)
                    {
                        isrepa = true;
                        break;
                    }
                    else
                    {
                        isrepa = false;
                        continue;
                    }
                }
                if (isrepa == false)
                {
                    newdfas.Add(newdfa);
                }
            }
            List <DFAD.DFA> result = new List <DFAD.DFA>();

            for (int i = 0; i < newdfas.Count; i++)
            {
                txtTest += newdfas[i].From + "\t" + newdfas[i].Varch + "\t" + newdfas[i].To + "\r\n";
                result.Add(newdfas[i]);
            }
            txtTest += "终结状态:";
            for (int i = 0; i < ChangeDFA(Dfas).Count; i++)
            {
                if (ChangeDFA(Dfas)[i].accept == 0)
                {
                    txtTest += ChangeDFA(Dfas)[i].pos.ToString() + ",";
                }
            }
            txtTest  = txtTest.Substring(0, txtTest.Length - 1);
            txtTest += "\r\n开始状态:";
            for (int i = 0; i < node.Count; i++)
            {
                if (node[i].num == starts)
                {
                    txtTest += SearchPos(starts).ToString();
                }
            }
            System.IO.File.WriteAllText(@"c:\result.dfa", txtTest);
            return(result);
        }
        /// <summary>
        /// 返回I,Ia,Ib.......Ix组成的表格
        /// </summary>
        /// <param name="nfam">要转换的nfam</param>
        /// <param name="start">起始状态</param>
        /// <returns>I,Ia,Ib.......Ix组成的表格</returns>
        public List <DFAD.DFA> Transfrom(NFA.NFAM[] nfam, string start, string end)
        {
            //List<List<int>> TableState = new List<List<int>>();
            TableState = StartAnalysis(nfam, start);
            List <Char>     UsedAllChar = GetAllChar(nfam);//NFAM中用到的所有的非空字符
            List <int>      Starts      = new List <int>();
            List <int>      Ends        = new List <int>();
            List <DFAD.DFA> Result      = new List <DFAD.DFA>();
            int             m           = 0;
            int             n           = 0;

            List <int>[,] TableDfaD = new List <int> [TableState.Count / (UsedAllChar.Count + 1), UsedAllChar.Count + 1];
            for (int i = 0; i < TableState.Count; i++)
            {
                if (m < TableState.Count / (UsedAllChar.Count + 1))
                {
                    TableDfaD[m, n] = TableState[i];
                }
                n++;
                if (n % (UsedAllChar.Count + 1) == 0)
                {
                    n = 0;
                    m++;
                }
            }
            int[,] newTable = new int[TableDfaD.GetLength(0), TableDfaD.GetLength(1)];
            rrr             = TableDfaD;

            for (int i = 0; i < newTable.GetLength(0); i++)
            {
                newTable[i, 0] = i;
                aaaaaa.Add(i);
            }
            for (int i = 0; i < TableDfaD.GetLength(0); i++)
            {
                for (int j = 1; j < TableDfaD.GetLength(1); j++)
                {
                    for (int k = 0; k < newTable.GetLength(0); k++)
                    {
                        if (ListCompare(TableDfaD[i, j], TableDfaD[k, 0]))
                        {
                            newTable[i, j] = k;
                        }
                        if (TableDfaD[i, j].Count == 0)
                        {
                            newTable[i, j] = -1;
                        }
                    }
                }
            }
            for (int i = 0; i < newTable.GetLength(0); i++)
            {
                for (int j = 0; j < UsedAllChar.Count; j++)
                {
                    DFAD.DFA dfa = new DFAD.DFA();
                    dfa.From  = newTable[i, 0];
                    dfa.Varch = UsedAllChar[j];
                    dfa.To    = newTable[i, j + 1];
                    Result.Add(dfa);
                }
            }

            return(Result);
        }