{ let lines = ref 0;; let words = ref 0;; let chars = ref 0;; } rule count = parse | '\n' { lines := !lines +1 ; chars := !chars +1 ; count lexbuf } | [^ ' ' '\t' '\n']+ as word {words := !words + 1;chars := (!chars + String.length word) ;count lexbuf } | _ {chars := !chars +1 ; count lexbuf } | eof { (!lines, !words, !chars) } { let main () = let cin = if Array.length Sys.argv > 1 then open_in Sys.argv.(1) else stdin in let lexbuf = Lexing.from_channel cin in let (l, w, c) = count lexbuf in Printf.printf "%d lines, %d words, %d chars\n" l w c let _ = Printexc.print main () }