-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.mly
59 lines (48 loc) · 1019 Bytes
/
parse.mly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
%{
open Infer
let rec expr_app e = function
| [e'] -> App (e, e')
| h :: ((_ :: _) as t) -> expr_app (App (e, h)) t
| _ -> failwith "Unreachable"
%}
%token <string> VAR
%token <int> INT
%token <bool> BOOL
%token RPAREN LPAREN
%token ARROW BACKSLASH EQUAL COMMA
%token REC LET IN IF THEN ELSE
%token EOF
%left COMMA
%start main
%type <Infer.expr> main
%%
main:
| expr; EOF { $1 }
;
expr:
| expr2 { $1 }
| expr2; expr3 { expr_app $1 $2 }
| BACKSLASH; VAR; ARROW; expr { Abs ($2, $4) }
| LET; VAR; EQUAL; expr; IN; expr { Let ($2, $4, $6) }
| REC; VAR; EQUAL; expr; IN; expr { Rec ($2, $4, $6) }
| IF; expr; THEN; expr; ELSE; expr { If ($2, $4, $6) }
| expr4 { Tup (List.rev $1) }
;
expr2:
| VAR { Var $1 }
| lit { Lit $1 }
| RPAREN; LPAREN { Tup [] }
| RPAREN; expr; LPAREN { $2 }
;
expr3:
| expr2 { [$1] }
| expr2; expr3 { $1 :: $2 }
;
expr4:
| expr; COMMA; expr { [$1; $3] }
| expr4; COMMA; expr { $3 :: $1 }
;
lit:
| INT { Int $1 }
| BOOL { Bool $1 }
;