-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.mly
60 lines (49 loc) · 1.23 KB
/
parser.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
60
%{
open Syntax
let reserv_types = [
"int", IntT;
]
let type_of_string s =
match OptionList.assoc s reserv_types with
None -> VarT s
| Some t -> t
%}
%token BACKSLA BACKSLA2 COLON DOT SEMICOLON2
%token LSQPAREN RSQPAREN RARROW
%token LPAREN RPAREN
%token PLUS ASTER SLASH
%token<Syntax.id> IDENT APOSTIDENT
%token<int> INTLIT
%token EOF
%left PLUS
%left ASTER SLASH
%start main
%type<Syntax.program> main
%%
main:
Expr SEMICOLON2 { Prog $1 }
| EOF { Syntax.EOF }
Expr:
BACKSLA IDENT COLON FunExpr DOT Expr { Fun ($2, $4, $6) }
| BACKSLA2 IDENT DOT Expr { TypeFun ($2, $4) }
| ArithExpr { $1 }
ArithExpr:
ArithExpr PLUS ArithExpr { BinOp (Plus, $1, $3) }
| ArithExpr ASTER ArithExpr { BinOp (Mult, $1, $3) }
| ArithExpr SLASH ArithExpr { BinOp (Div, $1, $3) }
| AppExpr { $1 }
AppExpr:
AppExpr AtomExpr { App ($1, $2) }
| AppExpr LSQPAREN FunExpr RSQPAREN { TypeApp ($1, $3) }
| AtomExpr { $1 }
AtomExpr:
INTLIT { IntLit $1 }
| IDENT { Var $1 }
| LPAREN Expr RPAREN { $2 }
FunExpr:
AtomFunExpr RARROW FunExpr { FunT ($1, $3) }
| APOSTIDENT RARROW FunExpr { TypeFunT ($1, $3) }
| AtomFunExpr { $1 }
AtomFunExpr:
IDENT { type_of_string $1 }
| LPAREN FunExpr RPAREN { $2 }