Example #1
0
        private Dictionary <Nonterminal, Set <Terminal <TTokenKind> > > ComputeFirst(
            Grammar <TTokenKind, TNonterminal> grammar, IErasableSymbolsAnalyzer analyzer)
        {
            var(initFirstSets, graph) = DigraphAlgorithm.GetFirstGraph(grammar, analyzer);

            var firstSets = DigraphAlgorithm.Traverse(graph, initFirstSets);

            var firstMap = grammar.Nonterminals.ToDictionary(v => v, v => firstSets[v.Index]);

            return(firstMap);
        }
Example #2
0
        private static Dictionary <Nonterminal, Set <Terminal <TTokenKind> > > ComputeFollow(
            Grammar <TTokenKind, TNonterminal> grammar,
            IFirstSymbolsAnalyzer <TTokenKind> analyzer)
        {
            var(initFollowSets, graph) = DigraphAlgorithm.GetFollowGraph(grammar, analyzer);

            var followSets = DigraphAlgorithm.Traverse(graph, initFollowSets);

            var followMap = grammar.Nonterminals.ToDictionary(v => v, v => followSets[v.Index]);

            return(followMap);
        }
        // Relational formulation:
        // t ∈ LA(q, A → ω)  iff  (q, A → ω) lookback (p,A) includes* (p',B) reads* (r,C) directly-reads t
        //
        // (r,C) directly-reads t  iff  t ∈ DR(r,C)

        private static Dictionary <(int, int), Set <Terminal <TTokenKind> > > ComputeLookaheadSets(
            Grammar <TTokenKind, TNonterminal> grammar,
            LrItemsDfa <TTokenKind> dfaLr0,
            IErasableSymbolsAnalyzer analyzer)
        {
            var vertices = LalrLookaheadSetsAlgorithm.GetGotoTransitionPairs(grammar, dfaLr0);

            var(directReads, graphRead) = LalrLookaheadSetsAlgorithm.GetGraphReads(grammar, dfaLr0, vertices, analyzer);

            // Read(p,A) sets
            Set <Terminal <TTokenKind> >[] readSets = DigraphAlgorithm.Traverse(graphRead, directReads);

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

            // Follow(p,A) sets
            Set <Terminal <TTokenKind> >[] followSets = DigraphAlgorithm.Traverse(graphLaFollow, readSets);

            // LA(q, A → ω) = ∪{ Follow(p,A) | (q, A → ω) lookback (p,A) }
            var lookaheadSets = LalrLookaheadSetsAlgorithm.GetLaUnion(grammar, dfaLr0, vertices, followSets);

            return(lookaheadSets);
        }