public ShuntingYard(Tokenizer T, string input) { T.setInput(input); Token previous = null; Token t = null; while (!(T.atEnd())) { previous = t; t = T.next(); //Console.WriteLine("My token symbol:" + t.Symbol); //Console.WriteLine("My token Lexeme:" + t.Lexeme); if (t.Lexeme == "-") { if ((previous == null) || (previous.Symbol == "LPAREN") || (previous.Symbol == "NUM") || (previous.Symbol == "ID")) { t.Symbol = "NEGATE"; } } if ((t.Symbol == "NUM") || (t.Symbol == "ID")) //ID could be variable num is just numbers { numStack.Push(new TreeNode(t.Symbol, t)); } /*else if (t.Symbol == "WHITESPACE") * { * Console.WriteLine("WHit"); * }*/ else { handleOperator(t, operatorStack, numStack); } } while (operatorStack.Count() != 0) //while not empty { var opNode = operatorStack.Pop(); var c1 = numStack.Pop(); var c2 = numStack.Pop(); opNode.addChild(c2); opNode.addChild(c1); //If we got something on the operator stack, +-*, at this point we have an operator that needs dealt with //opNode is +, get it's two children, two most recent. Pop those c1 and c2. c2, this is like creating the leaf nodes //the thing on numstack that gets pushed is like // + // / \ <--opNode // 1 2 numStack.Push(opNode); // Console.WriteLine("numStack Peek" + numStack.Peek().sym); // Parse through these nodes and solves // if sym opNode // At this point we want to check to // sym == "ADDOP" // c2 + c1 // // COME BACK TO THIS } // nothing left in operatorStack // ALL NODES ARE IN NUMSTACK! //Console.WriteLine(numStack.Peek().sym); //recursively goes var numNode = numStack.Pop(); var answer = handleNode(numNode); Console.WriteLine(answer); // if you have an op on numstack it has children // to reference pop //Console.WriteLine(handleNode(numStack.Peek().children[0])); //Console.WriteLine(handleNode(numStack.Peek().children[1])); }
public PostfixedTokens(List <IToken> inputTokens) { foreach (IToken token in inputTokens) { switch (token.Type) { case TokenType.keyword: InAList.Add(token); break; case TokenType.number: InAList.Add(token); break; case TokenType.variable: InAList.Add(token); break; case TokenType.function: operatorStack.Push(token); numberOfFunctionParameters.Add(0); break; case TokenType.argSeperator: if (operatorStack.Count() == 0) { ErrorLog.Add(new ErrorMessage(token.TokenString + " operator syntax error.")); } else { if (numberOfFunctionParameters.Count > 0) { numberOfFunctionParameters[numberOfFunctionParameters.Count - 1] = numberOfFunctionParameters.Last() + 1; } while (operatorStack.First().Type != TokenType.openBrace) { InAList.Add(operatorStack.Pop()); } } break; case TokenType.infixOperator: handleOperator(token); break; case TokenType.suffixOperator: InAList.Add(token); break; case TokenType.openBrace: operatorStack.Push(token); break; case TokenType.closedBrace: if (numberOfFunctionParameters.Count() > 0) { numberOfFunctionParameters[numberOfFunctionParameters.Count - 1] = numberOfFunctionParameters.Last() + 1; } while (operatorStack.First().Type != TokenType.openBrace) { if (operatorStack.Count() == 0) { ErrorLog.Add(new ErrorMessage("mismatched parenthesis error")); } InAList.Add(operatorStack.Pop()); } operatorStack.Pop(); //Pop the left parenthesis off the stack if (operatorStack.Count > 0 && operatorStack.First().Type == TokenType.function) { FunctionToken tokenToAdd = (FunctionToken)operatorStack.Pop(); tokenToAdd.numberOfChildren = numberOfFunctionParameters.Last(); numberOfFunctionParameters.RemoveAt(numberOfFunctionParameters.Count() - 1); InAList.Add(tokenToAdd); } break; default: throw new Exception("Unknown token type"); } } while (operatorStack.Count() > 0) { InAList.Add(operatorStack.Pop()); } //TODO: Handle mismatched bracket exception }
public ShuntingYard(Tokenizer T, string input) { T.setInput(input); Token previous = null; Token t = null; while (!(T.atEnd())) { previous = t; t = T.next(); if (t.Lexeme == "-") { if ((previous == null) || (previous.Symbol == "LPAREN") || ((previous.Symbol != "NUM") && (previous.Symbol != "ID") && (previous.Symbol != "RPAREN"))) { t.Symbol = "NEGATE"; } } if ((t.Symbol == "NUM") || (t.Symbol == "ID")) //ID could be variable num is just numbers { numStack.Push(new TreeNode(t.Symbol, t)); } else if (t.Symbol == "LPAREN") { operatorStack.Push(new TreeNode(t.Symbol, t)); } else if (t.Symbol == "RPAREN") { //Console.WriteLine("HERE"); while (operatorStack.Peek().sym.Contains("LPAREN") == false) { var opNode = operatorStack.Pop(); var c1 = numStack.Pop(); if (opNode.sym != "NEGATE") { var c2 = numStack.Pop(); opNode.addChild(c2); } opNode.addChild(c1); numStack.Push(opNode); } operatorStack.Pop(); } else { handleOperator(t, operatorStack, numStack); } } while (operatorStack.Count() != 0) //while not empty m { var opNode = operatorStack.Pop(); var c1 = numStack.Pop(); if (opNode.sym != "NEGATE") { var c2 = numStack.Pop(); opNode.addChild(c2); } opNode.addChild(c1); numStack.Push(opNode); } var numNode = numStack.Pop(); var answer = handleNode(numNode); Console.WriteLine(answer); }