/// <summary> /// Constructs the initial look-ahead for the transitions. /// </summary> /// <remarks></remarks> public void ConstructInitialLookahead(GrammarSymbolSet grammarSymbols, Dictionary <SyntacticalDFAState, PredictionTreeLeaf> fullSeries, Dictionary <IOilexerGrammarProductionRuleEntry, GrammarVocabulary> ruleVocabulary) { /* * * Left-recursive rules will use the relevant rule * paths within their projections within a loop to avoid * stack overflow. * */ bool includeRules = this.RootLeaf.Veins.LeftRecursionType != ProductionRuleLeftRecursionType.None; var result = new ControlledDictionary <GrammarVocabulary, PredictionTree>(); this.LookAhead = result; if (includeRules) { /* * * On left-recursive rules, the requirements change. * * * The rules must be considered at the start to * ensure that the left recursive branches * can be switched 'off' to allow for reductions * to function properly. * */ foreach (var key in this.Veins.Keys) { var targets = this.Veins[key]; if (targets.Any(k => k.GetRecursionType() != ProductionRuleLeftRecursionType.None && this.RootLeaf == this)) { var intersection = key.Intersect(this.Veins.DFAOriginState.OutTransitions.FullCheck); var reducedRules = key.GetRuleVariant().GetSymbols().Select(k => (IGrammarRuleSymbol)k).Select(k => new { Node = fullSeries.GetRuleNodeFromFullSeries(k.Source), Symbol = k }).Where(k => k.Node.HasBeenReduced || k.Node == k.Node.RootLeaf && k.Node.Veins.LeftRecursionType != ProductionRuleLeftRecursionType.None).Select(k => k.Symbol); var ruleVar = new GrammarVocabulary(key.symbols, reducedRules.ToArray()); intersection |= (key.GetTokenVariant() | ruleVar); if (!intersection.IsEmpty) { result._Add(intersection, targets); } } else { DoReductionAware(fullSeries, result, key); } } } else { foreach (var key in this.Veins.Keys.ToArray()) { DoReductionAware(fullSeries, result, key); } } /* * * Lexical ambiguity handling follows after the full transition table is known. * */ SyntacticAnalysisCore.CreateLexicalAmbiguityTransitions(grammarSymbols, result, null, this, fullSeries, ruleVocabulary); }
private void DoReductionAware(Dictionary <SyntacticalDFAState, PredictionTreeLeaf> fullSeries, ControlledDictionary <GrammarVocabulary, PredictionTree> result, GrammarVocabulary key) { var tokenVar = key.GetTokenVariant(); var reducedRules = key .GetRuleVariant() .GetSymbols() .Select(k => (IGrammarRuleSymbol)k) .Select(k => new { Node = fullSeries.GetRuleNodeFromFullSeries(k.Source), Symbol = k }) .Where(k => k.Node.HasBeenReduced).Select(k => k.Symbol);// || k.Node == k.Node.RootLeaf && k.Node.Value.LeftRecursionType != ProductionRuleLeftRecursionType.None && this == this.RootLeaf var ruleVar = new GrammarVocabulary(key.symbols, reducedRules.ToArray()); if (!tokenVar.IsEmpty) { result._Add(tokenVar, this.Veins[key]); } if (!ruleVar.IsEmpty) { result._Add(ruleVar, this.Veins[key]); } }
public IControlledCollection <ICliMetadataCustomAttributeTableRow> GetTaggedEntries(CliMetadataHasCustomAttributeTag hasCustomAttrTag) { lock (this.syncObject) { TaggedEntryCollection result; if (!lookupTable.TryGetValue(hasCustomAttrTag, out result)) { lookupTable._Add(hasCustomAttrTag, result = new TaggedEntryCollection(this, hasCustomAttrTag)); } return(result); } }
protected static void Reduce(TDFAState target, bool recognizer, Func <TDFAState, TDFAState, bool> additionalReducer = null) { int last = 0; Repeat: Dictionary <TDFAState, List <TDFAState> > forwardDuplications = new Dictionary <TDFAState, List <TDFAState> >(); List <TDFAState> flatForm = new List <TDFAState>(); flatForm.Add(target); FlatlineState(target, flatForm); flatForm = flatForm.Distinct().ToList(); bool[] found = new bool[flatForm.Count]; Parallel.For(0, flatForm.Count, i => //for (int i = 0; i < flatForm.Count; i++) { lock (found) { if (found[i]) { return; } } TDFAState iItem = flatForm[i]; /* * * If the state is already being replaced, * no need to check it again. * */ if (recognizer) { /* * * Merge backwards as well as forwards on cases where the * exact way the match occurred is not important * */ if (RecognizerTerminalEquivalencyCheck(iItem, i, flatForm, found, forwardDuplications)) { return; } } //Merge forward. StateEquivalencyCheck(iItem, i, flatForm, found, recognizer, forwardDuplications, additionalReducer); }); var maxReduces = (from f in flatForm where f.IsReductionSite where !forwardDuplications.Values.Any(reducedSet => reducedSet.Contains(f)) select f).ToArray(); var reductionZones = new ControlledDictionary <TDFAState, List <TDFAState> >(); foreach (var maximalReducer in maxReduces) { if (reductionZones.Values.Any(zone => zone.Contains(maximalReducer))) { continue; } reductionZones._Add(maximalReducer, (from k in maximalReducer.GetReductionZone() where !forwardDuplications.Values.Any(reducedSet => reducedSet.Contains(k)) select k).ToList()); } /* * * Now we have the zones which need reduced to the max. * * * These are for optimization purposes where you know * the order isn't important, but the data itself is. * */ for (int i = 0; i < reductionZones.Count; i++) { var maxReduceSet = reductionZones.Values[i]; bool[] maxFound = new bool[maxReduceSet.Count]; for (int j = 0; j < maxReduceSet.Count; j++) { var iItem = maxReduceSet[j]; if (maxFound[j]) { continue; } if (RecognizerTerminalEquivalencyCheck(iItem, i, maxReduceSet, maxFound, forwardDuplications)) { continue; } StateEquivalencyCheck(iItem, j, maxReduceSet, maxFound, true, forwardDuplications, additionalReducer); } } /* * * Replace the states that are considered extraneous. * */ foreach (var masterState in forwardDuplications.Keys) { var currentSet = forwardDuplications[masterState]; for (int i = 0; i < currentSet.Count; i++) { ReplaceState(masterState, currentSet[i]); } } /* * * If there were reductions performed in this step, repeat * the process, since two states that didn't pass the equivalency * examination pointed to two different, yet similar, states, * the process can be repeated on these, now redundant, states. * */ CondenseTransitions(target); last = flatForm.Count; flatForm.Clear(); if (forwardDuplications.Count > 0) { forwardDuplications.Clear(); //Reduces recursive dependency. No need to call the method again. goto Repeat; } }