public Tokenizer(string szExpression, ExpressionType exType) { m_szExpression = szExpression; m_exType = exType; m_RegEx = new Regex(OperatorHelper.GetOperatorsRegEx(m_exType)); m_strarrTokens = SplitExpression(szExpression); }
/// <summary> /// Factory method to create Operator objects. /// </summary> /// <param name="szOperator"></param> /// <returns></returns> static public IOperator CreateOperator(string szOperator) { IOperator oprtr = null; if (OperatorHelper.IsArithmeticOperator(szOperator)) { oprtr = new ArithmeticOperator(szOperator); return(oprtr); } if (OperatorHelper.IsComparisonOperator(szOperator)) { oprtr = new ComparisonOperator(szOperator); return(oprtr); } if (OperatorHelper.IsLogicalOperator(szOperator)) { oprtr = new LogicalOperator(szOperator); return(oprtr); } throw new RPN_Exception("Unhandled Operator : " + szOperator); }
/// <summary> /// Algo of GetPostFixNotation (source : Expression Evaluator : using RPN by lallous /// in the C++/MFC section of www.CodeProject.com. /// 1. Initialize an empty stack (string stack), prepare input infix expression and clear RPN string /// 2. Repeat until we reach end of infix expression /// I. Get token (operand or operator); skip white spaces /// II. If token is: /// a. Left parenthesis: Push it into stack /// b. Right parenthesis: Keep popping from the stack and appending to /// RPN string until we reach the left parenthesis. /// If stack becomes empty and we didn't reach the left parenthesis /// then break out with error "Unbalanced parenthesis" /// c. Operator: If stack is empty or operator has a higher precedence than /// the top of the stack then push operator into stack. /// Else if operator has lower precedence then we keep popping and /// appending to RPN string, this is repeated until operator in stack /// has lower precedence than the current operator. /// d. An operand: we simply append it to RPN string. /// III. When the infix expression is finished, we start popping off the stack and /// appending to RPN string till stack becomes empty. /// /// </summary> /// <param name=szExpr></param> /// public ArrayList GetPostFixNotation(string szExpr, Type varType, bool bFormula) { Stack stkOp = new Stack(); ArrayList arrFinalExpr = new ArrayList(); string szResult = ""; Tokenizer tknzr = new Tokenizer(szExpr); foreach (Token token in tknzr) { string szToken = token.Value.Trim(); if (szToken.Length == 0) { continue; } if (!OperatorHelper.IsOperator(szToken)) { Operand oprnd = OperandHelper.CreateOperand(szToken, varType); oprnd.ExtractAndSetValue(szToken, bFormula); arrFinalExpr.Add(oprnd); szResult += szToken; continue; } string szOp = szToken; if (szOp == "(") { stkOp.Push(szOp); } else if (szOp == ")") { string szTop; while ((szTop = (string)stkOp.Pop()) != "(") { IOperator oprtr = OperatorHelper.CreateOperator(szTop); arrFinalExpr.Add(oprtr); szResult += szTop; if (stkOp.Count == 0) { throw new RPN_Exception("Unmatched braces!"); } } } else { if (stkOp.Count == 0 || (string)stkOp.Peek() == "(" || OperatorHelper.IsHigherPrecOperator(szOp, (string)stkOp.Peek())) { stkOp.Push(szOp); } else { while (stkOp.Count != 0) { if (OperatorHelper.IsLowerPrecOperator(szOp, (string)stkOp.Peek()) || OperatorHelper.IsEqualPrecOperator(szOp, (string)stkOp.Peek())) { string szTop = (string)stkOp.Peek(); if (szTop == "(") { break; } szTop = (string)stkOp.Pop(); IOperator oprtr = OperatorHelper.CreateOperator(szTop); arrFinalExpr.Add(oprtr); szResult += szTop; } else { break; } } stkOp.Push(szOp); } } } while (stkOp.Count != 0) { string szTop = (string)stkOp.Pop(); if (szTop == "(") { throw new RPN_Exception("Unmatched braces"); } IOperator oprtr = OperatorHelper.CreateOperator(szTop); arrFinalExpr.Add(oprtr); szResult += szTop; } return(arrFinalExpr); }