812a1686219b2f85db051d2aa3f640b4d7a23713
[bpt/coccinelle.git] / bundles / menhirLib / menhir-20120123 / demos / calc-param / calc.ml
1 (**************************************************************************)
2 (* *)
3 (* Menhir *)
4 (* *)
5 (* François Pottier, INRIA Rocquencourt *)
6 (* Yann Régis-Gianas, PPS, Université Paris Diderot *)
7 (* *)
8 (* Copyright 2005-2008 Institut National de Recherche en Informatique *)
9 (* et en Automatique. All rights reserved. This file is distributed *)
10 (* under the terms of the Q Public License version 1.0, with the change *)
11 (* described in file LICENSE. *)
12 (* *)
13 (**************************************************************************)
14
15 (* Let's do floating-point evaluation, for a change. *)
16
17 module FloatSemantics = struct
18
19 type number =
20 float
21
22 let inject =
23 float_of_int
24
25 let ( + ) = ( +. )
26 let ( - ) = ( -. )
27 let ( * ) = ( *. )
28 let ( / ) = ( /. )
29 let (~- ) = (~-. )
30
31 end
32
33 (* Let us now specialize our parameterized parser. *)
34
35 module FloatParser =
36 Parser.Make(FloatSemantics)
37
38 (* The rest is as usual. *)
39
40 let () =
41 let stdinbuf = Lexing.from_channel stdin in
42 while true do
43 (* Read line by line. *)
44 let linebuf = Lexing.from_string (Lexer.line stdinbuf) in
45 try
46 (* Run the parser on a single line of input. *)
47 Printf.printf "%.1f\n%!" (FloatParser.main Lexer.token linebuf)
48 with
49 | Lexer.Error msg ->
50 Printf.fprintf stderr "%s%!" msg
51 | FloatParser.Error ->
52 Printf.fprintf stderr "At offset %d: syntax error.\n%!" (Lexing.lexeme_start linebuf)
53 done