private StateCollection CreateParserStates(CGTContent content) { rules = CreateRules(content); StateCollection states = new StateCollection(); foreach (LALRStateRecord record in content.LALRStateTable) { State state = new State(record.Index); states.Add(state); } foreach (LALRStateRecord record in content.LALRStateTable) { State state = states[record.Index]; foreach (ActionSubRecord subRecord in record.ActionSubRecords) { GPAction action = ActionFactory.CreateAction(subRecord, states, symbols, rules); state.Actions.Add(action); } } return(states); }
private void DoReduce(Token token, ReduceAction action) { int reduceLength = action.Rule.Rhs.Length; State currentState; // Do not reduce if the rule is single nonterminal and TrimReductions is on bool skipReduce = ((TrimReductions) && (reduceLength == 1) && (action.Rule.Rhs[0] is SymbolNonterminal)); if (skipReduce) { stateStack.Pop(); currentState = stateStack.Peek(); } else { Token[] tokens = new Token[reduceLength]; for (int i = 0; i < reduceLength; i++) { stateStack.Pop(); tokens[reduceLength - i - 1] = tokenStack.Pop(); } NonterminalToken nttoken = new NonterminalToken(action.Rule, tokens); tokenStack.Push(nttoken); currentState = stateStack.Peek(); if (OnReduce != null) { ReduceEventArgs args = new ReduceEventArgs(action.Rule, nttoken, currentState); OnReduce(this, args); DoReleaseTokens(args.Token); continueParsing = args.Continue; } } GPAction gotoAction = currentState.Actions.Get(action.Rule.Lhs); if (gotoAction is GotoAction) { DoGoto(token, (GotoAction)gotoAction); } else { throw new ParserException("Invalid action table in state"); } }
private void ParseTerminal(TerminalToken token) { State currentState = stateStack.Peek(); GPAction action = currentState.Actions.Get(token.Symbol); if (action is ShiftAction) { DoShift(token, (ShiftAction)action); } else if (action is ReduceAction) { DoReduce(token, (ReduceAction)action); } else if (action is AcceptAction) { DoAccept(token, (AcceptAction)action); } else { continueParsing = false; FireParseError(token); } }
public virtual void SetAction(GPAction action) { m_targetAction = action; m_serialObject = new SerializedObject(m_targetAction); }