-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.mly
164 lines (135 loc) · 5.21 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
%{ open Ast %}
%token SEMI COLON LPAREN RPAREN LBRACE RBRACE LBRACK RBRACK COMMA
%token MINUS TIMES DIVIDE MOD STRING INT EOF OR AND NOT PLUS
%token ASSIGN EQ NEQ LT LEQ GT GEQ RETURN IF ELSE FOR WHILE BOOL
%token ACCESS STRUCT ASSERT UNIT THIS VOID EQUALS ACCEPT REJECT
%token <string> ID
%token <int> INT_LITERAL
%token <string> STRING_LITERAL
%token <bool> BOOL_LITERAL
%nonassoc ID
%nonassoc NOELSE /* Precedence and associativity of each operator */
%nonassoc ELSE
%nonassoc LBRACK RBRACK
%left ASSERT
%left ACCESS
%right ASSIGN
%left OR AND
%left EQ NEQ
%left LT GT LEQ GEQ
%left PLUS MINUS
%left TIMES DIVIDE MOD
%right NOT
%start program /* Start symbol */
%type <Ast.program> program /* Type returned by a program */
%%
program:
/* nothing */ { [], [], [], [] }
| program sdecl { let (str, var, func, unt) = $1 in $2::str, var, func, unt }
| program vdecl { let (str, var, func, unt) = $1 in str, $2::var, func, unt } /* int world = 4; */
| program fdecl { let (str, var, func, unt) = $1 in str, var, $2::func, unt }
| program udecl { let (str, var, func, unt) = $1 in str, var, func, $2::unt }
fdecl:
the_type ID LPAREN formals_opt RPAREN LBRACE vdecl_list stmt_list udecl_list RBRACE
{ { ftype = $1;
fname = $2;
formals = $4;
locals = List.rev $7;
body = List.rev $8;
units = List.rev $9; } }
formals_opt:
/* nothing */ { [] }
| formal_list { List.rev $1 }
formal_list:
the_type ID { [Param($1, $2)] }
| formal_list COMMA the_type ID { Param($3, $4) :: $1 }
vdecl_list:
/* nothing */ { [] }
| vdecl_list vdecl { $2 :: $1 }
vdecl:
the_type LBRACK RBRACK ID ASSIGN LBRACE expr_list RBRACE SEMI { Array_Initialization(Array($1, Noexpr), $4, List.rev $7) }
| the_type ID SEMI { Variable($1, $2) }
| the_type ID ASSIGN expr SEMI { Variable_Initialization($1, $2, $4) }
| the_type ID ASSIGN LBRACE expr_list RBRACE SEMI { Struct_Initialization($1, $2, List.rev $5) }
/* ------------- udecl stuff --------------*/
udecl_list:
/* nothing */ { [] }
| udecl_list udecl { $2 :: $1 }
udecl:
UNIT LPAREN actuals_opt RPAREN COLON EQUALS LPAREN expr RPAREN COLON ACCEPT SEMI { Local_udecl($3, $8, true) }
| UNIT LPAREN actuals_opt RPAREN COLON EQUALS LPAREN expr RPAREN COLON REJECT SEMI { Local_udecl($3, $8, false) }
| UNIT COLON ID LPAREN actuals_opt RPAREN COLON EQUALS LPAREN expr RPAREN COLON ACCEPT SEMI { Outer_udecl($3, $5, $10, true) }
| UNIT COLON ID LPAREN actuals_opt RPAREN COLON EQUALS LPAREN expr RPAREN COLON REJECT SEMI { Outer_udecl($3, $5, $10, false) }
/* ------------- end udecl stuff --------------*/
assert_list:
/* nothing */ { [] }
| assert_list asrt { $2 :: $1 }
asrt:
ASSERT LPAREN expr RPAREN stmt_list { $3, List.rev $5 }
expr_list:
expr { [$1] }
| expr_list SEMI expr { $3 :: $1 }
| expr_list COMMA expr { $3 :: $1 } /*will this work for udecl? */
sdecl:
STRUCT ID LBRACE vdecl_list assert_list RBRACE
{ { sname = $2;
variable_decls = List.rev $4;
asserts = List.rev $5; } }
the_type:
INT { Int }
| VOID { Void }
| STRING { String }
| BOOL { Boolean }
| STRUCT ID { Struct($2) }
| the_type LBRACK expr RBRACK { Array($1, $3) }
stmt_list:
/* nothing */ { [] }
| stmt_list stmt { $2 :: $1 }
/*| stmt_list init { $2 :: $1 }
init:
ID LBRACK RBRACK ASSIGN block SEMI { Array_Initialization($1, $5) }*/
stmt:
expr SEMI { Expr($1) }
| RETURN expr SEMI { Return($2) }
| block { $1 }
/*| IF LPAREN expr RPAREN stmt %prec NOELSE { If($3, $5, Block([])) } */
| IF LPAREN expr RPAREN stmt ELSE stmt { If($3, $5, $7) }
| FOR LPAREN expr_opt SEMI expr_opt SEMI expr_opt RPAREN stmt { For($3, $5, $7, $9) }
| WHILE LPAREN expr RPAREN stmt { While($3, $5) }
block:
LBRACE stmt_list RBRACE { Block(List.rev $2) }
expr_opt:
/* nothing */ { Noexpr }
| expr { $1 }
expr:
ID { Id($1) }
| INT_LITERAL { Integer_literal($1) }
| STRING_LITERAL { String_literal($1) }
| BOOL_LITERAL { Boolean_literal($1) }
| NOT expr { Uniop(Not, $2) }
| expr PLUS expr { Binop($1, Add, $3) }
| expr MINUS expr { Binop($1, Sub, $3) }
| expr TIMES expr { Binop($1, Mult, $3) }
| expr DIVIDE expr { Binop($1, Div, $3) }
| expr MOD expr { Binop($1, Mod, $3) }
| expr EQ expr { Binop($1, Equal, $3) }
| expr NEQ expr { Binop($1, Neq, $3) }
| expr LT expr { Binop($1, Less, $3) }
| expr LEQ expr { Binop($1, Leq, $3) }
| expr GT expr { Binop($1, Greater, $3) }
| expr GEQ expr { Binop($1, Geq, $3) }
| expr OR expr { Binop ($1, Or, $3) }
| expr AND expr { Binop ($1, And, $3) }
| ID ACCESS ID { Access ($1, $3) }
| ID ASSIGN expr { Assign ($1, $3) }
| ID LPAREN actuals_opt RPAREN { Call ($1, $3) }
| ID ACCESS ID ASSIGN expr { Struct_Member_Assign($1, $3, $5) }
| ID LBRACK expr RBRACK ASSIGN expr { Array_Member_Assign($1, $3, $6) }
| LPAREN expr RPAREN { $2 }
| ID LBRACK expr RBRACK { Array_access($1, $3) }
actuals_opt:
/* nothing */ { [] }
| actuals_list { List.rev $1 }
actuals_list:
expr { [$1] }
| actuals_list COMMA expr { $3 :: $1 }