Exemplo n.º 1
0
        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]);
            }
        }
Exemplo n.º 2
0
 public SyntacticalNFAState(ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, GrammarSymbolSet symbols)
 {
     this.lookup  = lookup;
     this.symbols = symbols;
 }
 public SyntacticalDFARootState(IOilexerGrammarProductionRuleEntry entry, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, GrammarSymbolSet symbols)
     : base(lookup, symbols)
 {
     this.entry = entry;
 }
Exemplo n.º 4
0
        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;
            }
        }
Exemplo n.º 5
0
 internal _ValuesCollection(_DeclarationsBase <TDeclarationIdentifier, TDeclaration, TDeclarationSpecificIdentifier, TDeclarationSpecific, TOriginalContainer, TDictionary> parentTypes)
     : base(parentTypes)
 {
     this.ParentTypes = parentTypes;
     this.values      = new ControlledDictionary <TDeclarationSpecific, TDeclarationSpecific>();
 }
        public static SyntacticalNFAState BuildNFA(this ITokenReferenceProductionRuleItem tokenReference, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements)
        {
            SyntacticalNFAState state = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols);
            var symbol = symbols[tokenReference.Reference];
            GrammarVocabulary movement = null;

            if (symbol is IGrammarConstantItemSymbol)
            {
                IGrammarConstantItemSymbol constantItemSymbol = (IGrammarConstantItemSymbol)symbol;
                movement = new GrammarVocabulary(symbols, (from s in symbols
                                                           let cis = s as IGrammarConstantItemSymbol
                                                                     where cis != null && cis.Source == constantItemSymbol.Source
                                                                     select cis).ToArray());
            }
            else
            {
                movement = new GrammarVocabulary(symbols, symbol);
            }
            var stateEnd = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols);

            state.MoveTo(movement, stateEnd);
            return(state);
        }
        public static SyntacticalNFAState BuildNFA(this IRuleReferenceProductionRuleItem ruleReference, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements)
        {
            SyntacticalNFAState state    = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols);
            GrammarVocabulary   movement = new GrammarVocabulary(symbols, symbols[ruleReference.Reference]);
            var stateEnd = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols);

            state.MoveTo(movement, stateEnd);
            return(state);
        }
        public static SyntacticalNFAState BuildNFA <T, TLiteral>(this ILiteralReferenceProductionRuleItem <T, TLiteral> item, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements)
            where TLiteral :
        ILiteralTokenItem <T>
        {
            SyntacticalNFAState state    = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols);
            GrammarVocabulary   movement = new GrammarVocabulary(symbols, symbols[item.Literal]);
            var stateEnd = new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols);

            state.MoveTo(movement, stateEnd);
            return(state);
        }
        public static SyntacticalNFAState BuildNFA(this IProductionRuleItem item, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements)
        {
            SyntacticalNFAState result = null;

            if (item is IProductionRuleGroupItem)
            {
                result = ((IProductionRuleSeries)item).BuildNFA(symbols, lookup, replacements);
            }
            else if (item is ILiteralCharReferenceProductionRuleItem)
            {
                result = ((ILiteralCharReferenceProductionRuleItem)(item)).BuildNFA(symbols, lookup, replacements);
            }
            else if (item is ILiteralStringReferenceProductionRuleItem)
            {
                result = ((ILiteralStringReferenceProductionRuleItem)(item)).BuildNFA(symbols, lookup, replacements);
            }
            else if (item is ITokenReferenceProductionRuleItem)
            {
                result = ((ITokenReferenceProductionRuleItem)(item)).BuildNFA(symbols, lookup, replacements);
            }
            else if (item is IRuleReferenceProductionRuleItem)
            {
                result = ((IRuleReferenceProductionRuleItem)(item)).BuildNFA(symbols, lookup, replacements);
            }
            else
            {
                throw new ArgumentException("series");
            }
            if (result == null)
            {
                return(null);
            }
            var source = replacements.ContainsKey(item) ? (IProductionRuleSource)replacements[item] : (IProductionRuleSource)item;

            result.HandleRepeatCycle <GrammarVocabulary, SyntacticalNFAState, SyntacticalDFAState, IProductionRuleSource, SyntacticalNFARootState, IProductionRuleItem>(item, item, OilexerGrammarInliningCore.ProductionRuleRootStateClonerCache, () => new SyntacticalNFAState(lookup, (GrammarSymbolSet)symbols));
            List <SyntacticalNFAState> flatForm = new List <SyntacticalNFAState>();

            SyntacticalNFAState.FlatlineState(result, flatForm);
            result.SetInitial(source);
            foreach (var fState in flatForm)
            {
                fState.SetIntermediate(source);
            }
            foreach (var edge in result.ObtainEdges())
            {
                edge.SetFinal(source);
            }
            return(result);
        }
        public static SyntacticalNFAState BuildNFA(this IProductionRule rule, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements)
        {
            SyntacticalNFAState state = null;

            foreach (var item in rule)
            {
                if (state == null)
                {
                    state = item.BuildNFA(symbols, lookup, replacements);
                }
                else
                {
                    var nextState = item.BuildNFA(symbols, lookup, replacements);
                    if (nextState != null)
                    {
                        state.Concat(nextState);
                    }
                }
            }
            return(state);
        }
        public static SyntacticalNFAState BuildNFA(this IProductionRuleSeries series, SyntacticalNFAState root, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements)
        {
            SyntacticalNFAState state = root;
            bool first = true;

            foreach (var expression in series)
            {
                if (state == null)
                {
                    state = expression.BuildNFA(symbols, lookup, replacements);
                    first = false;
                }
                else
                {
                    var expressionNFA = expression.BuildNFA(symbols, lookup, replacements);
                    if (first)
                    {
                        bool isEdge = expressionNFA.IsEdge;
                        first = false;
                        state.Union(expressionNFA);
                        if (state.IsEdge && !isEdge)
                        {
                            state.IsEdge = isEdge;
                        }
                    }
                    else
                    {
                        state.Union(expressionNFA);
                    }
                }
            }
            if (!(series is IProductionRuleGroupItem))
            {
                List <SyntacticalNFAState> flatForm = new List <SyntacticalNFAState>();
                SyntacticalNFAState.FlatlineState(state, flatForm);
                var source = (replacements.ContainsKey(series) ? (IProductionRuleSource)replacements[series] : (IProductionRuleSource)series);
                state.SetInitial(source);
                foreach (var fState in flatForm)
                {
                    fState.SetIntermediate(source);
                }
                foreach (var edge in state.ObtainEdges())
                {
                    edge.SetFinal(source);
                }
            }
            return(state);
        }
 public static SyntacticalNFAState BuildNFA(this IProductionRuleSeries series, IGrammarSymbolSet symbols, ControlledDictionary <IOilexerGrammarProductionRuleEntry, SyntacticalDFARootState> lookup, Dictionary <IProductionRuleSource, IProductionRuleCaptureStructuralItem> replacements)
 {
     return(series.BuildNFA(null, symbols, lookup, replacements));
 }