X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fparser.ml;h=1dd4b583c8886216ee6910392103965253b1be05;hb=417e6bbe7973c9413216a87dff01f920d39e4657;hp=902da8d38bfdb8473d14f05dd5fbedee2e55da51;hpb=1398932771b9914e73cbef512195305ae60af8a5;p=fireball-separation.git diff --git a/ocaml/parser.ml b/ocaml/parser.ml index 902da8d..1dd4b58 100644 --- a/ocaml/parser.ml +++ b/ocaml/parser.ml @@ -10,22 +10,23 @@ let isSpace c = c = ' ' || c = '\n' || c = '\t' ;; (* FIXME *) let mk_var' (bound, free) x = - if List.mem x bound + if x <> "@" && List.mem x bound then free, mk_var (Util.index_of x bound) - else if List.mem x free + else if x <> "@" && List.mem x free then free, mk_var (List.length bound + Util.index_of x free) else (free @ [x]), mk_var (List.length bound + List.length free) ;; let mk_app' = function - | [] -> raise (ParsingError "bug") + | [] -> assert false | t :: ts -> List.fold_left mk_app t ts ;; let explode s = + let len = String.length s in let rec aux i l = - if i < 0 then l else aux (i - 1) (s.[i] :: l) - in aux (String.length s - 1) [] + if i >= len || s.[i] = '#' then l else aux (i+1) (s.[i] :: l) + in List.rev (aux 0 []) ;; let implode l = @@ -44,7 +45,12 @@ let rec strip_spaces = function let read_var s = let rec aux = function | [] -> None, [] - | c::cs as x -> if isAlphaNum c + | c::cs as x -> + if c = '@' then + (if cs <> [] && (let hd = List.hd cs in hd = '@' || isAlphaNum hd) + then raise (ParsingError ("Unexpected `"^String.make 1 (List.hd cs)^"` after `@`.")) + else Some['@'], cs) + else if isAlphaNum c then match aux cs with | (Some x), cs' -> Some (c :: x), cs' | None, cs' -> (Some [c]), cs' @@ -59,7 +65,7 @@ let read_var' (bound, free as vars) s = | Some varname, cs -> let free, v = mk_var' vars varname in Some [v], cs, (bound, free) - | _, _ -> raise (ParsingError ("Can't read variable")) + | None, _ -> raise (ParsingError ("Can't read variable")) ;; let rec read_smt vars = @@ -73,12 +79,12 @@ let rec read_smt vars = | Some varname, cs -> let vars' = (varname::bound, free) in (match strip_spaces cs with - | [] -> raise (ParsingError "manca dopo variabile lambda") + | [] -> raise (ParsingError "Lambda expression incomplete") | c::cs -> (if c = '.' then (match read_smt vars' cs with - | None, _, _ -> raise (ParsingError "manca corpo lambda") + | None, _, _ -> raise (ParsingError "Lambda body expected") | Some [x], y, (_, free) -> Some [mk_lam x], y, (bound, free) - | Some _, _, _ -> raise (ParsingError "???") - ) else raise (ParsingError "manca . nel lambda") + | Some _, _, _ -> assert false + ) else raise (ParsingError "Expected `.` after variable in lambda") )) | _, _ -> assert false ) in let rec aux vars cs = @@ -96,7 +102,7 @@ let rec read_smt vars = | None, cs, vars -> Some [tm], cs, vars | Some ts, cs, vars -> Some (tm :: ts), cs, vars ) - | Some _ -> raise (ParsingError "bug") + | Some _ -> assert false | None -> None, x, vars in fun cs -> match aux vars cs with | None, cs, vars -> None, cs, vars @@ -108,102 +114,72 @@ and read_pars vars = function let cs = strip_spaces cs in match cs with | [] -> None, [], vars - | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError ") mancante") - ) else raise (ParsingError "???") + | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError "Missing `)`") + ) else assert false ;; let parse x = match read_smt ([],[]) (explode x) with | Some [y], [], _ -> y - | _, _, _ -> raise (ParsingError "???") + | _, _, _ -> assert false ;; -let var_zero = "ω";; (* w is an invalid variable name *) - let parse_many strs = let f (x, y) z = match read_smt y (explode z) with | Some[tm], [], vars -> (tm :: x, vars) - | _, _, _ -> raise (ParsingError "???") - in let aux = List.fold_left f ([], ([], [var_zero])) (* index zero is reserved *) + | _, _, _ -> assert false + in let aux = List.fold_left f ([], ([], [])) in let (tms, (_, free)) = aux 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.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 "xxxxxx" in let strs = [div] @ ps @ conv in - if strs = ["BOT"] - then raise (ParsingError "empty problem"); + if List.length ps = 0 && List.length conv = 0 + then raise (ParsingError "Parsed 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) + | `I((n,_), args) -> `I((n,(if lev = 0 then 0 else 1) + Listx.length args), Listx.map (aux lev) args) + | `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 @@ -215,24 +191,24 @@ let div = if not div_provided then None else match div with | `I _ as t -> Some t - | _ -> raise (ParsingError "div is not an inert or BOT in the initial problem") in + | _ -> raise (ParsingError "D is not an inert in the initial problem") in let conv = Util.filter_map ( function | #i_n_var as t -> Some t | `Lam _ -> None - | _ -> raise (ParsingError "A term in conv is not i_n_var") + | _ -> raise (ParsingError "A C-term is not i_n_var") ) conv in let ps = List.map ( function | #i_n_var as y -> y - | _ as y -> raise (ParsingError ("A term in num is not i_n_var" ^ Num.string_of_nf y)) + | _ -> raise (ParsingError "An N-term is not i_n_var") ) ps in name, div, conv, ps, free ;; let from_file path = - let lines = ref [""] in + let lines = ref ["#"] in let chan = open_in path in let _ = try while true; do