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); }
// For testing purposes only! public void InjectAutomatons(Dictionary <TSymbol, Dfa <TSymbol> > automatons) { this.Automatons = automatons; TargetStatesDictionary = GrammarUtils <TSymbol> .computeTargetStatesDictionary(Automatons); AccStateOwnerDictionary = GrammarUtils <TSymbol> .computeAccStateOwnerDictionary(Automatons); }