(* plftypes.ml Syntaxe abstraite pour plf *) (* ************************************** *) type plf = Const of int | Var of string | Cond of plf * plf * plf | Appl of plf * plf | Abs of string * plf | Let of string * plf * plf ;; exception Erreur_de_syntaxe;; { (* lexer.mll Analyse lexicale pour PLF *) (* *********************************** *) #use "parser.ml";; exception Fin_inopinee;; let mots_cles = [ "let" , LET ; "be" , BE ; "in" , IN ; "if" , IF ; "then" , THEN ; "else" , ELSE ; "fi" , FI ] ;; } rule token = parse [' ' '\t'] { token lexbuf } | ['\n'] { token lexbuf } | ['0'-'9']+ { INT (int_of_string(get_lexeme lexbuf)) } | ['a'-'z' 'A'-'Z']+ { let s = get_lexeme lexbuf in try assoc s mots_cles with Not_found -> IDENT s } | ['+' '-' '*' '/' '='] { IDENT (get_lexeme lexbuf) } | "\\" { LAMBDA } | "." { POINT } | "(" { PARENG } | ")" { PAREND } | ";" { PVIRGULE } | eof { raise Fin_inopinee } %{ (* parser.mly Grammaire du langage PLF *) (* *********************************** *) #use "plftypes";; %} %token INT %token IDENT %token LET BE IN IF THEN ELSE FI %token PARENG PAREND LAMBDA POINT PVIRGULE EOL %start debut %type debut %% debut : expr PVIRGULE { $1 }; expr0 : INT { Const $1 } | IDENT { Var $1 } | PARENG expr PAREND { $2 }; expr : expr0 { $1 } | IF expr THEN expr ELSE expr FI { Cond ($2,$4,$6) } | LAMBDA IDENT POINT expr { Abs ($2,$4) } | LET IDENT BE expr IN expr { Let ($2,$4,$6) } | expr expr0 { Appl ($1,$2) } ;