From: acondolu Date: Sat, 15 Jul 2017 18:23:20 +0000 (+0200) Subject: Improved parsing X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=commitdiff_plain;h=2d56686ea25883e77d0a69f1245e6e840fc8be5f;p=fireball-separation.git Improved parsing - removed unused parse' - fixed bug which was ignoring the first problems in some files --- diff --git a/ocaml/parser.ml b/ocaml/parser.ml index 570809d..15c72bd 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 @@ -140,70 +140,37 @@ let rec string_of_term = function 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 n_z = try Util.index_of "Z" free with Not_found -> min_int in - let fix lev v = - if v = lev + n_bot then `Bottom - else if v = lev + n_pac then `Pacman - else if v = lev + n_bomb then `Lam(true, `Bottom) - else if v = lev + n_z then `Var(lev, 0) (* FIXME why zero? *) - else `Var(v,1) in (* 1 by default when variable not applied *) - (* Fix arity *) - let open Num in - let exclude_bottom = function - | #nf_nob as t -> t - | `Bottom -> raise (ParsingError "Input term not in normal form") in - let rec aux_nob lev : nf_nob -> nf = function - | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> exclude_bottom (aux_nob lev t)) args) - | `Var(n,_) -> fix lev n - | `Lam(_,t) -> `Lam (true, aux (lev+1) t) - | `Match _ | `N _ -> assert false - | `Pacman -> assert false - and aux lev : Num.nf -> Num.nf = function - | #nf_nob as t -> aux_nob lev t - | `Bottom -> 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.trim (String.sub line 1 (String.length line - 1)) in - if line = "" then chr, name, div, conv, ps else + if line = "" then chr, div, conv, ps else let rec aux' chr line = if chr = "#" - then chr, name, div, conv, ps - else if chr = "$" - then "#", 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 + 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 ("#", "no label", "", [], []) 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 "BOT" in let strs = [div] @ ps @ conv in - if strs = ["BOT"] + if List.length ps = 0 && List.length conv = 0 then raise (ParsingError "empty problem"); (* parse' *) @@ -216,6 +183,7 @@ prerr_endline (s); if v = lev + n_bot then `Bottom else if v = lev + n_pac then `Pacman else if v = lev + n_bomb then `Lam(true, `Bottom) + 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 @@ -257,7 +225,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 diff --git a/ocaml/parser.mli b/ocaml/parser.mli index 47cfb28..d131edb 100644 --- a/ocaml/parser.mli +++ b/ocaml/parser.mli @@ -1,6 +1,5 @@ exception ParsingError of string -val parse': string list -> Num.nf list * string list val problem_of_string: string -> string (* problem label *) diff --git a/ocaml/problems/w b/ocaml/problems/w index d55e1b8..c603cfb 100644 --- a/ocaml/problems/w +++ b/ocaml/problems/w @@ -70,6 +70,7 @@ N c (k. l. l b k) (k. l. d) (e f (k. k) (g e)) (k. l. m. n. n b m) (k. b (b (k e $! new failing problem # it is backtracking, but it shouldn't +# why? well, D occurs in some C's D (a b) C ((((((((a (v (y. y))) (v c)) ((x ((a (v (y. y))) (v c))) y)) (((a (v (y. y))) (((v c) ((((z z) b) (c. a)) (w (c. (v c))))) (w. (v. (z. (v (y. y))))))) (((v (x. (v c))) (v (x. (v c)))) (((((z z) b) (c. a)) (z z)) (b c))))) (a. ((((z z) b)(c. a)) (z z)))) (y. (z z))) (((a (v (y. y))) w) (z. x))) (w. (z. (v (y. y))))) C ((((((((z z) b) (c. a)) (z z)) ((v (y. y)) a)) (v (y. y))) ((((a (v (y. y))) (v c)) (z z)) ((a (v (y. y))) (((v c) ((((z z) b) (c. a)) (w (c. (v c))))) (w. (v. (z. (v (y. y))))))))) (z. ((v (y. y)) a)))