separated into modules, added parser and lexer for miniImp
This commit is contained in:
5
lib/dune
5
lib/dune
@ -1,5 +0,0 @@
|
||||
(library
|
||||
(name lang)
|
||||
(public_name lang))
|
||||
|
||||
(include_subdirs qualified)
|
||||
5
lib/exercises/dune
Normal file
5
lib/exercises/dune
Normal file
@ -0,0 +1,5 @@
|
||||
(library
|
||||
(name exercises)
|
||||
(public_name exercises))
|
||||
|
||||
(include_subdirs qualified)
|
||||
@ -34,10 +34,10 @@ type 'a my_tree =
|
||||
let mod_list y =
|
||||
(List.fold_left
|
||||
(fun acc x ->
|
||||
match acc with
|
||||
| [a] when ((List.hd a) = x) -> [x :: a]
|
||||
| a :: tl when ((List.hd a) = x) -> (x :: a) :: tl
|
||||
| _ -> [x] :: acc)
|
||||
match acc with
|
||||
| [a] when ((List.hd a) = x) -> [x :: a]
|
||||
| a :: tl when ((List.hd a) = x) -> (x :: a) :: tl
|
||||
| _ -> [x] :: acc)
|
||||
[]
|
||||
y)
|
||||
|> List.rev
|
||||
@ -46,7 +46,7 @@ let mod_list y =
|
||||
|
||||
let to_tup f g =
|
||||
fun x -> match x with
|
||||
(a, b) -> (f a, g b)
|
||||
(a, b) -> (f a, g b)
|
||||
|
||||
let partialsum l =
|
||||
snd (List.fold_left_map (fun acc x -> (acc+x, acc+x)) 0 l)
|
||||
6
lib/miniFun/dune
Normal file
6
lib/miniFun/dune
Normal file
@ -0,0 +1,6 @@
|
||||
(library
|
||||
(name miniFun)
|
||||
(public_name miniFun)
|
||||
(libraries utility))
|
||||
|
||||
(include_subdirs qualified)
|
||||
88
lib/miniImp/Lexer.mll
Normal file
88
lib/miniImp/Lexer.mll
Normal file
@ -0,0 +1,88 @@
|
||||
{
|
||||
open Parser
|
||||
exception LexingError of string
|
||||
|
||||
let create_hashtable size init =
|
||||
let tbl = Hashtbl.create size in
|
||||
List.iter (fun (key, data) -> Hashtbl.add tbl key data) init;
|
||||
tbl
|
||||
|
||||
let keyword_table =
|
||||
let mapping = [
|
||||
("main", MAIN);
|
||||
("skip", SKIP);
|
||||
("if", IF);
|
||||
("else", ELSE);
|
||||
("while", WHILE);
|
||||
("for", FOR);
|
||||
("do", DO);
|
||||
("true", BOOL(true));
|
||||
("false", BOOL(false));
|
||||
("not", BNOT);
|
||||
("rand", RAND);
|
||||
("powmod", POWERMOD);
|
||||
]
|
||||
in create_hashtable (List.length mapping) mapping
|
||||
}
|
||||
|
||||
let digit = ['0'-'9']
|
||||
let alpha = ['a'-'z' 'A'-'Z']
|
||||
let white = [' ' '\t']+ | '\r' | '\n' | "\r\n"
|
||||
|
||||
let integer = (digit)(digit*)
|
||||
let var = (alpha|'_') (alpha|digit|'_')*
|
||||
|
||||
let symbols = ['!'-'/' ':'-'?' '[' ']' '^' '{'-'}' '~']
|
||||
|
||||
(* lexing rules *)
|
||||
rule read = parse
|
||||
| white {read lexbuf}
|
||||
| var as v {
|
||||
match Hashtbl.find_opt keyword_table v with
|
||||
| Some keyword -> keyword
|
||||
| None -> VARIABLE(v)
|
||||
}
|
||||
| ";" {SEQUENCE}
|
||||
| "," {COMMA}
|
||||
| "{" {LEFTGPAR}
|
||||
| "}" {RIGHTGPAR}
|
||||
| "(" {LEFTPAR}
|
||||
| ")" {RIGHTPAR}
|
||||
| "<" {BCMPLESS}
|
||||
| ">" {BCMPGREATER}
|
||||
| "+" {PLUS}
|
||||
| "-" {MINUS}
|
||||
| "*" {TIMES}
|
||||
| "/" {DIVISION}
|
||||
| "%" {MODULO}
|
||||
| "^" {POWER}
|
||||
| ":=" {ASSIGNMENT}
|
||||
| "&&" {BAND}
|
||||
| "||" {BOR}
|
||||
| "==" {BCMP}
|
||||
| "<=" {BCMPLESSEQ}
|
||||
| ">=" {BCMPGREATEREQ}
|
||||
| integer as i {INT(int_of_string i)}
|
||||
| "(*" {comments 0 lexbuf}
|
||||
| eof {EOF}
|
||||
| _ {
|
||||
raise
|
||||
(LexingError
|
||||
(Printf.sprintf
|
||||
"Error scanning %s on line %d at char %d"
|
||||
(Lexing.lexeme lexbuf)
|
||||
(lexbuf.Lexing.lex_curr_p.Lexing.pos_lnum)
|
||||
(lexbuf.Lexing.lex_curr_p.Lexing.pos_lnum)
|
||||
))}
|
||||
and comments level = parse
|
||||
| "*)" {if level = 0
|
||||
then read lexbuf
|
||||
else comments (level-1) lexbuf}
|
||||
| "(*" {comments (level+1) lexbuf}
|
||||
| _ {comments level lexbuf}
|
||||
| eof {raise (LexingError ("Comment is not closed"))}
|
||||
|
||||
|
||||
{
|
||||
let lex = read
|
||||
}
|
||||
1016
lib/miniImp/Parser.messages
Normal file
1016
lib/miniImp/Parser.messages
Normal file
File diff suppressed because it is too large
Load Diff
85
lib/miniImp/Parser.mly
Normal file
85
lib/miniImp/Parser.mly
Normal file
@ -0,0 +1,85 @@
|
||||
(* code to be copied in the scanner module *)
|
||||
(*
|
||||
*)
|
||||
%{
|
||||
open Types
|
||||
%}
|
||||
|
||||
(* tokens *)
|
||||
%token MAIN SKIP ASSIGNMENT SEQUENCE IF ELSE WHILE FOR DO COMMA
|
||||
%token LEFTGPAR RIGHTGPAR
|
||||
%token <bool> BOOL
|
||||
%token BAND BOR BNOT BCMP BCMPLESS BCMPLESSEQ BCMPGREATER BCMPGREATEREQ
|
||||
%token <string> VARIABLE
|
||||
%token <int> INT
|
||||
%token LEFTPAR RIGHTPAR
|
||||
%token PLUS MINUS TIMES DIVISION MODULO POWER POWERMOD RAND
|
||||
%token EOF
|
||||
|
||||
%type <c_exp> cexpp
|
||||
%type <b_exp> bexpp
|
||||
%type <a_exp> aexpp
|
||||
%type <p_exp> prg
|
||||
|
||||
(* start nonterminal *)
|
||||
%start prg
|
||||
|
||||
(* associativity in order of precedence *)
|
||||
%left twoseq
|
||||
%left SEQUENCE
|
||||
%left ELSE
|
||||
%left PLUS MINUS BOR BAND
|
||||
%left BNOT
|
||||
%left DIVISION
|
||||
%left MODULO
|
||||
%left TIMES
|
||||
%left POWER
|
||||
|
||||
%%
|
||||
|
||||
(* grammar *)
|
||||
prg:
|
||||
| MAIN; a = VARIABLE; b = VARIABLE; LEFTGPAR; t = cexpp; RIGHTGPAR; EOF
|
||||
{Main (a, b, t)} // main a b {...}
|
||||
cexpp:
|
||||
| SKIP {Skip} // skip
|
||||
| a = VARIABLE; ASSIGNMENT; body = aexpp
|
||||
{Assignment (a, body)} // a := ...
|
||||
| t1 = cexpp; SEQUENCE; t2 = cexpp %prec twoseq
|
||||
{Sequence (t1, t2)} // ...; ...
|
||||
| t = cexpp; SEQUENCE {t} // ...;
|
||||
| IF; LEFTPAR; guard = bexpp; RIGHTPAR; body1 = cexpp; ELSE; body2 = cexpp
|
||||
{If (guard, body1, body2)} // if (...) ... else ...
|
||||
| WHILE; guard = bexpp; DO; LEFTGPAR; body = cexpp; RIGHTGPAR
|
||||
{While (guard, body)} // while ... do {...}
|
||||
| FOR; LEFTPAR; ass = cexpp; COMMA; guard = bexpp; COMMA; iter = cexpp; RIGHTPAR;
|
||||
DO; LEFTGPAR; body = cexpp; RIGHTGPAR
|
||||
{For (ass, guard, iter, body)} // for (..., ..., ...) do {...}
|
||||
| LEFTGPAR; t = cexpp; RIGHTGPAR {t} // {...}
|
||||
bexpp:
|
||||
| b = BOOL {Boolean (b)}
|
||||
| b1 = bexpp; BAND; b2 = bexpp {BAnd (b1, b2)}
|
||||
| b1 = bexpp; BOR; b2 = bexpp {BOr (b1, b2)}
|
||||
| BNOT; b = bexpp {BNot (b)}
|
||||
| a1 = aexpp; BCMP; a2 = aexpp {BCmp (a1, a2)}
|
||||
| a1 = aexpp; BCMPLESS; a2 = aexpp {BCmpLess (a1, a2)}
|
||||
| a1 = aexpp; BCMPLESSEQ; a2 = aexpp {BCmpLessEq (a1, a2)}
|
||||
| a1 = aexpp; BCMPGREATER; a2 = aexpp {BCmpGreater (a1, a2)}
|
||||
| a1 = aexpp; BCMPGREATEREQ; a2 = aexpp {BCmpGreaterEq (a1, a2)}
|
||||
| LEFTPAR; b = bexpp; RIGHTPAR {b}
|
||||
aexpp:
|
||||
| a = VARIABLE {Variable (a)}
|
||||
| i = INT {Integer (i)}
|
||||
| t1 = aexpp; PLUS; t2 = aexpp {Plus (t1, t2)}
|
||||
| t1 = aexpp; MINUS; t2 = aexpp {Minus (t1, t2)}
|
||||
| MINUS; i = INT {Integer (-i)}
|
||||
| t1 = aexpp; TIMES; t2 = aexpp {Times (t1, t2)}
|
||||
| t1 = aexpp; DIVISION; t2 = aexpp {Division (t1, t2)}
|
||||
| t1 = aexpp; MODULO; t2 = aexpp {Modulo (t1, t2)}
|
||||
| t1 = aexpp; POWER; t2 = aexpp {Power (t1, t2)}
|
||||
| POWERMOD; LEFTPAR; t1 = aexpp; COMMA;
|
||||
t2 = aexpp; COMMA;
|
||||
t3 = aexpp; RIGHTPAR
|
||||
{PowerMod (t1, t2, t3)} // powmod (..., ..., ...)
|
||||
| RAND; LEFTPAR; t = aexpp; RIGHTPAR {Rand (t)}
|
||||
| LEFTPAR; a = aexpp; RIGHTPAR {a}
|
||||
@ -8,13 +8,13 @@ and c_exp =
|
||||
| Sequence of c_exp * c_exp (* c; c *)
|
||||
| If of b_exp * c_exp * c_exp (* if b then c else c *)
|
||||
| While of b_exp * c_exp (* while b do c *)
|
||||
| For of c_exp * b_exp * c_exp * c_exp (* for c; b; c do c *)
|
||||
| For of c_exp * b_exp * c_exp * c_exp (* for (c; b; c) do c *)
|
||||
and b_exp =
|
||||
Boolean of bool (* v *)
|
||||
| BAnd of b_exp * b_exp (* b and b *)
|
||||
| BOr of b_exp * b_exp (* b or b *)
|
||||
| BAnd of b_exp * b_exp (* b && b *)
|
||||
| BOr of b_exp * b_exp (* b || b *)
|
||||
| BNot of b_exp (* not b *)
|
||||
| BCmp of a_exp * a_exp (* a = a *)
|
||||
| BCmp of a_exp * a_exp (* a == a *)
|
||||
| BCmpLess of a_exp * a_exp (* a < a *)
|
||||
| BCmpLessEq of a_exp * a_exp (* a <= a *)
|
||||
| BCmpGreater of a_exp * a_exp (* a > a *)
|
||||
|
||||
@ -8,13 +8,13 @@ and c_exp =
|
||||
| Sequence of c_exp * c_exp (* c; c *)
|
||||
| If of b_exp * c_exp * c_exp (* if b then c else c *)
|
||||
| While of b_exp * c_exp (* while b do c *)
|
||||
| For of c_exp * b_exp * c_exp * c_exp (* for c; b; c do c *)
|
||||
| For of c_exp * b_exp * c_exp * c_exp (* for (c; b; c) do c *)
|
||||
and b_exp =
|
||||
Boolean of bool (* v *)
|
||||
| BAnd of b_exp * b_exp (* b and b *)
|
||||
| BOr of b_exp * b_exp (* b or b *)
|
||||
| BAnd of b_exp * b_exp (* b && b *)
|
||||
| BOr of b_exp * b_exp (* b || b *)
|
||||
| BNot of b_exp (* not b *)
|
||||
| BCmp of a_exp * a_exp (* a = a *)
|
||||
| BCmp of a_exp * a_exp (* a == a *)
|
||||
| BCmpLess of a_exp * a_exp (* a < a *)
|
||||
| BCmpLessEq of a_exp * a_exp (* a <= a *)
|
||||
| BCmpGreater of a_exp * a_exp (* a > a *)
|
||||
|
||||
16
lib/miniImp/dune
Normal file
16
lib/miniImp/dune
Normal file
@ -0,0 +1,16 @@
|
||||
(ocamllex Lexer)
|
||||
|
||||
(menhir
|
||||
(modules Parser)
|
||||
(explain true)
|
||||
(infer true)
|
||||
(flags --dump --table)
|
||||
)
|
||||
|
||||
(library
|
||||
(name miniImp)
|
||||
(public_name miniImp)
|
||||
(modules Lexer Parser Types Semantics)
|
||||
(libraries utility menhirLib))
|
||||
|
||||
(include_subdirs qualified)
|
||||
5
lib/utility/dune
Normal file
5
lib/utility/dune
Normal file
@ -0,0 +1,5 @@
|
||||
(library
|
||||
(name utility)
|
||||
(public_name utility))
|
||||
|
||||
(include_subdirs qualified)
|
||||
Reference in New Issue
Block a user