-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathre.ml
226 lines (190 loc) · 6.27 KB
/
re.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
(* Several ways to match a regular expression against a string.
The point here is not performance, but rather exploring
elementary/elegant ways of doing it.
*)
type re =
| Empty
| Epsilon
| Char of char
| Alt of re * re
| Concat of re * re
| Star of re
type accepter = re -> string -> bool
let accepters = Queue.create ()
let tobench = Queue.create ()
let declare ?(bench=true) name accept =
Queue.add (name, accept) accepters;
if bench then Queue.add (name, accept) tobench
let direct r w =
let n = String.length w in
let rec a r i k = match r with
| Empty -> false
| Epsilon -> k i
| Char c -> i < n && w.[i] = c && k (i + 1)
| Alt (r1, r2) -> a r1 i k || a r2 i k
| Concat (r1, r2) -> a r1 i (fun j -> a r2 j k)
| Star r1 -> k i || a r1 i (fun j -> i < j && a r j k) in
a r 0 (fun i -> i = n)
let () = declare "direct" direct
(* CPS-style *)
let accept1 r s =
let n = String.length s in
let rec a r i k = match r with
| Empty -> false
| Epsilon -> k i
| Char c -> i < n && s.[i] = c && k (i+1)
| Alt (r1, r2) -> a r1 i k || a r2 i k
| Concat (r1, r2) -> a r1 i (fun j -> a r2 j k)
| Star r1 -> k i || a r1 i (fun j -> j > i && a r j k)
in
a r 0 (fun j -> j = n )
let () = declare "cps1" accept1
let accept2 r s =
let n = String.length s in
(* a may accept "" *)
let rec a r i k = match r with
| Empty -> false
| Epsilon -> k i
| Char c -> i < n && s.[i] = c && k (i+1)
| Alt (r1, r2) -> a r1 i k || a r2 i k
| Concat (r1, r2) -> a r1 i (fun j -> a r2 j k)
| Star r1 -> k i || a' r1 i (fun j -> j > i && a r j k)
(* but not a' *)
and a' r i k = match r with
| Empty | Epsilon -> false
| Char c -> i < n && s.[i] = c && k (i+1)
| Alt (r1, r2) -> a' r1 i k || a' r2 i k
| Concat (r1, r2) -> a r1 i (fun j -> (if j=i then a' else a) r2 j k)
| Star r1 -> k i || a' r1 i (fun j -> a r j k)
in
a r 0 (fun j -> j = n )
let () = declare "cps2" accept2
let accept3 r s =
let n = String.length s in
(* eps means we can accept "" *)
let rec a r eps i k = match r with
| Empty -> false
| Epsilon -> eps && k i
| Char c -> i < n && s.[i] = c && k (i+1)
| Alt (r1, r2) -> a r1 eps i k || a r2 eps i k
| Concat (r1, r2) -> a r1 true i (fun j -> a r2 (eps || j>i) j k)
| Star r1 -> eps && k i || a r1 false i (fun j -> a r true j k)
in
a r true 0 (fun j -> j = n )
let () = declare "cps3" accept3
let accept4 r w =
let n = String.length w in
let rec a r i k o = match r with
| Empty -> o ()
| Epsilon -> k i o
| Char c ->
if i < n && w.[i] = c then k (i + 1) o else o ()
| Alt (r1, r2) ->
a r1 i k (fun () -> a r2 i k o)
| Concat (r1, r2) ->
a r1 i (fun j h -> a r2 j k h) o
| Star r1 ->
k i (fun () ->
a r1 i (fun j h -> if i < j then a r j k h else h ()) o) in
a r 0 (fun i h -> i = n || h ()) (fun () -> false)
let () = declare "double-barrel-cps" accept4
let uncps r w =
let n = String.length w in
let rec a r i k = match r with
| Empty -> ()
| Epsilon -> k i
| Char c -> if i < n && w.[i] = c then k (i + 1)
| Alt (r1, r2) -> a r1 i k; a r2 i k
| Concat (r1, r2) -> a r1 i (fun j -> a r2 j k)
| Star r1 -> k i; a r1 i (fun j -> if i < j then a r j k) in
try a r 0 (fun i -> if i = n then raise Exit); false with Exit -> true
let () = declare "uncps" uncps
(** Other, less efficient solutions *)
(* brute force *)
let accept0 r s =
let n = String.length s in
let rec exists i j p = i <= j && (p i || exists (i+1) j p) in
let rec a i j r = (* r matches s[i..j[ *) match r with
| Empty -> false
| Epsilon -> i = j
| Char c -> i = j-1 && s.[i] = c
| Alt (r1, r2) -> a i j r1 || a i j r2
| Concat (r1, r2) -> exists i j (fun k -> a i k r1 && a k j r2)
| Star r1 as r -> i = j || exists (i+1) j (fun k -> a i k r1 && a k j r)
in
a 0 (String.length s) r
let () = declare ~bench:false "brute force" accept0
(* Brzozowski derivative *)
(* Is the empty word part of the language? *)
let rec null = function
| Empty | Char _ -> false
| Epsilon | Star _ -> true
| Alt (r1, r2) -> null r1 || null r2
| Concat (r1, r2) -> null r1 && null r2
let concat = function
| Empty, _ | _, Empty -> Empty
| Epsilon, r | r, Epsilon -> r
| r1, r2 -> Concat (r1, r2)
let alt = function
| Empty, r | r, Empty -> r
| r1, r2 -> Alt (r1, r2)
(* Brzozowski derivative:
returns a regexp r1 such that L(r1) = { w | cw in L(r) } *)
let rec derivative r c = match r with
| Empty | Epsilon ->
Empty
| Char d ->
if c = d then Epsilon else Empty
| Alt (r1, r2) ->
alt (derivative r1 c, derivative r2 c)
| Concat (r1, r2) ->
let r' = concat (derivative r1 c, r2) in
if null r1 then alt (r', derivative r2 c) else r'
| Star r1 ->
concat (derivative r1 c, r)
let brzozowski r w =
let n = String.length w in
let rec a r i = if i = n then null r else a (derivative r w.[i]) (i + 1) in
a r 0
let () = declare ~bench:false "brzozowski-derivative" brzozowski
(* various regexps for a* *)
let a = Char 'a' and b = Char 'b'
let ra = [ Star a;
Star (Star a);
Star (Alt (Epsilon, a));
Star (Alt (a, Epsilon));
Star (Alt (Empty, a));
Star (Alt (a, Empty));
Alt (Concat (Star a, Empty), Star a);
Star (Concat (Epsilon, a));
Star (Concat (a, Epsilon));
]
(* Sanity checks *)
let test (name, accept) =
Format.printf "testing %s...@?" name;
let r = Concat (Star a, b) in
assert (accept r "b");
assert (accept r "aab");
assert (not (accept r ""));
assert (not (accept r "bb"));
assert (not (accept r "aaba"));
for n = 0 to 10 do
let s = String.make n 'a' in
List.iter (fun r -> assert (accept r s)) ra;
let s = s ^ "#" in
List.iter (fun r -> assert (not (accept r s))) ra;
done;
Format.printf "OK@."
let () = Queue.iter test accepters
(* Benchmarks *)
let bench str ok (name, accept) =
let f () = List.iter (fun r -> assert (accept r str = ok)) ra in
Time.print_time ~msg:name f ()
let () =
Format.printf "-- good --@.";
let good = String.make 1_000_000 'a' in
Queue.iter (bench good true) tobench;
Format.printf "-- bad --@.";
let bad = String.make 25 'a' ^ "#" in
Queue.iter (bench bad false) tobench;
()