private void PrepareToParse() { Token token = new Token(); token.State = m_initLalrState; token.SetParent(m_symbols[m_startSymbol]); m_tempStack.PushToken(token); }
/// This function analyzes a token and either: /// 1. Makes a SINGLE reduction and pushes a complete Reduction object on the stack /// 2. Accepts the token and shifts /// 3. Errors and places the expected symbol indexes in the Tokens list /// The Token is assumed to be valid and WILL be checked private ParseResult ParseToken(Token p_token) { ParseResult result = ParseResult.InternalError; LRActionTable table = m_LalrTables[m_LalrState]; LRAction action = table.GetActionForSymbol(p_token.TableIndex); if (action != null) { m_haveReduction = false; m_outputTokens.Clear(); switch (action.Action) { case Action.Accept: m_haveReduction = true; result = ParseResult.Accept; break; case Action.Shift: p_token.State = m_LalrState = action.Value; m_tempStack.PushToken(p_token); result = ParseResult.Shift; break; case Action.Reduce: result = Reduce(m_rules[action.Value]); break; } } else { // syntax error - fill expected tokens. m_outputTokens.Clear(); foreach (LRAction a in table.Members) { SymbolType kind = a.Symbol.Kind; if (kind == SymbolType.Terminal || kind == SymbolType.End) { m_outputTokens.PushToken(new Token(a.Symbol)); } } result = ParseResult.SyntaxError; } return(result); }
/* public methods */ /// Pushes the specified token onto the internal input queue. /// It will be the next token analyzed by the parsing engine. public void PushInputToken(Token p_token) { m_inputTokens.PushToken(p_token); }