Commit 75a08d51 authored by POTTIER Francois's avatar POTTIER Francois

Introduce [accepting_state_can_have_successors].

parent 8fa7e1f1
...@@ -392,6 +392,18 @@ type dfa = { ...@@ -392,6 +392,18 @@ type dfa = {
expressions. Then, we construct a DFA whose states are the reachable expressions. Then, we construct a DFA whose states are the reachable
expressions and whose transitions correspond to derivation. *) expressions and whose transitions correspond to derivation. *)
(* If one wishes to stop as soon as an accepting state is encountered, then a
final state need not have any outgoing transitions; that would be
pointless. However, in some applications, one might wish to record that an
accepting state was reached and nevertheless continue reading the input. In
that case, the following flag should be set to true]. *)
let accepting_state_can_have_successors =
false
let may_have_successors (e : regexp) : bool =
accepting_state_can_have_successors || not (nullable e)
let dfa (e : regexp) : dfa = let dfa (e : regexp) : dfa =
(* Discover and number the nonempty reachable expressions. The most (* Discover and number the nonempty reachable expressions. The most
nontrivial aspect of this phase is termination. The fact that expressions nontrivial aspect of this phase is termination. The fact that expressions
...@@ -401,14 +413,15 @@ let dfa (e : regexp) : dfa = ...@@ -401,14 +413,15 @@ let dfa (e : regexp) : dfa =
more expressions being identified, therefore smaller automata. *) more expressions being identified, therefore smaller automata. *)
let module G = struct let module G = struct
type t = regexp type t = regexp
(* The successors of [e] are its derivatives along every character [a],
provided they are nonempty. *)
let foreach_successor e f = let foreach_successor e f =
Char.foreach (fun a -> if may_have_successors e then
let e' = delta a e in (* The successors of [e] are its derivatives along every character
if nonempty e' then [a], provided they are nonempty. *)
f e' Char.foreach (fun a ->
) let e' = delta a e in
if nonempty e' then
f e'
)
(* The single root is [e], if it is nonempty. *) (* The single root is [e], if it is nonempty. *)
let foreach_root f = let foreach_root f =
if nonempty e then if nonempty e then
...@@ -425,7 +438,12 @@ let dfa (e : regexp) : dfa = ...@@ -425,7 +438,12 @@ let dfa (e : regexp) : dfa =
(* The initial state is the encoding of [e]. *) (* The initial state is the encoding of [e]. *)
let init = encode e in let init = encode e in
(* The transition function is as follows. *) (* The transition function is as follows. *)
let transition q a = encode (delta a (decode q)) in let transition q a =
if may_have_successors (decode q) then
encode (delta a (decode q))
else
None
in
(* We are done. *) (* We are done. *)
{ n; init; decode; transition } { n; init; decode; transition }
......
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