//very Important function-=-Self Explainatory private void PopConnectPush() { ExpTreeNode Temp = OpStack.Pop(); //Get Top operator and associate the operands //Find here if the Operator is "=" - then dont make the Symbol InVALID--Give some value Temp._right = NodeStack.Pop(); //Get Left and right from NodeStck if (Temp._value.ToString() == "=") { ExpTreeNode TmpLeft = NodeStack.Pop(); //Now this needs to be Symbol with no Sub tree if (TmpLeft._ndType != ExpTreeNode.NodeType.Symbol) { ErrorException Er = new ErrorException("Operator = at the wrong Place Left Operand must be lvalue "); throw Er; } if (TmpLeft._left == null || TmpLeft._right == null) { TmpLeft._value = -1; //SYMBOL TO BE CALCULATED Temp._left = TmpLeft; NodeStack.Push(Temp); // puch the sub tree on the stack return; } else { ErrorException Er = new ErrorException("Operator = at the Wrong Place Left Operand must be lvalue"); throw Er; } } Temp._left = NodeStack.Pop(); NodeStack.Push(Temp); // puch the sub tree on the stack }
//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); }