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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
open Tree
exception Syntax_error of string
let print re =
let rec stringify_ast = function
| Literal a -> a
| Epsilon -> "ε"
| Union (r1, r2) -> "(" ^ stringify_ast r1 ^ " + " ^ stringify_ast r2 ^ ")"
| Concat (r1, r2) -> "(" ^ stringify_ast r1 ^ " . " ^ stringify_ast r2 ^ ")"
| Star r1 -> stringify_ast r1 ^ "*"
| Empty -> "∅"
in
print_string (stringify_ast re);
print_newline ()
;;
let export_graphviz re =
let count = ref 0 in
let rec graphvizify parent = function
| Literal a ->
incr count;
string_of_int !count
^ " [label=\""
^ a
^ "\", shape=ellipse, ];\n"
^ string_of_int parent
^ " -> "
^ string_of_int !count
^ "[label=\"\", ];\n"
| Epsilon ->
incr count;
string_of_int !count
^ " [label=\"ε\", shape=ellipse, ];\n"
^ string_of_int parent
^ " -> "
^ string_of_int !count
^ "[label=\"\", ];\n"
| Union (r1, r2) ->
incr count;
let c = !count in
graphvizify c r1
^ graphvizify c r2
^ string_of_int c
^ " [label=\"Union\", shape=ellipse, ];\n"
^ string_of_int parent
^ " -> "
^ string_of_int c
^ "[label=\"\", ];\n"
| Concat (r1, r2) ->
incr count;
let c = !count in
graphvizify c r1
^ graphvizify c r2
^ string_of_int c
^ " [label=\"Concat\", shape=ellipse, ];\n"
^ string_of_int parent
^ " -> "
^ string_of_int c
^ "[label=\"\", ];\n"
| Star r1 ->
incr count;
let c = !count in
graphvizify c r1
^ string_of_int c
^ " [label=\"Star\", shape=ellipse, ];\n"
^ string_of_int parent
^ " -> "
^ string_of_int c
^ "[label=\"\", ];\n"
| Empty ->
incr count;
string_of_int !count
^ " [label=\"∅\", shape=ellipse, ];\n"
^ string_of_int parent
^ " -> "
^ string_of_int !count
^ "[label=\"\", ];\n"
in
"digraph G {\n0 [label=\"\", shape=none, height=0, width=0, ]\n"
^ graphvizify 0 re
^ "}"
;;
let rec get_alphabet = function
| Literal a -> [ a ]
| Epsilon | Empty -> []
| Union (r1, r2) | Concat (r1, r2) ->
Utils.list_union (get_alphabet r1) (get_alphabet r2)
| Star r1 -> get_alphabet r1
;;
let is_literal = function
| Literal _ | Epsilon | Empty -> true
| _ -> false
;;
let rec contains a re =
if re = a
then true
else (
match re with
| Union (r1, r2) -> contains a r1 || contains a r2
| Star r1 -> if a = Epsilon then true else r1 = a
| _ -> false)
;;
let rec containsNonLit = function
| Union (r1, r2) -> containsNonLit r1 || containsNonLit r2
| Epsilon | Empty | Concat (_, _) | Star _ -> true
| _ -> false
;;
let rec repeated w re =
if re = w
then true
else (
match re with
| Concat (r1, r2) -> repeated w r1 && repeated w r2
| _ -> false)
;;
let rec simplify_re = function
| Union (Union (r1, r2), r3) ->
simplify_re (Union (r1, Union (r2, r3)))
| Union (r1, Empty) -> simplify_re r1
| Union (Empty, r1) -> simplify_re r1
| Concat (Concat (r1, r2), r3) ->
simplify_re (Concat (r1, Concat (r2, r3)))
| Concat (Epsilon, r1) -> simplify_re r1
| Concat (r1, Epsilon) -> simplify_re r1
| Union (Concat (r1, r2), Concat (r3, r4)) when r1 = r3 ->
simplify_re (Concat (r1, Union (r2, r4)))
| Union (Concat (r1, r2), Concat (r3, r4)) when r2 = r4 ->
simplify_re (Concat (Union (r1, r3), r2))
| Concat (Empty, _) -> Empty
| Concat (_, Empty) -> Empty
| Union (Epsilon, Concat (r1, Star r2)) when r1 = r2 ->
simplify_re (Star r1)
| Union (a, Epsilon) when a <> Epsilon -> simplify_re (Union (Epsilon, a))
| Union (r1, Union (Epsilon, r2)) when r1 <> Epsilon ->
simplify_re (Union (Epsilon, Union (r1, r2)))
| Union (Literal r1, Union (Literal r2, r3)) when r2 < r1 ->
simplify_re (Union (Literal r2, Union (Literal r1, r3)))
| Union (Literal r1, Literal r2) when r2 < r1 ->
simplify_re (Union (Literal r2, Literal r1))
| Union (r1, Union (Literal r2, r3)) when not (is_literal r1) ->
simplify_re (Union (Literal r2, Union (r1, r3)))
| Union (r1, Literal r2) when not (is_literal r1) ->
simplify_re (Union (Literal r2, r1))
| Concat (Union (Epsilon, r1), Star r2) when r1 = r2 ->
simplify_re (Star r1)
| Concat (Star r1, Union (Epsilon, r2)) when r1 = r2 ->
simplify_re (Star r1)
| Concat (r1, Concat (Union (Epsilon, r2), Star r3)) when r2 = r3 ->
simplify_re (Concat (r1, Star r2))
| Star (Concat (Star r1, Star r2)) ->
simplify_re (Star (Union (r1, r2)))
| Concat (Star r1, r2) when r1 = r2 ->
simplify_re (Concat (r1, Star r1))
| Concat (Star r1, Concat (r2, r3)) when r1 = r2 ->
simplify_re (Concat (r1, Concat (Star r2, r3)))
| Star (Star r1) -> simplify_re (Star r1)
| Star Empty -> Epsilon
| Star Epsilon -> Epsilon
| Union (r1, r2) when contains r1 r2 ->
simplify_re r2
| Union (r1, r2) when contains r2 r1 -> simplify_re r1
| Union (r1, Star r2) when repeated r2 r1 ->
simplify_re (Star r2)
| Union (Star r1, r2) when repeated r1 r2 ->
simplify_re (Star r1)
| Concat (Star r1, Star r2) when contains r1 r2 ->
simplify_re (Star r2)
| Concat (Star r1, Star r2) when contains r2 r1 ->
simplify_re (Star r1)
| Concat (Star r1, Concat (Star r2, r3)) when contains r1 r2 ->
simplify_re (Concat (Star r2, r3))
| Concat (Star r1, Concat (Star r2, r3)) when contains r2 r1 ->
simplify_re (Concat (Star r1, r3))
| Star r1
when let alph = get_alphabet r1 in
List.length alph > 0
&& containsNonLit r1
&& List.for_all (fun a -> contains (Literal a) r1) alph ->
let alph = get_alphabet r1 in
simplify_re
(Star
(List.fold_right
(fun a acc -> Union (Literal a, acc))
(List.tl alph)
(Literal (List.hd alph))))
| Literal a -> Literal a
| Epsilon -> Epsilon
| Union (r1, r2) -> Union (simplify_re r1, simplify_re r2)
| Concat (r1, r2) -> Concat (simplify_re r1, simplify_re r2)
| Star r1 -> Star (simplify_re r1)
| Empty -> Empty
;;
let simplify re =
let r = ref re
and newr = ref (simplify_re re) in
while !r <> !newr do
r := !newr;
newr := simplify_re !r
done;
!r
;;
let rec is_nullable = function
| Epsilon | Star _ -> true
| Literal _ | Empty -> false
| Union (r1, r2) -> is_nullable r1 || is_nullable r2
| Concat (r1, r2) -> is_nullable r1 && is_nullable r2
;;
let rec derivative re w =
match re with
| Literal a when w = a -> Epsilon
| Literal _ | Epsilon | Empty -> Empty
| Star r -> Concat (derivative r w, Star r)
| Union (r1, r2) -> Union (derivative r1 w, derivative r2 w)
| Concat (r1, r2) when is_nullable r1 ->
Union (Concat (derivative r1 w, r2), derivative r2 w)
| Concat (r1, r2) -> Concat (derivative r1 w, r2)
;;
let parse s =
let lexbuf = Lexing.from_string s in
try Parser.regex Lexer.token lexbuf with
| Parsing.Parse_error ->
let tok = Lexing.lexeme lexbuf in
raise (Syntax_error ("Syntax Error at token " ^ tok))
;;