コード例 #1
0
ファイル: Parser.cs プロジェクト: waqashaneef/NosDB
        /// 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);
        }