//
        // Context-Free languages, CFG and LR Parsing
        //

        /// <summary>
        /// G3 in "A Survey of LR-Parsing Methods", Gallier.
        /// </summary>
        public static void GallierLookaheadLR_Example3()
        {
            var grammar = DragonBookExample4_48.GetGrammar();

            // characteristic automaton (LR(0) automaton)
            var dfaLr0 = grammar.GetLr0AutomatonDfa();

            SaveFile("GallierEx3_LR0Automaton.dot", DotLanguagePrinter.ToDotLanguage(dfaLr0, DotRankDirection.LeftRight, skipStateLabeling: true));

            var analyzer = Analyzers.CreateErasableSymbolsAnalyzer(grammar);

            //(ImmutableArray<IReadOnlySet<Terminal>> initfirstSets, IGraph graphFirst) = DigraphAlgorithm.GetFirstGraph(grammar, analyzer);
            (var initfirstSets, IGraph graphFirst) = DigraphAlgorithm.GetFirstGraph(grammar, analyzer);

            SaveFile("GallierEx3_FirstGraph.dot",
                     DigraphDotLanguagePrinter.PrintGraph("INITFIRST", initfirstSets, graphFirst, v => grammar.Nonterminals[v].Name));

            var firstSymbolsAnalyzer = Analyzers.CreateFirstSymbolsAnalyzer(grammar);

            (var initFollowSets, IGraph graphFollow) = DigraphAlgorithm.GetFollowGraph(grammar, firstSymbolsAnalyzer);

            SaveFile("GallierEx3_FollowGraph.dot",
                     DigraphDotLanguagePrinter.PrintGraph("INITFOLLOW", initFollowSets, graphFollow, v => grammar.Nonterminals[v].Name));

            var stringWriter = new StringWriter();

            grammar.PrintFirstAndFollowSets(stringWriter);
            SaveFile("GallierEx3_FirstAndFollowSets.txt", stringWriter.ToString());

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

            lr0Parser.PrintParsingTable(writer);

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

            SaveFile("GallierEx3_Lr0ParsingTable.txt", writer.ToString());

            ////

            var vertices = LalrLookaheadSetsAlgorithm.GetGotoTransitionPairs(grammar, dfaLr0);

            // Read (INITFOLLOW) sets
            var(directReads, graphRead) = LalrLookaheadSetsAlgorithm.GetGraphReads(grammar, dfaLr0, vertices, analyzer);

            SaveFile("GallierEx3_ReadGraph.dot",
                     DigraphDotLanguagePrinter.PrintGraph("DR", directReads, graphRead, v => vertices[v].ToString()));

            var graphLaFollow = LalrLookaheadSetsAlgorithm.GetGraphLaFollow(grammar, dfaLr0, vertices, analyzer);

            SaveFile("GallierEx3_LaFollowGraph.dot",
                     DigraphDotLanguagePrinter.PrintGraph("INITFOLLOW", directReads, graphLaFollow, v => vertices[v].ToString()));
        }
Exemple #2
0
 public void Vertices()
 {
     foreach (var vertices in new []
     {
         LalrLookaheadSetsAlgorithm.GetGotoTransitionPairs(Grammar, DfaLr0),
         LalrLookaheadSetsAlgorithm.GetGotoTransitionPairs(GrammarEof, DfaLr0Eof)
     })
     {
         vertices.IndexOf((1, Grammar.V(Var.S))).ShouldBeGreaterThanOrEqualTo(0);
         vertices.IndexOf((1, Grammar.V(Var.R))).ShouldBeGreaterThanOrEqualTo(0);
         vertices.IndexOf((1, Grammar.V(Var.L))).ShouldBeGreaterThanOrEqualTo(0);
         vertices.IndexOf((5, Grammar.V(Var.R))).ShouldBeGreaterThanOrEqualTo(0);
         vertices.IndexOf((5, Grammar.V(Var.L))).ShouldBeGreaterThanOrEqualTo(0);
         vertices.IndexOf((7, Grammar.V(Var.R))).ShouldBeGreaterThanOrEqualTo(0);
         vertices.IndexOf((7, Grammar.V(Var.L))).ShouldBeGreaterThanOrEqualTo(0);
     }
 }