Exemple #1
0
        private SortedSet <IDottedRule> GetConfirmedStates(SortedSet <IDottedRule> states)
        {
            var pool = SharedPools.Default <Queue <IDottedRule> >();

            var queue   = pool.AllocateAndClear();
            var closure = new SortedSet <IDottedRule>();

            foreach (var state in states)
            {
                if (closure.Add(state))
                {
                    queue.Enqueue(state);
                }
            }

            while (queue.Count > 0)
            {
                var state = queue.Dequeue();
                if (IsComplete(state))
                {
                    continue;
                }

                var production = state.Production;
                for (var s = state.Position; s < state.Production.RightHandSide.Count; s++)
                {
                    var postDotSymbol = production.RightHandSide[s];
                    if (postDotSymbol.SymbolType != SymbolType.NonTerminal)
                    {
                        break;
                    }

                    var nonTerminalPostDotSymbol = postDotSymbol as INonTerminal;
                    if (!Grammar.IsTransativeNullable(nonTerminalPostDotSymbol))
                    {
                        break;
                    }

                    var preComputedState = GetPreComputedState(production, s + 1);
                    if (closure.Add(preComputedState))
                    {
                        queue.Enqueue(preComputedState);
                    }
                }
            }
            pool.ClearAndFree(queue);
            return(closure);
        }
Exemple #2
0
        private SortedSet <IDottedRule> GetPredictedStates(DottedRuleSet frame)
        {
            var pool = SharedPools.Default <Queue <IDottedRule> >();

            var queue   = pool.AllocateAndClear();
            var closure = new SortedSet <IDottedRule>();

            for (int i = 0; i < frame.Data.Count; i++)
            {
                var state = frame.Data[i];
                if (!IsComplete(state))
                {
                    queue.Enqueue(state);
                }
            }

            while (queue.Count > 0)
            {
                var state = queue.Dequeue();
                if (IsComplete(state))
                {
                    continue;
                }

                var postDotSymbol = GetPostDotSymbol(state);
                if (postDotSymbol.SymbolType != SymbolType.NonTerminal)
                {
                    continue;
                }

                var nonTerminalPostDotSymbol = postDotSymbol as INonTerminal;
                if (Grammar.IsTransativeNullable(nonTerminalPostDotSymbol))
                {
                    var preComputedState = GetPreComputedState(state.Production, state.Position + 1);
                    if (!frame.Contains(preComputedState))
                    {
                        if (closure.Add(preComputedState))
                        {
                            if (!IsComplete(preComputedState))
                            {
                                queue.Enqueue(preComputedState);
                            }
                        }
                    }
                }

                var predictions = Grammar.RulesFor(nonTerminalPostDotSymbol);
                for (var p = 0; p < predictions.Count; p++)
                {
                    var prediction       = predictions[p];
                    var preComputedState = GetPreComputedState(prediction, 0);
                    if (frame.Contains(preComputedState))
                    {
                        continue;
                    }
                    if (!closure.Add(preComputedState))
                    {
                        continue;
                    }
                    if (!IsComplete(preComputedState))
                    {
                        queue.Enqueue(preComputedState);
                    }
                }
            }

            pool.ClearAndFree(queue);
            return(closure);
        }