my $token_count = 0; while (@tokens) { my $token = shift @tokens; # if its an opening paran then ... if ($token eq '(') { push(@operator_stack, $token); } # if it's an operator then ... elsif (is_operator($token)) { push(@operator_stack, $token); } # if its a closing paran then ... elsif ($token eq ')') { # empty the operator stack until # we find the opening paren while (@operator_stack && $operator_stack[-1] ne '(') { # and push everything onto the # operator stack push @program_stack, (pop @operator_stack); } # then pop the opening paren off pop @operator_stack; } # if its not an operator, or a paran ... else { push @program_stack, $token; $token_count++; # if we have 2 or more tokens on the # program stack (since out last operator) # we need to get an operator .... if ($token_count >= 2){ # unless the top of our operator stack # is a opening paran, in which case # we have a sub-expression which needs # to be handled first next if $operator_stack[-1] eq '('; # otherwise grab the operator, and ... push @program_stack, (pop @operator_stack); # reset the token count $token_count = 1; } } } # and lastly, empty whats left of your # operator stack onto your program stack push @program_stack, (pop @operator_stack) while (@operator_stack);