예제 #1
0
        public override bool Equals(Object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            PDATransition pdat = (PDATransition)obj;

            return(InputChar == pdat.InputChar &&
                   StackHead == pdat.StackHead &&
                   StackReplace.SequenceEqual(pdat.StackReplace) &&
                   NewState == pdat.NewState &&
                   OldState == pdat.OldState);
        }
예제 #2
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);
        }
예제 #3
0
        private static PDACondition ApplyTransition(PDATransition transition, PDACondition condition)
        {
            var newCurrentInput = new Stack <string>(new Stack <string>(condition.currentInput));
            var newStack        = new Stack <string>(new Stack <string>(condition.stack));

            //We always pop something from stack
            newStack.Pop();

            //If we should read something from input, we remove it from current input
            if (transition.readFromInput != "")
            {
                newCurrentInput.Pop();
            }
            //Pushing elements to stack
            foreach (string elementToPush in transition.pushToStack.Reverse())
            {
                if (elementToPush != "")
                {
                    newStack.Push(elementToPush);
                }
            }

            return(new PDACondition(newCurrentInput, newStack, transition.nextState));
        }
예제 #4
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;
        }