diff --git a/src/parser_laurian/Makefile b/src/parser_laurian/Makefile new file mode 100644 index 0000000..b55f18e --- /dev/null +++ b/src/parser_laurian/Makefile @@ -0,0 +1,46 @@ +all: lang.cmo parser.cmo lexer.cmo interf.cmo + +# Compilation of .ml files +lang.cmo: lang.ml + ocamlc -c $< + +# typing.cmo: typing.ml lang.cmo +# ocamlc -c $< + +# instrs.cmo: instrs.ml lang.cmo +# ocamlc -c $< + +# gen.cmo: gen.ml lang.cmo instrs.cmo typing.cmo +# ocamlc -c $< + +#interf.cmo: interf.ml lexer.cmo parser.cmo gen.cmo typing.cmo +interf.cmo: interf.ml lexer.cmo parser.cmo + ocamlc -c $< + +# comp.cmo: comp.ml gen.cmo typing.cmo parser.cmo interf.cmo +# ocamlc -c $< + +# ocaml lexer and parser + +lexer.ml: lexer.mll lang.cmo + ocamllex $< + +parser.ml parser.mli: parser.mly lang.cmo + ocamlyacc $< + +lexer.cmo: lexer.ml parser.cmo + ocamlc -c $< +parser.cmo: parser.ml parser.cmi lang.cmo + ocamlc -c $< + + +#### Generic rules + +%.cmi: %.mli + ocamlc -c $< + + +.PHONY: clean + +clean: + rm -f lexer.ml parser.ml *.mli *.cmi *.cmo diff --git a/src/parser_laurian/interf.ml b/src/parser_laurian/interf.ml new file mode 100644 index 0000000..17ca594 --- /dev/null +++ b/src/parser_laurian/interf.ml @@ -0,0 +1,33 @@ +(* Interface with parser *) + +exception ParseLexError of exn * (string * int * int * string * string) + +let parse_file infile = + let lexbuf = Lexing.from_channel (open_in infile) in + try + Parser.start Lexer.token lexbuf + with exn -> + begin + let curr = lexbuf.Lexing.lex_curr_p in + let line = curr.Lexing.pos_lnum in + let cnum = curr.Lexing.pos_cnum - curr.Lexing.pos_bol in + let tok = Lexing.lexeme lexbuf in + let tail = Lexer.ruleTail "" lexbuf in + raise (ParseLexError (exn,(infile, line,cnum,tok,tail))) + end +;; + +let print_parse_error (filename, line,cnum,tok,tail) = + print_string ("Parsing error in file: " ^ filename ^ + " on line: " ^ (string_of_int line) ^ + " column: " ^ (string_of_int cnum) ^ + " token: " ^ tok ^ + "\nrest: " ^ tail ^ "\n") +;; + +let parse infile = + try parse_file infile + with ParseLexError (e, r) -> + print_parse_error r; + failwith "Stopped execution." +;; diff --git a/src/parser_laurian/use.ml b/src/parser_laurian/use.ml new file mode 100644 index 0000000..63e5293 --- /dev/null +++ b/src/parser_laurian/use.ml @@ -0,0 +1,16 @@ + +#load "lang.cmo";; +#load "parser.cmo" ;; +#load "lexer.cmo" ;; +#load "interf.cmo";; + +open Interf;; +open Lang;; + +(* For using the parser: + +- Evaluate this file (use.ml) +- parse "Tests/progsimple.c" ;; + +*) +