static void SymbolsStackTest() { SymbolsStack s = new SymbolsStack(); string[] strs = { "1", "2", "3", "4", "5" }; s.Push(strs[0]); s.Push(strs[1]); Debug.Assert(s.Lenght == 2); string poped = s.Pop(); Debug.Assert(poped.Equals(strs[1])); Debug.Assert(s.Lenght == 1); s.Push(strs[1]); s.Push(strs[2]); Debug.Assert(s.Lenght == 3); string getted = s.GetFirstTerminalSymb(); Debug.Assert(s.Lenght == 3); Debug.Assert(getted.Equals(strs[2])); s.Push(SpecialSymbs.NOT_TERMINAL_SYMB); Debug.Assert(s.Lenght == 4); getted = s.GetFirstTerminalSymb(); Debug.Assert(s.Lenght == 4); Debug.Assert(getted.Equals(strs[2])); s = new SymbolsStack(); for (int i = 0; i < 100; i++) { s.Push(Convert.ToString(i)); } Debug.Assert(s.Lenght == 100); for (int i = 99; i >= 0; i--) { Debug.Assert(Convert.ToString(i).Equals(s.Pop())); } Debug.Assert(s.Lenght == 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()); }