Example #1
0
        /// <summary>
        /// Converts an infix expression represented an array of strings to a
        /// corresponding RPN expression as a stack.
        /// </summary>
        /// <param name="inputTokens"></param>
        /// <returns></returns>
        private CloneableStack <Symbol> InfixToRPN(string[] inputTokens)
        {
            //ArrayList outList = new ArrayList();
            CloneableStack <Symbol> result = new CloneableStack <Symbol>(0);
            Stack <string>          stack  = new Stack <string>();

            //for all the input tokens read the next token
            foreach (string token in inputTokens)
            {
                if (IsOperator(token))
                {
                    //If token is an operator
                    while (stack.Count != 0 && IsOperator(stack.Peek()))
                    {
                        if ((IsAssociative(token, LEFT_ASSOC) && CmpPrecedence(token, stack.Peek()) <= 0) ||
                            (IsAssociative(token, RIGHT_ASSOC) && CmpPrecedence(token, stack.Peek()) < 0))
                        {
                            result.Push(ToSymbol(stack.Pop()));
                            continue;
                        }
                        break;
                    }
                    //Push the new operator on the stack
                    stack.Push(token);
                }
                else if (token.Equals("("))
                {
                    stack.Push(token);
                }
                else if (token.Equals(")"))
                {
                    while (stack.Count != 0 && !stack.Peek().Equals("("))
                    {
                        result.Push(ToSymbol(stack.Pop()));
                    }
                    stack.Pop();
                }
                else
                {
                    result.Push(ToSymbol(token));
                }
            }

            while (stack.Count != 0)
            {
                result.Push(ToSymbol(stack.Pop()));
            }

            CloneableStack <Symbol> actualResult = new CloneableStack <Symbol>(result.Count());

            while (result.Count() > 0)
            {
                actualResult.Push(result.Pop());
            }
            return(actualResult);
        }
Example #2
0
 public void Push(IndentType type)
 {
     indentStack.Push(type);
     curIndent += GetIndent(type);
     Update();
 }