Exemplo n.º 1
0
        private cell MakeDFAcell(CloseSet C)
        {
            cell c = new cell();

            /*-------------------------------------------------------------------------*/
            for (int i = 0; i < C.CSet.Count; i++)
            {
                for (int j = 0; j < transymbol.Count; j++)
                {
                    if (C.CSet[i].next[j] != -2)
                    {
                        edge e = new edge();
                        e.StartState.StateName = i;
                        e.TransSymbol          = transymbol[j];
                        e.EndState.StateName   = C.CSet[i].next[j];
                        c.EdgeSet.Add(e);
                    }
                }
            }

            c.StartState.StateName = 0;
            c.EndState.StateName   = C.CSet.Count - 1;
            c.EdgeCount            = c.EdgeSet.Count;
            return(c);
        }
Exemplo n.º 2
0
        /*-------------------------------------------------------DFA转MFA----------------------------------------------------------*/

        private cell MakeMFAcell()
        {
            cell m = new cell();

            /*-------------------------------------------------------------------------*/
            for (int i = 0; i < P.Count; i++)
            {
                for (int j = 0; j < transymbol.Count; j++)
                {
                    if (P[i][0].next[j] != -2)
                    {
                        edge e = new edge();
                        e.StartState.StateName = P[i][0].StateName;
                        e.TransSymbol          = transymbol[j];
                        e.EndState.StateName   = P[i][0].next[j];
                        m.EdgeSet.Add(e);
                    }
                }
            }

            m.StartState.StateName = 0;
            m.EndState.StateName   = DFA.EndState.StateName;
            m.EdgeCount            = m.EdgeSet.Count;
            return(m);
        }
Exemplo n.º 3
0
        private void DFAToMFA(char[] ch)
        {
            if (READFILE == false)
            {
                NFAToDFA(ch);
                MFAmax = DFAe + 1;
            }

            P = new List <List <state> >();
            P = InitP();
            while (true)
            {
                int lastCount = P.Count;
                for (int i = 0; i < P.Count; i++)
                {
                    for (int j = 0; j < transymbol.Count; j++)
                    {
                        List <List <state> > D = Divide(P[i], transymbol[j]);
                        P.RemoveAt(i);
                        P.InsertRange(i, D);
                    }
                }
                if (P.Count == lastCount)
                {
                    break;
                }
            }
            Simplification();

            MFA = MakeMFAcell();
            cell   result = MFA;
            string output = null;

            output = output + "开始状态    接受符号    到达状态\n";
            for (int i = 0; i < result.EdgeCount; i++)
            {
                string s = String.Format("{0,-12}{1,-12}{2,-12}\n", result.EdgeSet[i].StartState.StateName, result.EdgeSet[i].TransSymbol, result.EdgeSet[i].EndState.StateName);
                output = output + s;
            }

            richTextBox3.Text = output;

            MFAb          = MFA.StartState.StateName;
            MFAe          = MFA.EndState.StateName;
            textBox6.Text = MFAb.ToString();
            if (READFILE == false)
            {
                textBox7.Text = MFAe.ToString();
            }
            else
            {
                string m = String.Join(",", MFAes.ToArray());
                textBox7.Text = m;
            }
        }
Exemplo n.º 4
0
        private void DFAToMFA(char[] ch)
        {
            NFAToDFA(ch);
            P = new List <List <state> >();
            P = InitP();
            while (true)
            {
                int lastCount = P.Count;
                for (int i = 0; i < P.Count; i++)
                {
                    for (int j = 0; j < transymbol.Count; j++)
                    {
                        List <List <state> > D = Divide(P[i], transymbol[j]);
                        P.RemoveAt(i);
                        P.InsertRange(i, D);
                    }
                    /*-------------------------------------------------------------------------*/
                    //List<List<state>> D1 = Divide(P[i],'a');
                    //P.RemoveAt(i);
                    //P.InsertRange(i,D1);
                    //List<List<state>> D2 = Divide(P[i],'b');
                    //P.RemoveAt(i);
                    //P.InsertRange(i, D2);
                    /*-------------------------------------------------------------------------*/
                }
                if (P.Count == lastCount)
                {
                    break;
                }
            }
            Simplification();

            MFA = MakeMFAcell();
            cell   result = MFA;
            string output = null;

            output = output + "开始状态    接受符号    到达状态\n";
            for (int i = 0; i < result.EdgeCount; i++)
            {
                string s = String.Format("{0,-12}{1,-12}{2,-12}\n", result.EdgeSet[i].StartState.StateName, result.EdgeSet[i].TransSymbol, result.EdgeSet[i].EndState.StateName);
                output = output + s;
            }

            richTextBox3.Text = output;

            MFAb          = MFA.StartState.StateName;
            MFAe          = MFA.EndState.StateName;
            textBox6.Text = MFAb.ToString();
            textBox7.Text = MFAe.ToString();
        }
Exemplo n.º 5
0
        private cell OR(cell Left, cell Right)
        {
            cell NewCell = new cell();

            NewCell.EdgeCount = 0;
            edge Edge1 = new edge();
            edge Edge2 = new edge();
            edge Edge3 = new edge();
            edge Edge4 = new edge();

            state StartState = new state();
            state EndState   = new state();

            StartState.StateName = ++stateName;
            EndState.StateName   = ++stateName;
            //构建边
            Edge1.StartState  = StartState;
            Edge1.EndState    = Left.EdgeSet[0].StartState;
            Edge1.TransSymbol = '#';

            Edge2.StartState  = StartState;
            Edge2.EndState    = Right.EdgeSet[0].StartState;
            Edge2.TransSymbol = '#';

            Edge3.StartState  = Left.EdgeSet[Left.EdgeSet.Count - 1].EndState;
            Edge3.EndState    = EndState;
            Edge3.TransSymbol = '#';

            Edge4.StartState  = Right.EdgeSet[Right.EdgeSet.Count - 1].EndState;
            Edge4.EndState    = EndState;
            Edge4.TransSymbol = '#';

            //构建单元
            NewCell.EdgeSet.AddRange(Left.EdgeSet);
            NewCell.EdgeSet.AddRange(Right.EdgeSet);

            NewCell.EdgeSet.Add(Edge1);
            NewCell.EdgeSet.Add(Edge2);
            NewCell.EdgeSet.Add(Edge3);
            NewCell.EdgeSet.Add(Edge4);
            NewCell.EdgeCount = NewCell.EdgeSet.Count;

            NewCell.StartState = StartState;
            NewCell.EndState   = EndState;
            return(NewCell);
        }//'|'的运算
Exemplo n.º 6
0
        }//'+''-'的运算

        private cell CLOSURE(cell Cell)
        {
            cell NewCell = new cell();

            NewCell.EdgeCount = 0;
            edge Edge1 = new edge();
            edge Edge2 = new edge();
            edge Edge3 = new edge();
            edge Edge4 = new edge();

            state StartState = new state();
            state EndState   = new state();

            StartState.StateName = ++stateName;
            EndState.StateName   = ++stateName;

            //e1
            Edge1.StartState  = StartState;
            Edge1.EndState    = EndState;
            Edge1.TransSymbol = '#';  //闭包取空串
            //e2
            Edge2.StartState  = Cell.EndState;
            Edge2.EndState    = Cell.StartState;
            Edge2.TransSymbol = '#';  //取字符,自连接
            //e3
            Edge3.StartState  = StartState;
            Edge3.EndState    = Cell.StartState;
            Edge3.TransSymbol = '#';
            //e4
            Edge4.StartState  = Cell.EndState;
            Edge4.EndState    = EndState;
            Edge4.TransSymbol = '#';

            NewCell.EdgeSet.AddRange(Cell.EdgeSet);

            NewCell.EdgeSet.Add(Edge1);
            NewCell.EdgeSet.Add(Edge2);
            NewCell.EdgeSet.Add(Edge3);
            NewCell.EdgeSet.Add(Edge4);

            NewCell.StartState = StartState;
            NewCell.EndState   = EndState;
            NewCell.EdgeCount  = NewCell.EdgeSet.Count;
            return(NewCell);
        }//'*'的运算
Exemplo n.º 7
0
        private void WriteNFA()
        {
            cell   result = NFA;
            string output = null;

            output = output + "开始状态    接受符号    到达状态\n";
            for (int i = 0; i < result.EdgeCount; i++)
            {
                string s = String.Format("{0,-12}{1,-12}{2,-12}\n", result.EdgeSet[i].StartState.StateName, result.EdgeSet[i].TransSymbol, result.EdgeSet[i].EndState.StateName);
                output = output + s;
            }

            richTextBox1.Text = output;

            NFAb          = NFA.StartState.StateName;
            NFAe          = NFA.EndState.StateName;
            textBox2.Text = NFAb.ToString();
            textBox3.Text = NFAe.ToString();
        }//将读取的NFA输出
Exemplo n.º 8
0
        private cell MakeDFAcell(CloseSet C)
        {
            cell c = new cell();

            /*-------------------------------------------------------------------------*/
            for (int i = 0; i < C.CSet.Count; i++)
            {
                for (int j = 0; j < transymbol.Count; j++)
                {
                    edge e = new edge();
                    e.StartState.StateName = i;
                    e.TransSymbol          = transymbol[j];
                    e.EndState.StateName   = C.CSet[i].next[j];
                    c.EdgeSet.Add(e);
                }
            }
            /*-------------------------------------------------------------------------*/

            //for (int i = 0; i < C.CSet.Count; i++)
            //{
            //    edge e = new edge();
            //    e.StartState.StateName = i;
            //    e.TransSymbol = 'a';
            //    e.EndState.StateName = C.CSet[i].anext;
            //    c.EdgeSet.Add(e);
            //}

            //for (int i = 0; i < C.CSet.Count; i++)
            //{
            //    edge e = new edge();
            //    e.StartState.StateName = i;
            //    e.TransSymbol = 'b';
            //    e.EndState.StateName = C.CSet[i].bnext;
            //    c.EdgeSet.Add(e);
            //}
            /*-------------------------------------------------------------------------*/
            c.StartState.StateName = 0;
            c.EndState.StateName   = C.CSet.Count - 1;
            c.EdgeCount            = c.EdgeSet.Count;
            return(c);
        }
Exemplo n.º 9
0
        }//将读取的NFA输出

        private void WriteDFA()
        {
            cell   result = DFA;
            string output = null;

            output = output + "开始状态    接受符号    到达状态\n";
            for (int i = 0; i < result.EdgeCount; i++)
            {
                string s = String.Format("{0,-12}{1,-12}{2,-12}\n", result.EdgeSet[i].StartState.StateName, result.EdgeSet[i].TransSymbol, result.EdgeSet[i].EndState.StateName);
                output = output + s;
            }

            richTextBox2.Text = output;


            textBox4.Text = DFAb.ToString();

            string d = String.Join(",", DFAes.ToArray());

            textBox5.Text = d;
        }
Exemplo n.º 10
0
        }//'|'的运算

        private cell PLUS(cell Left, cell Right)
        {
            for (int i = 0; i < Right.EdgeCount; i++)
            {
                if (Right.EdgeSet[i].StartState.StateName == Right.StartState.StateName)
                {
                    Right.EdgeSet[i].StartState = Left.EndState;
                }
                else if (Right.EdgeSet[i].EndState.StateName == Right.StartState.StateName)
                {
                    Right.EdgeSet[i].EndState = Left.EndState;
                }
            }
            //

            Right.StartState = Left.EndState;
            Left.EdgeSet.AddRange(Right.EdgeSet);
            Left.EndState  = Right.EndState;
            Left.EdgeCount = Left.EdgeSet.Count;
            return(Left);
        }//'+''-'的运算
Exemplo n.º 11
0
        /*-------------------------------------------------------DFA转MFA----------------------------------------------------------*/

        private cell MakeMFAcell()
        {
            cell m = new cell();

            /*-------------------------------------------------------------------------*/
            for (int i = 0; i < P.Count; i++)
            {
                for (int j = 0; j < transymbol.Count; j++)
                {
                    edge e = new edge();
                    e.StartState.StateName = P[i][0].StateName;
                    e.TransSymbol          = transymbol[j];
                    e.EndState.StateName   = P[i][0].next[j];
                    m.EdgeSet.Add(e);
                }
            }
            /*-------------------------------------------------------------------------*/
            //for (int i = 0; i < P.Count; i++)
            //{
            //    edge e = new edge();
            //    e.StartState.StateName = P[i][0].StateName;
            //    e.TransSymbol = 'a';
            //    e.EndState.StateName = P[i][0].anext;
            //    m.EdgeSet.Add(e);
            //}

            //for (int i = 0; i < P.Count; i++)
            //{
            //    edge e = new edge();
            //    e.StartState.StateName = P[i][0].StateName;
            //    e.TransSymbol = 'b';
            //    e.EndState.StateName = P[i][0].bnext;
            //    m.EdgeSet.Add(e);
            //}
            /*-------------------------------------------------------------------------*/
            m.StartState.StateName = 0;
            m.EndState.StateName   = DFA.EndState.StateName;
            m.EdgeCount            = m.EdgeSet.Count;
            return(m);
        }
Exemplo n.º 12
0
        private cell MakeCell(char symbol)
        {
            cell NewCell = new cell();

            NewCell.EdgeCount = 0;
            edge NewEdge = new edge();
            //新节点
            state StartState = new state();
            state EndState   = new state();

            StartState.StateName = ++stateName;
            EndState.StateName   = ++stateName;
            //新边,加入两个新节点
            NewEdge.StartState  = StartState;
            NewEdge.EndState    = EndState;
            NewEdge.TransSymbol = symbol;
            //构建新单元
            NewCell.EdgeSet.Add(NewEdge);
            NewCell.StartState = NewCell.EdgeSet[0].StartState;
            NewCell.EndState   = NewCell.EdgeSet[0].EndState;
            NewCell.EdgeCount  = NewCell.EdgeSet.Count;
            return(NewCell);
        }
Exemplo n.º 13
0
        private void Calculation()
        {
            cell c = new cell();

            for (int i = 0; i < output.Count; i++)
            {
                if (output[i] == '+')
                {
                    List <cell> para = new List <cell>();
                    for (int j = 0; j < 2; j++)
                    {
                        para.Add(calculate.Pop());          //查看计算过的Cell
                    }
                    calculate.Push(PLUS(para[1], para[0])); //左->右
                }
                else if (output[i] == '|')
                {
                    List <cell> para = new List <cell>();
                    for (int j = 0; j < 2; j++)
                    {
                        para.Add(calculate.Pop());
                    }
                    calculate.Push(OR(para[0], para[1]));
                }
                else if (output[i] == '*')
                {
                    List <cell> para = new List <cell>();

                    para.Add(calculate.Pop());
                    calculate.Push(CLOSURE(para[0]));
                }
                else
                {
                    calculate.Push(MakeCell(output[i]));
                }
            }
        }
Exemplo n.º 14
0
        private void NFAToDFA(char[] ch)
        {
            if (READFILE == false)
            {
                NormalToNFA(ch);
            }

            CloseSet C  = new CloseSet();
            TSet     T0 = new TSet();
            //T0.tSet.Add(NFAb);
            List <int> t = new List <int>();

            t.Add(NFAb);
            T0 = E_Closure(t);

            T0.tSet.Sort();
            //T0.Unrepeated();
            C.CSet.Add(T0);



            while (C.Check() != true)
            {
                int  order = C.Sign();
                TSet Ta    = new TSet();
                TSet Tb    = new TSet();

                List <TSet> Tedges = new List <TSet>();

                TSet T = new TSet();
                /*-------------------------------------------------------------------------*/
                for (int i = 0; i < C.CSet[order].tSet.Count; i++)
                {
                    T.tSet.Add(C.CSet[order].tSet[i]);
                }

                for (int i = 0; i < transymbol.Count; i++)
                {
                    TSet temp = E_Closure(Move(T, transymbol[i]));
                    temp.tSet.Sort();
                    Tedges.Add(temp);
                }

                for (int i = 0; i < transymbol.Count; i++)
                {
                    if (C.JoinNew(Tedges[i]) == -1)
                    {
                        C.CSet.Add(Tedges[i]);
                        C.CSet[order].next[i] = C.CSet.Count - 1;
                    }
                    else
                    {
                        C.CSet[order].next[i] = C.JoinNew(Tedges[i]);
                    }
                }
            }

            MFAset = C;
            DFAes.Add(MFAset.CSet.Count - 1);
            DFA = MakeDFAcell(C);

            cell   result = DFA;
            string output = null;

            output = output + "开始状态    接受符号    到达状态\n";
            for (int i = 0; i < result.EdgeCount; i++)
            {
                string s = String.Format("{0,-12}{1,-12}{2,-12}\n", result.EdgeSet[i].StartState.StateName, result.EdgeSet[i].TransSymbol, result.EdgeSet[i].EndState.StateName);
                output = output + s;
            }

            richTextBox2.Text = output;

            DFAb          = DFA.StartState.StateName;
            DFAe          = DFA.EndState.StateName;
            textBox4.Text = DFAb.ToString();
            textBox5.Text = DFAe.ToString();

            if (READFILE == false)
            {
                textBox5.Text = DFAe.ToString();
            }
            else
            {
                List <int> ends = new List <int>();
                DFAes = ends;
                for (int i = 0; i < C.CSet.Count; i++)
                {
                    if (C.CSet[i].tSet.Contains(NFAe) == true)
                    {
                        ends.Add(i);
                    }
                }
                string d = String.Join(",", ends.ToArray());
                textBox5.Text = d;
            }
        }
Exemplo n.º 15
0
        private void NormalToNFA(char[] ch)
        {
            List <char> chList = new List <char>();


            for (int i = 0; i < ch.Length; i++)
            {
                chList.Add(ch[i]);
            }
            transymbol.Clear();
            transymbol.AddRange(chList.Distinct());

            for (int i = 0; i < transymbol.Count; i++)
            {
                if (transymbol[i] < 'a' || transymbol[i] > 'z')
                {
                    transymbol.RemoveAt(i);
                    i--;
                }
            }

            int len = chList.Count;

            /*
             *
             * 插入加号
             * 加号表示左->右
             * 考虑 a+b a* ()左右两边的情况
             *
             */
            for (int i = 0; i < chList.Count; i++)
            {
                if (chList[i] == '(' && i > 0)
                {
                    if (chList[i - 1] != '(')
                    {
                        chList.Insert(i, '+');
                        i++;
                    }
                }
                else if (transymbol.Contains(chList[i]) && i != 0)
                {
                    if (transymbol.Contains(chList[i - 1]) || chList[i - 1] == ')' || chList[i - 1] == '*')
                    {
                        chList.Insert(i, '+');
                        i++;
                    }
                }
                else if (transymbol.Contains(chList[i]) && i != 0)
                {
                    if (transymbol.Contains(chList[i - 1]) || chList[i - 1] == ')' || chList[i - 1] == '*')
                    {
                        chList.Insert(i, '+');
                        i++;
                    }
                }
            }

            string str = String.Join("", chList);

            char[] newch = chList.ToArray();

            FrontToBack(newch);
            Calculation();

            NFA = calculate.Pop();
            cell   result = NFA;
            string output = null;

            output = output + "开始状态    接受符号    到达状态\n";
            for (int i = 0; i < result.EdgeCount; i++)
            {
                string s = String.Format("{0,-12}{1,-12}{2,-12}\n", result.EdgeSet[i].StartState.StateName, result.EdgeSet[i].TransSymbol, result.EdgeSet[i].EndState.StateName);
                output = output + s;
            }

            richTextBox1.Text = output;

            NFAb          = NFA.StartState.StateName;
            NFAe          = NFA.EndState.StateName;
            textBox2.Text = NFAb.ToString();
            textBox3.Text = NFAe.ToString();
        }
Exemplo n.º 16
0
        private void NFAToDFA(char[] ch)
        {
            NormalToNFA(ch);
            CloseSet C  = new CloseSet();
            TSet     T0 = new TSet();
            //T0.tSet.Add(NFAb);
            List <int> t = new List <int>();

            t.Add(NFAb);
            T0 = E_Closure(t);

            T0.tSet.Sort();
            //T0.Unrepeated();
            C.CSet.Add(T0);



            while (C.Check() != true)
            {
                int  order = C.Sign();
                TSet Ta    = new TSet();
                TSet Tb    = new TSet();

                List <TSet> Tedges = new List <TSet>();

                TSet T = new TSet();
                /*-------------------------------------------------------------------------*/
                for (int i = 0; i < C.CSet[order].tSet.Count; i++)
                {
                    T.tSet.Add(C.CSet[order].tSet[i]);
                }

                for (int i = 0; i < transymbol.Count; i++)
                {
                    TSet temp = E_Closure(Move(T, transymbol[i]));
                    temp.tSet.Sort();
                    Tedges.Add(temp);
                }

                for (int i = 0; i < transymbol.Count; i++)
                {
                    if (C.JoinNew(Tedges[i]) == -1)
                    {
                        C.CSet.Add(Tedges[i]);
                        C.CSet[order].next[i] = C.CSet.Count - 1;
                    }
                    else
                    {
                        C.CSet[order].next[i] = C.JoinNew(Tedges[i]);
                    }
                }
                /*-------------------------------------------------------------------------*/
                //Ta = E_Closure(Move(T,'a'));
                //Tb = E_Closure(Move(T,'b'));
                //Ta.tSet.Sort();
                //Tb.tSet.Sort();

                //if (C.JoinNew(Ta) == -1)
                //{
                //    C.CSet.Add(Ta);
                //    C.CSet[order].anext = C.CSet.Count-1;
                //}
                //else
                //{
                //    C.CSet[order].anext = C.JoinNew(Ta);
                //}
                //if (C.JoinNew(Tb) == -1)
                //{

                //    C.CSet.Add(Tb);
                //    C.CSet[order].bnext = C.CSet.Count - 1;
                //}
                //else
                //{
                //    C.CSet[order].bnext = C.JoinNew(Tb);
                //}
                /*-------------------------------------------------------------------------*/
            }

            MFAset = C;
            DFA    = MakeDFAcell(C);

            cell   result = DFA;
            string output = null;

            output = output + "开始状态    接受符号    到达状态\n";
            for (int i = 0; i < result.EdgeCount; i++)
            {
                string s = String.Format("{0,-12}{1,-12}{2,-12}\n", result.EdgeSet[i].StartState.StateName, result.EdgeSet[i].TransSymbol, result.EdgeSet[i].EndState.StateName);
                output = output + s;
            }

            richTextBox2.Text = output;

            DFAb          = DFA.StartState.StateName;
            DFAe          = DFA.EndState.StateName;
            textBox4.Text = DFAb.ToString();
            textBox5.Text = DFAe.ToString();
        }