Beispiel #1
0
        private void PrepareToParse()
        {
            Token token = new Token();

            token.State = m_initLalrState;
            token.SetParent(m_symbols[m_startSymbol]);
            m_tempStack.PushToken(token);
        }
Beispiel #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);
        }
Beispiel #3
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;
		}
Beispiel #4
0
		private void PrepareToParse()
		{
			Token token = new Token();
			token.State = m_initLalrState;
			token.SetParent(m_symbols[m_startSymbol]);
			m_tempStack.PushToken(token);
		}