Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Genlex - and which parser?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi VANICAT <vanicat@l...>
Subject: Re: [Caml-list] Genlex - and which parser?
Oliver Bandel <oliver@first.in-berlin.de> writes:

> Hello Rémi,
>
>
> On Mon, 2 Sep 2002, Remi VANICAT wrote:
>
>> Oliver Bandel <oliver@first.in-berlin.de> writes:
> [...]
>> > Which parser can be used together with Genlex?
>> > Is it ocamlyacc? Or others?
>> 
>> ocamlyacc goes with ocamllex. parser that goes with Genlex are stream
>> parser, describe in chapter 2 of the Camlp4 reference manual and
>> chapter 2 of the Camlp4 tutorial
>
> Hmhhh in the campl4-tutorial/-reference they explain an other
> Lexer (Plexer). I think it's similar.
> Will Genlex not be used? Or is Plexer based on Genlex?
>
> Any way, thanks for your hint.

Well, there is to type of parser that you can read about in the camlp4
documentation :
those of the chapter 2 (Streams and parsers) which correspond to the
Genlex lexer, and those of the Camlp4 Grammar module, which use
Plexer. 

Here an example of the Stream parser :

open Genlex;;

let lexer = make_lexer ["+"; "-"; "("; ")"];;

let rec simple_calc = parser
   | [< v = simple_value ; rest >] -> 
          simple_calc_rest v rest
and simple_value = parser
   | [< 'Int i >] -> i
   | [< 'Kwd "("; v = simple_calc; 'Kwd ")" >] -> v
and simple_calc_rest v1 = parser
   | [< 'Kwd "+"; v2 = simple_calc >] -> v1 + v2
   | [< 'Kwd "-"; v2 = simple_calc >] -> v1 - v2
   | [< >] -> v1;;

let make_calc () =
  let l = read_line () in
  let st = Stream.of_string l in
  let le = lexer st in
  simple_calc le;;

(notice that this parser is buggy : it evaluated 3 - 1 + 3 as -1 when
it should answer 5).

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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