Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #load "str.cma";;
- let string_to_tokens = Str.split (Str.regexp " +");;
- let push nombre liste = nombre::liste;;
- let pop = function
- | [] ->failwith "Pile vide ! "
- | head::tail -> (head,tail);;
- let is_operator = function
- |"+"|"-"|"/"|"*" -> true
- | _ -> false
- let operator_to_function = function
- | "+" -> ( + )
- | "-" -> ( - )
- | "/" -> ( / )
- | "*" -> ( * )
- | _ -> failwith "Operateur inconnu !";;
- let rec parse liste pile =
- match liste with
- | [] -> let (reponse,pilevide) = pop pile in
- reponse
- | token::reste -> Printf.printf "Token:%s \n" token;
- let pilenew = if (is_operator token) then
- let (b,pileun) = pop pile in
- let (a,piledeux) = pop pileun in
- push ((operator_to_function token) a b) piledeux
- else push (int_of_string token) pile in
- parse reste pilenew;;
- let () =
- if (Array.length Sys.argv = 2) then
- let arg = Sys.argv.(1) in
- let chaine = string_to_tokens arg in
- try (
- let result = parse chaine [] in
- Printf.printf "\n==> Resultat : %d\n\n" result;
- exit 0
- ) with
- | Failure a -> Printf.printf "[Erreur] %s\n" (if (a="int_of_string") then "conversion en nombre impossible !" else a)
- else exit 1;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement