Task

Given the operator characteristics and input from the Shunting-yard algorithm page and tables, use the algorithm to show the changes in the operator stack and RPN output
as each individual token is processed.

 Assume an input of a correct, space separated, string of tokens representing an infix expression
 Generate a space separated output string representing the RPN
 Test with the input string:
  3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3  
 print and display the output here.
 Operator precedence is given in this table:


 operator 
 precedence 
 associativity 
 operation


  ^  
 4 
 right 
 exponentiation


  *  
 3 
 left 
 multiplication


  /  
 3 
 left 
 division


  +  
 2 
 left 
 addition


  -  
 2 
 left 
 subtraction



Extra credit
Add extra text explaining the actions and an optional comment for the action on receipt of each token.



Note
The handling of functions and arguments is not required.



See also

 Parsing/RPN calculator algorithm for a method of calculating a final value from this output RPN expression.
 Parsing/RPN to infix conversion.


 
type associativity = Left | Right;;
 
 
let prec op =
  match op with
  | "^" -> 4
  | "*" -> 3
  | "/" -> 3
  | "+" -> 2
  | "-" -> 2
  | _ -> -1;;
 
 
let assoc op =
  match op with
  | "^" -> Right
  | _ -> Left;;
 
 
let split_while p =
  let rec go ls xs =
    match xs with
    | x::xs' when p x -> go (x::ls) xs'
    | _ -> List.rev ls, xs
  in go [];;
 
 
let rec intercalate sep xs =
  match xs with
  | [] -> ""
  | [x] -> x
  | x::xs' -> x ^ sep ^ intercalate sep xs';;
 
 
let shunting_yard = 
  let rec pusher stack queue tkns =
    match tkns with
    | [] -> List.rev queue @ stack
    | "("::tkns' -> pusher ("("::stack) queue tkns'
    | ")"::tkns' ->
        let mv, "("::stack' = split_while ((<>) "(") stack in
        pusher stack' (mv @ queue) tkns'
    | t::tkns' when prec t < 0 -> pusher stack (t::queue) tkns'
    | op::tkns' ->
        let mv_to_queue op2 = 
          (match assoc op with
            | Left -> prec op <= prec op2
            | Right -> prec op < prec op2)
        in
        let mv, stack' = split_while mv_to_queue stack in
        pusher (op::stack') (mv @ queue) tkns'
  in pusher [] [];;
 
 
let () = 
  let inp = read_line () in
  let tkns = String.split_on_char ' ' inp in
  let postfix = shunting_yard tkns in
  print_endline (intercalate " " postfix);;
 