Commit f268a31a authored by POTTIER Francois's avatar POTTIER Francois

Refactoring: split off [LRijkstraCore].

Also, move some auxiliary functions to [Default].
parent 2afd7e7c
This diff is collapsed.
......@@ -11,19 +11,30 @@
(* *)
(******************************************************************************)
(* 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 module implements [--list-errors]. Its purpose 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. *)
(* The result of this analysis is a [.messages] file. It is written to the
standard output channel. No result is returned. *)
(* In this analysis, we explicitly ignore the [error] token. (We display a
warning if the grammar uses this token.) Thus, we disregard any reductions
or transitions that take place when the lookahead symbol is [error]. As a
result, any state whose incoming symbol is [error] is found 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. *)
module Run (X : sig
(* If [verbose] is set, produce various messages on [stderr]. *)
val verbose: bool
(* If [statistics] is defined, it is interpreted as the name of
a file to which one line of statistics is appended. *)
val statistics: string option
end) : sig end
end) : sig
(* The result of this analysis is a [.messages] file. It is written to the
standard output channel. No result is returned. *)
end
This diff is collapsed.
(******************************************************************************)
(* *)
(* Menhir *)
(* *)
(* François Pottier, Inria Paris *)
(* Yann Régis-Gianas, PPS, Université Paris Diderot *)
(* *)
(* Copyright Inria. All rights reserved. This file is distributed under the *)
(* terms of the GNU General Public License version 2, as described in the *)
(* file LICENSE. *)
(* *)
(******************************************************************************)
open Grammar
(* This is the core of the reachability analysis. After the automaton has been
constructed, this (expensive) analysis determines exactly under which
conditions each nonterminal edge in the automaton can be taken. This
information can then be used to determine how to reach certain states
in the automaton; see, e.g., [LRijkstra]. *)
(* In this analysis, we explicitly ignore the [error] token. (We display a
warning if the grammar uses this token.) Thus, we disregard any reductions
or transitions that take place when the lookahead symbol is [error]. As a
result, any state whose incoming symbol is [error] is found 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. *)
module Run (X : sig
(* If [verbose] is set, produce various messages on [stderr]. *)
val verbose: bool
end) : sig
(* A representation of words of terminal symbols. See [GrammarFunctor]. *)
module W : sig
type word
val singleton: Terminal.t -> word
val append: word -> word -> word
val length: word -> int
val elements: word -> Terminal.t list
val compare: word -> word -> int
end
(* [query s nt a] enumerates all words [w] and all symbols [z] such that, in
state [s], the outgoing edge labeled [nt] can be taken by consuming the
word [w], under the assumption that the next symbol is [z], and the first
symbol of the word [w.z] is [a]. *)
val query:
(* s: *) Lr1.node ->
(* nt: *) Nonterminal.t ->
(* a: *) Terminal.t ->
(* f: *) (W.word -> Terminal.t -> unit) ->
unit
(* [facts] is the total number of facts discovered. [edge_facts] is the
total number of edge facts discovered. [total_trie_size] is the sum of
the sizes of the tries that are internally constructed in the module
[Trie]. These numbers are provided for information only. *)
val facts: int
val edge_facts: int
val total_trie_size: int
end
......@@ -12,7 +12,7 @@ true: \
<menhir.{byte,native}>: use_unix
# Turn off assertions in some modules, where they are useful when debugging, but costly.
<LRijkstra.*>: noassert
<LRijkstra*>: noassert
# Declare that our temporary build directories should not be traversed.
# This is required by ocamlbuild 4.03; it will otherwise complain that
......
......@@ -83,3 +83,48 @@ let () =
let () =
Time.tick "Computing default reductions"
(* ------------------------------------------------------------------------ *)
(* Here are a number of auxiliary functions that provide information about the
LR(1) automaton. *)
(* [reductions_on s z] is the list of reductions permitted in state [s] when
the lookahead symbol is [z]. This is a list of zero or one elements. This
does not take default reductions into account. [z] must be real. *)
let reductions_on s z : Production.index list =
assert (Terminal.real z);
try
TerminalMap.find z (Lr1.reductions s)
with Not_found ->
[]
(* [has_reduction s z] tells whether state [s] is willing to reduce some
production (and if so, which one) when the lookahead symbol is [z]. It
takes a possible default reduction into account. [z] must be real. *)
let has_reduction s z : Production.index option =
assert (Terminal.real z);
match has_default_reduction s with
| Some (prod, _) ->
Some prod
| None ->
match reductions_on s z with
| prod :: prods ->
assert (prods = []);
Some prod
| [] ->
None
(* [causes_an_error s z] tells whether state [s] will initiate an error on the
lookahead symbol [z]. [z] must be real. *)
let causes_an_error s z : bool =
assert (Terminal.real z);
match has_default_reduction s with
| Some _ ->
false
| None ->
reductions_on s z = [] &&
not (SymbolMap.mem (Symbol.T z) (Lr1.transitions s))
......@@ -17,3 +17,14 @@ open Grammar
and, if so, upon which set of tokens. *)
val has_default_reduction : Lr1.node -> (Production.index * TerminalSet.t) option
(* [has_reduction s z] tells whether state [s] is willing to reduce some
production (and if so, which one) when the lookahead symbol is [z]. It
takes a possible default reduction into account. [z] must be real. *)
val has_reduction: Lr1.node -> Terminal.t -> Production.index option
(* [causes_an_error s z] tells whether state [s] will initiate an error on the
lookahead symbol [z]. [z] must be real. *)
val causes_an_error: Lr1.node -> Terminal.t -> bool
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment