Ejemplo n.º 1
0
 private IGrammarLexerRule CreateGrammarLexerRule(IList<ITerminal> terminalNeighborList)
 {
     var startNonTerminal = new NonTerminal("S");
     var production = new Production(
         startNonTerminal,
         terminalNeighborList.ToArray());
     var grammar = new Grammar(startNonTerminal, new[] { production }, null, null);
     return new GrammarLexerRule(Guid.NewGuid().ToString(), grammar);
 }
Ejemplo n.º 2
0
        private SortedSet <DottedRule> GetPredictedStates(Frame frame)
        {
            var pool = SharedPools.Default <Queue <DottedRule> >();

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

            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.IsNullable(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);
        }