-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatcher.ml
42 lines (37 loc) · 1.07 KB
/
matcher.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
open Accept
let rec string_of_regexp r =
match r with
| R_zero -> "[zero]"
| R_unit -> "[unit]"
| R_char c -> Printf.sprintf "%c" c
| R_plus (r1, r2) ->
let s1 = string_of_regexp r1 in
let s2 = string_of_regexp r2 in
Printf.sprintf "(%s|%s)" s1 s2
| R_times (r1, r2) ->
let s1 = string_of_regexp r1 in
let s2 = string_of_regexp r2 in
Printf.sprintf "(%s%s)" s1 s2
| R_star r ->
let s = string_of_regexp r in
Printf.sprintf "(%s*)" s
(* the name of the file which contains the expressions *)
let r = Sys.argv.(1)
let s = Sys.argv.(2)
let char_list_of_string s =
let rec exp i l =
if i < 0 then l else exp (i - 1) (s.[i] :: l) in
exp (String.length s - 1) []
let main () =
let buf = Lexing.from_string r in
try
let re = Parser.main Lexer.token buf in
(*Printf.printf "%s\n" (string_of_regexp re)*)
let mt = accept (=) (re, char_list_of_string s) in
Printf.printf "%b\n" mt
with
| Lexer.Error msg ->
Printf.eprintf "%s%!" msg
| Parser.Error ->
Printf.eprintf "At offset %d: syntax error.\n%!" (Lexing.lexeme_start buf)
let _ = main ()