6 tag_to_id : (string, int) Hashtbl.t;
7 tag_of_id : string array
15 let buffer = Buffer.create 512
17 let parse_tree cin open_tag close_tag =
19 let c = input_char cin in
22 | ' ' | ',' | '-' -> loop ()
23 | 'a'..'z' | 'B'..'Z' | '0'..'9' | '_' ->
25 Buffer.add_char buffer c;
28 | 'A' -> Buffer.clear buffer;
29 Buffer.add_char buffer c;
31 | ')' -> close_tag (); loop ()
32 | _ -> failwith ("Invalid character: " ^ (String.make 1 c))
35 let c = input_char cin in
37 | 'a'..'z' | 'A'..'Z' | '0'..'9' | '_' ->
38 Buffer.add_char buffer c;
40 | '(' -> let s = Buffer.contents buffer in
45 | ',' | '-' -> let s = Buffer.contents buffer in
50 | ')' -> let s = Buffer.contents buffer in
56 | _ -> failwith ("Invalid character: " ^ (String.make 1 c))
61 let tag_info = Hashtbl.create 1023
62 let tag_of_id = Hashtbl.create 1023
63 let current_id = ref 4
65 Hashtbl.clear tag_info;
66 Hashtbl.clear tag_of_id;
68 Hashtbl.add tag_info "_ROOT" (0, ~-1, false);
69 Hashtbl.add tag_info "_A" (1, ~-1, false);
70 Hashtbl.add tag_info "_T" (2, ~-1, false);
71 Hashtbl.add tag_info "_AT" (3, ~-1, false);
72 Hashtbl.add tag_info "_" (4, ~-1, false);
73 Hashtbl.add tag_of_id 0 "_ROOT";
74 Hashtbl.add tag_of_id 1 "_A";
75 Hashtbl.add tag_of_id 2 "_T";
76 Hashtbl.add tag_of_id 3 "_AT";
77 Hashtbl.add tag_of_id 4 "_"
81 let id, count, nterm =
82 try Hashtbl.find tag_info s with
85 let id = !current_id in
86 Hashtbl.add tag_of_id id s;
87 (!current_id, ~-1, nterm || s = "START")
89 let r = id, count+1, nterm in
90 Hashtbl.replace tag_info s r;
94 type tree = Node of string * tree list
96 let parse_small_tree cin =
97 let stack = ref [ Node("", []) ] in
98 let open_tag s isnterm =
99 if s <> "y0" && s <> "y1" then ignore(add_tag s isnterm);
100 stack := Node(s, []) :: !stack
104 Node(t1, l1) :: Node(t2, l2) :: r ->
105 stack := Node(t2, Node(t1, List.rev l1)::l2) :: r
108 parse_tree cin open_tag close_tag;
110 [ Node(_, [ l ]) ] -> l
111 | _ -> raise End_of_file
113 let parse_big_tree cin =
114 let bv = Bp.bitmap_create () in
115 let tags = IntArray.create () in
116 let open_tag s isnterm =
117 let id, _, _ = add_tag s isnterm in
118 Bp.bitmap_push_back bv 1;
119 IntArray.push_back tags id
122 Bp.bitmap_push_back bv 0
124 parse_tree cin open_tag close_tag;
125 Bp.create bv, IntArray.pack tags
127 let eat_char cin = ignore (input_char cin)
129 let h_find ?(msg="") h i =
134 let r = Obj.repr i in
135 if Obj.is_int r then Printf.eprintf "Not_found (%s): %i\n%!" msg (Obj.magic i);
136 if Obj.tag r = Obj.string_tag then Printf.eprintf "Not_found (%s): %s\n%!" msg (Obj.magic i);
141 let rules = Hashtbl.create 1023 in
144 ignore (parse_small_tree cin);
146 (* ignore (input_char cin); *)
147 let bv, tags = parse_big_tree cin in
151 let lhs = parse_small_tree cin in
152 let rhs = parse_small_tree cin in
153 Hashtbl.add rules lhs rhs
155 with End_of_file -> ()
157 (* First, re-order the tags *)
158 let old_new_mapping =
159 Array.init (Hashtbl.length tag_of_id)
160 (fun i -> h_find ~msg:"1" tag_of_id i)
162 Array.fast_sort (fun tag1 tag2 ->
163 let t1, count1, isnterm1 =
164 h_find ~msg:"2" tag_info tag1
165 and t2, count2, isnterm2 =
166 h_find ~msg:"3" tag_info tag2
168 if t1 <= 4 && t2 <= 4 then compare t1 t2
169 else if t1 <= 4 then -1
170 else if t2 <= 4 then 1
172 if (not isnterm1) && (not isnterm2) then compare t1 t2
173 else if isnterm1 && isnterm2 then
174 match tag1, tag2 with
175 "START", "START" -> 0
178 | _ -> compare count2 count1
179 else if isnterm2 then -1
180 else 1) old_new_mapping;
181 let tag_to_id = Hashtbl.create 503 in
182 Array.iteri (fun i s ->
183 Hashtbl.add tag_to_id s i) old_new_mapping;
184 let renum_tags = Array.copy tags in
185 for i = 0 to Array.length tags - 1 do
187 h_find ~msg:"4" tag_to_id (h_find ~msg:"5" tag_of_id (tags.(i)))
189 let r_array = Array.create (Hashtbl.length rules) 0 in
190 let rules_offset = h_find ~msg:"6" tag_to_id "START" + 1 in
195 | Node(tag, children) :: ll ->
196 if tag <> "y0" && tag <> "y1" then
202 Hashtbl.iter (fun lhs rhs ->
203 let Node( head, _ ) = lhs in
204 let Node( tag1, params) = rhs in
205 let tag2, pos2 = pos_id2 params in
206 let id1 = h_find ~msg:"7" tag_to_id tag1
207 and id2 = h_find ~msg:"8" tag_to_id tag2
209 let rule_ = id2 lsl 27 in
210 let rule_ = (rule_ lor id1) lsl 2 in
211 let rule_ = (rule_ lor pos2) lsl 2 in
212 let rule_ = rule_ lor (List.length params) in
213 r_array.((h_find ~msg:"9" tag_to_id head) - rules_offset ) <- rule_
215 (*let l = Array.length renum_tags in *)
216 (*let tag32 = Array32.create l 0 in
217 for i = 0 to l - 1 do
218 Array32.set tag32 i (renum_tags.(i) land 0x7ffffff);
220 (* Remove the non-terminal names from the hash tables *)
221 let tag_to_id2 = Hashtbl.create 31 in
222 Hashtbl.iter (fun s i -> if i < rules_offset then Hashtbl.add tag_to_id2 s i)
227 rules_offset = rules_offset;
228 tag_to_id = tag_to_id2;
229 tag_of_id = Array.sub old_new_mapping 0 rules_offset
235 let cin = open_in file in
236 let g = Parse.parse cin in
240 let _GRAMMAR_MAGIC = 0xaabbcc
241 let _GRAMMAR_VERSION = 2
244 let cout = open_out f in
245 let write a = Marshal.to_channel cout a [ ]
247 write _GRAMMAR_MAGIC;
248 write _GRAMMAR_VERSION;
251 write g.rules_offset;
255 let fd = Unix.descr_of_out_channel cout in
260 let cin = open_in f in
262 Printf.eprintf "Position: %i kiB\n" (pos_in cin / 1024)
264 let read () = Marshal.from_channel cin in
265 if read () != _GRAMMAR_MAGIC then failwith "Invalid grammar file";
266 if read () != _GRAMMAR_VERSION then failwith "Deprecated grammar format";
268 let tags : int array = read () in
270 let rules : int array = read () in
272 let rules_offset : int = read () in
274 let tag_to_id : (string, int) Hashtbl.t = read () in
276 let tag_of_id : string array = read () in
278 let fd = Unix.descr_of_in_channel cin in
279 let pos = pos_in cin in
280 ignore(Unix.lseek fd pos Unix.SEEK_SET);
281 let bp = Bp.load fd in
287 rules_offset = rules_offset;
288 tag_to_id = tag_to_id;
289 tag_of_id = tag_of_id;
293 type node = [ `Start ] Node.t
295 type n_type = [ `NonTerminal ]
296 type t_type = [ `Terminal ]
297 type r_type = [ `Rule ]
298 type any_type = [ n_type | t_type ]
299 type rhs = [ r_type ] Node.t
301 type n_symbol = n_type Node.t
302 type t_symbol = t_type Node.t
303 type tn_symbol = [ any_type ] Node.t
306 let is_nil (t : t_symbol) =
309 let nil_symbol : t_symbol =
312 let translate_tag _ t = if t == 4 then ~-1 else t
313 let to_string t tag =
314 if tag < Array.length t.tag_of_id then t.tag_of_id.(Tag.to_int tag)
315 else "<!INVALIDTAG!>"
317 let register_tag t s =
318 try Hashtbl.find t.tag_to_id s with
321 let tag_operations t = {
322 Tag.tag = (fun s -> register_tag t s);
323 Tag.to_string = (fun s -> to_string t s);
324 Tag.translate = (fun s -> translate_tag t s);
327 let start_root : node = Node.of_int 0
328 let start_tag t (idx : node) =
329 t.tags.(Bp.preorder_rank t.start (Node.to_int idx))
331 let start_first_child t (idx : node) =
332 Bp.first_child t.start (Node.to_int idx)
334 let start_next_sibling t (idx : node) =
335 Bp.next_sibling t.start (Node.to_int idx)
337 let is_non_terminal t (n : [< any_type ] Node.t) =
338 let n = Node.to_int n in
341 let is_terminal t (n : [< any_type ] Node.t) = not(is_non_terminal t n)
343 external terminal : [< any_type ] Node.t -> t_symbol = "%identity"
344 external non_terminal : [< any_type ] Node.t -> t_symbol = "%identity"
347 let tag (n : t_symbol) : Tag.t = Obj.magic n
349 let get_rule g (r : n_symbol) : rhs =
350 Node.of_int (g.rules.((Node.to_int r) - g.rules_offset))
352 let get_id1 (r : rhs) : tn_symbol =
354 ((Node.to_int r) lsr 4) land 0x7ffffff)
356 let get_id2 (r : rhs) : tn_symbol =
357 Node.of_int((Node.to_int r) lsr 31)
359 let get_param_pos (r : rhs) : int =
360 ((Node.to_int r) lsr 2) land 0b11
362 let num_params (r : rhs) : int =
363 (Node.to_int r) land 0b11