Пример #1
0
        static void MovingMatrixTest()
        {
            MovingMatrix m = new MovingMatrix();

            char?[] trueRez = { null, '=', null, '>', '<' };
            Debug.Assert(trueRez[0].Equals(m.GetMove(SpecialSymbs.START_SYMB, SpecialSymbs.END_SYMB)));
            Debug.Assert(trueRez[1].Equals(m.GetMove("for", "(")));
            Debug.Assert(trueRez[2].Equals(m.GetMove("a", "a")));
            Debug.Assert(trueRez[3].Equals(m.GetMove(";", ")")));
            Debug.Assert(trueRez[4].Equals(m.GetMove(";", "for")));
        }
Пример #2
0
        /// <summary>
        /// Запуск основного алгоритма синтаксического анализатора - анализа синтаксиса.
        /// </summary>
        /// <returns></returns>
        public List <OutputTreeCell> DoAnalysis()
        {
            //Logger.Debug("Analysis started!");
            string inputLex = null;
            string stackLex = null;

            lexemTable.Add(new LexemDataCell(0, SpecialSymbs.END_SYMB, LexemType.Splitter));
            LexemDataCell[] lexemArray = lexemTable.ToArray();
            for (int i = 0; i < lexemArray.Length; i++)
            {
                LexemDataCell cell = lexemArray[i];

                string inputInner = string.Join(" ", this.InnerOfLexemArrayToStringArray(lexemArray, i));
                string stackInner = string.Join(" ", stack.StackToArray());
                //Logger.Info("Input = {0} || Stack = {1}\n", inputInner, stackInner);

                inputLex = cell.Lexem;

                // Некоторые замены для алгоритма
                if (cell.LexType.Equals(LexemType.Identificator))
                {
                    inputLex = "a";
                }
                if (cell.LexType.Equals(LexemType.Number_Constant))
                {
                    inputLex = "a";
                }
                if (cell.LexType.Equals(LexemType.Char_Constant))
                {
                    inputLex = "a";
                }

                stackLex = stack.GetFirstTerminalSymb();

                if (stackLex.Equals(SpecialSymbs.START_SYMB) & inputLex.Equals(SpecialSymbs.END_SYMB))
                {
                    break;
                }

                char?move = movingMatrix.GetMove(stackLex, inputLex);

                //Logger.Info("{0} {1} {2}\n", stackLex, move, inputLex);

                try
                {
                    switch (move)
                    {
                    case '=':
                        this.MakeShifting(inputLex);
                        break;

                    case '<':
                        this.MakeShifting(inputLex);
                        break;

                    case '>':
                        this.MakeRollingUp();
                        i -= 1;
                        break;

                    default:
                        if (inputLex.Equals(SpecialSymbs.END_SYMB))
                        {
                            MakeRollingUp();
                            break;
                        }
                        throw new Exception(
                                  "Нарушен синтаксис входного языка: связки слов " + stackLex + " + " + inputLex + " не предусмотрено!");
                    }
                } catch (Exception ex) {
                    Console.WriteLine("Message: {0}\nStaackTracke{1}", ex.Message, ex.StackTrace);
                    //Logger.Error(ex,
                    //"Во время очередного сдвига-свёртки произошла ошибка. Работа программы прекращена!\nMessage = {0}\nStack-trace:\n{1}\n", ex.Message, ex.StackTrace);
                    return(null);
                }
            }

            stackLex = stack.GetFirstTerminalSymb();

            if (!stackLex.Equals(SpecialSymbs.START_SYMB))
            {
                throw new Exception("Нарушен синтаксис входного языка: непредусмотренный обрыв конструкции!");
            }
            else
            {
                //Logger.Info("Разбор успешно окончен!\n");
            }

            return(this.BuildOutputTree());
        }