]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/parser.ml
902da8d38bfdb8473d14f05dd5fbedee2e55da51
[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 var_zero = "ω";; (* w is an invalid variable name *)\r
122 \r
123 let parse_many strs =\r
124   let f (x, y) z = match read_smt y (explode z) with\r
125   | Some[tm], [], vars -> (tm :: x, vars)\r
126   | _, _, _ -> raise (ParsingError "???")\r
127   in let aux = List.fold_left f ([], ([], [var_zero])) (* index zero is reserved *)\r
128   in let (tms, (_, free)) = aux strs\r
129   in (List.rev tms, free)\r
130 ;;\r
131 \r
132 (**********************************************************************\r
133 \r
134 let rec string_of_term = function\r
135   | Tvar n -> string_of_int n\r
136   | Tapp(t1, t2) -> "(" ^ string_of_term t1 ^ " " ^ string_of_term t2 ^ ")"\r
137   | Tlam(t1) -> "(\\" ^ string_of_term t1 ^ ")"\r
138 ;;\r
139 \r
140 let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j"));;\r
141 \r
142 \r
143 **********************************************************************)\r
144 \r
145  let parse' strs =\r
146    let (tms, free) = parse_many strs in\r
147    (* Replace pacmans and bottoms *)\r
148    let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in\r
149    let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in\r
150    let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in\r
151    let fix lev v =\r
152     if v = lev + n_bot then failwith "Example with `Bottom"\r
153      else if v = lev + n_pac then failwith "Example with `Pacman"\r
154       else if v = lev + n_bomb then failwith "Example with `Bomb"\r
155        else `Var(v,1) in (* 1 by default when variable not applied *)\r
156    (* Fix arity *)\r
157    let open Num in\r
158    let rec aux lev : nf -> nf = function\r
159    | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> aux lev t) args)\r
160    | `Var(n,_) -> fix lev n\r
161    | `Lam(_,t) -> `Lam (true, aux (lev+1) t)\r
162    | `Match _ | `N _ -> assert false in\r
163  List.map (aux 0) (tms :> Num.nf list), free\r
164 ;;\r
165 \r
166 let problem_of_string s =\r
167 prerr_endline (s);\r
168  let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in\r
169  (* let lines = List.map String.trim lines in *)\r
170  let lines = List.filter ((<>) "") lines in\r
171  prerr_endline("number of lines" ^ string_of_int (List.length lines));\r
172  let aux (last, name, div, conv, ps) line =\r
173   let chr = String.sub line 0 1 in\r
174   let line = String.trim (String.sub line 1 (String.length line - 1)) in\r
175   if line = "" then chr, name, div, conv, ps else\r
176   let rec aux' chr line =\r
177    if chr = "#"\r
178     then chr, name, div, conv, ps\r
179    else if chr = "$"\r
180     then "#", line, div, conv, ps\r
181    else if chr = "D"\r
182     then chr, name, line, conv, ps\r
183    else if chr = "C"\r
184     then chr, name, div, line::conv, ps\r
185    else if chr = "N"\r
186     then chr, name, div, conv, line::ps\r
187    else if chr = " "\r
188     then aux' last line\r
189    else raise (ParsingError\r
190      ("Unexpected at beginning of problem: `" ^ chr ^ "` " ^ line)) in\r
191   aux' chr line in\r
192  let _, name, div, conv, ps = List.fold_left aux ("#", "no label", "", [], []) lines in\r
193  let div_provided = div <> "" in\r
194  let div = if div_provided then div else "xxxxxx" in\r
195  let strs = [div] @ ps @ conv in\r
196 \r
197  if strs = ["BOT"]\r
198   then raise (ParsingError "empty problem");\r
199 \r
200  (* parse' *)\r
201  let (tms, free) = parse_many strs in\r
202  (* Fix arity *)\r
203  let open Num in\r
204  let rec aux lev : nf -> nf = function\r
205  | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> aux lev t) args)\r
206  | `Var(n,_) -> `Var(n,1)\r
207  | `Lam(_,t) -> `Lam (true, aux (lev+1) t)\r
208  | `Match _ | `N _ -> assert false in\r
209 let all_tms = List.map (aux 0) (tms :> Num.nf list) in\r
210 \r
211 (* problem_of *)\r
212 let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in\r
213 \r
214 let div = if not div_provided\r
215  then None\r
216  else match div with\r
217   | `I _ as t -> Some t\r
218   | _ -> raise (ParsingError "div is not an inert or BOT in the initial problem") in\r
219 let conv = Util.filter_map (\r
220  function\r
221  | #i_n_var as t -> Some t\r
222  | `Lam _ -> None\r
223  | _ -> raise (ParsingError "A term in conv is not i_n_var")\r
224  ) conv in\r
225 let ps = List.map (\r
226  function\r
227   | #i_n_var as y -> y\r
228   | _ as y -> raise (ParsingError ("A term in num is not i_n_var" ^ Num.string_of_nf y))\r
229  ) ps in\r
230  name, div, conv, ps, free\r
231 ;;\r
232 \r
233 \r
234 let from_file path =\r
235  let lines = ref [""] in\r
236  let chan = open_in path in\r
237  let _ = try\r
238   while true; do\r
239     lines := input_line chan :: !lines\r
240   done\r
241  with End_of_file ->\r
242   close_in chan in\r
243  let txt = String.concat "\n" (List.rev !lines) in\r
244  let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in\r
245  List.map problem_of_string (List.tl (List.map ((^) "$") problems))\r
246 ;;\r