Esempio n. 1
0
        public void TestShiftReduceError()
        {
            INonTerminal <string> ifStatement = null;

            try
            {
                // This configuration is not a valid LR1 parser. It contains shift reduce conflicts
                // clasical dangling else case
                var configurator = ParserFactory.Configure <string>();
                var ident        = configurator.CreateTerminal("[a-z]+");
                ident.DebugName = "ident";

                ifStatement           = configurator.CreateNonTerminal();
                ifStatement.DebugName = "ifStatement";

                var statement = configurator.CreateNonTerminal();
                statement.DebugName = "statement";

                ifStatement.AddProduction("if", "\\(", ident, "\\)", "then", statement);
                ifStatement.AddProduction("if", "\\(", ident, "\\)", "then", statement, "else", statement);

                statement.AddProduction(ifStatement);
                statement.AddProduction(ident, "=", ident);

                configurator.LexerSettings.CreateLexer = false;
                configurator.CreateParser();

                Assert.Fail("No exception for ambiguous grammar");
            }
            catch (ShiftReduceConflictException <string> e)
            {
                Assert.AreEqual(ifStatement, e.ReduceSymbol);
                Assert.AreEqual("else", e.ShiftSymbol.DebugName);
            }
        }
Esempio n. 2
0
        public void TestACalculator()
        {
            // This is a full on integration test that builds a parser and performs a simple calculation.
            var configurator = ParserFactory.Configure <int>();

            ITerminal <int> number = configurator.CreateTerminal("\\d+", int.Parse);

            number.DebugName = "number";

            INonTerminal <int> expr = configurator.CreateNonTerminal();

            expr.DebugName = "expr";
            INonTerminal <int> term = configurator.CreateNonTerminal();

            term.DebugName = "term";
            INonTerminal <int> factor = configurator.CreateNonTerminal();

            factor.DebugName = "factor";

            expr.AddProduction(expr, "+", term).SetReduceFunction(s => s[0] + s[2]);
            expr.AddProduction(expr, "-", term).SetReduceFunction(s => s[0] - s[2]);
            expr.AddProduction(term).SetReduceFunction(s => s[0]);

            term.AddProduction(term, "*", factor).SetReduceFunction(s => s[0] * s[2]);
            term.AddProduction(term, "/", factor).SetReduceFunction(s => s[0] / s[2]);
            term.AddProduction(factor).SetReduceFunction(s => s[0]);

            factor.AddProduction(number).SetReduceFunction(s => s[0]);
            factor.AddProduction("(", expr, ")").SetReduceFunction(s => s[1]);

            var parser = configurator.CreateParser();
            int result = parser.Parse(new StringReader("2-2-5"));

            Assert.AreEqual(-5, result);
        }
Esempio n. 3
0
        public void TestCanMultiplyDefineTerminalStringsInConfiguration()
        {
            var configurator = ParserFactory.Configure <int>();
            INonTerminal <int> nonTerminal = configurator.CreateNonTerminal();

            nonTerminal.DebugName = "NonTerm";
            nonTerminal.AddProduction("this", "is", "a", "string");
            nonTerminal.AddProduction("this", "is", "a", "test");

            var parser = configurator.CreateParser();

            Assert.IsNotNull(parser);
        }
Esempio n. 4
0
        public void TestReduceReduceConflict()
        {
            // Represents the grammar
            //
            // x := t | y
            // t := "A"
            // y := "A"
            //
            INonTerminal <object> y = null, t = null;

            try
            {
                var configurator        = ParserFactory.Configure <object>();
                INonTerminal <object> x = configurator.CreateNonTerminal();
                x.DebugName = "X";

                t           = configurator.CreateNonTerminal();
                t.DebugName = "T";

                y           = configurator.CreateNonTerminal();
                y.DebugName = "Y";

                x.AddProduction(t);
                x.AddProduction(y);

                t.AddProduction("A");
                y.AddProduction("A");

                configurator.CreateParser();
                Assert.Fail();
            }
            catch (ReduceReduceConflictException <object> e)
            {
                Assert.AreEqual(y, e.NewReduceSymbol);
                Assert.AreEqual(t, e.PreviousReduceSymbol);
            }
        }
 public static void AddReduceProduction <T>(this INonTerminal <T> symb, params object[] args) => symb.AddProduction(args).SetReduceToFirst();
Esempio n. 6
0
 public ProductionWrapper <TResult> Add()
 {
     return(new ProductionWrapper <TResult>(_nonTerminal.AddProduction()));
 }