Version française
Home     About     Download     Resources     Contact us    
Browse thread
unix.select + camlp4
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] unix.select + camlp4
On Sunday 01 July 2007 21:30:44 Anastasia Gornostaeva wrote:
> Is it possible to use camlp4-based parser with data which comes from
> Unix.select input? Read a line, parse it, if a line was incomplete to parse
> then save a state, continue.... How do to do it?

Bruno gave an excellent example of parsing strings using camlp4, posted on 
22/06/2007:

# open Camlp4.PreCast ;;
# let expr = Gram.Entry.mk "expr" ;;
val expr : '_a Camlp4.PreCast.Gram.Entry.t = <abstr>
# EXTEND Gram
     expr:
       [ "add" LEFTA
         [ e1 = expr; "+"; e2 = expr -> e1 +: e2
         | e1 = expr; "-"; e2 = expr -> e1 -: e2 ]
       | "mult" LEFTA
         [ e1 = expr; "*"; e2 = expr -> e1 *: e2 ]
       | "simple" NONA
         [ n = INT -> Num(int_of_string n)
         | "("; e = expr; ")" -> e ] ]
       ;
   END ;;
- : unit = ()
# Gram.parse expr Loc.ghost (Stream.of_string "1-2+3*4") ;;
- : expr = Add (Sub (Num 1, Num 2), Mult (Num 3, Num 4))

If you can read a line from Unix.select then you should be able to feed it 
into this.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal
http://www.ffconsultancy.com/products/ocaml_journal/?e