public void Append(ConstraintOperator op) { op.LeftContext = lastPushed; if (lastPushed is ConstraintOperator) { SetTopOperatorRightContext(op); } ReduceOperatorStack(op.LeftPrecedence); ops.Push(op); lastPushed = op; }
public void Push(Token p_token) { //push in operator/ operand stack if (p_token is Operator) { //operatorStack. if (operatorStack.Count == 0 || (operatorStack.Count > 0 && operatorStack.Peek().Priority < (p_token as Operator).Priority)) { //do remaining operation operatorStack.Push(p_token as Operator); } else { List <Token> args = new List <Token>(); for (int i = 0; i < 2; i++) //as these all are binary operators { args.Insert(0, LiteralStack.Pop()); } Token tkn = OperatorStack.Pop().Evalute(args); LiteralStack.Push(tkn as Literal); OperatorStack.Push(p_token as Operator); } } else if (p_token is Literal) { LiteralStack.Push(p_token as Literal); } }
private void BuildNodes(string[] expressionArray) { foreach (string item in expressionArray) { double tempNumber; if (double.TryParse(item, out tempNumber)) { TreeNode <string> number_node = new TreeNode <string> { Data = tempNumber.ToString() }; NumberStack.Push(number_node); } else { TreeNode <string> operator_node = new TreeNode <string> { Data = item }; OperatorStack.Push(operator_node); } } }
/// <summary> /// Performs the Shunting-Yard Algorithm /// </summary> public void Shunt() { // O(n) if (!ShuntValid) { throw new Exception("Shunt() was called without proper initialisation. Check ShuntValid first!"); } var working = new List <Symbol>(); foreach (var symbol in Input) { if (symbol.GetType() == typeof(Operand)) { working.Add(symbol); // Add any numbers straight to output } else if (symbol.ToString() != "(" && symbol.ToString() != ")") { // Operators var op = (Operator)symbol; // If function, Push() to stack and continue; if (op.Op == Operators.Function) { OperatorStack.Push(op); continue; } while ((OperatorStack.Count != 0 && // Stack not empty! (OperatorStack.Peek().Precedence > op.Precedence || // Top operator has higher precedence than current one (OperatorStack.Peek().Precedence == op.Precedence && !OperatorStack.Peek().IsRightAssociative)) ) && // Top op has equal precendence and is left associative OperatorStack.Peek().Op != Operators.OpenBracket) // Top op is not opening bracket { working.Add(OperatorStack.Pop()); } OperatorStack.Push(op); // Add operator to stack } else { switch (symbol.ToString()) { case "(": // Opening Bracket OperatorStack.Push((Operator)symbol); break; case ")": // Closing Bracket { while (OperatorStack.Peek().Op != Operators.OpenBracket) { if (OperatorStack.Count == 0) { throw new Exception("Ran out of stack while looking for left bracket: input had mismatched brackets."); } working.Add(OperatorStack.Pop()); } if (OperatorStack.Peek().Op == Operators.OpenBracket) { OperatorStack.Pop(); // Discard extra brackets } break; } } } } working.AddRange(OperatorStack); // Final step: pop out entire stack to output OperatorStack.Clear(); // Apparently AddRange() will Peek() instead of Pop() if the stack only has one element, failing to clear it Output = working.ToArray(); }