From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA19972; Fri, 28 Mar 2003 21:13:17 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA18639 for ; Fri, 28 Mar 2003 21:13:16 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h2SKD7521753; Fri, 28 Mar 2003 21:13:07 +0100 (MET) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA20483; Fri, 28 Mar 2003 21:13:06 +0100 (MET) From: Pierre Weis Message-Id: <200303282013.VAA20483@pauillac.inria.fr> Subject: Re: [Caml-list] Announce: Regexp/OCaml syntax extension In-Reply-To: from Yutaka OIWA at "Mar 21, 103 01:50:36 am" To: oiwa@yl.is.s.u-tokyo.ac.jp (Yutaka OIWA) Date: Fri, 28 Mar 2003 21:13:06 +0100 (MET) Cc: caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; pierre:01 weis:01 caml-list:01 camlp:01 -macro:01 parses:01 foobar:01 yutaka:01 oiwa:01 u-tokyo:01 shibuya:01 scanf:01 bscanf:01 regexp:01 cristal:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > Hello subscribers: > > I release a camlp4-macro package called Regexp/OCaml. [...] > For example, parsing an entry for some log file becomes as easy as follows: > > try > while true do > let line = input_line ic in > Regexp.match line with > "^\((\d\d):(\d\d):(\d\d)\)\[(.*?)\] (.*)$" > as hour : int, min: int, sec : int, name, line -> > let time = hour * 3600 + min * 60 + sec in > ... > | "^# (.*)$" as meta_info -> > ... > | _ -> () > done > with End_of_file -> () > > This short code parses both line in format like > "(00:34:32) [foobar] something" and "# some meta info" > and binds appropriate data into variables which can be used inside "...". > Compare the code above with an equivalent without using syntax extension. [...] > -- > Yutaka Oiwa Yonezawa Lab., Dept. of Computer Science, > Graduate School of Information Sci. & Tech., Univ. of Tokyo. > , > PGP fingerprint = C9 8D 5C B8 86 ED D8 07 EA 59 34 D8 F4 65 53 61 Your camlp4 expertise is impressive :) However, since you propose to compare with other way to program your example, I would suggest the simple use of Scanf, that gives: try while true do let ib = Scanning.from_string (input_line ic) in match bscanf ib "%c" (fun x -> x) with | '(' -> bscanf ib "%d:%d:%d) [%s] %s" (fun hour min sec name line -> let time = hour * 3600 + min * 60 + sec in ...) | '#' -> Scanf.scanf " %s" (fun meta_info -> ...) | _ -> () done with End_of_file -> () This sounds pretty simple, compact, and easy to understand as well... Best regards, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners