private void Shift(int stateIndex) { #if TRACE_ACTIONS Console.Error.Write("Shifting token {0}, ", TerminalToString(NextToken)); #endif FsaState = states[stateIndex]; valueStack.Push(scanner.yylval); StateStack.Push(FsaState); LocationStack.Push(scanner.yylloc); if (recovering) { if (NextToken != errorToken) { tokensSinceLastError++; } if (tokensSinceLastError > 5) { recovering = false; } } if (NextToken != endOfFileToken) { NextToken = 0; } }
/// <summary> /// Shift. /// </summary> /// <param name="stateIndex">state index.</param> private void Shift(int stateIndex) { FsaState = states[stateIndex]; ValueStack.Push(Scanner.yylval); StateStack.Push(FsaState); LocationStack.Push(Scanner.yylloc); if (recovering) { if (NextToken != errorToken) { tokensSinceLastError++; } if (tokensSinceLastError > 5) { recovering = false; } } if (NextToken != endOfFileToken) { NextToken = 0; } }
private void Reduce(int ruleNumber) { #if TRACE_ACTIONS DisplayRule(ruleNumber); #endif Rule rule = rules[ruleNumber]; // // Default actions for unit productions. // if (rule.RightHandSide.Length == 1) { CurrentSemanticValue = valueStack.TopElement(); // Default action: $$ = $1; CurrentLocationSpan = LocationStack.TopElement(); // Default action "@$ = @1; } else { if (rule.RightHandSide.Length == 0) { // Create a new blank value. // Explicit semantic action may mutate this value CurrentSemanticValue = default(TValue); // The location span for an empty production will start with the // beginning of the next lexeme, and end with the finish of the // previous lexeme. This gives the correct behaviour when this // nonsense value is used in later Merge operations. CurrentLocationSpan = (scanner.yylloc != null && LastSpan != null ? scanner.yylloc.Merge(LastSpan) : default(TSpan)); } else { // Default action: $$ = $1; CurrentSemanticValue = valueStack.TopElement(); // Default action "@$ = @1.Merge(@N)" for location info. TSpan at1 = LocationStack[LocationStack.Depth - rule.RightHandSide.Length]; TSpan atN = LocationStack[LocationStack.Depth - 1]; CurrentLocationSpan = ((at1 != null && atN != null) ? at1.Merge(atN) : default(TSpan)); } } DoAction(ruleNumber); for (int i = 0; i < rule.RightHandSide.Length; i++) { StateStack.Pop(); valueStack.Pop(); LocationStack.Pop(); } FsaState = StateStack.TopElement(); if (FsaState.Goto.ContainsKey(rule.LeftHandSide)) { FsaState = states[FsaState.Goto[rule.LeftHandSide]]; } StateStack.Push(FsaState); valueStack.Push(CurrentSemanticValue); LocationStack.Push(CurrentLocationSpan); }
private void Reduce(int ruleNumber) { #if TRACE_ACTIONS DisplayRule(ruleNumber); #endif Rule rule = rules[ruleNumber]; if (rule.RightHandSide.Length == 1) { CurrentSemanticValue = valueStack.TopElement(); CurrentLocationSpan = LocationStack.TopElement(); } else { if (rule.RightHandSide.Length == 0) { CurrentSemanticValue = default(TValue); CurrentLocationSpan = (scanner.yylloc != null && LastSpan != null ? scanner.yylloc.Merge(LastSpan) : default(TSpan)); } else { CurrentSemanticValue = valueStack.TopElement(); TSpan at1 = LocationStack[LocationStack.Depth - rule.RightHandSide.Length]; TSpan atN = LocationStack[LocationStack.Depth - 1]; CurrentLocationSpan = ((at1 != null && atN != null) ? at1.Merge(atN) : default(TSpan)); } } DoAction(ruleNumber); for (int i = 0; i < rule.RightHandSide.Length; i++) { StateStack.Pop(); valueStack.Pop(); LocationStack.Pop(); } #if TRACE_ACTIONS DisplayStack(); #endif FsaState = StateStack.TopElement(); if (FsaState.Goto.ContainsKey(rule.LeftHandSide)) { FsaState = states[FsaState.Goto[rule.LeftHandSide]]; } StateStack.Push(FsaState); valueStack.Push(CurrentSemanticValue); LocationStack.Push(CurrentLocationSpan); }
/// <summary> /// Main entry point of the Shift-Reduce Parser. /// </summary> /// <returns>True if parse succeeds, else false for /// unrecoverable errors</returns> public bool Parse() { Initialize(); // allow derived classes to instantiate rules, states and nonTerminals NextToken = 0; FsaState = states[0]; StateStack.Push(FsaState); valueStack.Push(CurrentSemanticValue); LocationStack.Push(CurrentLocationSpan); while (true) { #if TRACE_ACTIONS Console.Error.WriteLine("Entering state {0} ", FsaState.number); DisplayStack(); #endif int action = FsaState.defaultAction; if (FsaState.ParserTable != null) { if (NextToken == 0) { // We save the last token span, so that the location span // of production right hand sides that begin or end with a // nullable production will be correct. LastSpan = scanner.yylloc; NextToken = scanner.yylex(); #if TRACE_ACTIONS Console.Error.WriteLine("Reading: Next token is {0}", TerminalToString(NextToken)); #endif } #if TRACE_ACTIONS else { Console.Error.WriteLine("Next token is still {0}", TerminalToString(NextToken)); } #endif if (FsaState.ParserTable.ContainsKey(NextToken)) { action = FsaState.ParserTable[NextToken]; } } if (action > 0) // shift { Shift(action); } else if (action < 0) // reduce { try { Reduce(-action); if (action == -1) // accept { return(true); } } catch (AbortException) { return(false); } catch (AcceptException) { return(true); } catch (ErrorException) { if (!ErrorRecovery()) { return(false); } else { throw; // Rethrow x, preserving information. } } } else if (action == 0 && !ErrorRecovery()) // error { return(false); } } }
/// <summary> /// Reduce. /// </summary> /// <param name="ruleNumber">The rule to reduce by.</param> private void Reduce(int ruleNumber) { Rule rule = rules[ruleNumber]; // // Default actions for unit productions. // if (rule.RightHandSide.Length == 1) { CurrentSemanticValue = ValueStack.TopElement(); // Default action: $$ = $1; CurrentLocationSpan = LocationStack.TopElement(); // Default action "@$ = @1; } else { if (rule.RightHandSide.Length == 0) { // Create a new blank value. // Explicit semantic action may mutate this value CurrentSemanticValue = default(TValue); // The location span for an empty production will start with the // beginning of the next lexeme, and end with the finish of the // previous lexeme. This gives the correct behaviour when this // nonsense value is used in later Merge operations. CurrentLocationSpan = (Scanner.yylloc != null && LastSpan != null ? Scanner.yylloc.Merge(LastSpan) : default(TSpan)); } else { // Default action: $$ = $1; CurrentSemanticValue = ValueStack.TopElement(); // Default action "@$ = @1.Merge(@N)" for location info. TSpan at1 = LocationStack[LocationStack.Depth - rule.RightHandSide.Length]; TSpan atN = LocationStack[LocationStack.Depth - 1]; CurrentLocationSpan = ((at1 != null && atN != null) ? at1.Merge(atN) : default(TSpan)); } } DoAction(ruleNumber); for (int i = rule.RightHandSide.Length - 1; i >= 0; i--) { _valueParameterList[i] = ValueStack.Pop(); // capture the values - added by Nate Wallace StateStack.Pop(); LocationStack.Pop(); } if (!_errorOccured) { ProcessReduce(rule.LeftHandSide, _valueParameterList, rule.RightHandSide.Length); // process the reduce action - added by Nate Wallace } else { _errorOccured = false; } FsaState = StateStack.TopElement(); if (FsaState.Goto.ContainsKey(rule.LeftHandSide)) { FsaState = states[FsaState.Goto[rule.LeftHandSide]]; } StateStack.Push(FsaState); ValueStack.Push(CurrentSemanticValue); LocationStack.Push(CurrentLocationSpan); }
/// <summary> /// Main entry point of the Shift-Reduce Parser. /// </summary> /// <returns>True if parse succeeds, else false for /// unrecoverable errors</returns> public bool Parse() { _valueParameterList = new TValue[15]; // added Nate Wallace _errorOccured = false; Initialize(); // allow derived classes to instantiate rules, states and nonTerminals NextToken = 0; FsaState = states[0]; StateStack.Push(FsaState); ValueStack.Push(CurrentSemanticValue); LocationStack.Push(CurrentLocationSpan); while (true) { int action = FsaState.defaultAction; if (FsaState.ParserTable != null) { if (NextToken == 0) { // We save the last token span, so that the location span // of production right hand sides that begin or end with a // nullable production will be correct. LastSpan = Scanner.yylloc; NextToken = Scanner.yylex(); } if (FsaState.ParserTable.ContainsKey(NextToken)) { action = FsaState.ParserTable[NextToken]; } } if (action > 0) // shift { Shift(action); } else if (action < 0) // reduce { try { Reduce(-action); if (action == -1) // accept { return(true); } } catch (Exception x) { if (x is AbortException) { return(false); } else if (x is AcceptException) { return(true); } else if (x is ErrorException && !ErrorRecovery()) { return(false); } else { throw; // Rethrow x, preserving information. } } } else if (action == 0) // error { _tokenOnError = NextToken; _locationOnError = Scanner.yylloc; if (!ErrorRecovery()) { return(false); } } } }
public bool Parse() { Initialize(); NextToken = 0; FsaState = states[0]; StateStack.Push(FsaState); valueStack.Push(CurrentSemanticValue); LocationStack.Push(CurrentLocationSpan); while (true) { #if TRACE_ACTIONS Console.Error.WriteLine("Entering state {0} ", FsaState.number); #endif int action = FsaState.defaultAction; if (FsaState.ParserTable != null) { if (NextToken == 0) { #if TRACE_ACTIONS Console.Error.Write("Reading a token: "); #endif LastSpan = scanner.yylloc; NextToken = scanner.yylex(); } #if TRACE_ACTIONS Console.Error.WriteLine("Next token is {0}", TerminalToString(NextToken)); #endif if (FsaState.ParserTable.ContainsKey(NextToken)) { action = FsaState.ParserTable[NextToken]; } } if (action > 0) { Shift(action); } else if (action < 0) { try { Reduce(-action); if (action == -1) { return(true); } } catch (Exception x) { if (x is AbortException) { return(false); } else if (x is AcceptException) { return(true); } else if (x is ErrorException && !ErrorRecovery()) { return(false); } else { throw; } } } else if (action == 0) { if (!ErrorRecovery()) { return(false); } } } }