/// <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); }
/// <summary> /// Remove os tokens que estão no topo da pilha e adiciona /// a quantidade informada para a redução. /// </summary> /// <param name="reduction"></param> /// <param name="count">Quantidade de itens que serão recuperados,</param> public void PopTokensInto(Reduction reduction, int count) { int index = _items.Count - count; int count2 = _items.Count; for (int i = index; i < count2; i++) { reduction.AddToken(_items[i]); } _items.RemoveRange(index, count); }