/// <summary> /// Return a list of all next possible states, having followed any /// applicable transitions, given the current state. /// </summary> private IEnumerable <PDARunState> FindNextStates(PDARunState state) { // Follow each possible transition available. return(from transition in Transitions where transition.MatchesConfiguration(state) select ApplyTransition(transition, state)); }
public bool MatchesConfiguration(PDARunState state) { return(InputChar == state.Input[0] && OldState == state.State && ((StackHead == '_' && state.Stack.Count() == 0) || (state.Stack.Count() > 0 && StackHead == state.Stack[0]))); }
/// <summary> /// Applies the given transition, to the given run state. /// </summary> /// <returns> /// An IEnumerable of the states resulting from applying the transition. /// </returns> private static PDARunState ApplyTransition(PDATransition transition, PDARunState state) { var newState = transition.NewState; var newStack = state.Stack; var newInput = state.Input.Substring(1); var newMatchedSoFar = state.MatchedSoFar + 1; var isFailure = false; // If we're not matching against the empty stack, try pop. if (transition.StackHead != '_') { if (newStack.Length > 0) { newStack = newStack.Substring(1); } else { // Cannot pop from the stack, so bail out here. isFailure = true; } } else if (newStack != "") { // Catch illegal use of empty stack transition. isFailure = true; } if (!isFailure) { newStack = transition.StackReplace + newStack; } var returnState = new PDARunState(newInput, newMatchedSoFar, newStack, newState); // Check for end-of-input without empty stack - failure. // We won't be able to empty the stack if there are fewer input // chars than stack elems, so fail to prevent unnecessary work. if (isFailure || newInput == "" && newStack.Length > 0 || newInput.Length < newStack.Length) { returnState.Failure = true; } return(returnState); }
public bool MatchesConfiguration(PDARunState state) { return InputChar == state.Input[0] && OldState == state.State && ((StackHead == '_' && state.Stack.Count() == 0) || (state.Stack.Count() > 0 && StackHead == state.Stack[0])); }
/// <summary> /// Return a list of all next possible states, having followed any /// applicable transitions, given the current state. /// </summary> private IEnumerable<PDARunState> FindNextStates(PDARunState state) { // Follow each possible transition available. return from transition in Transitions where transition.MatchesConfiguration(state) select ApplyTransition(transition, state); }
/// <summary> /// Applies the given transition, to the given run state. /// </summary> /// <returns> /// An IEnumerable of the states resulting from applying the transition. /// </returns> private static PDARunState ApplyTransition(PDATransition transition, PDARunState state) { var newState = transition.NewState; var newStack = state.Stack; var newInput = state.Input.Substring(1); var newMatchedSoFar = state.MatchedSoFar + 1; var isFailure = false; // If we're not matching against the empty stack, try pop. if (transition.StackHead != '_') { if (newStack.Length > 0) { newStack = newStack.Substring(1); } else { // Cannot pop from the stack, so bail out here. isFailure = true; } } else if (newStack != "") { // Catch illegal use of empty stack transition. isFailure = true; } if (!isFailure) { newStack = transition.StackReplace + newStack; } var returnState = new PDARunState(newInput, newMatchedSoFar, newStack, newState); // Check for end-of-input without empty stack - failure. // We won't be able to empty the stack if there are fewer input // chars than stack elems, so fail to prevent unnecessary work. if (isFailure || newInput == "" && newStack.Length > 0 || newInput.Length < newStack.Length) { returnState.Failure = true; } return returnState; }