From 475b52957b06bbd8765c46ad0ebe127bcaadfd4a Mon Sep 17 00:00:00 2001 From: Adrien Guatto Date: Mon, 2 Oct 2023 09:39:12 +0200 Subject: [PATCH] Sources du TP Menhir --- tp/tp-menhir/code/AST.ml | 8 ++++++++ tp/tp-menhir/code/Makefile | 9 +++++++++ tp/tp-menhir/code/dune | 11 +++++++++++ tp/tp-menhir/code/dune-project | 2 ++ tp/tp-menhir/code/lexer.mll | 17 ++++++++++++++++ tp/tp-menhir/code/marthe.ml | 36 ++++++++++++++++++++++++++++++++++ tp/tp-menhir/code/parser.mly | 31 +++++++++++++++++++++++++++++ tp/tp-menhir/code/printer.ml | 17 ++++++++++++++++ 8 files changed, 131 insertions(+) create mode 100644 tp/tp-menhir/code/AST.ml create mode 100644 tp/tp-menhir/code/Makefile create mode 100644 tp/tp-menhir/code/dune create mode 100644 tp/tp-menhir/code/dune-project create mode 100644 tp/tp-menhir/code/lexer.mll create mode 100644 tp/tp-menhir/code/marthe.ml create mode 100644 tp/tp-menhir/code/parser.mly create mode 100644 tp/tp-menhir/code/printer.ml diff --git a/tp/tp-menhir/code/AST.ml b/tp/tp-menhir/code/AST.ml new file mode 100644 index 0000000..502a012 --- /dev/null +++ b/tp/tp-menhir/code/AST.ml @@ -0,0 +1,8 @@ +type exp = + | Id of identifier + | LInt of int + | Add of exp * exp + | Mul of exp * exp + | Sum of identifier * exp * exp * exp + +and identifier = string diff --git a/tp/tp-menhir/code/Makefile b/tp/tp-menhir/code/Makefile new file mode 100644 index 0000000..a6a98aa --- /dev/null +++ b/tp/tp-menhir/code/Makefile @@ -0,0 +1,9 @@ +.PHONY: all clean + +all: + dune build marthe.exe + ln -sf _build/default/marthe.exe marthe + +clean: + dune clean + rm -fr *~ marthe diff --git a/tp/tp-menhir/code/dune b/tp/tp-menhir/code/dune new file mode 100644 index 0000000..464c664 --- /dev/null +++ b/tp/tp-menhir/code/dune @@ -0,0 +1,11 @@ +(ocamllex lexer) + +(menhir + (flags --explain --inspection --table) + (modules parser)) + +(executable + (name marthe) + (ocamlopt_flags :standard) + (libraries menhirLib) +) \ No newline at end of file diff --git a/tp/tp-menhir/code/dune-project b/tp/tp-menhir/code/dune-project new file mode 100644 index 0000000..ba417fe --- /dev/null +++ b/tp/tp-menhir/code/dune-project @@ -0,0 +1,2 @@ +(lang dune 1.4) +(using menhir 2.0) diff --git a/tp/tp-menhir/code/lexer.mll b/tp/tp-menhir/code/lexer.mll new file mode 100644 index 0000000..560cb6e --- /dev/null +++ b/tp/tp-menhir/code/lexer.mll @@ -0,0 +1,17 @@ +{ (* Emacs, open this file with -*- tuareg -*- *) + open Parser +} + +let layout = ' ' | '\t' | '\n' +let number = ['0'-'9']+ +let identifier = ['a'-'z']['A'-'Z' '0'-'9' 'a'-'z' '_']* + +rule token = parse +| eof { EOF } +| layout { token lexbuf } +| number as i { INT (int_of_string i) } +| identifier as s { ID s } +| "+" { PLUS } +| _ as c { + failwith (Printf.sprintf "Invalid character: %c\n" c) +} diff --git a/tp/tp-menhir/code/marthe.ml b/tp/tp-menhir/code/marthe.ml new file mode 100644 index 0000000..1d3719e --- /dev/null +++ b/tp/tp-menhir/code/marthe.ml @@ -0,0 +1,36 @@ +let rec interactive_loop () = + welcome_message (); + let rec loop () = + begin try + read () |> eval |> print + with exn -> + Printf.printf "Error: %s\n%!" (Printexc.to_string exn) + end; + loop () + in + loop () + +and welcome_message () = + Printf.printf " + ====================================================\n + Welcome to the incredible Marthe interactive loop! \n + ====================================================\n +" + +and read () = + invite (); input_line stdin |> parse + +and invite () = + Printf.printf "> %!" + +and parse input = + let lexbuf = Lexing.from_string input in + Parser.phrase Lexer.token lexbuf + +and print e = + Printf.printf ":- %s\n%!" (Printer.string_of_exp e) + +and eval e = + e + +let main = interactive_loop () diff --git a/tp/tp-menhir/code/parser.mly b/tp/tp-menhir/code/parser.mly new file mode 100644 index 0000000..39c62a9 --- /dev/null +++ b/tp/tp-menhir/code/parser.mly @@ -0,0 +1,31 @@ +%{ (* Emacs, open this with -*- tuareg -*- *) +open AST +%} + +%token INT +%token ID +%token PLUS EOF + +%start phrase + +%left PLUS + +%% + +phrase: e=exp EOF +{ + e +} + +exp: x=INT +{ + LInt x +} +| x=ID +{ + Id x +} +| e1=exp PLUS e2=exp +{ + Add (e1, e2) +} diff --git a/tp/tp-menhir/code/printer.ml b/tp/tp-menhir/code/printer.ml new file mode 100644 index 0000000..eb99a21 --- /dev/null +++ b/tp/tp-menhir/code/printer.ml @@ -0,0 +1,17 @@ +open AST + +let string_of_exp e = + let rec aux = function + | Id x -> + x + | LInt x -> + string_of_int x + | Add (e1, e2) -> + Printf.sprintf "(%s + %s)" (aux e1) (aux e2) + | Mul (e1, e2) -> + Printf.sprintf "(%s * %s)" (aux e1) (aux e2) + | Sum (x, start, stop, exp) -> + Printf.sprintf "sum(%s, %s, %s, %s)" + x (aux start) (aux stop) (aux exp) + in + aux e