public void Shift(Lexeme z, int targetStateIndex) { #if HISTORY var from = m_topStack.StateIndex; #endif StackNode shiftNode = new StackNode(targetStateIndex, m_topStack, z); m_topStack = shiftNode; #if HISTORY var to = m_topStack.StateIndex; History.Add(String.Format("S{0}:{1}", from, to)); #endif }
public IProduction PanicRecover(TransitionTable transitions, SourceSpan lastLocation, bool isEos) { while (true) { int currentStateIndex = m_topStack.StateIndex; for (int i = 0; i < transitions.ProductionCount; i++) { int gotoState = transitions.GetGoto(currentStateIndex, i); if (gotoState > 0) { var recoverNT = transitions.NonTerminals[i]; if (isEos) { //the recoverNT must have EOS in its follow, otherwise continue var follow = ((ProductionBase)recoverNT).Info.Follow; if (!follow.Contains(EndOfStream.Instance)) { continue; } } m_topStack = m_topStack.PrevNode; var newNode = new StackNode(gotoState, m_topStack, null); IncreaseErrorRecoverLevel(); AddError(new ErrorRecord(null, lastLocation)); m_topStack = newNode; return(recoverNT); } } if (m_topStack.PrevNode == null) { throw new ParsingFailureException("There's no way to recover from parser error"); } m_topStack = m_topStack.PrevNode; m_lastShiftStack = m_topStack; } }
public void Reduce(IProduction production, ReduceVisitor reducer, Lexeme lookahead) { #if HISTORY var from = m_topStack.StateIndex; #endif if (production == null) { //Accept Debug.Assert(m_topStack.PrevNode.StateIndex == 0); //TODO: accepted IsAccepted = true; return; } if (production.AggregatesAmbiguities) { AmbiguityAggregator = ((ProductionBase)production).CreateAggregator(); } var reduceResult = production.Accept(reducer, m_topStack); m_topStack = reduceResult.NewTopStack; var reduceError = reduceResult.ReduceError; if (reduceError != null) { IncreaseErrorRecoverLevel(); if (reduceError.ErrorPosition == null) { reduceError = new ErrorRecord(reduceError.ErrorId, lookahead.Value.Span); } AddError(reduceError); } #if HISTORY var to = m_topStack.StateIndex; History.Add(String.Format("R{0}:{1}", from, to)); #endif }
/// <summary> /// Restore the stack to the state after last shift action, for error recovery /// </summary> public void RestoreToLastShift() { m_topStack = m_lastShiftStack; }
public ParserHead(StackNode topStack) { m_topStack = topStack; m_lastShiftStack = topStack; m_errorRecoverLevel = 0; }
public void Shift(Lexeme z, int targetStateIndex) { #if HISTORY var from = m_topStack.StateIndex; #endif StackNode shiftNode = new StackNode(targetStateIndex, m_topStack, z); m_topStack = shiftNode; m_lastShiftStack = shiftNode; #if HISTORY var to = m_topStack.StateIndex; History.Add(String.Format("S{0}:{1}", from, to)); #endif }
public IProduction PanicRecover(TransitionTable transitions, SourceSpan lastLocation, bool isEos) { while(true) { int currentStateIndex = m_topStack.StateIndex; for (int i = 0; i < transitions.ProductionCount; i++) { int gotoState = transitions.GetGoto(currentStateIndex, i); if (gotoState > 0) { var recoverNT = transitions.NonTerminals[i]; if (isEos) { //the recoverNT must have EOS in its follow, otherwise continue var follow = ((ProductionBase)recoverNT).Info.Follow; if (!follow.Contains(EndOfStream.Instance)) { continue; } } m_topStack = m_topStack.PrevNode; var newNode = new StackNode(gotoState, m_topStack, null); IncreaseErrorRecoverLevel(); AddError(new ErrorRecord(null, lastLocation)); m_topStack = newNode; return recoverNT; } } if (m_topStack.PrevNode == null) { throw new ParsingFailureException("There's no way to recover from parser error"); } m_topStack = m_topStack.PrevNode; m_lastShiftStack = m_topStack; } }
public StackNode(int stateIndex, StackNode prev, object value) { StateIndex = stateIndex; PrevNode = prev; ReducedValue = value; }
public IProduction PanicRecover(TransitionTable transitions, SourceSpan lastLocation) { while(true) { int currentStateIndex = m_topStack.StateIndex; for (int i = 0; i < transitions.ProductionCount; i++) { int gotoState = transitions.GetGoto(currentStateIndex, i); if (gotoState > 0) { var recoverNT = transitions.NonTerminals[i]; m_topStack = m_topStack.PrevNode; var newNode = new StackNode(gotoState, m_topStack, null); IncreaseErrorRecoverLevel(); AddError(new ErrorRecord(null, lastLocation)); m_topStack = newNode; return recoverNT; } } if (m_topStack.PrevNode == null) { throw new ParsingFailureException("There's no way to recover from parser error"); } m_topStack = m_topStack.PrevNode; } }
public ReduceResult(StackNode newTopStack, ErrorRecord reduceError = null) { NewTopStack = newTopStack; ReduceError = reduceError; }
public void Reduce(IProduction production, ReduceVisitor reducer, Lexeme lookahead) { #if HISTORY var from = m_topStack.StateIndex; #endif if (production == null) { //Accept Debug.Assert(m_topStack.PrevNode.StateIndex == 0); //TODO: accepted IsAccepted = true; return; } if (Priority < production.Priority) Priority = production.Priority; var reduceResult = production.Accept(reducer, m_topStack); m_topStack = reduceResult.NewTopStack; var reduceError = reduceResult.ReduceError; if (reduceError != null) { IncreaseErrorRecoverLevel(); if (reduceError.ErrorPosition == null) { reduceError.ErrorPosition = lookahead.Span; } AddError(reduceError); } #if HISTORY var to = m_topStack.StateIndex; History.Add(String.Format("R{0}:{1}", from, to)); #endif }