(* 문법 파일을 읽어 기계가 소비할 수 있는지 확인하는 도구. *) let read file = let ic = open_in_bin file in let n = in_channel_length ic in let s = really_input_string ic n in close_in ic; s let () = if Array.length Sys.argv > 1 && Sys.argv.(1) = "--lexical" then ( print_string (Coollang.Lexical_doc.render ()); print_newline (); exit 0); let file = if Array.length Sys.argv > 1 then Sys.argv.(1) else "docs/grammar.ebnf" in match Coollang.Ebnf.parse_result (read file) with | Error e -> Printf.printf "%s:%d: %s\n" file e.line e.msg | Ok g -> Printf.printf "프로덕션 %d개\n" (List.length g); Printf.printf "\n정의되지 않은 채 참조된 이름:\n"; List.iter (fun n -> Printf.printf " %s\n" n) (Coollang.Ebnf.undefined g); Printf.printf "\n어디서도 참조되지 않는 프로덕션 (시작 기호 module 제외):\n"; List.iter (fun n -> Printf.printf " %s\n" n) (Coollang.Ebnf.unreachable g ~start:"module"); (* 문법이 쓰는 단말 전부. 렉서가 만드는 토큰과 대조하기 위한 것. *) let terms = Hashtbl.create 64 in let rec walk : Coollang.Ebnf.expr -> unit = function | Term s -> Hashtbl.replace terms s () | Ref _ | RefArg _ -> () | Seq xs | Alt xs -> List.iter walk xs | Opt e | Rep e -> walk e | Except (a, b) -> walk a; walk b in List.iter (fun (r : Coollang.Ebnf.rule) -> walk r.body) g; let ts = Hashtbl.fold (fun k () acc -> k :: acc) terms [] |> List.sort compare in Printf.printf "\n문법이 쓰는 단말 %d개:\n %s\n" (List.length ts) (String.concat " " ts); (* 어휘 절의 이름은 파서 층에서 단말이다 *) let tokens = [ "ident"; "int_lit"; "string_lit"; "NEWLINE"; "letter"; "digit"; "char" ] in let g = Coollang.Ebnf.expand g in let a = Coollang.Ebnf.analyze ~tokens g in Printf.printf "\n비어도 되는(nullable) 프로덕션:\n %s\n" (String.concat " " (List.filter_map (fun (r : Coollang.Ebnf.rule) -> if Coollang.Ebnf.nullable a r.name then Some r.name else None) g)); Printf.printf "\nFIRST 표본:\n"; List.iter (fun n -> Printf.printf " %-14s %s\n" n (String.concat " " (Coollang.Ebnf.SS.elements (Coollang.Ebnf.first a n)))) [ "decl"; "stmt"; "primary"; "type"; "pattern"; "item" ]; let cs = Coollang.Ebnf.conflicts ~tokens ~greedy:[ "NEWLINE" ] g in let real = List.filter (fun (c : Coollang.Ebnf.conflict) -> not c.c_greedy) cs in let soft = List.filter (fun (c : Coollang.Ebnf.conflict) -> c.c_greedy) cs in Printf.printf "\n== LL(1) 충돌: 진짜 %d건, greedy로 해소 %d건 ==\n" (List.length real) (List.length soft); List.iter (fun (c : Coollang.Ebnf.conflict) -> Printf.printf "\n%s (%s:%d) [%s]\n 겹치는 토큰: %s\n %s\n" c.c_rule file c.c_line c.c_kind (String.concat " " c.c_tokens) c.c_detail) real; if soft <> [] then begin Printf.printf "\n-- greedy로 해소되는 것 --\n"; List.iter (fun (c : Coollang.Ebnf.conflict) -> Printf.printf " %s:%d %s [%s]\n" file c.c_line c.c_rule (String.concat " " c.c_tokens)) soft end