~ubuntu-branches/ubuntu/vivid/menhir/vivid

« back to all changes in this revision

Viewing changes to demos/calc/calc.ml

  • Committer: Package Import Robot
  • Author(s): Mehdi Dogguy
  • Date: 2014-04-27 14:49:45 UTC
  • mfrom: (1.2.9)
  • Revision ID: package-import@ubuntu.com-20140427144945-vlmb6p3kget3s65q
Tags: 20140422.dfsg-1
New upstream relese.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
(*                                                                        *)
13
13
(**************************************************************************)
14
14
 
 
15
let process (line : string) =
 
16
  let linebuf = Lexing.from_string line in
 
17
  try
 
18
    (* Run the parser on this line of input. *)
 
19
    Printf.printf "%d\n%!" (Parser.main Lexer.token linebuf)
 
20
  with
 
21
  | Lexer.Error msg ->
 
22
      Printf.fprintf stderr "%s%!" msg
 
23
  | Parser.Error ->
 
24
      Printf.fprintf stderr "At offset %d: syntax error.\n%!" (Lexing.lexeme_start linebuf)
 
25
 
 
26
let process (optional_line : string option) =
 
27
  match optional_line with
 
28
  | None ->
 
29
      ()
 
30
  | Some line ->
 
31
      process line
 
32
 
 
33
let rec repeat channel =
 
34
  (* Attempt to read one line. *)
 
35
  let optional_line, continue = Lexer.line channel in
 
36
  process optional_line;
 
37
  if continue then
 
38
    repeat channel
 
39
  
15
40
let () =
16
 
  let stdinbuf = Lexing.from_channel stdin in
17
 
  while true do
18
 
    (* Read line by line. *)
19
 
    let linebuf = Lexing.from_string (Lexer.line stdinbuf) in
20
 
    try
21
 
      (* Run the parser on a single line of input. *)
22
 
      Printf.printf "%d\n%!" (Parser.main Lexer.token linebuf)
23
 
    with
24
 
    | Lexer.Error msg ->
25
 
        Printf.fprintf stderr "%s%!" msg
26
 
    | Parser.Error ->
27
 
        Printf.fprintf stderr "At offset %d: syntax error.\n%!" (Lexing.lexeme_start linebuf)
28
 
  done
 
41
  repeat (Lexing.from_channel stdin)
 
42