Пример #1
0
        /// Pops the specified number of tokens from the stack and adds them
        /// to the specified <c>Reduction</c>.
        public void PopTokensInto(Reduction p_reduction, int p_count)
        {
            int start = m_items.Count - p_count;
            int end   = m_items.Count;

            for (int i = start; i < end; i++)
            {
                p_reduction.AddToken((Token)m_items[i]);
            }

            m_items.RemoveRange(start, p_count);
        }
Пример #2
0
        /// <summary>Produces a reduction.</summary>
        /// <remarks>Removes as many tokens as members in the rule and pushes a
        ///          non-terminal token.</remarks>
        private ParseResult Reduce(Rule p_rule)
        {
            ParseResult result;
            Token       head;

            if (m_trimReductions && p_rule.ContainsOneNonTerminal)
            {
                // The current rule only consists of a single nonterminal and can be trimmed from the
                // parse tree. Usually we create a new Reduction, assign it to the Data property
                // of Head and push it on the stack. However, in this case, the Data property of the
                // Head will be assigned the Data property of the reduced token (i.e. the only one
                // on the stack). In this case, to save code, the value popped of the stack is changed
                // into the head.
                head = m_tempStack.PopToken();
                head.SetParent(p_rule.RuleNonTerminal);

                result = ParseResult.ReduceEliminated;
            }
            else
            {
                Reduction reduction = new Reduction();
                reduction.ParentRule = p_rule;

                m_tempStack.PopTokensInto(reduction, p_rule.SymbolCount);

                head      = new Token();
                head.Data = reduction;
                head.SetParent(p_rule.RuleNonTerminal);

                m_haveReduction = true;
                result          = ParseResult.ReduceNormal;
            }

            int      index  = m_tempStack.PeekToken().State;
            LRAction action = m_LalrTables[index].GetActionForSymbol(p_rule.RuleNonTerminal.TableIndex);

            if (action != null)
            {
                head.State = m_LalrState = action.Value;;
                m_tempStack.PushToken(head);
            }
            else
            {
                throw new ParserException("Action for LALR state is null");
            }

            return(result);
        }