]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/parser.ml
79650955de2f5c5f7fa73606dee2e68a9613f73d
[fireball-separation.git] / ocaml / parser.ml
1 exception ParsingError of string;;\r
2 \r
3 let mk_app x y = Num.mk_app x y;;\r
4 let mk_lam x = `Lam(true, x);;\r
5 let mk_var x = `Var(x, -666);;\r
6 \r
7 let isAlphaNum c = let n = Char.code c in\r
8  (48 <= n && n <= 90) || (95 <= n && n <= 122) ;;\r
9 let isSpace c = c = ' ' || c = '\n' || c = '\t' ;;\r
10 \r
11 (* FIXME *)\r
12 let mk_var' (bound, free) x =\r
13   if List.mem x bound\r
14   then free, mk_var (Util.index_of x bound)\r
15   else if List.mem x free\r
16        then free, mk_var (List.length bound + Util.index_of x free)\r
17        else (free @ [x]), mk_var (List.length bound + List.length free)\r
18 ;;\r
19 \r
20 let mk_app' = function\r
21   | [] ->  raise (ParsingError "bug")\r
22   | t :: ts -> List.fold_left mk_app t ts\r
23 ;;\r
24 \r
25 let explode s =\r
26   let rec aux i l =\r
27     if i < 0 then l else aux (i - 1) (s.[i] :: l)\r
28   in aux (String.length s - 1) []\r
29 ;;\r
30 \r
31 let implode l =\r
32   let res = String.create (List.length l) in\r
33   let rec aux i = function\r
34     | [] -> res\r
35     | c :: l -> res.[i] <- c; aux (i + 1) l in\r
36   aux 0 l\r
37 ;;\r
38 \r
39 let rec strip_spaces = function\r
40   | c::cs when isSpace c -> strip_spaces cs\r
41   | cs -> cs\r
42 ;;\r
43 \r
44 let read_var s =\r
45   let rec aux = function\r
46   | [] -> None, []\r
47   | c::cs as x -> if isAlphaNum c\r
48     then match aux cs with\r
49          | (Some x), cs' -> Some (c :: x), cs'\r
50          | None, cs' -> (Some [c]), cs'\r
51     else None, x\r
52   in match aux s with\r
53     | None, y -> None, y\r
54     | Some x, y -> Some (implode x), y\r
55 ;;\r
56 \r
57 let read_var' (bound, free as vars) s =\r
58   match read_var s with\r
59   | Some varname, cs ->\r
60     let free, v = mk_var' vars varname in\r
61     Some [v], cs, (bound, free)\r
62   | _, _ -> raise (ParsingError ("Can't read variable"))\r
63 ;;\r
64 \r
65 let rec read_smt vars =\r
66   let check_if_lambda cs = match read_var cs with\r
67     | None, _ -> false\r
68     | Some x, cs -> match strip_spaces cs with\r
69       | [] -> false\r
70       | c::_ -> c = '.'\r
71   in let read_lambda (bound, free) cs = (\r
72     match read_var (strip_spaces cs) with\r
73       | Some varname, cs ->\r
74       let vars' = (varname::bound, free) in\r
75       (match strip_spaces cs with\r
76         | [] -> raise (ParsingError "manca dopo variabile lambda")\r
77         | c::cs -> (if c = '.' then (match read_smt vars' cs with\r
78           | None, _, _ -> raise (ParsingError "manca corpo lambda")\r
79           | Some [x], y, (_, free) -> Some [mk_lam x], y, (bound, free)\r
80           | Some _, _, _ -> raise (ParsingError "???")\r
81           ) else raise (ParsingError "manca . nel lambda")\r
82         ))\r
83       | _, _ -> assert false\r
84  ) in let rec aux vars cs =\r
85   match strip_spaces cs with\r
86   | [] -> None, [], vars\r
87   | c::_ as x ->\r
88       let tms, cs, vars = (\r
89            if c = '(' then read_pars vars x\r
90       else if c = ')' then (None, x, vars)\r
91       else if check_if_lambda x then read_lambda vars x\r
92       else read_var' vars x) in\r
93       match tms with\r
94       | Some [tm] -> (\r
95         match aux vars cs with\r
96           | None, cs, vars -> Some [tm], cs, vars\r
97           | Some ts, cs, vars -> Some (tm :: ts), cs, vars\r
98         )\r
99       | Some _ -> raise (ParsingError "bug")\r
100       | None -> None, x, vars\r
101   in fun cs -> match aux vars cs with\r
102     | None, cs, vars -> None, cs, vars\r
103     | Some ts, cs, vars -> Some [mk_app' ts], cs, vars\r
104 and read_pars vars = function\r
105   | [] -> None, [], vars\r
106   | c::cs -> if c = '(' then (\r
107     let tm, cs, vars = read_smt vars cs in\r
108     let cs = strip_spaces cs in\r
109     match cs with\r
110       | [] -> None, [], vars\r
111       | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError ") mancante")\r
112     ) else raise (ParsingError "???")\r
113 ;;\r
114 \r
115 let parse x =\r
116   match read_smt ([],[]) (explode x) with\r
117   | Some [y], [], _ -> y\r
118   | _, _, _ -> raise (ParsingError "???")\r
119 ;;\r
120 \r
121 let parse_many strs =\r
122   let f (x, y) z = match read_smt y (explode z) with\r
123   | Some[tm], [], vars -> (tm :: x, vars)\r
124   | _, _, _ -> raise (ParsingError "???")\r
125   in let aux = List.fold_left f ([], ([], [])) (* index zero is reserved *)\r
126   in let (tms, (_, free)) = aux strs\r
127   in (List.rev tms, free)\r
128 ;;\r
129 \r
130 let problem_of_string s =\r
131  let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in\r
132  let head, lines = List.hd lines, List.tl lines in\r
133  let name = String.trim (String.sub head 1 (String.length head - 1)) in\r
134  let lines = List.filter ((<>) "") lines in\r
135  let aux (last, div, conv, ps) line =\r
136   let chr = String.sub line 0 1 in\r
137   let line = String.trim (String.sub line 1 (String.length line - 1)) in\r
138   if line = "" then chr, div, conv, ps else\r
139   let rec aux' chr line =\r
140    if chr = "#"\r
141     then chr, div, conv, ps\r
142    else if chr = "D"\r
143     then chr, line, conv, ps\r
144    else if chr = "C"\r
145     then chr, div, line::conv, ps\r
146    else if chr = "N"\r
147     then chr, div, conv, line::ps\r
148    else if chr = " "\r
149     then aux' last line\r
150    else raise (ParsingError\r
151      ("Unexpected at beginning of problem: `" ^ chr ^ "` " ^ line)) in\r
152   aux' chr line in\r
153  let _, div, conv, ps = List.fold_left aux ("#", "", [], []) lines in\r
154  let div_provided = div <> "" in\r
155  let div = if div_provided then div else "xxxxxx" in\r
156  let strs = [div] @ ps @ conv in\r
157 \r
158  if List.length ps = 0 && List.length conv = 0\r
159   then raise (ParsingError "empty problem");\r
160 \r
161  (* parse' *)\r
162  let (tms, free) = parse_many strs in\r
163  let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in\r
164  let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in\r
165  let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in\r
166  let fix lev v =\r
167  if v = lev + n_bot then failwith "Example with `Bottom"\r
168   else if v = lev + n_pac then failwith "Example with `Pacman"\r
169    else if v = lev + n_bomb then failwith "Example with `Bomb"\r
170     else if v = lev then `Var(v, min_int) (* zero *)\r
171       else `Var(v,1) in (* 1 by default when variable not applied *)\r
172  (* Fix arity *)\r
173  let open Num in\r
174  let rec aux lev : nf -> nf = function\r
175  | `I((n,_), args) -> `I((n,(if lev = 0 then 0 else 1) + Listx.length args), Listx.map (aux lev) args)\r
176  | `Var(n,_) -> fix lev n\r
177  | `Lam(_,t) -> `Lam (true, aux (lev+1) t)\r
178  | `Match _ | `N _ -> assert false in\r
179 let all_tms = List.map (aux 0) (tms :> Num.nf list) in\r
180 \r
181 (* problem_of *)\r
182 let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in\r
183 \r
184 let div = if not div_provided\r
185  then None\r
186  else match div with\r
187   | `I _ as t -> Some t\r
188   | _ -> raise (ParsingError "div is not an inert or BOT in the initial problem") in\r
189 let conv = Util.filter_map (\r
190  function\r
191  | #i_n_var as t -> Some t\r
192  | `Lam _ -> None\r
193  | _ -> raise (ParsingError "A term in conv is not i_n_var")\r
194  ) conv in\r
195 let ps = List.map (\r
196  function\r
197   | #i_n_var as y -> y\r
198   | _ as y -> raise (ParsingError ("A term in num is not i_n_var" ^ Num.string_of_nf y))\r
199  ) ps in\r
200  name, div, conv, ps, free\r
201 ;;\r
202 \r
203 \r
204 let from_file path =\r
205  let lines = ref ["#"] in\r
206  let chan = open_in path in\r
207  let _ = try\r
208   while true; do\r
209     lines := input_line chan :: !lines\r
210   done\r
211  with End_of_file ->\r
212   close_in chan in\r
213  let txt = String.concat "\n" (List.rev !lines) in\r
214  let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in\r
215  List.map problem_of_string (List.tl (List.map ((^) "$") problems))\r
216 ;;\r