예제 #1
0
        /// <summary>
        /// Reduz a regra.
        /// </summary>
        /// <param name="rule"></param>
        /// <returns></returns>
        private ParseResult Reduce(Rule rule)
        {
            ParseResult reduceEliminated;
            Token       token;

            if (this._trimReductions && rule.ContainsOneNonTerminal)
            {
                token = _tempStack.PopToken();
                token.SetParent(rule.RuleNonTerminal);
                reduceEliminated = ParseResult.ReduceEliminated;
            }
            else
            {
                Reduction reduction = new Reduction();
                reduction.ParentRule = rule;
                _tempStack.PopTokensInto(reduction, rule.SymbolCount);
                token      = new Token();
                token.Data = reduction;
                token.SetParent(rule.RuleNonTerminal);
                this._haveReduction = true;
                reduceEliminated    = ParseResult.ReduceNormal;
            }
            int      state           = _tempStack.PeekToken().State;
            LRAction actionForSymbol = _LalrTables[state].GetActionForSymbol(rule.RuleNonTerminal.TableIndex);

            if (actionForSymbol == null)
            {
                throw new ParserException("Action for LALR state is null");
            }
            token.State = _LalrState = actionForSymbol.Value;
            this._tempStack.PushToken(token);
            return(reduceEliminated);
        }
예제 #2
0
        /// <summary>
        /// Adiciona um novo item
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="action"></param>
        /// <param name="value"></param>
        public void AddItem(Symbol symbol, Text.Parser.Action action, int value)
        {
            LRAction lrAction = new LRAction();

            lrAction.Symbol = symbol;
            lrAction.Action = action;
            lrAction.Value  = value;
            _members.Add(lrAction);
        }
예제 #3
0
        /// <summary>
        /// Executa parser para o token.
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private ParseResult ParseToken(Token token)
        {
            ParseResult   result = ParseResult.InternalError;
            LRActionTable table  = _LalrTables[_LalrState];
            LRAction      action = table.GetActionForSymbol(token.TableIndex);

            if (action != null)
            {
                _haveReduction = false;
                _outputTokens.Clear();
                switch (action.Action)
                {
                case Action.Accept:
                    _haveReduction = true;
                    result         = ParseResult.Accept;
                    break;

                case Action.Shift:
                    token.State = _LalrState = action.Value;
                    _tempStack.PushToken(token);
                    result = ParseResult.Shift;
                    break;

                case Action.Reduce:
                    result = Reduce(_rules[action.Value]);
                    break;
                }
            }
            else
            {
                _outputTokens.Clear();
                foreach (LRAction a in table.Members)
                {
                    SymbolType kind = a.Symbol.Kind;
                    if (kind == SymbolType.Terminal || kind == SymbolType.End)
                    {
                        _outputTokens.PushToken(new Token(a.Symbol));
                    }
                }
                result = ParseResult.SyntaxError;
            }
            return(result);
        }