The trailing ;
is the source of the syntax error, it makes the rest of your code reads as
let _ =
add my_stack 3;
let popped = pop my_stack in Printf.printf "%d\n" popped
The playground toplevel is then discarding this last incomplete sentence because you may complete it later whereas ocamlc
has to fail on the incomplete sentence.