Beispiel #1
0
        //What cleaning i have to do
        public void Clear()
        {
            current = Token.InValid;
            //Current Sysmboool
            symbol  = null;
            Curr_op = null;                     // current operator
            literal = 0;                        // current literal

            Curr_idx = 0;                       // subExpression Start position
            _exp     = null;                    // Expression to be searched
            while (OpStack.IsEmpty() != true)
            {
                OpStack.Pop();
            }
            while (NodeStack.IsEmpty() != true)
            {
                NodeStack.Pop();
            }
        }
Beispiel #2
0
        //Parse Expression funtion main funtion
        //Creating the Exp tree from the Infix Expression
        public ExpTreeNode ParseExpression(string exp)
        {
            //Check for the Expresiioon validity
            if (!Isvalid_Exp(exp))
            {
                //Throw an Error Saying Commas are not proper
                ErrorException Er = new ErrorException("Operators ( or ) are not at the right places");
                throw Er;
                //Is c# need this return after that -- i dont think so but anyway
            }
            //Fill ur _exp Variable will need that
            _exp = exp;

            NextToken();
            do
            {
                //The errors that might come would be from stack
                //If that went wrong -- gone case -- What use rwould do
                //No USer specific Errors from this funtion
                switch (current)
                {
                case Token.Literal:
                    //CreateLeafNode and push it on the stack
                    ExpTreeNode LNode = new ExpTreeNode();
                    LNode._value  = literal;
                    LNode._ndType = ExpTreeNode.NodeType.Literal;
                    NodeStack.Push(LNode);
                    break;

                case Token.Operator:
                    //pop the St
                    ExpTreeNode ONode = new ExpTreeNode();
                    ONode._value  = Curr_op;
                    ONode._ndType = ExpTreeNode.NodeType.Operator;
                    if (Curr_op == "(")
                    {
                        OpStack.Push(ONode);
                        break;
                    }
                    if (Curr_op == ")")
                    {
                        while ("(" != (string)OpStack.Top._value)
                        {
                            PopConnectPush();
                        }
                        OpStack.Pop();                                  //Throw away {{
                        break;
                    }

                    if (OpStack.IsEmpty())
                    {
                        OpStack.Push(ONode);
                    }
                    else if ((string)OpStack.Top._value == "(")
                    {
                        OpStack.Push(ONode);
                    }
                    else if (Priority(OpStack.Top._value.ToString()) < Priority(ONode._value.ToString()))
                    {
                        OpStack.Push(ONode);
                    }
                    else
                    {
                        while (true)
                        {
                            PopConnectPush();
                            if (OpStack.IsEmpty() == true || (string)OpStack.Top._value == "(" || Priority(OpStack.Top._value.ToString()) < Priority(ONode._value.ToString()))
                            {
                                break;
                            }
                        }
                        OpStack.Push(ONode);
                    }
                    break;

                case Token.Symbol:
                {
                    ExpTreeNode SNode = new ExpTreeNode();
                    SNode._symbol = symbol;
                    SNode._ndType = ExpTreeNode.NodeType.Symbol;
                    NodeStack.Push(SNode);
                }
                break;

                default:
                    break;
                }
            }while(NextToken() != Token.Eof);

            while (OpStack.IsEmpty() == false)
            {
                PopConnectPush();
            }
            //Get the Top of the Node Stack thats the root
            return(NodeStack.Top);
        }