Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- type expr =
- | Op of (float -> float -> float)
- | Const of float
- | NL
- }
- let digit = ['0'-'9']
- let float = digit+ ('.' digit+)?
- rule token = parse
- | [ ' ' '\t' ] { token lexbuf }
- | float as f { let f = float_of_string f in Const f }
- | '+' { Op (+.) }
- | '/' { Op (/.) }
- | '-' { Op (-.) }
- | '*' { Op ( *. ) }
- | '\n' { NL }
- | eof { exit 0 }
- | _ as c { failwith (Printf.sprintf "invalid char: %c\n" c) }
- {
- let main =
- let lexbuf = Lexing.from_channel stdin in
- let rec aux stack =
- match token lexbuf with
- | Const f -> aux (f::stack)
- | NL -> Format.printf "%f@." (List.hd stack); aux stack
- | Op f ->
- match stack with
- | x :: y :: r -> aux ( f y x :: r)
- | _ -> assert false
- in
- aux []
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement