# Implementing the Shunting Yard Algorithm

• 03-01-2011
manasij7479
Implementing the Shunting Yard Algorithm
What I already have correctly done :
Code:

```class token {     public:     token(const token& _t){num = _t.num;opcode = _t.opcode;};     token(std::string _in); //For parts of the input string     token(double n){opcode='n';num=n;};//     token(void){opcode=0;num=0;};     char opcode;  // 'n' for operands and the first character in operators     double num; //value for operands and no of operands for operators     int oppr;  //for precedence       token operator+(token t){num+=t.num;return *this;};     token operator-(token t){num-=t.num;return *this;};     token operator*(token t){num*=t.num;return *this;};     token operator/(token t){num/=t.num;return *this;}; };```
Everything works correctly in this.

Say I want a function
Code:

`std::deque<token> infix_to_rpn(std::deque<token>)`
I wrote the following ...but it doesn't work and I am not able to follow what is inside the containers ...when debugging.
Code:

```deque<token> infix_to_rpn(deque<token> infix) {     deque<token> rpn,op_s;//op_s for operator stack     token *t,*s_t;//s_t for stack temp     while(!infix.empty())     {         t = new token(infix.front());         if(t->opcode=='n')rpn.push_back(*t);         else if(t->opcode!='('&&t->opcode!=')'&&t->opcode!=','&&t->num==1)         {             op_s.push_back(*t);         }         else if(t->opcode == ',')         {             bool flag(1);             while(flag)             {                 s_t = new token(op_s.back());                 if(s_t->opcode=='('){flag=0;break;}                 op_s.pop_back();                 rpn.push_back(*s_t);             }         }         else if(t->opcode!='('&&t->opcode!=')'&&t->opcode!=','&&t->num==2)         {             bool flag(1);             while(flag)             {                 s_t = new token(op_s.back());                 if(t->oppr>=s_t->oppr)                 {                     op_s.pop_back();                     rpn.push_back(*s_t);                 }                 else{flag=0;rpn.push_back(*t);break;}             }         }         else if(t->opcode == '(')         {             rpn.push_back(*t);         }         else if(t->opcode == ')')         {             bool flag(1);             while(flag)             {                 s_t = new token(op_s.back());                 if(s_t->opcode!='(')                   {                       op_s.pop_back();                       rpn.push_back(*s_t);                   }                 else if (s_t->opcode == '(')                     {                         op_s.pop_back();                         flag = 0;                         break;                     }             }         }         else if(!op_s.empty())         {             while(!op_s.empty())             {                 s_t = new token(op_s.back());                 op_s.pop_back();                 rpn.push_back(*s_t);             }         }     }     return rpn; }```
I am 'almost' sure that I followed the steps of the algorithm correctly ! ...though it is clear that I didn't.. :(