Ejemplo n.º 1
0
        // Returns the list of all accepting states of an automaton.
        private static List <DfaState <TSymbol> > GetAllAcceptingStates(Dfa <TSymbol> dfa)
        {
            var result  = new List <DfaState <TSymbol> >();
            var queue   = new Queue <DfaState <TSymbol> >();
            var visited = new HashSet <DfaState <TSymbol> >();

            visited.Add(dfa.Start);
            queue.Enqueue(dfa.Start);

            while (queue.Count > 0)
            {
                var state = queue.Dequeue();
                if (state.Accepting > 0)
                {
                    result.Add(state);
                }
                foreach (var kv in state.Transitions)
                {
                    var nextState = kv.Value;
                    if (!visited.Contains(nextState))
                    {
                        visited.Add(nextState);
                        queue.Enqueue(nextState);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public Grammar(TSymbol start, IDictionary <TSymbol, IProduction <TSymbol>[]> productions)
        {
            Start           = start;
            Productions     = productions;
            WhichProduction = new Dictionary <uint, IProduction <TSymbol> >();
            Automatons      = new Dictionary <TSymbol, Dfa <TSymbol> >();
            // Here we prepare automatons for each symbol.
            uint productionMarker = 1;

            foreach (var symbolProductions in Productions)
            {
                var automatons = new List <Lexer.DfaUtils.MinimizedDfa <TSymbol> >();
                foreach (var production in symbolProductions.Value)
                {
                    automatons.Add(Dfa <TSymbol> .RegexDfa(production.Rhs, productionMarker));
                    WhichProduction[productionMarker] = production;
                    productionMarker++;
                }
                Automatons[symbolProductions.Key] = Dfa <TSymbol> .ProductDfa(automatons.ToArray());
            }

            Nullable = GrammarUtils <TSymbol> .ComputeNullable(Automatons);

            First = GrammarUtils <TSymbol> .ComputeFirst(Automatons, Nullable);

            Follow = GrammarUtils <TSymbol> .ComputeFollow(Automatons, Nullable, First);

            SymbolListLeftRecursion = GrammarUtils <TSymbol> .HasLeftRecursion(Automatons, Nullable);

            TargetStatesDictionary = GrammarUtils <TSymbol> .computeTargetStatesDictionary(Automatons);

            AccStateOwnerDictionary = GrammarUtils <TSymbol> .computeAccStateOwnerDictionary(Automatons);
        }