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"))
                    {
                        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
                {
                    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);

            Console.WriteLine(handleNode(numStack.Peek()));
            //Console.WriteLine(handleNode(numStack.Peek().children[0]));
            //Console.WriteLine(handleNode(numStack.Peek().children[1]));
        }
示例#2
0
        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);
        }