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
|
|
|
(* Compiling a lexer definition *)
|
|
|
|
|
|
|
|
open Syntax
|
|
|
|
|
|
|
|
(* Deep abstract syntax for regular expressions *)
|
|
|
|
|
|
|
|
type regexp =
|
|
|
|
Empty
|
|
|
|
| Chars of int
|
|
|
|
| Action of int
|
|
|
|
| Seq of regexp * regexp
|
|
|
|
| Alt of regexp * regexp
|
|
|
|
| Star of regexp
|
|
|
|
|
1996-02-25 06:45:47 -08:00
|
|
|
type lexer_entry =
|
|
|
|
{ lex_name: string;
|
|
|
|
lex_regexp: regexp;
|
|
|
|
lex_actions: (int * location) list }
|
|
|
|
|
|
|
|
(* Representation of automata *)
|
|
|
|
|
|
|
|
type automata =
|
|
|
|
Perform of int
|
|
|
|
| Shift of automata_trans * automata_move array
|
|
|
|
and automata_trans =
|
|
|
|
No_remember
|
|
|
|
| Remember of int
|
|
|
|
and automata_move =
|
|
|
|
Backtrack
|
|
|
|
| Goto of int
|
|
|
|
|
|
|
|
(* Representation of entry points *)
|
|
|
|
|
|
|
|
type automata_entry =
|
|
|
|
{ auto_name: string;
|
|
|
|
auto_initial_state: int;
|
|
|
|
auto_actions: (int * location) list }
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* From shallow to deep syntax *)
|
|
|
|
|
1997-04-15 12:18:03 -07:00
|
|
|
let chars = ref ([] : int list list)
|
1995-05-04 03:15:53 -07:00
|
|
|
let chars_count = ref 0
|
|
|
|
let actions = ref ([] : (int * location) list)
|
|
|
|
let actions_count = ref 0
|
|
|
|
|
|
|
|
let rec encode_regexp = function
|
|
|
|
Epsilon -> Empty
|
|
|
|
| Characters cl ->
|
|
|
|
let n = !chars_count in
|
|
|
|
chars := cl :: !chars;
|
|
|
|
incr chars_count;
|
|
|
|
Chars(n)
|
|
|
|
| Sequence(r1,r2) ->
|
|
|
|
Seq(encode_regexp r1, encode_regexp r2)
|
|
|
|
| Alternative(r1,r2) ->
|
|
|
|
Alt(encode_regexp r1, encode_regexp r2)
|
|
|
|
| Repetition r ->
|
|
|
|
Star (encode_regexp r)
|
|
|
|
|
1996-02-25 06:45:47 -08:00
|
|
|
let encode_casedef casedef =
|
1995-05-04 03:15:53 -07:00
|
|
|
List.fold_left
|
1996-02-25 06:45:47 -08:00
|
|
|
(fun reg (expr, act) ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let act_num = !actions_count in
|
|
|
|
incr actions_count;
|
|
|
|
actions := (act_num, act) :: !actions;
|
|
|
|
Alt(reg, Seq(encode_regexp expr, Action act_num)))
|
1996-02-25 06:45:47 -08:00
|
|
|
Empty
|
|
|
|
casedef
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-08-25 06:54:14 -07:00
|
|
|
let encode_lexdef def =
|
1995-05-04 03:15:53 -07:00
|
|
|
chars := [];
|
|
|
|
chars_count := 0;
|
1996-02-25 06:45:47 -08:00
|
|
|
let entry_list =
|
1995-08-25 06:54:14 -07:00
|
|
|
List.map
|
1996-02-25 06:45:47 -08:00
|
|
|
(fun (entry_name, casedef) ->
|
|
|
|
actions := [];
|
|
|
|
actions_count := 0;
|
|
|
|
let re = encode_casedef casedef in
|
|
|
|
{ lex_name = entry_name;
|
|
|
|
lex_regexp = re;
|
|
|
|
lex_actions = List.rev !actions })
|
1995-08-25 06:54:14 -07:00
|
|
|
def.entrypoints in
|
1996-02-25 06:45:47 -08:00
|
|
|
let chr = Array.of_list (List.rev !chars) in
|
1995-05-04 03:15:53 -07:00
|
|
|
chars := [];
|
|
|
|
actions := [];
|
1996-02-25 06:45:47 -08:00
|
|
|
(chr, entry_list)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* To generate directly a NFA from a regular expression.
|
|
|
|
Confer Aho-Sethi-Ullman, dragon book, chap. 3 *)
|
|
|
|
|
|
|
|
type transition =
|
|
|
|
OnChars of int
|
|
|
|
| ToAction of int
|
|
|
|
|
1996-02-25 06:45:47 -08:00
|
|
|
module TransSet =
|
|
|
|
Set.Make(struct type t = transition let compare = compare end)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let rec nullable = function
|
|
|
|
Empty -> true
|
|
|
|
| Chars _ -> false
|
|
|
|
| Action _ -> false
|
2000-12-28 05:07:42 -08:00
|
|
|
| Seq(r1,r2) -> nullable r1 && nullable r2
|
|
|
|
| Alt(r1,r2) -> nullable r1 || nullable r2
|
1995-05-04 03:15:53 -07:00
|
|
|
| Star r -> true
|
|
|
|
|
|
|
|
let rec firstpos = function
|
1996-02-25 06:45:47 -08:00
|
|
|
Empty -> TransSet.empty
|
|
|
|
| Chars pos -> TransSet.add (OnChars pos) TransSet.empty
|
|
|
|
| Action act -> TransSet.add (ToAction act) TransSet.empty
|
1995-05-04 03:15:53 -07:00
|
|
|
| Seq(r1,r2) -> if nullable r1
|
1996-02-25 06:45:47 -08:00
|
|
|
then TransSet.union (firstpos r1) (firstpos r2)
|
1995-05-04 03:15:53 -07:00
|
|
|
else firstpos r1
|
1996-02-25 06:45:47 -08:00
|
|
|
| Alt(r1,r2) -> TransSet.union (firstpos r1) (firstpos r2)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Star r -> firstpos r
|
|
|
|
|
|
|
|
let rec lastpos = function
|
1996-02-25 06:45:47 -08:00
|
|
|
Empty -> TransSet.empty
|
|
|
|
| Chars pos -> TransSet.add (OnChars pos) TransSet.empty
|
|
|
|
| Action act -> TransSet.add (ToAction act) TransSet.empty
|
1995-05-04 03:15:53 -07:00
|
|
|
| Seq(r1,r2) -> if nullable r2
|
1996-02-25 06:45:47 -08:00
|
|
|
then TransSet.union (lastpos r1) (lastpos r2)
|
1995-05-04 03:15:53 -07:00
|
|
|
else lastpos r2
|
1996-02-25 06:45:47 -08:00
|
|
|
| Alt(r1,r2) -> TransSet.union (lastpos r1) (lastpos r2)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Star r -> lastpos r
|
|
|
|
|
1996-02-25 06:45:47 -08:00
|
|
|
let followpos size entry_list =
|
1996-04-22 04:15:41 -07:00
|
|
|
let v = Array.create size TransSet.empty in
|
1996-02-25 06:45:47 -08:00
|
|
|
let fill_pos first = function
|
|
|
|
OnChars pos -> v.(pos) <- TransSet.union first v.(pos)
|
|
|
|
| ToAction _ -> () in
|
|
|
|
let rec fill = function
|
|
|
|
Seq(r1,r2) ->
|
|
|
|
fill r1; fill r2;
|
|
|
|
TransSet.iter (fill_pos (firstpos r2)) (lastpos r1)
|
|
|
|
| Alt(r1,r2) ->
|
|
|
|
fill r1; fill r2
|
|
|
|
| Star r ->
|
|
|
|
fill r;
|
|
|
|
TransSet.iter (fill_pos (firstpos r)) (lastpos r)
|
|
|
|
| _ -> () in
|
|
|
|
List.iter (fun entry -> fill entry.lex_regexp) entry_list;
|
|
|
|
v
|
|
|
|
|
|
|
|
let no_action = max_int
|
|
|
|
|
|
|
|
let split_trans_set trans_set =
|
|
|
|
TransSet.fold
|
|
|
|
(fun trans (act, pos_set as act_pos_set) ->
|
|
|
|
match trans with
|
|
|
|
OnChars pos -> (act, pos :: pos_set)
|
|
|
|
| ToAction act1 -> if act1 < act then (act1, pos_set) else act_pos_set)
|
|
|
|
trans_set
|
|
|
|
(no_action, [])
|
|
|
|
|
|
|
|
module StateMap =
|
|
|
|
Map.Make(struct type t = TransSet.t let compare = TransSet.compare end)
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
let state_map = ref (StateMap.empty : int StateMap.t)
|
1996-04-22 04:15:41 -07:00
|
|
|
let todo = (Stack.create() : (TransSet.t * int) Stack.t)
|
1996-02-25 06:45:47 -08:00
|
|
|
let next_state_num = ref 0
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let reset_state_mem () =
|
1996-02-25 06:45:47 -08:00
|
|
|
state_map := StateMap.empty;
|
|
|
|
Stack.clear todo;
|
|
|
|
next_state_num := 0
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let get_state st =
|
|
|
|
try
|
1996-02-25 06:45:47 -08:00
|
|
|
StateMap.find st !state_map
|
1995-05-04 03:15:53 -07:00
|
|
|
with Not_found ->
|
1996-02-25 06:45:47 -08:00
|
|
|
let num = !next_state_num in
|
|
|
|
incr next_state_num;
|
|
|
|
state_map := StateMap.add st num !state_map;
|
|
|
|
Stack.push (st, num) todo;
|
|
|
|
num
|
|
|
|
|
|
|
|
let map_on_all_states f =
|
|
|
|
let res = ref [] in
|
|
|
|
begin try
|
|
|
|
while true do
|
|
|
|
let (st, i) = Stack.pop todo in
|
|
|
|
let r = f st in
|
|
|
|
res := (r, i) :: !res
|
|
|
|
done
|
|
|
|
with Stack.Empty -> ()
|
|
|
|
end;
|
|
|
|
!res
|
|
|
|
|
|
|
|
let goto_state st =
|
|
|
|
if TransSet.is_empty st then Backtrack else Goto (get_state st)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let transition_from chars follow pos_set =
|
1997-04-15 12:18:03 -07:00
|
|
|
let tr = Array.create 257 TransSet.empty in
|
|
|
|
let shift = Array.create 257 Backtrack in
|
1995-05-04 03:15:53 -07:00
|
|
|
List.iter
|
|
|
|
(fun pos ->
|
|
|
|
List.iter
|
|
|
|
(fun c ->
|
1997-04-15 12:18:03 -07:00
|
|
|
tr.(c) <- TransSet.union tr.(c) follow.(pos))
|
1995-05-04 03:15:53 -07:00
|
|
|
chars.(pos))
|
|
|
|
pos_set;
|
1997-04-15 12:18:03 -07:00
|
|
|
for i = 0 to 256 do
|
1995-05-04 03:15:53 -07:00
|
|
|
shift.(i) <- goto_state tr.(i)
|
|
|
|
done;
|
|
|
|
shift
|
|
|
|
|
|
|
|
let translate_state chars follow state =
|
|
|
|
match split_trans_set state with
|
1996-02-25 06:45:47 -08:00
|
|
|
(n, []) -> Perform n
|
|
|
|
| (n, ps) -> Shift((if n = no_action then No_remember else Remember n),
|
|
|
|
transition_from chars follow ps)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let make_dfa lexdef =
|
1996-02-25 06:45:47 -08:00
|
|
|
let (chars, entry_list) = encode_lexdef lexdef in
|
|
|
|
let follow = followpos (Array.length chars) entry_list in
|
1995-05-04 03:15:53 -07:00
|
|
|
reset_state_mem();
|
|
|
|
let initial_states =
|
1996-02-25 06:45:47 -08:00
|
|
|
List.map
|
|
|
|
(fun le ->
|
|
|
|
{ auto_name = le.lex_name;
|
|
|
|
auto_initial_state = get_state(firstpos le.lex_regexp);
|
|
|
|
auto_actions = le.lex_actions })
|
|
|
|
entry_list in
|
|
|
|
let states = map_on_all_states (translate_state chars follow) in
|
1996-04-22 04:15:41 -07:00
|
|
|
let actions = Array.create !next_state_num (Perform 0) in
|
1996-02-25 06:45:47 -08:00
|
|
|
List.iter (fun (act, i) -> actions.(i) <- act) states;
|
1995-05-04 03:15:53 -07:00
|
|
|
reset_state_mem();
|
1996-02-25 06:45:47 -08:00
|
|
|
(initial_states, actions)
|