示例#1
0
        public static void PrintArguments(object options)
        {
            var type  = options.GetType();
            var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            var printer = new TestWriter();

            if (props.Length == 0)
            {
                return;
            }
            var length = props.Max(p => p.Name.Length);

            printer.WriteLine("/green", title);

            printer.WriteLine("Executing", "/yellow", type.Name, "/reset", "with arguments:");
            foreach (var propertyInfo in props)
            {
                var name = propertyInfo.Name;
                name = name + new string(' ', length - name.Length);
                var value = propertyInfo.GetValue(options);

                printer.WriteLine("  ", "/yellow", name, "/reset", " = ", "/gray", value);
            }
        }
示例#2
0
        public void StanfordReduceReduceConflictGrammar()
        {
            var grammar = StanfordReduceReduceConflict.GetGrammar();

            var writer = new TestWriter();

            writer.WriteLine(grammar.ToString());
            writer.WriteLine();

            grammar.PrintFirstAndFollowSets(new TestWriter());
            writer.WriteLine();
            writer.WriteLine();

            // Grammar is not LR(0)
            grammar.ComputeLr0ParsingTable().AnyConflicts.ShouldBeTrue();

            var slrParser = grammar.ComputeSlrParsingTable();

            // The grammar is SLR(1)
            slrParser.AnyConflicts.ShouldBeTrue();

            slrParser.PrintParsingTable(writer);
            writer.WriteLine();
            writer.WriteLine();

            foreach (var conflict in slrParser.Conflicts)
            {
                writer.WriteLine(conflict);
                writer.WriteLine(
                    $"In state {conflict.State}: {slrParser.GetItems(conflict.State).KernelItems.ToVectorString()} (kernel items)");
            }

            writer.WriteLine();
            writer.WriteLine();
        }
示例#3
0
        public void TestColourClearing()
        {
            const OmniColours colour = OmniColours.BrightCyan;
            var initialColour        = TestWriter.GetDecoration();

            TestWriter.SetDecoration(new OmniDecoration(colour));

            TestWriter.Write("Test01");
            AssertLastColour(colour);

            TestWriter.Write("Test02");
            AssertLastColour(colour);

            TestWriter.ClearDecoration();
            AssertLastColour(initialColour);

            TestWriter.Write(colour, "Test03");
            AssertLastColour(initialColour);

            TestWriter.WriteLine("Test04");
            AssertLastColour(initialColour);

            var message = new ColourMessage();

            message.SetDecoration(new OmniDecoration(colour));
            message.AppendLine("Test05");
            TestWriter.Write(message);

            AssertEqual(initialColour, TestColours.Last());
        }
        public void StanfordGrammar()
        {
            var grammar = StanfordExprGrammar.GetGrammar();

            var writer = new TestWriter();

            // Grammar is LR(0)
            var lr0Parser = grammar.ComputeLr0ParsingTable();

            lr0Parser.AnyConflicts.ShouldBeFalse();
            writer.WriteLine("LR(0) table:");
            lr0Parser.PrintParsingTable(writer);

            // Grammar is SLR(1)
            var slrParser = grammar.ComputeSlrParsingTable();

            slrParser.AnyConflicts.ShouldBeFalse();
            writer.WriteLine("SLR(1) table:");
            slrParser.PrintParsingTable(writer);
        }
        public void CreateSlrTable()
        {
            var grammar = DragonBook_ExprGrammarCh4.GetGrammar();

            // Grammar is not LR(0)
            grammar.ComputeLr0ParsingTable().AnyConflicts.ShouldBeTrue();

            var parser = grammar.ComputeSlrParsingTable();

            // Grammar is not SLR(1)
            parser.AnyConflicts.ShouldBeFalse();

            var writer = new TestWriter();

            parser.PrintParsingTable(writer);
            writer.WriteLine();
            writer.WriteLine();

            // "a*a+a"
            var lexer = new FakeLexer <Sym>((Sym.ID, "a"), (Sym.ASTERISK, "*"), (Sym.ID, "a"), (Sym.PLUS, "+"),
                                            (Sym.ID, "a"));

            parser.Parse(lexer, writer);
        }
        public void DragonBookEx4_52()
        {
            var writer = new TestWriter();

            var grammar = DragonBookExample4_52.GetGrammar();

            //
            // LR(0)
            //

            var lr0Parser = grammar.ComputeLr0ParsingTable();

            WriteLine("LR(0) Parsing Table");
            lr0Parser.PrintParsingTable(writer);

            // The grammar is LR(0)
            lr0Parser.AnyConflicts.ShouldBeFalse();

            WriteLine("Moves of LR(0) parser");
            lr0Parser.Parse(DragonBookExample4_52.GetLexer("baab"), writer);

            //
            // SLR(1)
            //

            var slrParser = grammar.ComputeSlrParsingTable();

            WriteLine("SLR(1) Parsing Table");
            slrParser.PrintParsingTable(writer);

            // The grammar is SLR(1)
            slrParser.AnyConflicts.ShouldBeFalse();

            WriteLine("Moves of SLR(1) parser");
            slrParser.Parse(DragonBookExample4_52.GetLexer("baab"), writer);

            //
            // LR(1)
            //

            var lr1Parser = grammar.ComputeLr1ParsingTable();

            WriteLine("LR(1) Parsing Table");
            lr1Parser.PrintParsingTable(writer);

            // The grammar is LR(1)
            lr1Parser.AnyConflicts.ShouldBeFalse();

            WriteLine("Moves of LR(1) parser");
            lr1Parser.Parse(DragonBookExample4_52.GetLexer("baab"), writer);

            //
            // LALR(1)
            //

            var lalr1Parser = grammar.ComputeLalrParsingTable();

            WriteLine("LALR(1) Parsing Table");
            lalr1Parser.PrintParsingTable(writer);

            // TODO: Create utility method PrintAnyConflicts....and use it everywhere
            foreach (var conflict in lalr1Parser.Conflicts)
            {
                writer.WriteLine(conflict);
                // TODO: Show lookahead sets of items
                writer.WriteLine($"State {conflict.State}: {slrParser.GetItems(conflict.State).KernelItems.ToVectorString()} (kernel items)");
            }

            // The grammar is LALR(1)
            lalr1Parser.AnyConflicts.ShouldBeFalse();

            WriteLine("Moves of LALR(1) parser");
            lalr1Parser.Parse(DragonBookExample4_52.GetLexer("baab"), writer);
        }
        public void DragonBookEx4_48()
        {
            // NOTE: We are using nonterminal L for l-value (a location), nonterminal R for r-value (value
            //       that can be stored in a location), and terminal * for 'content-of' prefix operator.
            // 0: S' → S
            // 1: S → L = R
            // 2: S → R
            // 3: L → *R
            // 4: L → ID
            // 5: R → L
            Grammar <Sym, Var> grammar = DragonBookExample4_48.GetGrammar();

            var writer = new TestWriter();


            var lr0Dfa = grammar.GetLr0AutomatonDfa();

            lr0Dfa.PrintKernelItems(writer);

            grammar.PrintFirstAndFollowSets(writer);
            // ╔═══════════════════════════════════════════════════════════╗
            // ║                   First and Follow sets                   ║
            // ╠════════════╤════════════╤════════════════╤════════════════╣
            // ║  Variable  │  Nullable  │     First      │     Follow     ║
            // ╠════════════╪════════════╪════════════════╪════════════════╣
            // ║     S'     │   false    │ {ASTERISK, ID} │       {}       ║
            // ║     S      │   false    │ {ASTERISK, ID} │     {EOF}      ║
            // ║     R      │   false    │ {ASTERISK, ID} │  {EOF, EQUAL}  ║
            // ║     L      │   false    │ {ASTERISK, ID} │  {EQUAL, EOF}  ║
            // ╚════════════╧════════════╧════════════════╧════════════════╝

            grammar.Follow(grammar.V(Var.Start)).ShouldBeEmpty();
            grammar.Follow(grammar.V(Var.S)).ShouldSetEqual(grammar.Eof());
            grammar.Follow(grammar.V(Var.R)).ShouldSetEqual(grammar.T(Sym.EQUAL), grammar.Eof());
            grammar.Follow(grammar.V(Var.L)).ShouldSetEqual(grammar.T(Sym.EQUAL), grammar.Eof());

            WriteLine("SLR(1) Parsing Table");
            var slrParser = grammar.ComputeSlrParsingTable();

            // SLR(1) Parsing Table
            // ╔════════════════════════════════╗╔══════════════════════════╗
            // ║             ACTION             ║║           GOTO           ║
            // ╠════════╤═════╤═════╤═════╤═════╣╠════════╤═════╤═════╤═════╣
            // ║ State  │EQUAL│ ID  │ASTER│ EOF ║║ State  │  S  │  R  │  L  ║
            // ╠════════╪═════╪═════╪═════╪═════╣╠════════╪═════╪═════╪═════╣
            // ║   0    │     │ s5  │ s4  │     ║║   0    │  1  │  3  │  2  ║
            // ║   1    │     │     │     │ acc ║║   1    │     │     │     ║
            // ║   2    │ s6  │     │     │ r5  ║║   2    │     │     │     ║
            // ║   3    │     │     │     │ r2  ║║   3    │     │     │     ║
            // ║   4    │     │ s5  │ s4  │     ║║   4    │     │  7  │  8  ║
            // ║   5    │ r4  │     │     │ r4  ║║   5    │     │     │     ║
            // ║   6    │     │ s5  │ s4  │     ║║   6    │     │  9  │  8  ║
            // ║   7    │ r3  │     │     │ r3  ║║   7    │     │     │     ║
            // ║   8    │ r5  │     │     │ r5  ║║   8    │     │     │     ║
            // ║   9    │     │     │     │ r1  ║║   9    │     │     │     ║
            // ╚════════╧═════╧═════╧═════╧═════╝╚════════╧═════╧═════╧═════╝
            slrParser.PrintParsingTable(writer);

            // Grammar is not SLR(1)
            slrParser.AnyConflicts.ShouldBeTrue();
            slrParser.Conflicts.Count().ShouldBe(1);

            // This will print
            //      State 2: { shift 6, reduce 5} on 'EQUAL'
            //      State 2: { S → L•EQUAL R, R → L•} (kernel items)
            // The correct choice of the parser is to shift, because no right sentential form begins with....TODO
            // Therefore....TODO
            var conflict = slrParser.Conflicts.Single();

            writer.WriteLine(conflict);
            writer.WriteLine($"State {conflict.State}: {slrParser.GetItems(conflict.State).KernelItems.ToVectorString()} (kernel items)");

            // a=a$
            var lexer = new FakeLexer <Sym>((Sym.ID, "a"), (Sym.EQUAL, "="), (Sym.ID, "a"));

            slrParser.Parse(lexer, writer);
            //  ╔════════╤══════════════╤══════════════╤══════════╤══════════════════════════════════╗
            //  ║ SeqNo  │    Stack     │   Symbols    │  Input   │              Action              ║
            //  ╠════════╪══════════════╪══════════════╪══════════╪══════════════════════════════════╣
            //  ║  (1)   │ 0            │              │   a = a$ │ shift 5                          ║
            //  ║  (2)   │ 0 5          │ ID           │     = a$ │ reduce by L → ID, goto 2         ║
            //  ║  (3)   │ 0 2          │ L            │     = a$ │ shift 6                          ║<--- shift/reduce conflict here (shift wins)
            //  ║  (4)   │ 0 2 6        │ L EQUAL      │       a$ │ shift 5                          ║
            //  ║  (5)   │ 0 2 6 5      │ L EQUAL ID   │        $ │ reduce by L → ID, goto 8         ║
            //  ║  (6)   │ 0 2 6 8      │ L EQUAL L    │        $ │ reduce by R → L, goto 9          ║
            //  ║  (7)   │ 0 2 6 9      │ L EQUAL R    │        $ │ reduce by S → L EQUAL R, goto 1  ║
            //  ║  (8)   │ 0 1          │ S            │        $ │ accept                           ║
            //  ╚════════╧══════════════╧══════════════╧══════════╧══════════════════════════════════╝


            var lr1Parser = grammar.ComputeLr1ParsingTable();

            WriteLine("LR(1) Parsing Table");
            lr1Parser.PrintParsingTable(writer);

            // LR(1) Parsing Table
            // ╔════════════════════════════════╗╔══════════════════════════╗
            // ║             ACTION             ║║           GOTO           ║
            // ╠════════╤═════╤═════╤═════╤═════╣╠════════╤═════╤═════╤═════╣
            // ║ State  │EQUAL│ ID  │ASTER│ EOF ║║ State  │  S  │  R  │  L  ║
            // ╠════════╪═════╪═════╪═════╪═════╣╠════════╪═════╪═════╪═════╣
            // ║   0    │     │ s5  │ s4  │     ║║   0    │  1  │  3  │  2  ║
            // ║   1    │     │     │     │ acc ║║   1    │     │     │     ║
            // ║   2    │ s6  │     │     │ r5  ║║   2    │     │     │     ║
            // ║   3    │     │     │     │ r2  ║║   3    │     │     │     ║
            // ║   4    │     │ s5  │ s4  │     ║║   4    │     │  7  │  8  ║
            // ║   5    │ r4  │     │     │ r4  ║║   5    │     │     │     ║
            // ║   6    │     │ s12 │ s11 │     ║║   6    │     │  9  │ 10  ║
            // ║   7    │ r3  │     │     │ r3  ║║   7    │     │     │     ║
            // ║   8    │ r5  │     │     │ r5  ║║   8    │     │     │     ║
            // ║   9    │     │     │     │ r1  ║║   9    │     │     │     ║
            // ║   10   │     │     │     │ r5  ║║   10   │     │     │     ║
            // ║   11   │     │ s12 │ s11 │     ║║   11   │     │ 13  │ 10  ║
            // ║   12   │     │     │     │ r4  ║║   12   │     │     │     ║
            // ║   13   │     │     │     │ r3  ║║   13   │     │     │     ║
            // ╚════════╧═════╧═════╧═════╧═════╝╚════════╧═════╧═════╧═════╝

            // Grammar is LR(1)
            lr1Parser.AnyConflicts.ShouldBeFalse();

            // a=a$
            var lexer2 = new FakeLexer <Sym>((Sym.ID, "a"), (Sym.EQUAL, "="), (Sym.ID, "a"));

            lr1Parser.Parse(lexer2, writer);
            //  ╔════════╤══════════════╤══════════════╤══════════╤══════════════════════════════════╗
            //  ║ SeqNo  │    Stack     │   Symbols    │  Input   │              Action              ║
            //  ╠════════╪══════════════╪══════════════╪══════════╪══════════════════════════════════╣
            //  ║  (1)   │ 0            │              │     a=a$ │ shift 5                          ║
            //  ║  (2)   │ 0 5          │ ID           │      =a$ │ reduce by L → ID, goto 2         ║
            //  ║  (3)   │ 0 2          │ L            │      =a$ │ shift 6                          ║<--- no reduce, '=' is not a valid lookahead
            //  ║  (4)   │ 0 2 6        │ L EQUAL      │       a$ │ shift 12                         ║
            //  ║  (5)   │ 0 2 6 12     │ L EQUAL ID   │        $ │ reduce by L → ID, goto 10        ║
            //  ║  (6)   │ 0 2 6 10     │ L EQUAL L    │        $ │ reduce by R → L, goto 9          ║
            //  ║  (7)   │ 0 2 6 9      │ L EQUAL R    │        $ │ reduce by S → L EQUAL R, goto 1  ║
            //  ║  (8)   │ 0 1          │ S            │        $ │ accept                           ║
            //  ╚════════╧══════════════╧══════════════╧══════════╧══════════════════════════════════╝


            // TODO: Compute LALR(1) parser
            //      - brute force merging: LR(1) -> LALR(1)
            //      - fixed-point algorithm of propagated lookaheads: LR(0) -> SLR(1) extended follow sets --> LALR(1)
        }