+let get_trans g auto tag states =
+ StateSet.fold (fun q tr_acc ->
+ List.fold_left
+ (fun ((lstates, rstates, tacc) as acc) (ts, trs) ->
+ if TagSet.mem (Tag.translate tag) ts then
+ if not (TagSet.mem Tag.attribute ts) && Grammar2.is_attribute g tag
+ then acc
+ else
+ let _, _, _, phi = Transition.node trs in
+ let l, r = Formula.st phi in
+ (StateSet.union l lstates,
+ StateSet.union r rstates,
+ Translist.cons trs tacc)
+ else acc)
+ tr_acc (Hashtbl.find auto.trans q)
+ ) states (StateSet.empty, StateSet.empty, Translist.nil)
+
+(* Grammar run *)
+let dispatch_param0 conf id2 y0 y1 =
+ match conf with
+ | Grammar2.C0 | Grammar2.C2 -> Grammar2.Node0 id2
+ | Grammar2.C1 | Grammar2.C5 -> Grammar2.Node1(id2,y0)
+ | Grammar2.C3 | Grammar2.C6 -> y0
+ | Grammar2.C4 -> Grammar2.Node2(id2, y0, y1)
+
+let dispatch_param1 conf id2 y0 y1 =
+ match conf with
+ | Grammar2.C2 -> y0
+ | Grammar2.C3 -> Grammar2.Node0 id2
+ | Grammar2.C5 -> y1
+ | Grammar2.C6 -> Grammar2.Node1(id2, y1)
+ | _ -> Grammar2.dummy_param
+
+ module K_down = struct
+ type t = Grammar2.n_symbol * StateSet.t
+ let hash (x,y) = HASHINT2(Node.to_int x, Uid.to_int y.StateSet.Node.id)
+ let equal (x1,y1) (x2,y2) = x1 == x2 && y1 == y2
+ end
+
+ module K_up = struct
+ type t = Grammar2.n_symbol * StateSet.t * StateSet.t * StateSet.t
+ let hash (a,b,c,d) =
+ HASHINT4 (Node.to_int a,
+ Uid.to_int b.StateSet.Node.id,
+ Uid.to_int c.StateSet.Node.id,
+ Uid.to_int d.StateSet.Node.id)
+ let equal (a1, b1, c1, d1) (a2, b2, c2, d2) =
+ a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2
+ end
+
+ module DCache =
+ struct
+ include Hashtbl.Make(K_down)
+ let dummy = StateSet.singleton State.dummy
+ let notfound l = l.(0) == dummy && l.(1) == dummy
+ let find h k =
+ try
+ find h k
+ with
+ Not_found ->
+ let a = [| dummy; dummy |] in
+ add h k a;
+ a
+ end
+ module UCache = Hashtbl.Make(K_up)
+ type result = {
+ in0 : StateSet.t;
+ in1 : StateSet.t;
+ out0 : StateSet.t * U.t;
+ out1 : StateSet.t * U.t;
+ main : StateSet.t * U.t
+ }
+ let mk_empty e =
+ { in0 = StateSet.empty;
+ in1 = StateSet.empty;
+ out0 = e;
+ out1 = e;
+ main = e
+ }
+ let mk_nil s v =
+ {
+ mk_empty (s,v) with
+ out0 = StateSet.empty,v;
+ out1 = StateSet.empty,v;
+ }
+
+ let grammar_run auto g () =
+ let dummy_leaf = Grammar2.dummy_param in
+ let dummy_set = StateSet.singleton State.dummy in
+ let res_len = (StateSet.max_elt auto.states) + 1 in
+ let empty_slot = Array.create res_len U.NS.empty in
+ let nil_res = mk_nil auto.bottom_states empty_slot in
+ let empty_res = mk_empty (StateSet.empty, empty_slot) in
+ let cache3 = L3JIT.create () in
+ let dummy2 = (StateSet.empty, StateSet.empty, Translist.nil) in
+ let cache2 = Cache.Lvl2.create 512 dummy2 in
+ let rule_counter = ref 0 in
+ let preorder_counter = ref 0 in
+ let dcache = DCache.create 1023 in
+ let ucache = UCache.create 1023 in
+ let term_array = [| StateSet.empty; StateSet.empty |] in
+ let get_trans tag states =
+ let c = Cache.Lvl2.find cache2 tag (Uid.to_int states.StateSet.Node.id) in
+ if c == dummy2 then
+ let c = get_trans g auto tag states in
+ begin
+ Cache.Lvl2.add cache2 tag (Uid.to_int states.StateSet.Node.id) c;
+ c
+ end
+ else c
+ in
+ let lambda = ref 0 in
+ let rec start_loop idx states =
+ TRACE("grammar", 2, __ "Node %i\n%!" (Node.to_int idx));
+ if states == dummy_set then nil_res else
+ if idx < Node.null then nil_res
+ else begin
+ let symbol = Grammar2.start_tag g idx in
+ let fc = Grammar2.start_first_child g idx in
+ let ns = Grammar2.start_next_sibling g fc in
+ if Grammar2.is_terminal g symbol then
+ let t = Grammar2.terminal symbol in
+ terminal_loop t states (Grammar2.Leaf (~-1,0,term_array, fc)) (Grammar2.Leaf (~-1,1,term_array, ns))
+ else
+ let nt = Grammar2.non_terminal symbol in
+ incr lambda;
+ let lmbd = !lambda in
+ let y0 = (Grammar2.Leaf (lmbd,0, term_array, fc))
+ and y1 = (Grammar2.Leaf (lmbd,1, term_array, ns)) in
+ rule_loop nt states y0 y1
+ end
+ and rule_loop (t : Grammar2.n_symbol) states y0 y1 =
+ if t = Node.nil || states == dummy_set then nil_res else
+ let () = incr rule_counter in
+ if !rule_counter land 65535 == 0 then begin Gc.minor() end;
+(* let k = (t, states) in*)
+(* let pstates = DCache.find dcache k in
+ let notfound = DCache.notfound pstates in *)
+ let rhs = Grammar2.get_rule g t in
+ let id1 = Grammar2.get_id1 rhs in
+ let id2 = Grammar2.get_id2 rhs in
+ let conf = Grammar2.get_conf rhs in
+(* if notfound then*)
+ let ny0 = dispatch_param0 conf id2 y0 y1 in
+ let ny1 = dispatch_param1 conf id2 y0 y1 in
+ let res = dispatch_loop id1 states ny0 ny1 in
+(* pstates.(0) <- res.in0;
+ pstates.(1) <- res.in1; *)
+ res (*
+ UCache.add ucache (t, states, fst res.out0, fst res.out1)
+ res.main;
+ let h = Hashtbl.create 7 in
+ for i = 0 to res_len - 1 do
+ Hashtbl.add h (0, i) (snd res.out0).(i);
+ Hashtbl.add h (1, i) (snd res.out1).(i);
+ done;
+ { res with
+ main = ((fst res.main), (U.close h (snd res.main)));
+ } *)
+(*
+ else
+ let res0 = partial_loop y0 pstates.(0) in
+ let res1 = partial_loop y1 pstates.(1) in
+ let k2 = (t, states, fst res0.main, fst res1.main) in
+ let s, r =
+ try
+ UCache.find ucache k2
+ with
+ Not_found ->
+ let ores0 = { res0 with main = fst res0.main, U.var 0 (snd res0.main) }
+ and ores1 = { res1 with main = fst res1.main, U.var 1 (snd res1.main) }
+ in
+ let res = dispatch_loop id1 states (Grammar2.Cache (0,ores0)) (Grammar2.Cache (1, ores1)) in
+ UCache.add ucache k2 res.main;
+ res.main
+ in
+ let h = Hashtbl.create 7 in
+ for i = 0 to res_len - 1 do
+ Hashtbl.add h (0, i) (snd res0.main).(i);
+ Hashtbl.add h (1, i) (snd res1.main).(i);
+ done;
+ { in0 = pstates.(0);
+ in1 = pstates.(1);
+ out0 = res0.main;
+ out1 = res1.main;
+ main = s, U.close h r;
+ }
+*)
+ and dispatch_loop id1 states ny0 ny1 =
+ if Grammar2.is_non_terminal g id1 then
+ rule_loop (Grammar2.non_terminal id1) states ny0 ny1
+ else
+ terminal_loop (Grammar2.terminal id1) states ny0 ny1
+
+ and terminal_loop (symbol : Grammar2.t_symbol) states y0 y1 =
+
+ if symbol == Grammar2.nil_symbol || symbol = Node.nil || states == dummy_set then nil_res else begin
+ let tag = Grammar2.tag symbol in
+ let lst, rst, trans = get_trans tag states in
+ let res0 = partial_loop y0 lst in
+ let res1 = partial_loop y1 rst in
+ let s1, slot1 = res0.main
+ and s2, slot2 = res1.main in
+ let opcode = L3JIT.find cache3 trans s1 s2 in
+ let node = Node.of_int !preorder_counter in
+ incr preorder_counter;
+ let res =
+ if opcode == L3JIT.dummy then
+ (L3JIT.cache_apply cache3 auto trans s1 s2) empty_slot slot1 slot2 (Obj.magic ()) node
+ else
+ opcode empty_slot slot1 slot2 (Obj.magic()) (node)
+ in
+ { in0 = lst;
+ in1 = rst;
+ out0 = res0.main;
+ out1 = res1.main;
+ main = res }
+ end
+
+ and partial_loop l states =
+ if l == dummy_leaf then nil_res else
+ match l with
+ | Grammar2.Cache (_, r) -> r
+ | Grammar2.Leaf (_,_, _, id) -> start_loop id states
+ | Grammar2.Node0 id ->
+ if (Grammar2.terminal id) == Grammar2.nil_symbol then nil_res
+ else
+ rule_loop (Grammar2.non_terminal id) states dummy_leaf dummy_leaf
+
+ | Grammar2.Node1 (id, y0) ->
+ rule_loop (Grammar2.non_terminal id) states y0 dummy_leaf
+ | Grammar2.Node2 (id, y0, y1) ->
+ if Grammar2.is_terminal g id then
+ terminal_loop (Grammar2.terminal id) states y0 y1
+ else
+ rule_loop (Grammar2.non_terminal id) states y0 y1
+ in
+
+ let (_, slot) = (start_loop (Node.null) auto.init).main in
+ slot.(StateSet.min_elt auto.topdown_marking_states)
+ ;;
+
+
+ (* Slow reference top-down implementation *)
+ let naive_top_down auto tree root states ctx =
+ let res_len = StateSet.max_elt auto.states + 1 in
+ let empty_slot = Array.create res_len U.NS.empty in
+ let nil_res = auto.bottom_states, empty_slot in
+ let cache3 = L3JIT.create () in
+ let l3jit_dispatch trl s1 s2 t sl1 sl2 =
+ let f = L3JIT.find cache3 trl s1 s2 in
+ if f == L3JIT.dummy then (L3JIT.cache_apply cache3 auto trl s1 s2) empty_slot sl1 sl2 tree t
+ else f empty_slot sl1 sl2 tree t
+ in
+ let dummy = Translist.nil, StateSet.singleton State.dummy, StateSet.singleton State.dummy in
+ let cache2 = Cache.Lvl2.create 512 dummy in
+ let rec loop t states ctx =
+ if states == StateSet.empty then nil_res
+ else if t == Tree.nil then (*StateSet.inter states auto.bottom_states, empty_slot *) nil_res
+ else
+ let tag = Tree.tag tree t in
+
+ let trans, lstates, rstates =
+ let c = Cache.Lvl2.find cache2 (Uid.to_int states.StateSet.Node.id) tag in
+ if c == dummy then
+ let c = Ata.get_trans auto states tag in
+ Cache.Lvl2.add cache2 (Uid.to_int states.StateSet.Node.id) tag c;
+ c
+ else c
+ in
+ let s1, res1 = loop (Tree.first_child tree t) lstates ctx
+ and s2, res2 = loop (Tree.next_sibling tree t) rstates ctx in
+ l3jit_dispatch trans s1 s2 t res1 res2
+ in
+ loop root states ctx
+
+
+
+
+ let naive_top_down_run auto tree root =
+ let res, slot = naive_top_down auto tree root auto.init (Tree.closing tree root) in
+ slot.(StateSet.min_elt auto.topdown_marking_states)
+
+
+
+ let eval_form auto s1 s2 f =
+ let rec loop f =
+ match Formula.expr f with
+ | Formula.False | Formula.True | Formula.Pred _ -> f
+ | Formula.Atom(`Left, b, q) ->
+ Formula.of_bool (b == (StateSet.mem q s1))
+ | Formula.Atom (`Right, b, q) ->
+ Formula.of_bool(b == (StateSet.mem q s2))
+ | Formula.Atom (`Epsilon, _, _) -> assert false
+
+ | Formula.Or(f1, f2) ->
+ let b1 = loop f1 in
+ let b2 = loop f2 in
+ Formula.or_pred b1 b2
+ | Formula.And(f1, f2) ->
+ let b1 = loop f1 in
+ let b2 = loop f2 in
+ Formula.and_pred b1 b2
+ in
+ loop f
+
+ let eval_trans auto s1 s2 trans =
+ Translist.fold
+ (fun t ((a_st, mark) as acc)->
+ let q, _, m, f = Transition.node t in
+ let form = eval_form auto s1 s2 f in
+ match Formula.expr form with
+ | Formula.True -> StateSet.add q a_st, mark || m
+ | Formula.False -> acc
+ | _ -> assert false
+ ) trans (StateSet.empty, false)
+
+
+ let set a i v =
+ TRACE("twopass", 2, __ "Setting node %i to state %a\n%!"
+ i StateSet.print v);
+ a.(i) <- v
+
+ let twopass_top_down states_array auto tree root states ctx =
+ let dummy3 = StateSet.singleton State.dummy in
+ let cache3 = Cache.Lvl3.create 512 dummy3 in
+ let dummy2 = Translist.nil, StateSet.singleton State.dummy, StateSet.singleton State.dummy in
+ let cache2 = Cache.Lvl2.create 512 dummy2 in
+ let attributes = TagSet.inj_positive (Tree.attribute_tags tree) in
+ let rec loop t states ctx =
+ if t == Tree.nil then auto.bottom_states
+ else if states == StateSet.empty then
+ let () = set states_array (Node.to_int t) auto.bottom_states in
+ auto.bottom_states
+ else
+ let tag = Tree.tag tree t in
+ TRACE("twopass", 2, __ "Traversing node %i (tag %s) in states %a\n%!" (Node.to_int t) (Tag.to_string tag)
+ StateSet.print states
+ );
+ let trans, lstates, rstates =
+ let c = Cache.Lvl2.find cache2 (Uid.to_int states.StateSet.Node.id) tag in
+ if c == dummy2 then
+ let c = Ata.get_trans ~attributes:attributes auto states tag in
+ Cache.Lvl2.add cache2 (Uid.to_int states.StateSet.Node.id) tag c;
+ c
+ else c
+ in
+ TRACE("twopass", 2, __ "\nTransitions are:\n%!");
+ TRACE("twopass", 2, __ "\nTransitions are:\n%a\n%!"
+ Translist.print trans
+ );
+ let s1 = loop (Tree.first_child tree t) lstates ctx
+ and s2 = loop (Tree.next_sibling tree t) rstates ctx in
+ let st =
+ let c = Cache.Lvl3.find cache3
+ (Uid.to_int s1.StateSet.Node.id)
+ (Uid.to_int s2.StateSet.Node.id)
+ (Uid.to_int trans.Translist.Node.id)
+ in
+ if c == dummy3 then
+ let c, _ = eval_trans auto s1 s2 trans in
+ Cache.Lvl3.add cache3
+ (Uid.to_int s1.StateSet.Node.id)
+ (Uid.to_int s2.StateSet.Node.id)
+ (Uid.to_int trans.Translist.Node.id) c;c
+ else c
+ in
+ set states_array (Node.to_int t) st;
+ st
+ in
+ loop root states ctx, (dummy2, cache2)
+
+
+ type action = Nop | Mark | Dummy
+
+ let twopass_top_down_scan states_array (dummy2, cache2) auto tree root states ctx =
+ let attributes = TagSet.inj_positive (Tree.attribute_tags tree) in
+ let cache3 = Cache.Lvl3.create 512 Dummy in
+ let rec loop t states acc =
+ if states == StateSet.empty || t = Tree.nil then acc
+ else
+ let tag = Tree.tag tree t in
+ let trans, _, _ =
+ let c = Cache.Lvl2.find cache2 (Uid.to_int states.StateSet.Node.id) tag in
+ if c == dummy2 then
+ let c = Ata.get_trans ~attributes:attributes auto states tag in
+ Cache.Lvl2.add cache2 (Uid.to_int states.StateSet.Node.id) tag c;
+ c
+ else c
+ in
+ let fs = Tree.first_child tree t in
+ let ns = Tree.next_sibling tree t in
+ let s1 = if fs != Tree.nil then states_array.(Node.to_int fs) else auto.bottom_states
+ and s2 = if ns != Tree.nil then states_array.(Node.to_int ns) else auto.bottom_states
+ in
+ let mark =
+ let c = Cache.Lvl3.find cache3
+ (Uid.to_int s1.StateSet.Node.id)
+ (Uid.to_int s2.StateSet.Node.id)
+ (Uid.to_int trans.Translist.Node.id)
+ in
+ if c == Dummy then
+ let _, c = eval_trans auto s1 s2 trans in
+ let c = if c then Mark else Nop in
+ Cache.Lvl3.add cache3
+ (Uid.to_int s1.StateSet.Node.id)
+ (Uid.to_int s2.StateSet.Node.id)
+ (Uid.to_int trans.Translist.Node.id) c;c
+ else c
+ in
+ TRACE("twopass", 2, __ "Evaluating node %i (tag %s).\n%!States=%a\n%!"
+ (Node.to_int t)
+ (Tag.to_string tag)
+ StateSet.print states
+ );
+ TRACE("twopass", 2, __ "Translist=%a\nLeft=%a\nRight=%a\nMark=%s\n\n%!"
+ Translist.print trans
+ StateSet.print s1
+ StateSet.print s2
+ (match mark with
+ Dummy -> "Dummy"
+ | Mark -> "Mark"
+ | Nop -> "Nop"));
+ if mark == Mark then
+ loop ns s2 (loop fs s1 (U.NS.snoc acc t))
+ else
+ loop ns s2 (loop fs s1 acc)
+ in
+ loop root states U.NS.empty
+
+ let twopass_top_down_run auto tree root =
+ let len = Node.to_int (Tree.closing tree root) + 1 in
+ TRACE("twopass", 2, __ "Creating array of size: %i\n%!" len);
+ let states_array = Array.make len StateSet.empty in
+ let _, cache =
+ twopass_top_down states_array auto tree root auto.init Tree.nil
+ in
+ twopass_top_down_scan states_array cache auto tree root auto.init Tree.nil
+
+
+
+
+
+
+
+
+