Skip to content

Commit 31bc4b5

Browse files
committed
Lahenda regex praktikumis
1 parent 9e85b63 commit 31bc4b5

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

src/regex/brzozowski.ml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ open Re
33

44
(** Kas regulaaravaldis sobitub tühisõnega? *)
55
let rec matches_eps (r: t): bool =
6-
failwith "TODO"
6+
match r with
7+
| Empty -> false
8+
| Eps -> true
9+
| Char _ -> false
10+
| Choice (l, r) -> matches_eps l || matches_eps r
11+
| Concat (l, r) -> matches_eps l && matches_eps r
12+
| Star _ -> true
713

814
(** Regulaaravaldise tuletis antud tähe järgi.
915
Antud regulaaravaldise keelest võetakse kõik sõned, mis algavad antud tähega ja eemaldatakse algusest see täht.
@@ -15,10 +21,23 @@ let rec matches_eps (r: t): bool =
1521
1622
Vihje: matches_eps. *)
1723
let rec derive (r: t) (c: char): t =
18-
failwith "TODO"
24+
(* print_endline (show r); *)
25+
match r with
26+
| Empty (* -> Empty *)
27+
| Eps -> Empty
28+
| Char c' -> if c = c' then Eps else Empty
29+
| Choice (l, r) -> Choice (derive l c, derive r c)
30+
| Star r' -> Concat (derive r' c, r)
31+
| Concat (l, r) ->
32+
let dlr = Concat (derive l c, r) in
33+
if matches_eps l then
34+
Choice (dlr, derive r c)
35+
else
36+
dlr
1937

2038
(** Kas regulaaravaldis sobitub antud sõnega?
2139
Kasutada derive ja matches_eps funktsioone.
2240
Vihje: String.fold_left. *)
2341
let matches (s: string) (r: t): bool =
24-
failwith "TODO"
42+
let r' = String.fold_left derive r s in
43+
matches_eps r'

src/regex/simplify.ml

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,29 @@ open Re
2323
Vihje: Re.equal.
2424
Vihje: Fixpoint. *)
2525

26-
let rec simplify (r: t): t =
27-
failwith "TODO"
26+
let rec simplify_step (r: t): t =
27+
match r with
28+
| Choice (r, r') when Re.equal r r' -> r
29+
| Concat (r, Eps)
30+
| Concat (Eps, r) -> r
31+
| Star (Star _ as r') -> r'
32+
| Concat (Star r, Star r') when Re.equal r r' -> Star r
33+
| Choice (Empty, r)
34+
| Choice (r, Empty) -> r
35+
| Concat (Empty, r)
36+
| Concat (r, Empty) -> Empty
37+
| Star Empty -> Eps
38+
| Star Eps -> Eps
39+
| Empty
40+
| Eps
41+
| Char _ -> r
42+
| Choice (l, r) -> Choice (simplify_step l, simplify_step r)
43+
| Concat (l, r) -> Concat (simplify_step l, simplify_step r)
44+
| Star r -> Star (simplify_step r)
45+
46+
module ReFP = Fixpoint.Make (Re)
47+
48+
let simplify = ReFP.fp simplify_step
2849

2950

3051
(** Alt-üles lahendus.
@@ -34,5 +55,26 @@ let rec simplify (r: t): t =
3455
3556
Vihje: Re.equal. *)
3657

58+
let rec simplify'_one (r: t): t =
59+
match r with
60+
| Choice (r, r') when Re.equal r r' -> r
61+
| Concat (r, Eps)
62+
| Concat (Eps, r) -> r
63+
| Star (Star _ as r') -> r'
64+
| Concat (Star r, Star r') when Re.equal r r' -> Star r
65+
| Choice (Empty, r)
66+
| Choice (r, Empty) -> r
67+
| Concat (Empty, r)
68+
| Concat (r, Empty) -> Empty
69+
| Star Empty -> Eps
70+
| Star Eps -> Eps
71+
| _ -> r
72+
3773
let rec simplify' (r: t): t =
38-
failwith "TODO"
74+
match r with
75+
| Empty
76+
| Eps
77+
| Char _ -> r
78+
| Choice (l, r) -> simplify'_one (Choice (simplify' l, simplify' r))
79+
| Concat (l, r) -> simplify'_one (Concat (simplify' l, simplify' r))
80+
| Star r -> simplify'_one (Star (simplify' r))

0 commit comments

Comments
 (0)