| 1 |
type regexp =
|
| 2 |
<chr> Char
|
| 3 |
| <seq> (regexp,regexp)
|
| 4 |
| <alt> (regexp,regexp)
|
| 5 |
| <star> regexp
|
| 6 |
|
| 7 |
type f = String -> Bool
|
| 8 |
|
| 9 |
let loop (re : regexp, k : f) : f = fun (s : String) : Bool = match re with
|
| 10 |
| <chr> p -> (match s with (c,s) -> (c = p) && (k s) | _ -> `false)
|
| 11 |
| <seq> (r1,r2) -> loop (r1, (loop (r2,k))) s
|
| 12 |
| <alt> (r1,r2) -> loop (r1,k) s || loop (r2,k) s
|
| 13 |
| <star> r -> loop (r,(loop (re,k))) s || k s
|
| 14 |
|
| 15 |
let accept (re : regexp) : f =
|
| 16 |
loop (re, fun (String -> Bool) [] -> `true | _ -> `false)
|
| 17 |
|
| 18 |
let re = <seq> (<star> <chr>'a', <star><chr>'b')
|
| 19 |
let strs = [ "aaabbb" "abba" "aaab" "a" "" ]
|
| 20 |
let [] = print ((string_of (map strs with x -> (x,accept re x))) @ ['\n'])
|