Translating from Infix to Postfix (Pseudocode)


This algorithm assumes that the given infix expression is valid.


   get token
   while more tokens
      if token is an operand
         concat token to postexpr
      endif
      if token is '('
         push token
      endif
      if token is ')'
         while top is not '('
            pop val from stack
            concat val to postexpr
         enddo
         pop val   //  this is the '('
      endif
      if token is an operator
         while stack not empty && top is not '(' && 
                           precedence of token <= precedence of top
            pop val from stack
            concat val to postexpr
         enddo
         push token
      endif
      get token
   enddo
   while stack not empty
      pop val
      concat val to postexpr
   enddo
   output postexpr



Email Me | Office Hours | My Home Page | Department Home | MCC Home Page