Remove the need for a NOP operation in automata bytecode.
[SXSI/xpathcomp.git] / src / runtime.ml
1 INCLUDE "debug.ml"
2 INCLUDE "trace.ml"
3 INCLUDE "utils.ml"
4
5 open Format
6 open Ata
7 module type S = sig
8   type result_set
9   val top_down_run : Ata.t -> Tree.t -> Tree.node -> result_set
10   val bottom_up_run : Ata.t -> Tree.t -> Compile.text_query * string -> result_set
11   val grammar_run : Ata.t -> Grammar2.t -> unit -> result_set
12
13 end
14
15 module Make (U : ResJIT.S) : S with type result_set = U.NS.t =
16   struct
17
18     type result_set = U.NS.t;;
19
20     let eval_form auto s1 s2 f =
21       let rec loop f =
22         match Formula.expr f with
23           | Formula.False | Formula.True | Formula.Pred _ -> f, []
24           | Formula.Atom(`Left, b, q) ->
25               Formula.of_bool (b == (StateSet.mem q s1)),
26               if b && StateSet.mem q auto.topdown_marking_states then [ResJIT.LEFT q] else []
27           | Formula.Atom (`Right, b, q) ->
28               Formula.of_bool(b == (StateSet.mem q s2)),
29               if b && StateSet.mem q auto.topdown_marking_states then [ResJIT.RIGHT q] else []
30           | Formula.Atom (`Epsilon, _, _) -> assert false
31
32           | Formula.Or(f1, f2) ->
33               let b1, i1 = loop f1 in
34               let b2, i2 = loop f2 in
35               Formula.or_pred b1 b2, i1 @ i2
36           | Formula.And(f1, f2) ->
37               let b1, i1 = loop f1 in
38               let b2, i2 = loop f2 in
39               Formula.and_pred b1 b2, i1 @ i2
40       in
41       loop f
42
43
44     let eval_trans auto s1 s2 trans =
45       Translist.fold
46         (fun t ((a_st, a_op, a_todo) as acc)->
47            let q, _, m, f = Transition.node t in
48            let form, ops = eval_form auto s1 s2 f in
49            match Formula.expr form with
50              | Formula.True ->
51                StateSet.add q a_st,
52                (q, (if m then (ResJIT.SELF() :: ops) else ops)):: a_op,
53                a_todo
54              | Formula.False -> acc
55              | Formula.Pred p -> a_st, a_op,
56                (p.Tree.Predicate.node, q, [(q,(if m then (ResJIT.SELF() :: ops) else ops))]) :: a_todo
57              | _ -> assert false
58         ) trans (StateSet.empty, [], [])
59
60
61
62     module L3JIT =
63       struct
64
65         type opcode = (t -> t -> t -> Tree.t -> Tree.node -> StateSet.t * t)
66
67         type t = opcode Cache.Lvl3.t
68
69         let dummy _ _ _ _ _ = failwith "Uninitialized L3JIT"
70
71         let create () = Cache.Lvl3.create 1024 dummy
72         let find t tlist s1 s2 =
73           Cache.Lvl3.find t
74             (Uid.to_int s2.StateSet.Node.id)
75             (Uid.to_int s1.StateSet.Node.id)
76             (Uid.to_int tlist.Translist.Node.id)
77
78
79
80         let add t tlist s1 s2 v =
81           Cache.Lvl3.add t
82             (Uid.to_int s2.StateSet.Node.id)
83             (Uid.to_int s1.StateSet.Node.id)
84             (Uid.to_int tlist.Translist.Node.id)
85             v
86
87         let compile auto trl s1 s2 =
88           let orig_s1, orig_s2 =
89             Translist.fold (fun t (a1, a2) ->
90                           let _, _, _, f = Transition.node t in
91                           let (_, _, fs1), (_, _, fs2) = Formula.st f in
92                             (StateSet.union a1 fs1, StateSet.union a2 fs2)
93                        ) trl (StateSet.empty, StateSet.empty)
94           in
95           let ns1 = StateSet.inter s1 orig_s1
96           and ns2 = StateSet.inter s2 orig_s2 in
97           let res, ops, todo = eval_trans auto ns1 ns2 trl in
98           let code, not_marking = ResJIT.compile ops in
99           let todo_code, todo_notmarking =
100             List.fold_left (fun (l, b) (p, q, o) -> let c, b' = ResJIT.compile o in
101                                          (p, q, c)::l, b && b')
102               ([], not_marking) todo
103           in
104           let opcode = res, code, todo_notmarking, todo_code in
105           opcode
106
107         let gen_code auto tlist s1 s2 =
108           let res, code, not_marking, todo_code = compile auto tlist s1 s2 in
109           let f =
110             if todo_code == [] then
111               if not_marking then begin fun empty_slot sl1 sl2 _ node ->
112                 let slot1_empty = sl1 == empty_slot
113                 and slot2_empty = sl2 == empty_slot in
114                 if slot1_empty && slot2_empty then res,sl2
115                 else
116                   let sl =
117                     if slot2_empty then
118                       if slot1_empty then
119                         Array.copy empty_slot
120                       else sl1
121                     else sl2
122                   in
123                   U.exec sl sl1 sl2 node code;
124                   res, sl
125               end
126               else (* marking *) begin fun empty_slot sl1 sl2 _ node ->
127                 let sl =
128                   if sl2 == empty_slot  then
129                     if sl1 == empty_slot then
130                       Array.copy empty_slot
131                     else sl1
132                   else sl2
133                 in
134                 U.exec sl sl1 sl2 node code;
135                 res, sl
136               end
137               else (* todo != [] *)
138               begin fun empty_slot sl1 sl2 tree node ->
139                 let sl =
140                   if sl2 == empty_slot  then
141                     if sl1 == empty_slot then
142                       Array.copy empty_slot
143                     else sl1
144                   else sl2
145                 in
146                 U.exec sl sl1 sl2 node code;
147                 List.fold_left
148                   (fun ares (p, q, code) ->
149                     if !p tree node then begin
150                       if code != ResJIT.Nil then U.exec sl sl1 sl2 node code;
151                       StateSet.add q ares
152                     end
153                     else ares) res todo_code, sl
154
155               end
156           in
157           f
158
159         let cache_apply cache auto tlist s1 s2 =
160           let f = gen_code auto tlist s1 s2 in
161           TRACE("grammar", 2, __ "Inserting: %i, %a, %a\n%!"
162             (Uid.to_int tlist.Translist.Node.id) StateSet.print s1 StateSet.print s2);
163           add cache tlist s1 s2 f; f
164       end
165
166 DEFINE LOOP (t, states, ctx) = (
167   let _t = (t) in
168   TRACE("top-down-run", 3,
169         __ "Entering node %i with loop (tag %s, context %i) with states %a\n%!"
170           (Node.to_int _t)
171           (Tag.to_string (Tree.tag tree _t))
172           (Node.to_int (ctx))
173           (StateSet.print) (states));
174   if _t == Tree.nil then nil_res
175   else
176     let tag = Tree.tag tree _t in
177       l2jit_dispatch
178         _t tag (states) (ctx) (L2JIT.find cache2 tag (states))
179 )
180
181 DEFINE LOOP_TAG (t, states, tag, ctx) = (
182   let _t = (t) in (* to avoid duplicating expression t *)
183   TRACE("top-down-run", 3,
184         __ "Entering node %i with loop_tag (tag %s, context %i) with states %a\n%!"
185           (Node.to_int _t)
186           (Tag.to_string (tag))
187           (Node.to_int (ctx))
188           (StateSet.print) (states));
189   if _t == Tree.nil then nil_res
190   else
191     l2jit_dispatch
192       _t (tag) (states) (ctx) (L2JIT.find cache2 (tag) (states)))
193
194
195     let top_down_run auto tree root states ctx =
196       let res_len = StateSet.max_elt auto.states + 1 in
197       let empty_slot = Array.create res_len U.NS.empty in
198       let nil_res = auto.bottom_states, empty_slot in
199       let cache3 = L3JIT.create () in
200
201       let l3jit_dispatch trl s1 s2 t sl1 sl2 =
202         let f = L3JIT.find cache3 trl s1 s2 in
203         if f == L3JIT.dummy then (L3JIT.cache_apply cache3 auto trl s1 s2) empty_slot sl1 sl2 tree t
204         else f empty_slot sl1 sl2 tree t
205
206       in
207       let cache2 = L2JIT.create () in
208
209
210       let rec l2jit_dispatch t tag states ctx opcode =
211         match opcode with
212           | L2JIT.RETURN -> nil_res
213           | L2JIT.CACHE ->
214               let opcode = L2JIT.compile cache2 auto tree tag states in
215                 l2jit_dispatch t tag states ctx opcode
216
217           | L2JIT.LEFT (tr_list, instr) ->
218               let res1, slot1 =
219                 l2jit_dispatch_instr t tag states (Tree.closing tree t) instr
220               in
221                 l3jit_dispatch tr_list res1 auto.bottom_states t slot1 empty_slot
222
223           | L2JIT.RIGHT (tr_list, instr) ->
224             let res2, slot2 = l2jit_dispatch_instr t tag states ctx instr in
225               l3jit_dispatch tr_list auto.bottom_states res2 t empty_slot slot2
226
227           | L2JIT.BOTH (tr_list, instr1, instr2) ->
228               let res1, slot1 =
229                 l2jit_dispatch_instr t tag states (Tree.closing tree t) instr1
230               in
231               let res2, slot2 = l2jit_dispatch_instr t tag states ctx instr2 in
232                 l3jit_dispatch tr_list res1 res2 t slot1 slot2
233
234     and l2jit_dispatch_instr t tag states ctx instr =
235       match instr with
236         | L2JIT.FIRST_CHILD s -> LOOP ((Tree.first_child tree t), s, ctx)
237         | L2JIT.NEXT_SIBLING s -> LOOP ((Tree.next_sibling tree t), s, ctx)
238
239         | L2JIT.FIRST_ELEMENT s -> LOOP ((Tree.first_element tree t), s, ctx)
240         | L2JIT.NEXT_ELEMENT s -> LOOP ((Tree.next_element tree t), s, ctx)
241
242         | L2JIT.TAGGED_DESCENDANT (s, tag) ->
243             LOOP_TAG ((Tree.tagged_descendant tree t tag), s, tag, ctx)
244
245         | L2JIT.TAGGED_FOLLOWING (s, tag) ->
246             LOOP_TAG((Tree.tagged_following_before tree t tag ctx), s, tag, ctx)
247
248         | L2JIT.SELECT_DESCENDANT (s, _, us) ->
249             LOOP((Tree.select_descendant tree t us), s, ctx)
250
251         | L2JIT.SELECT_FOLLOWING (s, pt, us) ->
252             LOOP ((Tree.select_following_before tree t us ctx), s, ctx)
253
254         | L2JIT.TAGGED_CHILD (s, tag) ->
255             LOOP_TAG((Tree.tagged_child tree t tag), s, tag, ctx)
256
257         | L2JIT.TAGGED_FOLLOWING_SIBLING (s, tag) ->
258             LOOP_TAG((Tree.tagged_following_sibling tree t tag), s, tag, ctx)
259
260         | L2JIT.SELECT_CHILD (s, _, us) ->
261             LOOP ((Tree.select_child tree t us), s, ctx)
262
263         | L2JIT.SELECT_FOLLOWING_SIBLING (s, _, us) ->
264             LOOP ((Tree.select_following_sibling tree t us), s, ctx)
265
266         | L2JIT.TAGGED_SUBTREE(s, tag) ->
267
268           let count = U.NS.subtree_tags tree t tag in
269           if count != U.NS.empty then
270             let r = Array.copy empty_slot in
271             r.(auto.last) <- count;
272             s,r
273           else
274             s,empty_slot
275
276         | L2JIT.ELEMENT_SUBTREE(s) ->
277
278           let count = U.NS.subtree_elements tree t in
279           if count != U.NS.empty then
280             let r = Array.copy empty_slot in
281             r.(auto.last) <- count;
282             s,r
283           else
284             s,empty_slot
285
286       in
287       let r = LOOP (root, states, ctx) in
288       (*L3JIT.stats err_formatter cache3; *)
289       r
290
291     let full_top_down_run auto states tree root =
292       (*Ata.init (); *)
293       top_down_run auto tree root states (Tree.closing tree root)
294
295     let top_down_run auto tree root =
296       (*Ata.init (); *)
297       let res, slot = full_top_down_run auto auto.init tree root in
298
299       slot.(StateSet.min_elt auto.topdown_marking_states)
300
301
302     (*** Bottom-up evaluation function **)
303
304     let ns_print fmt t =
305       Format.fprintf fmt "{ ";
306       U.NS.iter begin fun node ->
307         Format.fprintf fmt "%a " Node.print node;
308       end t;
309       Format.fprintf fmt "}"
310
311     let slot_print fmt t =
312       Array.iteri begin fun state ns ->
313         Format.eprintf "%a -> %a\n" State.print state ns_print ns;
314       end t
315
316
317     let eval_trans auto tree parent res1 res2 = assert false
318
319
320     let bottom_up_run auto tree (query, pat) =
321       let leaves = Array.to_list (Tree.full_text_query query tree pat) in
322       let states = auto.states in
323       let res_len = (StateSet.max_elt states) + 1 in
324       let empty_slot = Array.create res_len U.NS.empty in
325       let nil_res = auto.bottom_states, empty_slot in
326       let cache = Cache.Lvl3.create 1024 L3JIT.dummy in
327       let rec loop_leaves l acc =
328         match l with
329             [] -> acc
330           | node :: ll ->
331             let res, lll = bottom_up_next node ll Tree.nil in
332             if (lll <> []) then Printf.eprintf "Leftover elements\n%!";
333             res
334
335       and bottom_up_next node rest stop =
336         let fs = Tree.first_child tree node in
337         let res1 =
338           if fs == Tree.nil then nil_res
339           else full_top_down_run auto states tree fs
340         in
341         move_up node res1 true rest stop
342
343       and move_up node res is_left rest stop =
344         if node == stop then res, rest
345         else
346           let prev_sibling = Tree.prev_sibling tree node in
347           let is_left' = prev_sibling == Tree.nil in
348           let real_parent = Tree.parent tree node in
349           let parent =
350             if is_left' then real_parent else max (Tree.first_child tree real_parent) stop
351           in
352           (* let parent = if is_left' then Tree.parent tree node else prev_sibling in *)
353           let (s1, sl1), (s2, sl2), rest' =
354             if is_left then match rest with
355                 [] -> res, nil_res, rest
356               | next :: rest' ->
357                 if Tree.is_right_descendant tree node next
358                 then
359                   let res2, rest' = bottom_up_next next rest' node in
360                   res, res2, rest'
361                 else res, nil_res, rest
362             else
363               nil_res, res, rest
364           in
365           let tag = Tree.tag tree node in
366           let id1 = Uid.to_int s1.StateSet.Node.id in
367           let id2 = Uid.to_int s2.StateSet.Node.id in
368           let code =
369             let code = Cache.Lvl3.find cache tag id1 id2 in
370             if code == L3JIT.dummy then
371               let trl =
372                 StateSet.fold
373                   (fun q acc ->
374                     List.fold_left (fun acc' (labels, tr) ->
375                       if labels == TagSet.any || TagSet.mem tag labels
376                       then Translist.cons tr acc' else acc')
377                       acc
378                       (Hashtbl.find auto.trans q)
379                   )
380                   states
381                   Translist.nil
382               in
383               let code = L3JIT.gen_code auto trl s1 s2 in
384               Cache.Lvl3.add cache tag id1 id2 code; code
385             else code
386           in
387           let res' = code empty_slot sl1 sl2 tree node in
388           move_up parent res' is_left' rest' stop
389       in
390       let _, slot = loop_leaves leaves (nil_res) in
391       slot.(StateSet.min_elt auto.topdown_marking_states)
392
393 let get_trans g auto tag states =
394   StateSet.fold (fun q tr_acc ->
395     List.fold_left
396       (fun ((lstates, rstates, tacc) as acc) (ts, trs) ->
397         if TagSet.mem (Tag.translate tag) ts then
398           if not (TagSet.mem Tag.attribute ts) && Grammar2.is_attribute g tag
399           then acc
400               else
401             let _, _, _, phi = Transition.node trs in
402                 let (_,_,l), (_,_,r) = Formula.st phi in
403                 (StateSet.union l lstates,
404                  StateSet.union r rstates,
405                  Translist.cons trs tacc)
406         else acc)
407       tr_acc (Hashtbl.find auto.trans q)
408   ) states (StateSet.empty, StateSet.empty, Translist.nil)
409
410 (*  Grammar run *)
411 let dispatch_param0 conf id2 y0 y1 =
412   match conf with
413   | Grammar2.C0 | Grammar2.C2 -> Grammar2.Node0 id2
414   | Grammar2.C1 | Grammar2.C5 -> Grammar2.Node1(id2,y0)
415   | Grammar2.C3 | Grammar2.C6 -> y0
416   | Grammar2.C4 -> Grammar2.Node2(id2, y0, y1)
417
418 let dispatch_param1 conf id2 y0 y1 =
419   match conf with
420   | Grammar2.C2 -> y0
421   | Grammar2.C3 -> Grammar2.Node0 id2
422   | Grammar2.C5 -> y1
423   | Grammar2.C6 -> Grammar2.Node1(id2, y1)
424   | _ -> Grammar2.dummy_param
425
426     module K_down = struct
427       type t = Grammar2.n_symbol * StateSet.t
428       let hash (x,y) = HASHINT2(Node.to_int x, Uid.to_int y.StateSet.Node.id)
429       let equal (x1,y1) (x2,y2) = x1 == x2 && y1 == y2
430     end
431
432     module K_up = struct
433       type t = Grammar2.n_symbol * StateSet.t * StateSet.t * StateSet.t
434       let hash (a,b,c,d) =
435         HASHINT4 (Node.to_int a,
436                   Uid.to_int b.StateSet.Node.id,
437                   Uid.to_int c.StateSet.Node.id,
438                   Uid.to_int d.StateSet.Node.id)
439       let equal (a1, b1, c1, d1) (a2, b2, c2, d2) =
440         a1 == a2 && b1  == b2 && c1 == c2 && d1 == d2
441     end
442
443     module DCache =
444       struct
445         include Hashtbl.Make(K_down)
446         let dummy = StateSet.singleton State.dummy
447         let notfound l = l.(0) == dummy && l.(1) == dummy
448         let find h k =
449           try
450             find h k
451           with
452             Not_found ->
453               let a = [| dummy; dummy |] in
454               add h k a;
455               a
456       end
457     module UCache = Hashtbl.Make(K_up)
458     type result = {
459       in0 : StateSet.t;
460       in1 : StateSet.t;
461       out0 : StateSet.t * U.t;
462       out1 : StateSet.t * U.t;
463       main : StateSet.t * U.t
464     }
465     let mk_empty e =
466       { in0 = StateSet.empty;
467         in1 = StateSet.empty;
468         out0 = e;
469         out1 = e;
470         main = e
471       }
472     let mk_nil s v  =
473       {
474         mk_empty (s,v) with
475           out0 = StateSet.empty,v;
476           out1 = StateSet.empty,v;
477       }
478
479     let grammar_run auto g () =
480       let dummy_leaf = Grammar2.dummy_param in
481       let dummy_set = StateSet.singleton State.dummy in
482       let res_len = (StateSet.max_elt auto.states) + 1 in
483       let empty_slot = Array.create res_len U.NS.empty in
484       let nil_res = mk_nil auto.bottom_states empty_slot in
485       let empty_res = mk_empty (StateSet.empty, empty_slot) in
486       let cache3 = L3JIT.create () in
487       let dummy2 = (StateSet.empty, StateSet.empty, Translist.nil) in
488       let cache2 = Cache.Lvl2.create 512 dummy2 in
489       let rule_counter = ref 0 in
490       let preorder_counter = ref 0 in
491       let dcache = DCache.create 1023 in
492       let ucache = UCache.create 1023 in
493       let term_array = [| StateSet.empty; StateSet.empty |] in
494       let get_trans tag states =
495         let c = Cache.Lvl2.find cache2 tag (Uid.to_int states.StateSet.Node.id) in
496         if c == dummy2 then
497           let c = get_trans g auto tag states in
498           begin
499             Cache.Lvl2.add cache2 tag (Uid.to_int states.StateSet.Node.id) c;
500             c
501           end
502         else c
503       in
504       let lambda = ref 0 in
505       let rec start_loop idx states =
506         TRACE("grammar", 2, __ "Node %i\n%!" (Node.to_int idx));
507         if states == dummy_set then nil_res else
508         if idx < Node.null then nil_res
509         else begin
510           let symbol = Grammar2.start_tag g idx in
511           let fc = Grammar2.start_first_child g idx in
512           let ns = Grammar2.start_next_sibling g fc in
513           if Grammar2.is_terminal g symbol then
514             let t = Grammar2.terminal symbol in
515               terminal_loop t states (Grammar2.Leaf (~-1,0,term_array, fc)) (Grammar2.Leaf (~-1,1,term_array, ns))
516           else
517             let nt = Grammar2.non_terminal symbol in
518             incr lambda;
519             let lmbd = !lambda in
520             let y0 = (Grammar2.Leaf (lmbd,0, term_array, fc))
521             and y1 = (Grammar2.Leaf (lmbd,1, term_array, ns)) in
522             rule_loop nt states y0 y1
523         end
524       and rule_loop (t : Grammar2.n_symbol) states y0 y1 =
525         if t = Node.nil || states == dummy_set then nil_res else
526           let () = incr rule_counter in
527           if !rule_counter land 65535 == 0 then begin Gc.minor() end;
528           let k = (t, states) in
529           let pstates = DCache.find dcache k in
530           let notfound = DCache.notfound pstates in
531           let rhs = Grammar2.get_rule g t in
532           let id1 = Grammar2.get_id1 rhs in
533           let id2 = Grammar2.get_id2 rhs in
534           let conf = Grammar2.get_conf rhs in
535           if notfound then
536             let ny0 = dispatch_param0 conf id2 y0 y1 in
537             let ny1 = dispatch_param1 conf id2 y0 y1 in
538             let res = dispatch_loop id1 states ny0 ny1 in
539             pstates.(0) <- res.in0;
540             pstates.(1) <- res.in1;
541             res (*
542             UCache.add ucache (t, states, fst res.out0, fst res.out1)
543               res.main;
544             let h = Hashtbl.create 7 in
545             for i = 0 to res_len - 1 do
546               Hashtbl.add h (0, i) (snd res.out0).(i);
547               Hashtbl.add h (1, i) (snd res.out1).(i);
548             done;
549             { res with
550               main = ((fst res.main), (U.close h (snd res.main)));
551             } *)
552
553             else
554               let res0 = partial_loop y0 pstates.(0) in
555               let res1 = partial_loop y1 pstates.(1) in
556               let k2 = (t, states, fst res0.main, fst res1.main) in
557               let s, r =
558                 try
559                   UCache.find ucache k2
560                 with
561                 Not_found ->
562                   let ores0 = { res0 with main = fst res0.main, U.var 0 (snd res0.main) }
563                   and ores1 = { res1 with main = fst res1.main, U.var 1 (snd res1.main) }
564                   in
565                   let res = dispatch_loop id1 states (Grammar2.Cache (0,ores0)) (Grammar2.Cache (1, ores1)) in
566                   UCache.add ucache k2 res.main;
567                   res.main
568               in
569               let h = Hashtbl.create 7 in
570               for i = 0 to res_len - 1 do
571                 Hashtbl.add h (0, i) (snd res0.main).(i);
572                 Hashtbl.add h (1, i) (snd res1.main).(i);
573               done;
574               { in0 = pstates.(0);
575                 in1 = pstates.(1);
576                 out0 = res0.main;
577                 out1 = res1.main;
578                 main = s, U.close h r;
579               }
580
581       and dispatch_loop id1 states ny0 ny1 =
582           if Grammar2.is_non_terminal g id1 then
583             rule_loop (Grammar2.non_terminal id1) states ny0 ny1
584           else
585             terminal_loop (Grammar2.terminal id1) states ny0 ny1
586
587       and terminal_loop (symbol : Grammar2.t_symbol) states y0 y1 =
588
589         if symbol == Grammar2.nil_symbol || symbol = Node.nil || states == dummy_set then nil_res else begin
590           let tag = Grammar2.tag symbol in
591           let lst, rst, trans = get_trans tag states in
592           let res0 = partial_loop y0 lst in
593           let res1 = partial_loop y1 rst in
594           let s1, slot1 = res0.main
595           and s2, slot2 = res1.main in
596           let opcode = L3JIT.find cache3 trans s1 s2 in
597           let node = Node.of_int !preorder_counter in
598           incr preorder_counter;
599           let res =
600             if opcode == L3JIT.dummy then
601               (L3JIT.cache_apply cache3 auto trans s1 s2) empty_slot slot1 slot2 (Obj.magic ()) node
602             else
603               opcode empty_slot slot1 slot2 (Obj.magic())  (node)
604           in
605           { in0 = lst;
606             in1 = rst;
607             out0 = res0.main;
608             out1 = res1.main;
609             main = res }
610         end
611
612       and partial_loop l states =
613         if l == dummy_leaf then nil_res else
614           match l with
615           | Grammar2.Cache (_, r) -> r
616           | Grammar2.Leaf (_,_, _, id) -> start_loop id states
617           | Grammar2.Node0 id ->
618             if (Grammar2.terminal id) == Grammar2.nil_symbol then nil_res
619             else
620               rule_loop (Grammar2.non_terminal id) states dummy_leaf dummy_leaf
621
622           | Grammar2.Node1 (id, y0) ->
623             rule_loop (Grammar2.non_terminal id) states y0 dummy_leaf
624           | Grammar2.Node2 (id, y0, y1) ->
625             if Grammar2.is_terminal g id then
626             terminal_loop (Grammar2.terminal id) states y0 y1
627             else
628               rule_loop (Grammar2.non_terminal id) states y0 y1
629       in
630
631       let (_, slot) = (start_loop (Node.null) auto.init).main in
632       slot.(StateSet.min_elt auto.topdown_marking_states)
633     ;;
634
635
636
637
638
639
640
641   end
642