示例#1
0
        public void ArithmeticStatementParseTest()
        {
            const string INPUT = @"2*(3+4)";//@"2*(3+4)$"
            ActionTableEntry[,] actionTable;
            int[,] gotoTable;
            string fileName;
            ILexer lexer;
            Stream fileStream;
            RegExpTokenizer tokenizer;

            fileName = Path.GetTempFileName();
            File.WriteAllText(fileName, INPUT);
            tokenizer = new RegExpTokenizer();
            tokenizer.SetTransitionFunction(new TableDrivenTransitionFunction());
            lexer = new Lexer(tokenizer);
            fileStream = File.OpenRead(fileName);
            lexer.SetDataSource(fileStream);

            actionTable = new ActionTableEntry[12, 6];
            #region Populating Action Table

            actionTable[0, 1].Action = ParserAction.Shift;
            actionTable[0, 1].Destination = 5;
            actionTable[0, 4].Action = ParserAction.Shift;
            actionTable[0, 4].Destination = 4;
            actionTable[1, 0].Action = ParserAction.Accept; // <-- !!!!
            actionTable[1, 2].Action = ParserAction.Shift;
            actionTable[1, 2].Destination = 6;
            actionTable[2, 0].Action = ParserAction.Reduce;
            actionTable[2, 0].Destination = 2;
            actionTable[2, 2].Action = ParserAction.Reduce;
            actionTable[2, 2].Destination = 2;
            actionTable[2, 3].Action = ParserAction.Shift;
            actionTable[2, 3].Destination = 7;
            actionTable[2, 5].Action = ParserAction.Reduce;
            actionTable[2, 5].Destination = 2;
            actionTable[3, 0].Action = ParserAction.Reduce;
            actionTable[3, 0].Destination = 4;
            actionTable[3, 2].Action = ParserAction.Reduce;
            actionTable[3, 2].Destination = 4;
            actionTable[3, 3].Action = ParserAction.Reduce;
            actionTable[3, 3].Destination = 4;
            actionTable[3, 5].Action = ParserAction.Reduce;
            actionTable[3, 5].Destination = 4;
            actionTable[4, 1].Action = ParserAction.Shift;
            actionTable[4, 1].Destination = 5;
            actionTable[4, 4].Action = ParserAction.Shift;
            actionTable[4, 4].Destination = 4;
            actionTable[5, 0].Action = ParserAction.Reduce;
            actionTable[5, 0].Destination = 6;
            actionTable[5, 2].Action = ParserAction.Reduce;
            actionTable[5, 2].Destination = 6;
            actionTable[5, 3].Action = ParserAction.Reduce;
            actionTable[5, 3].Destination = 6;
            actionTable[5, 5].Action = ParserAction.Reduce;
            actionTable[5, 5].Destination = 6;
            actionTable[6, 1].Action = ParserAction.Shift;
            actionTable[6, 1].Destination = 5;
            actionTable[6, 4].Action = ParserAction.Shift;
            actionTable[6, 4].Destination = 4;
            actionTable[7, 1].Action = ParserAction.Shift;
            actionTable[7, 1].Destination = 5;
            actionTable[7, 4].Action = ParserAction.Shift;
            actionTable[7, 4].Destination = 4;
            actionTable[8, 2].Action = ParserAction.Shift;
            actionTable[8, 2].Destination = 6;
            actionTable[8, 5].Action = ParserAction.Shift;
            actionTable[8, 5].Destination = 11;
            actionTable[9, 0].Action = ParserAction.Reduce;
            actionTable[9, 0].Destination = 1;
            actionTable[9, 2].Action = ParserAction.Reduce;
            actionTable[9, 2].Destination = 1;
            actionTable[9, 3].Action = ParserAction.Shift;
            actionTable[9, 3].Destination = 7;
            actionTable[9, 5].Action = ParserAction.Reduce;
            actionTable[9, 5].Destination = 1;
            actionTable[10, 0].Action = ParserAction.Reduce;
            actionTable[10, 0].Destination = 3;
            actionTable[10, 2].Action = ParserAction.Reduce;
            actionTable[10, 2].Destination = 3;
            actionTable[10, 3].Action = ParserAction.Reduce;
            actionTable[10, 3].Destination = 3;
            actionTable[10, 5].Action = ParserAction.Reduce;
            actionTable[10, 5].Destination = 3;
            actionTable[11, 0].Action = ParserAction.Reduce;
            actionTable[11, 0].Destination = 5;
            actionTable[11, 2].Action = ParserAction.Reduce;
            actionTable[11, 2].Destination = 5;
            actionTable[11, 3].Action = ParserAction.Reduce;
            actionTable[11, 3].Destination = 5;
            actionTable[11, 5].Action = ParserAction.Reduce;
            actionTable[11, 5].Destination = 5;
            //actionTable[0, 0].Action = ParserAction.Shift;
            //actionTable[0, 0].Destination = 5;
            //actionTable[0, 3].Action = ParserAction.Shift;
            //actionTable[0, 3].Destination = 4;
            //actionTable[1, 1].Action = ParserAction.Shift;
            //actionTable[1, 1].Destination = 6;
            //actionTable[1, 5].Action = ParserAction.Accept; // <-- !!!!
            //actionTable[2, 1].Action = ParserAction.Reduce;
            //actionTable[2, 1].Destination = 2;
            //actionTable[2, 2].Action = ParserAction.Shift;
            //actionTable[2, 2].Destination = 7;
            //actionTable[2, 4].Action = ParserAction.Reduce;
            //actionTable[2, 4].Destination = 2;
            //actionTable[2, 5].Action = ParserAction.Reduce;
            //actionTable[2, 5].Destination = 2;
            //actionTable[3, 1].Action = ParserAction.Reduce;
            //actionTable[3, 1].Destination = 4;
            //actionTable[3, 2].Action = ParserAction.Reduce;
            //actionTable[3, 2].Destination = 4;
            //actionTable[3, 4].Action = ParserAction.Reduce;
            //actionTable[3, 4].Destination = 4;
            //actionTable[3, 5].Action = ParserAction.Reduce;
            //actionTable[3, 5].Destination = 4;
            //actionTable[4, 0].Action = ParserAction.Shift;
            //actionTable[4, 0].Destination = 5;
            //actionTable[4, 3].Action = ParserAction.Shift;
            //actionTable[4, 3].Destination = 4;
            //actionTable[5, 1].Action = ParserAction.Reduce;
            //actionTable[5, 1].Destination = 6;
            //actionTable[5, 2].Action = ParserAction.Reduce;
            //actionTable[5, 2].Destination = 6;
            //actionTable[5, 4].Action = ParserAction.Reduce;
            //actionTable[5, 4].Destination = 6;
            //actionTable[5, 5].Action = ParserAction.Reduce;
            //actionTable[5, 5].Destination = 6;
            //actionTable[6, 0].Action = ParserAction.Shift;
            //actionTable[6, 0].Destination = 5;
            //actionTable[6, 3].Action = ParserAction.Shift;
            //actionTable[6, 3].Destination = 4;
            //actionTable[7, 0].Action = ParserAction.Shift;
            //actionTable[7, 0].Destination = 5;
            //actionTable[7, 3].Action = ParserAction.Shift;
            //actionTable[7, 3].Destination = 4;
            //actionTable[8, 1].Action = ParserAction.Shift;
            //actionTable[8, 1].Destination = 6;
            //actionTable[8, 4].Action = ParserAction.Shift;
            //actionTable[8, 4].Destination = 11;
            //actionTable[9, 1].Action = ParserAction.Reduce;
            //actionTable[9, 1].Destination = 1;
            //actionTable[9, 2].Action = ParserAction.Shift;
            //actionTable[9, 2].Destination = 7;
            //actionTable[9, 4].Action = ParserAction.Reduce;
            //actionTable[9, 4].Destination = 1;
            //actionTable[9, 5].Action = ParserAction.Reduce;
            //actionTable[9, 5].Destination = 1;
            //actionTable[10, 1].Action = ParserAction.Reduce;
            //actionTable[10, 1].Destination = 3;
            //actionTable[10, 2].Action = ParserAction.Reduce;
            //actionTable[10, 2].Destination = 3;
            //actionTable[10, 4].Action = ParserAction.Reduce;
            //actionTable[10, 4].Destination = 3;
            //actionTable[10, 5].Action = ParserAction.Reduce;
            //actionTable[10, 5].Destination = 3;
            //actionTable[11, 1].Action = ParserAction.Reduce;
            //actionTable[11, 1].Destination = 5;
            //actionTable[11, 2].Action = ParserAction.Reduce;
            //actionTable[11, 2].Destination = 5;
            //actionTable[11, 4].Action = ParserAction.Reduce;
            //actionTable[11, 4].Destination = 5;
            //actionTable[11, 5].Action = ParserAction.Reduce;
            //actionTable[11, 5].Destination = 5;

            #endregion

            gotoTable = new int[12, 3];
            #region Populating Goto Table

            gotoTable[0, 0] = 1;
            gotoTable[0, 1] = 2;
            gotoTable[0, 2] = 3;
            gotoTable[4, 0] = 8;
            gotoTable[4, 1] = 2;
            gotoTable[4, 2] = 3;
            gotoTable[6, 1] = 9;
            gotoTable[6, 2] = 3;
            gotoTable[7, 2] = 10;

            #endregion

            var stack = new Stack<int>();
            var parser = new LRParser();

            var E = new NonTerminal("E");
            var T = new NonTerminal("T");
            var F = new NonTerminal("F");

            var id = tokenizer.UseTerminal(RegExp.AtLeastOneOf(RegExp.Choice(
                RegExp.Literal('0'), RegExp.Literal('1'), RegExp.Literal('2'),
                RegExp.Literal('3'), RegExp.Literal('4'), RegExp.Literal('5'),
                RegExp.Literal('6'), RegExp.Literal('7'), RegExp.Literal('8'),
                RegExp.Literal('9'))));
            var plus = tokenizer.UseTerminal(RegExp.Literal('+'));
            var mult = tokenizer.UseTerminal(RegExp.Literal('*'));
            var leftBrace = tokenizer.UseTerminal(RegExp.Literal('('));
            var rightBrace = tokenizer.UseTerminal(RegExp.Literal(')'));

            tokenizer.BuildTransitions();

            var grammar = new AugmentedGrammar()
                              {
                                  E --> E & plus & T ^ (v => stack.Push(stack.Pop() + stack.Pop())),
                                  E --> T,
                                  T --> T & mult & F ^ (v => stack.Push(stack.Pop() * stack.Pop())),
                                  T --> F,
                                  F --> leftBrace & E & rightBrace,
                                  F --> id ^ (v => stack.Push(v[id].AsInt()))
                              };

            Console.WriteLine("Grammar is being tested: \n{0}", grammar);
            Console.WriteLine("Input is being parsed: {0}\n", INPUT);
            Console.WriteLine("Parsing process:\n");
            parser.Grammar = grammar;
            parser.Lexer = lexer;
            parser.ParsingTable = new LRParsingTable(actionTable, gotoTable);
            parser.InputAccepted += (sender, eventArgs) => Console.WriteLine("Accepted!");
            parser.ParseInput();

            Assert.That(stack.Pop(), Is.EqualTo(14));
            Assert.That(stack.Count, Is.EqualTo(0));

            fileStream.Close();
            File.Delete(fileName);
        }
示例#2
0
 internal SLRParsingTableBuilder(ActionTableEntry[,] actionTable, int[,] gotoTable)
     : this()
 {
     parsingTable.ActionTable = actionTable;
     parsingTable.GotoTable = gotoTable;
 }
示例#3
0
        private void Trace(ActionTableEntry action, Token current)
        {
            string actionStr = string.Empty;

            switch (action.Action)
            {
                case ParserAction.Reduce:
                    actionStr = "reduce by " + Grammar.Productions[action.Destination];
                    break;
                case ParserAction.Shift:
                    actionStr = "shift " + action.Destination;
                    break;
                case ParserAction.Error:
                    actionStr = "error";
                    break;
                case ParserAction.Accept:
                    actionStr = "accept";
                    break;
            }

            string stack = string.Empty;
            Array.ForEach(mStatesStack.ToArray(), i => stack = i + " " + stack);

            Console.WriteLine("{0,10}|{1,10}|{2}", stack, current, actionStr);
        }