Coverage.ml 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
(* The purpose of this algorithm is to find, for each pair of a state [s]
   and a terminal symbol [z] such that looking at [z] in state [s] causes
   an error, a minimal path (starting in some initial state) that actually
   triggers this error. *)

(* This is potentially useful for grammar designers who wish to better
   understand the properties of their grammar, or who wish to produce a
   list of all possible syntax errors (or, at least, one syntax error in
   each automaton state where an error may occur). *)

(* The problem seems rather tricky. One might think that it suffices to
   compute shortest paths in the automaton, and to use [Analysis.minimal]
   to replace each non-terminal symbol in a path with a minimal word
   that it generates. One can indeed do so, but this yields only a lower
   bound on the actual shortest path to the error at [s, z]. Indeed, two
   difficulties arise:

   - Some states have a default reduction. Thus, they will not trigger
     an error, even though they should. The error is triggered in some
     other state, after reduction takes place.

   - If the grammar has conflicts, conflict resolution removes some
     (shift or reduce) actions, hence may suppress the shortest path. *)

25 26 27 28 29 30 31
(* We explicitly ignore the [error] token. If the grammar mentions [error]
   and if some state is reachable only via an [error] transition, too bad;
   we report this state as unreachable. It would be too complicated to have
   to create a first error in order to be able to take certain transitions
   or drop certain parts of the input. *)
(* TEMPORARY warn if --coverage is set AND the grammar uses [error] *)

32 33
(* TEMPORARY explain how we approach the problem *)

34 35
open Grammar

36
(* ------------------------------------------------------------------------ *)
37

38 39 40 41 42 43
(* First, we implement the computation of forward shortest paths in the
   automaton. We view the automaton as a graph whose vertices are states. We
   label each edge with the minimum length of a word that it generates. This
   yields a lower bound on the actual distance to every state from any entry
   state. *)

44
let approximate : Lr1.node -> int =
45

46
  let module A = Astar.Make(struct
47

48 49
    type node =
      Lr1.node
50

51 52
    let equal s1 s2 =
      Lr1.Node.compare s1 s2 = 0
53

54 55
    let hash s =
      Hashtbl.hash (Lr1.number s)
56

57 58
    type label =
      unit
59

60 61 62
    let sources f =
      (* The sources are the entry states. *)
      ProductionMap.iter (fun _ s -> f s) Lr1.entry
63

64 65 66 67 68 69
    let successors s edge =
      SymbolMap.iter (fun sym s' ->
        (* The weight of the edge from [s] to [s'] is given by the function
           [Grammar.Analysis.minimal_symbol]. *)
        edge () (CompletedNatWitness.to_int (Analysis.minimal_symbol sym)) s'
      ) (Lr1.transitions s)
70

71 72 73
    let estimate _ =
      (* A* with a zero [estimate] behaves like Dijkstra's algorithm. *)
      0
74

75
  end) in
76
        
77 78 79
  let distance, _ = A.search (fun (_, _) -> ()) in
  distance

80 81 82
(* ------------------------------------------------------------------------ *)

(* Auxiliary functions. *)
83

84 85
(* This tests whether state [s] has a default reduction on [prod]. *)

86
let has_default_reduction_on s prod =
87 88 89 90 91 92
  match Invariant.has_default_reduction s with
  | Some (prod', _) ->
      prod = prod'
  | None ->
      false

93 94
(* This returns the list of reductions of [state] on token [z]. This
   should be a list of zero or one elements. *)
95 96

let reductions s z =
97
  assert (not (Terminal.equal z Terminal.error));
98 99 100 101
  try
    TerminalMap.find z (Lr1.reductions s)
  with Not_found ->
    []
102

103 104 105 106
(* This tests whether state [s] is willing to reduce production [prod]
   when the lookahead symbol is [z]. This test takes a possible default
   reduction into account. *)

107
let has_reduction s prod z : bool =
108
  assert (not (Terminal.equal z Terminal.error));
109
  has_default_reduction_on s prod ||
110
  List.mem prod (reductions s z)
111

112 113 114 115
(* This tests whether state [s] will initiate an error on the lookahead
   symbol [z]. *)

let causes_an_error s z =
116
  assert (not (Terminal.equal z Terminal.error));
117 118 119 120 121 122 123
  match Invariant.has_default_reduction s with
  | Some _ ->
      false
  | None ->
      reductions s z = [] &&
      not (SymbolMap.mem (Symbol.T z) (Lr1.transitions s))

POTTIER Francois's avatar
POTTIER Francois committed
124 125 126 127 128 129 130 131
(* ------------------------------------------------------------------------ *)

(* The analysis that follows is formulated as a least fixed point computation.
   The ordering is [CompletedNatWitness]. This represents the natural integers
   completed with infinity. The bottom element is [infinity], which means that
   no path is known, and the computation progresses towards smaller (finite)
   numbers, which means that shorter paths become known. *)

132
(* Using [CompletedNatWitness] means that we wish to compute shortest paths.
POTTIER Francois's avatar
POTTIER Francois committed
133 134 135 136 137
   We could instead use [BooleanWitness], which offers the same interface.
   That would mean we are happy as soon as we know an arbitrary path. That
   would be faster, but (according to a quick experiment) the paths thus
   obtained would be really far from optimal. *)

POTTIER Francois's avatar
POTTIER Francois committed
138 139 140 141
module P = struct
  include CompletedNatWitness
  type property = Terminal.t t
end
142

POTTIER Francois's avatar
POTTIER Francois committed
143 144 145 146
(* ------------------------------------------------------------------------ *)

(* More auxiliary functions for the analysis. *)

147 148 149 150
(* This tests whether state [s] has an outgoing transition labeled [sym].
   If so, [s'] is passed to the continuation [k]. Otherwise, [P.bottom] is
   returned. *)

POTTIER Francois's avatar
POTTIER Francois committed
151
let has_transition s sym k : P.property =
152 153 154 155 156
  try
    let s' = SymbolMap.find sym (Lr1.transitions s) in
    k s'
  with Not_found ->
    P.bottom
157 158 159

(* This computes a minimum over a set of terminal symbols. *)

160 161 162
exception Stop of P.property

let foreach_terminal_in toks bound (f : Terminal.t -> P.property) : P.property =
163 164
  (* Explicitly ignore the [error] token. *)
  let toks = TerminalSet.remove Terminal.error toks in
165 166 167 168
  (* Fast path. *)
  if TerminalSet.is_singleton toks then
    f (TerminalSet.choose toks)
  else try
169 170 171 172 173 174 175 176 177 178
    TerminalSet.fold (fun t accu ->
      let accu = P.min accu (f t) in
      match accu with
      | P.Finite (i, _) when i <= bound ->
          raise (Stop accu)
      | _ ->
          accu
    ) toks P.bottom
  with Stop accu ->
    accu
179 180 181

(* This computes a minimum over the productions associated with [nt]. *)

182 183 184 185 186 187 188 189 190 191 192 193
let productions nt =
  Production.foldnt nt [] (fun prod prods -> prod :: prods)

let productions nt =
  List.sort (fun prod1 prod2 ->
    P.compare (Analysis.minimal_prod prod1 0) (Analysis.minimal_prod prod2 0)
  ) (productions nt)

let productions : Nonterminal.t -> Production.index list =
  Obj.magic Misc.tabulate Nonterminal.n (Obj.magic productions) (* TEMPORARY *)

let foreach_production nt bound (f : Production.index -> P.property) : P.property =
194 195 196 197 198 199 200 201
  let prods = productions nt in
  match prods with
  (* Fast paths. *)
  | [] ->
      P.bottom
  | [ prod ] ->
      f prod
  | _ ->
202 203 204 205 206 207 208 209 210
  try
    List.fold_left (fun accu prod ->
      let accu = P.min accu (f prod) in
      match accu with
      | P.Finite (i, _) when i <= bound ->
          assert (i = bound);
          raise (Stop accu)
      | _ ->
          accu
211
    ) P.bottom prods
212 213
  with Stop accu ->
    accu
214

POTTIER Francois's avatar
POTTIER Francois committed
215 216 217 218 219 220 221 222 223 224 225 226
(* ------------------------------------------------------------------------ *)

(* The main analysis. *)

(* This analysis maps questions to properties.
   It is defined as a fixed point computation. *)

(* A question takes the form [s, a, prod, i, z], as defined by the record
   type below.

   [s] is a state. [prod/i] is a production suffix, which appears in the
   closure of state [s]. [a] and [z] are terminal symbols.
227

POTTIER Francois's avatar
POTTIER Francois committed
228
   This question means: what is the minimal length of a word [w] such that:
229

230
   1- the production suffix [prod/i] generates the word [w].
231 232 233 234 235 236

   2- [a] is in [FIRST(w.z)]
      i.e. either [w] is not epsilon and [a] is the first symbol in [w]
           or [w] is epsilon and [a] is [z].

   3- if, starting in state [s], the LR(1) automaton consumes [w] and looks at [z],
237
      then it reaches a state that is willing to reduce [prod] when looking at [z].
238 239 240 241 242 243 244 245

   The necessity for the parameter [z] arises from the fact that a state may
   have a reduction for some, but not all, values of [z]. (After conflicts
   have been resolved, we cannot predict which reduction actions we have.)
   This in turn makes the parameter [a] necessary: when trying to analyze a
   concatenation [AB], we must try all terminal symbols that come after [A]
   and form the beginning of [B]. *)

POTTIER Francois's avatar
POTTIER Francois committed
246 247 248 249 250 251 252 253 254 255
(* This analysis is very costly. Indeed, the number of possible questions is
   O(#items) * O(#alphabet^2), where #items is the total number of items in
   (the closure? of) all states of the automaton, and #alphabet is the number
   of terminal symbols. Furthermore, the lattice [CompletedNatWitness] is
   quite high (in fact, it does not have bounded height...) and it can take a
   while before the fixed point is reached. *)

(* The product [Lr1.n * Terminal.n * Terminal.n] is about 12 million when
   dealing with OCaml's grammar, in [--lalr] mode. *)

256 257 258 259 260 261 262 263
type question = {
  s: Lr1.node;
  a: Terminal.t;
  prod: Production.index;
  i: int;
  z: Terminal.t;
}

POTTIER Francois's avatar
POTTIER Francois committed
264
(* Debugging. TEMPORARY *)
265 266
let print_question q =
  Printf.fprintf stderr
267
    "{ s = %d; a = %s; prod/i = %s; z = %s }\n"
268 269 270 271 272 273
    (Lr1.number q.s)
    (Terminal.print q.a)
    (Item.print (Item.import (q.prod, q.i)))
    (Terminal.print q.z)

module QuestionMap =
POTTIER Francois's avatar
POTTIER Francois committed
274 275 276 277 278 279 280 281 282 283 284 285 286
  Map.Make(struct
    type t = question
    let compare q1 q2 =
      let c = Lr1.Node.compare q1.s q2.s in
      if c <> 0 then c else
      let c = Terminal.compare q1.a q2.a in
      if c <> 0 then c else
      let c = Production.compare q1.prod q2.prod in
      if c <> 0 then c else
      let c = Pervasives.compare q1.i q2.i in
      if c <> 0 then c else
      Terminal.compare q1.z q2.z
  end)
287

288 289 290
let first =
  Analysis.first_prod_lookahead

POTTIER Francois's avatar
POTTIER Francois committed
291 292 293 294 295 296 297
(* The following function defines the analysis. *)

(* We have a certain amount of flexibility in how much information we memoize;
   if we use a recursive call to [answer], we re-compute; if we use a call to
   [get], we memoize. As long as every direct recursive call is decreasing,
   either choice is acceptable. A quick experiment suggests that memoization
   everywhere is cost-effective. *)
298

POTTIER Francois's avatar
POTTIER Francois committed
299
let answer (q : question) (get : question -> P.property) : P.property =
300 301 302 303 304

  let rhs = Production.rhs q.prod in
  let n = Array.length rhs in
  assert (0 <= q.i && q.i <= n);

305 306 307 308
  (* According to conditions 2 and 3, the answer to this question is the empty
     set unless [a] is in [FIRST(prod/i.z)]. Thus, by convention, we will ask
     this question only when this precondition is satisfied. *)
  assert (TerminalSet.mem q.a (first q.prod q.i q.z));
309
  (* TEMPORARY ultimately disable this assertion? *)
310

311 312
  (* Now, three cases arise: *)
  if q.i = n then begin
313

314 315 316 317
    (* Case 1. The suffix determined by [prod] and [i] is epsilon. To satisfy
       condition 1, [w] must be the empty word. Condition 2 is implied by our
       precondition. There remains to check whether condition 3 is satisfied.
       If so, we return the empty word; otherwise, no word exists. *)
318

319 320 321 322
    assert (Terminal.equal q.a q.z); (* per our precondition *)
    if has_reduction q.s q.prod q.z  (* condition 3 *)
    then P.epsilon
    else P.bottom
323

324 325
  end
  else begin
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    match Analysis.minimal_prod q.prod q.i with
    | P.Infinity ->
        P.bottom
    | P.Finite (bound, _) ->

        (* Case 2. The suffix determined by [prod] and [i] begins with a symbol
           [sym]. The state [s] must have an outgoing transition along [sym];
           otherwise, no word exists. *)

        let sym = rhs.(q.i) in
        has_transition q.s sym (fun s' ->
          match sym with
          | Symbol.T t ->

              (* Case 2a. [sym] is a terminal symbol [t]. Our precondition implies
                 that [t] is equal to [a]. [w] must begin with [a]. The rest must
                 be some word [w'] such that, by starting from [s'] and by reading
                 [w'], we reach our goal. The first letter in [w'] could be any
                 terminal symbol [c], so we try all of them. *)

              assert (Terminal.equal q.a t); (* per our precondition *)
              assert (1 <= bound);
              P.add (P.singleton q.a) (
                foreach_terminal_in (first q.prod (q.i + 1) q.z) (bound - 1) (fun c ->
                  get { s = s'; a = c; prod = q.prod; i = q.i + 1; z = q.z }
                )
              )

          | Symbol.N nt ->

              (* Case 2b. [sym] is a nonterminal symbol [nt]. For each letter [c],
                 for each production [prod'] associated with [nt], we concatenate:
                 1- a word that takes us from [s], beginning with [a], to a state
                    where we can reduce [prod'], looking at [c];
                 2- a word that takes us from [s'], beginning with [c], to a state
                    where we reach our original goal. *)

              foreach_terminal_in (first q.prod (q.i + 1) q.z) bound (fun c ->
                foreach_production nt bound (fun prod' ->
                  if TerminalSet.mem q.a (first prod' 0 c) then
                    P.add_lazy
                      (get { s = q.s; a = q.a; prod = prod'; i = 0; z = c })
                      (fun () -> get { s = s'; a = c; prod = q.prod; i = q.i + 1; z = q.z })
                  else
                    P.bottom
                )
              )

        )
376 377 378

  end

POTTIER Francois's avatar
POTTIER Francois committed
379
(* Debugging. TEMPORARY *)
380 381 382
let qs = ref 0
let answer q get =
  incr qs;
383
  if !qs mod 10000 = 0 then
POTTIER Francois's avatar
POTTIER Francois committed
384
    Printf.fprintf stderr "qs = %d\n%!" !qs;
385
  answer q get
386

POTTIER Francois's avatar
POTTIER Francois committed
387 388
(* The fixed point. *)

389 390 391
module F =
  Fix.Make
    (Maps.PersistentMapsToImperativeMaps(QuestionMap))
POTTIER Francois's avatar
POTTIER Francois committed
392
    (P)
393

POTTIER Francois's avatar
POTTIER Francois committed
394
let answer : question -> P.property =
395 396
  F.lfp answer

POTTIER Francois's avatar
POTTIER Francois committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410
(* ------------------------------------------------------------------------ *)

(* We now wish to determine, given a state [s'] and a terminal symbol [z],
   a minimal path that takes us from some entry state to state [s'] with
   [z] as the next (unconsumed) symbol. *)

(* This can be formulated as a search for a shortest path in a graph. The
   graph is not just the automaton, though. It is a (much) larger graph
   whose vertices are pairs [s, z] and whose edges are obtained by calling
   the expensive analysis above. Because we perform a backward search, from
   [s', z] to any entry state, we use reverse edges, from a state to its
   predecessors in the automaton. *)

(* Debugging. TEMPORARY *)
411 412
let es = ref 0

413
exception Success of Lr1.node * P.property
POTTIER Francois's avatar
POTTIER Francois committed
414

415 416 417
let backward (s', z) : unit =

  let module A = Astar.Make(struct
POTTIER Francois's avatar
POTTIER Francois committed
418 419

    (* A vertex is a pair [s, z]. *)
420
    type node =
POTTIER Francois's avatar
POTTIER Francois committed
421 422
        Lr1.node * Terminal.t

POTTIER Francois's avatar
POTTIER Francois committed
423 424
    let equal (s'1, z1) (s'2, z2) =
      Lr1.Node.compare s'1 s'2 = 0 && Terminal.compare z1 z2 = 0
POTTIER Francois's avatar
POTTIER Francois committed
425 426 427 428 429 430 431

    let hash (s, z) =
      Hashtbl.hash (Lr1.number s, z)

    (* An edge is labeled with a property of the form [Finite (i, pi)],
       that is, a distance [i] and a witness path [pi]. *)
    type label =
432
      P.property
POTTIER Francois's avatar
POTTIER Francois committed
433 434

    (* Backward search from the single source [s', z]. *)
435
    let sources f = f (s', z)
POTTIER Francois's avatar
POTTIER Francois committed
436

437
    let successors (s', z) edge =
438 439
      match Lr1.incoming_symbol s' with
      | None ->
POTTIER Francois's avatar
POTTIER Francois committed
440
          (* An entry state has no predecessor states. *)
441
          ()
POTTIER Francois's avatar
POTTIER Francois committed
442

443
      | Some (Symbol.T t) ->
POTTIER Francois's avatar
POTTIER Francois committed
444 445
          (* There is an edge from [s] to [s'] labeled [t] in the automaton.
             Thus, our graph has an edge from [s', z] to [s, t], labeled [t]. *)
446
          let label = P.singleton t in
POTTIER Francois's avatar
POTTIER Francois committed
447
          List.iter (fun s ->
448
            edge label 1 (s, t)
449
          ) (Lr1.predecessors s')
POTTIER Francois's avatar
POTTIER Francois committed
450

451
      | Some (Symbol.N nt) ->
POTTIER Francois's avatar
POTTIER Francois committed
452 453 454 455 456 457 458
          (* There is an edge from [s] to [s'] labeled [nt] in the automaton.
             For every production [prod] associated with [nt], for every
             letter [a], we query the analysis for a path that begins in [s]
             and leads to reducing [prod] when the lookahead symbol is [z].
             Such a path [w] takes us from [s, a] to [s', z]. Thus, our graph
             has an edge, labeled [w], in the reverse direction. *)
          List.iter (fun s ->
459 460
            Production.foldnt nt () (fun prod () ->
              TerminalSet.iter (fun a ->
461 462
                let p = answer { s = s; a = a; prod = prod; i = 0; z = z } in
                match p with
463 464
                | P.Infinity ->
                    ()
465 466
                | P.Finite (w, _) ->
                    edge p w (s, a)
467 468 469
              ) (first prod 0 z)
            )
          ) (Lr1.predecessors s')
POTTIER Francois's avatar
POTTIER Francois committed
470

471
    let estimate (s', _z) =
472
      approximate s'
473

474
  end) in
POTTIER Francois's avatar
POTTIER Francois committed
475

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
  (* Search backwards from [s', z], stopping as soon as an entry state [s] is
     reached. In that case, return the state [s] and the path that has been
     found. *)

  let _, _ = A.search (fun ((s, _), ps) ->
    (* Debugging. TEMPORARY *)
    incr es;
    if !es mod 10000 = 0 then
      Printf.fprintf stderr "es = %d\n%!" !es;
    (* If [s] is a start state... *)
    if Lr1.incoming_symbol s = None then
      (* [labels] is a list of properties. Projecting onto the second
         component yields a list of paths (sequences of terminal symbols),
         which we concatenate to obtain a path. Because the edges that were
         followed last are in front of the list, and because this is a
         reverse graph, we obtain a path that makes direct sense: it is a
         sequence of terminal symbols that will take the automaton into
         state [s'] if the next (unconsumed) symbol is [z]. We append [z]
         at the end of this path. *)
      let p = List.fold_right P.add ps (P.singleton z) in
      raise (Success (s, p))
  ) in
  ()
499

POTTIER Francois's avatar
POTTIER Francois committed
500 501 502 503 504 505 506
(* ------------------------------------------------------------------------ *)

(* For each state [s'] and for each terminal symbol [z] such that [z] triggers
   an error in [s'], backward search is performed. For each state [s'], we
   stop as soon as one [z] is found, i.e., as soon as one way of causing an
   error in state [s'] is found. *)

507 508
open ReferenceInterpreter

POTTIER Francois's avatar
POTTIER Francois committed
509
let backward s' : P.property =
510
  
POTTIER Francois's avatar
POTTIER Francois committed
511
  (* Debugging. TEMPORARY *)
512 513 514
  Printf.fprintf stderr
    "Attempting to reach an error in state %d:\n%!"
    (Lr1.number s');
515

516 517 518 519
  try

    (* This loop stops as soon as we are able to reach one error at [s']. *)
    Terminal.iter (fun z ->
520
      if not (Terminal.equal z Terminal.error) && causes_an_error s' z then
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
        backward (s', z)
    );
    (* No error can be triggered in state [s']. *)
    P.bottom

  with Success (s, p) ->
    (* An error can be triggered in state [s'] by beginning in the initial
       state [s] and reading the sequence of terminal symbols [p]. *)

    (* Validation. *)
    let outcome =
      ReferenceInterpreter.check_error_path
        (Lr1.nt_of_entry s)
        (P.extract p)
    in
    let fail msg =
      Printf.fprintf stderr "coverage: internal error: %s.\n%!" msg;
      p
    in
    match outcome with
    | OInputReadPastEnd ->
        fail "input was read past its end"
    | OInputNotFullyConsumed ->
        fail "input was not fully consumed"
    | OUnexpectedAccept ->
        fail "input was unexpectedly accepted"
    | OK state ->
        if Lr1.Node.compare state s' = 0 then begin
          Printf.fprintf stderr "Validated.\n%!";
          p
        end
        else
          fail (
            Printf.sprintf "error occurred in state %d instead of %d"
              (Lr1.number state)
              (Lr1.number s')
          )
558

POTTIER Francois's avatar
POTTIER Francois committed
559
(* Test. TEMPORARY *)
POTTIER Francois's avatar
POTTIER Francois committed
560 561 562

let () =
  Lr1.iter (fun s' ->
POTTIER Francois's avatar
POTTIER Francois committed
563
    let p = backward s' in
564
    Printf.fprintf stderr "%s\n%!" (P.print Terminal.print p);
565 566 567 568 569
    let approx = approximate s'
    and real = P.to_int p - 1 in
    assert (approx <= real);
    if approx < real && real < max_int - 1 then
        Printf.fprintf stderr "Approx = %d, real = %d\n" approx real;
POTTIER Francois's avatar
POTTIER Francois committed
570
    Printf.fprintf stderr "Questions asked so far: %d\n" !qs;
571
    Printf.fprintf stderr "Edges so far: %d\n" !es
POTTIER Francois's avatar
POTTIER Francois committed
572
  )
573

POTTIER Francois's avatar
POTTIER Francois committed
574 575 576 577
(* TEMPORARY
   also: first compute an optimistic path using the simple algorithm
   and check if this path is feasible in the real automaton
*)
578
(* TEMPORARY what about the pseudo-token [#]? *)
579 580
(* TEMPORARY implement and exploit [Lr1.ImperativeNodeMap] using an array *)
(* TEMPORARY the code in this module should run only if --coverage is set *)