X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fparser.ml;h=1e69acb82328cd6d363dccdd728e86e1fb908a34;hb=3f5fd50fbe0c585e1d9aad84007fdbd82c573fa7;hp=a1a9d95a7dd85f91545009d81c132cfbc49676ef;hpb=0b5ad58b1454d58219dca88bd1d3e94f14ac50fc;p=fireball-separation.git diff --git a/ocaml/parser.ml b/ocaml/parser.ml index a1a9d95..1e69acb 100644 --- a/ocaml/parser.ml +++ b/ocaml/parser.ml @@ -118,7 +118,7 @@ let parse x = | _, _, _ -> raise (ParsingError "???") ;; -let var_zero = "ω";; (* w is an invalid variable name *) +let var_zero = "Z";; (* the name for the zero *) let parse_many strs = let f (x, y) z = match read_smt y (explode z) with @@ -129,75 +129,53 @@ let parse_many strs = in (List.rev tms, free) ;; -(********************************************************************** - -let rec string_of_term = function - | Tvar n -> string_of_int n - | Tapp(t1, t2) -> "(" ^ string_of_term t1 ^ " " ^ string_of_term t2 ^ ")" - | Tlam(t1) -> "(\\" ^ string_of_term t1 ^ ")" -;; - -let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j"));; - - -**********************************************************************) - - let parse' strs = - let (tms, free) = parse_many strs in - (* Replace pacmans and bottoms *) - let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in - let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in - let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in - let fix lev v = - if v = lev + n_bot then failwith "Example with `Bottom" - else if v = lev + n_pac then failwith "Example with `Pacman" - else if v = lev + n_bomb then failwith "Example with `Bomb" - else `Var(v,1) in (* 1 by default when variable not applied *) - (* Fix arity *) - let open Num in - let rec aux lev : nf -> nf = function - | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> aux lev t) args) - | `Var(n,_) -> fix lev n - | `Lam(_,t) -> `Lam (true, aux (lev+1) t) - | `Match _ | `N _ -> assert false in - List.map (aux 0) (tms :> Num.nf list), free -;; - let problem_of_string s = -prerr_endline (s); let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in - let lines = List.map String.trim lines in + let head, lines = List.hd lines, List.tl lines in + let name = String.trim (String.sub head 1 (String.length head - 1)) in let lines = List.filter ((<>) "") lines in - prerr_endline("number of lines" ^ string_of_int (List.length lines)); - let aux (last, name, div, conv, ps) line = + let aux (last, div, conv, ps) line = let chr = String.sub line 0 1 in - let line = String.sub line 1 (String.length line - 1) in - if line = "" then chr, name, div, conv, ps else + let line = String.trim (String.sub line 1 (String.length line - 1)) in + if line = "" then chr, div, conv, ps else let rec aux' chr line = if chr = "#" - then chr, line, div, conv, ps + then chr, div, conv, ps else if chr = "D" - then chr, name, line, conv, ps + then chr, line, conv, ps else if chr = "C" - then chr, name, div, line::conv, ps + then chr, div, line::conv, ps else if chr = "N" - then chr, name, div, conv, line::ps - else if last = "" - then raise (ParsingError ("Unexpected at beginning of problem: " ^ chr)) - else aux' last (chr ^ line) in + then chr, div, conv, line::ps + else if chr = " " + then aux' last line + else raise (ParsingError + ("Unexpected at beginning of problem: `" ^ chr ^ "` " ^ line)) in aux' chr line in - let _, name, div, conv, ps = List.fold_left aux ("#", "?", "", [], []) lines in + let _, div, conv, ps = List.fold_left aux ("#", "", [], []) lines in let div_provided = div <> "" in let div = if div_provided then div else "xxxxxx" in let strs = [div] @ ps @ conv in + if List.length ps = 0 && List.length conv = 0 + then raise (ParsingError "empty problem"); + (* parse' *) let (tms, free) = parse_many strs in + let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in + let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in + let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in + let fix lev v = + if v = lev + n_bot then failwith "Example with `Bottom" + else if v = lev + n_pac then failwith "Example with `Pacman" + else if v = lev + n_bomb then failwith "Example with `Bomb" + else if v = lev then `Var(v, min_int) (* zero *) + else `Var(v,1) in (* 1 by default when variable not applied *) (* Fix arity *) let open Num in let rec aux lev : nf -> nf = function | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> aux lev t) args) - | `Var(n,_) -> `Var(n,1) + | `Var(n,_) -> fix lev n | `Lam(_,t) -> `Lam (true, aux (lev+1) t) | `Match _ | `N _ -> assert false in let all_tms = List.map (aux 0) (tms :> Num.nf list) in @@ -226,7 +204,7 @@ let ps = List.map ( let from_file path = - let lines = ref [] in + let lines = ref ["#"] in let chan = open_in path in let _ = try while true; do @@ -235,6 +213,6 @@ let from_file path = with End_of_file -> close_in chan in let txt = String.concat "\n" (List.rev !lines) in - let problems = Str.split (Str.regexp "\r?\n\r?\n\\#") txt in - List.map problem_of_string problems + let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in + List.map problem_of_string (List.tl (List.map ((^) "$") problems)) ;;