-
Notifications
You must be signed in to change notification settings - Fork 4
/
miniml_parse.mly
59 lines (51 loc) · 1.13 KB
/
miniml_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
/*
CS 51 Final Project
MiniML -- Parser
Spring 2017
*/
%{
open Printf ;;
open Expr ;;
%}
%token EOF
%token OPEN CLOSE
%token LET DOT IN REC
%token NEG
%token PLUS MINUS
%token TIMES
%token LESSTHAN EQUALS
%token IF THEN ELSE
%token FUNCTION
%token RAISE
%token <string> ID
%token <int> INT
%token TRUE FALSE
%nonassoc LESSTHAN
%nonassoc EQUALS
%left PLUS MINUS
%left TIMES
%start input
%type <Expr.expr> input
/* Grammar follows */
%%
input: exp EOF { $1 }
exp: exp expnoapp { App($1, $2) }
| expnoapp { $1 }
expnoapp: INT { Num $1 }
| TRUE { Bool true }
| FALSE { Bool false }
| ID { Var $1 }
| exp PLUS exp { Binop(Plus, $1, $3) }
| exp MINUS exp { Binop(Minus, $1, $3) }
| exp TIMES exp { Binop(Times, $1, $3) }
| exp EQUALS exp { Binop(Equals, $1, $3) }
| exp LESSTHAN exp { Binop(LessThan, $1, $3) }
| NEG exp { Unop(Negate, $2) }
| IF exp THEN exp ELSE exp { Conditional($2, $4, $6) }
| LET ID EQUALS exp IN exp { Let($2, $4, $6) }
| LET REC ID EQUALS exp IN exp { Letrec($3, $5, $7) }
| FUNCTION ID DOT exp { Fun($2, $4) }
| RAISE { Raise }
| OPEN exp CLOSE { $2 }
;
%%