1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* The lexer generator. Command-line parsing. *)
|
|
|
|
|
|
|
|
open Syntax
|
|
|
|
open Lexgen
|
|
|
|
|
2002-10-28 08:46:50 -08:00
|
|
|
let ml_automata = ref false
|
|
|
|
let source_name = ref ""
|
|
|
|
|
|
|
|
let usage = "ocamlex [-option]* sourcefile"
|
|
|
|
|
|
|
|
let _ =
|
|
|
|
Arg.parse
|
|
|
|
["-ml", Arg.Set ml_automata, " outputed automaton is a caml program" ;
|
|
|
|
]
|
|
|
|
(fun name -> source_name := name)
|
|
|
|
usage
|
|
|
|
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let main () =
|
2002-10-28 08:46:50 -08:00
|
|
|
let source_name = !source_name in
|
1995-05-04 03:15:53 -07:00
|
|
|
let dest_name =
|
|
|
|
if Filename.check_suffix source_name ".mll" then
|
|
|
|
Filename.chop_suffix source_name ".mll" ^ ".ml"
|
|
|
|
else
|
|
|
|
source_name ^ ".ml" in
|
1996-12-10 02:42:07 -08:00
|
|
|
let ic = open_in_bin source_name in
|
1996-02-25 06:45:47 -08:00
|
|
|
let oc = open_out dest_name in
|
1996-12-10 02:42:07 -08:00
|
|
|
let lexbuf = Lexing.from_channel ic in
|
1998-05-26 02:56:41 -07:00
|
|
|
try
|
|
|
|
let def = Parser.lexer_definition Lexer.main lexbuf in
|
2002-10-28 08:46:50 -08:00
|
|
|
let (entries, transitions) = Lexgen.make_dfa def.entrypoints in
|
|
|
|
(*
|
|
|
|
let (entries, transitions) = Lexmin.zyva (entries,transitions) in
|
1998-05-26 02:56:41 -07:00
|
|
|
let tables = Compact.compact_tables transitions in
|
|
|
|
Output.output_lexdef source_name ic oc
|
|
|
|
def.header tables entries def.trailer;
|
2002-10-28 08:46:50 -08:00
|
|
|
*)
|
|
|
|
if !ml_automata then begin
|
|
|
|
Outputbis.output_lexdef
|
|
|
|
source_name ic oc
|
|
|
|
def.header entries transitions def.trailer
|
|
|
|
end else begin
|
|
|
|
let tables = Compact.compact_tables transitions in
|
|
|
|
Output.output_lexdef source_name ic oc
|
|
|
|
def.header tables entries def.trailer
|
|
|
|
end ;
|
|
|
|
close_in ic;
|
1998-05-26 02:56:41 -07:00
|
|
|
close_out oc
|
|
|
|
with exn ->
|
|
|
|
close_in ic;
|
|
|
|
close_out oc;
|
|
|
|
Sys.remove dest_name;
|
|
|
|
begin match exn with
|
|
|
|
Parsing.Parse_error ->
|
|
|
|
Printf.fprintf stderr
|
|
|
|
"File \"%s\", line %d, character %d: syntax error.\n"
|
|
|
|
source_name !Lexer.line_num
|
|
|
|
(Lexing.lexeme_start lexbuf - !Lexer.line_start_pos)
|
|
|
|
| Lexer.Lexical_error(msg, line, col) ->
|
|
|
|
Printf.fprintf stderr
|
|
|
|
"File \"%s\", line %d, character %d: %s.\n"
|
|
|
|
source_name line col msg
|
2002-10-28 08:46:50 -08:00
|
|
|
| Lexgen.Memory_overflow ->
|
|
|
|
Printf.fprintf stderr
|
|
|
|
"File \"%s\":\n Position memory overflow, too many bindings\n"
|
|
|
|
source_name
|
1998-05-26 02:56:41 -07:00
|
|
|
| Output.Table_overflow ->
|
|
|
|
Printf.fprintf stderr
|
|
|
|
"File \"%s\":\ntransition table overflow, automaton is too big\n"
|
|
|
|
source_name
|
|
|
|
| _ ->
|
|
|
|
raise exn
|
|
|
|
end;
|
|
|
|
exit 3
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-10-28 08:46:50 -08:00
|
|
|
let _ = (* Printexc.catch *) main (); exit 0
|
1995-05-04 03:15:53 -07:00
|
|
|
|