示例#1
0
        /// <summary>
        /// 获取开始和结束节点
        /// </summary>
        /// <returns>开始和结束节点组成的数组</returns>
        public int[] GetStartEndNode()
        {
            int[] StartEndNode = new int[2];
            NFAM  nfa1         = (NFAM)StartEndStatesStack.Pop();

            StartEndNode[0] = nfa1.startstatus;
            StartEndNode[1] = nfa1.endstatus;
            return(StartEndNode);
        }
示例#2
0
        /// <summary>
        /// 根据查表得到的优先级构造相应的状态
        /// </summary>
        /// <param name="compare">优先级关系</param>
        /// <param name="str">正规式字符串</param>
        private void Construction(char compare, string str)
        {
            switch (compare)
            {
            case '<':
                SignStack.Push(ch);
                if (index < str.Length - 1)
                {
                    index++;
                }
                break;

            case '>':
                switch (SignStack.Pop())
                {
                case '.':
                    NFAM nfa2 = (NFAM)StartEndStatesStack.Pop();
                    NFAM nfa3 = (NFAM)StartEndStatesStack.Pop();
                    NFAM nfa4 = new NFAM(nfa3.endstatus, nfa2.startstatus, 'ε');
                    NfamQueue.Enqueue(nfa4);
                    NFAM nfa5 = new NFAM(nfa3.startstatus, nfa2.endstatus, '#');
                    StartEndStatesStack.Push(nfa5);
                    break;

                case '|':
                    NFAM nfa6 = (NFAM)StartEndStatesStack.Pop();
                    NFAM nfa7 = (NFAM)StartEndStatesStack.Pop();
                    //构造或运算剩下的4条弧
                    NFAM nfa8  = new NFAM(nfa6.endstatus + 1, nfa7.startstatus, 'ε');
                    NFAM nfa9  = new NFAM(nfa6.endstatus + 1, nfa6.startstatus, 'ε');
                    NFAM nfa10 = new NFAM(nfa7.endstatus, nfa6.endstatus + 2, 'ε');
                    NFAM nfa11 = new NFAM(nfa6.endstatus, nfa6.endstatus + 2, 'ε');
                    //新构造的弧入队列
                    NfamQueue.Enqueue(nfa8);
                    NfamQueue.Enqueue(nfa9);
                    NfamQueue.Enqueue(nfa10);
                    NfamQueue.Enqueue(nfa11);
                    NFAM nfa12 = new NFAM(nfa6.endstatus + 1, nfa6.endstatus + 2, '#');
                    //起始节点入栈
                    StartEndStatesStack.Push(nfa12);
                    break;
                }
                break;

            case '=':
                SignStack.Pop();
                if (index < str.Length - 1)
                {
                    index++;
                }
                break;
            }
        }
示例#3
0
        /// <summary>
        /// 分析正规式并构造相应的NFAM
        /// </summary>
        /// <param name="str">输入一个正规式</param>
        /// <returns>构造的NFA</returns>
        private Queue <NFAM> AnalyseRegex(string str)
        {
            str += "#";
            ch   = str[index];
            int  start   = 0;
            int  end     = 1;
            char compare = '=';
            int  m       = 0;
            int  n       = 0;

            do
            {
                ch = str[index];
                if (Char.IsLetterOrDigit(ch))
                {
                    if (StartEndStatesStack.Count == 0)
                    {
                        NFAM nfa = new NFAM(start, end, ch);
                        StartEndStatesStack.Push(nfa); //入状态栈
                        NfamQueue.Enqueue(nfa);        //入队列
                    }
                    else
                    {
                        end = ((NFAM)StartEndStatesStack.Peek()).endstatus;
                        NFAM nfa = new NFAM(end + 1, end + 2, ch);
                        StartEndStatesStack.Push(nfa); //入状态栈
                        NfamQueue.Enqueue(nfa);        //弧入队列
                    }
                    if (index < str.Length - 1)
                    {
                        index++;
                    }
                }
                else if (str[index] == '*')
                {
                    NFAM nfa13 = (NFAM)StartEndStatesStack.Pop();
                    //构造4条空弧
                    NFAM nfa14 = new NFAM(nfa13.endstatus, nfa13.startstatus, 'ε');

                    NFAM nfa15 = new NFAM(nfa13.endstatus + 1, nfa13.startstatus, 'ε');

                    NFAM nfa16 = new NFAM(nfa13.endstatus, nfa13.endstatus + 2, 'ε');

                    NFAM nfa17 = new NFAM(nfa13.endstatus + 1, nfa13.endstatus + 2, 'ε');

                    NFAM nfa18 = new NFAM(nfa13.endstatus + 1, nfa13.endstatus + 2, '#');
                    //新弧入栈
                    NfamQueue.Enqueue(nfa14);
                    NfamQueue.Enqueue(nfa15);
                    NfamQueue.Enqueue(nfa16);
                    NfamQueue.Enqueue(nfa17);
                    //起始节点入队列
                    StartEndStatesStack.Push(nfa18);
                    if (index < str.Length - 1)
                    {
                        index++;
                    }
                }
                else
                {
                    #region                     //查表
                    for (int i = 0; i < 6; i++) //i
                    {
                        if (SignStack.Peek() == TableOfPriority[i, 0])
                        {
                            m = i;
                            break;
                        }
                    }
                    for (int j = 0; j < 6; j++)//i+1
                    {
                        if (str[index] == TableOfPriority[0, j])
                        {
                            n = j;
                            break;
                        }
                    }
                    #endregion
                    compare = TableOfPriority[m, n];
                    Construction(compare, str);
                }
            }while (SignStack.Peek() != '#' || str[index] != '#');
            return(NfamQueue);
        }