-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.mly
More file actions
79 lines (66 loc) · 1.53 KB
/
parser.mly
File metadata and controls
79 lines (66 loc) · 1.53 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
%{
open Syntax
%}
%token <string> IDENT
%token <int> INT
%token <bool> BOOL
%token <Syntax.pos> INPUT
%token <Syntax.pos> OUTPUT
%token PAR
%token REP
%token <Syntax.pos> NU
%token IN
%token PERIOD
%token ZERO
%token <Syntax.pos> IF
%token THEN
%token ELSE
%token <Syntax.pos> EQ
%token <Syntax.pos> GT
%token LPAREN
%token RPAREN
%token COMMA
%token <Syntax.pos> PLUS
%token <Syntax.pos> MINUS
%token <Syntax.pos> NIL
%token EOF
%token <string * Syntax.pos> OP
%nonassoc IN
%left PAR
%nonassoc ELSE
%nonassoc THEN
%right REP
%nonassoc PERIOD
%left COMMA
%left EQ GT
%left PLUS MINUS
%start main
%type <unit Syntax.procexp> main /*no type attatched yet*/
%type <unit Syntax.valexp> value
%%
main:
proc EOF { $1 }
value:
IDENT { Var($1) }
| LPAREN RPAREN { Unit }
| BOOL { Bool($1) }
| INT { Int($1) }
/* solve function-as-value and multiple-argument problems first*/
| OP { failwith("Operation '" ^ (fst $1) ^ "' not supported yet") }
| value PLUS value { App(Op("+", (), $2), Pair($1, $3)) }
| value MINUS value { App(Op("-", (), $2), Pair($1, $3)) }
| value EQ value { App(Op("==", (), $2), Pair($1, $3)) }
| value GT value { App(Op(">", (), $2), Pair($1, $3)) }
| LPAREN value RPAREN { $2 }
/*| NIL { Op("nil", (), $1) }*/
;
proc:
ZERO { Zero }
| proc PAR proc { Par($1, $3) }
| REP proc { Rep($2) }
| LPAREN proc RPAREN { $2 }
| NU IDENT IN proc { Nu($2, (), $4, $1) }
| IF value THEN proc ELSE proc { If($2, $4, $6, $1) }
| IDENT INPUT IDENT PERIOD proc { In($1, $3, (), $5, $2) }
| IDENT OUTPUT value PERIOD proc { Out($1, $3, $5, $2) }
;