This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
286 lines (239 loc) · 6 KB
/
parser.y
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
%expect 3
%{
#include "yyfunc.h"
#include <stdio.h>
#include <stdlib.h>
#include "ast_symbol.h"
#include "make_params.hpp"
#include "code_gen.hpp"
#include "error.h"
ast * t;
%}
%union{
ast * a;
char c;
int n;
char* s;
long double r;
kind k;
}
%token t_and "and"
%token t_array "array"
%token t_begin "begin"
%token t_boolean "boolean"
%token t_char "char"
%token t_dispose "dispose"
%token t_div "div"
%token t_do "do"
%token t_else "else"
%token t_end "end"
%token t_false "false"
%token t_forward "forward"
%token t_function "function"
%token t_goto "goto"
%token t_if "if"
%token t_integer "integer"
%token t_label "label"
%token t_mod "mod"
%token t_new "new"
%token t_nil "nil"
%token t_not "not"
%token t_of "of"
%token t_or "or"
%token t_procedure "procedure"
%token t_program "program"
%token t_real "real"
%token t_result "result"
%token t_return "return"
%token t_then "then"
%token t_true "true"
%token t_var "var"
%token t_while "while"
%token<s> t_id
%token<r> t_real_const
%token<n> t_int_const
%token<c> t_char_const
%token<s> t_string_const
%token t_neq "<>"
%token t_geq ">="
%token t_leq "<="
%token t_assign ":="
%left "<=" ">=" '<' '>' '=' "<>"
%left '+' '-' "or"
%left '*' '/' "div" "mod" "and"
%nonassoc '@' '^'
%left unary
%type<a> program
%type<a> body
%type<a> moreid
%type<a> localvar
%type<a> local
%type<a> header
%type<a> moreformal
%type<a> formal
%type<a> type
%type<a> morestmt
%type<a> stmt
%type<a> moreexpr
%type<a> expr
%type<a> lvalue
%type<a> rvalue
%type<a> call
%type<a> block
%type<a> morelocal
%type<k> unop
%%
program:
"program" t_id ';' body '.' {t = $$ = ast_program($2, $4);}
;
morelocal:
/*nothing*/ {$$ = NULL;}
|local morelocal {$$ = ast_seq_local(SEQ_LOCAL, $1, $2);}
body:
block {$$ = ast_body(NULL, $1);}
| morelocal block {$$ = ast_body($1, $2);}
;
moreid:
t_id {$$ = ast_seq_id($1, NULL);}
| t_id ',' moreid {$$ = ast_seq_id($1, $3);}
;
localvar:
moreid ':' type ';' {$$ = ast_seq_local_var(ast_local_var_instance($1, $3), NULL);}
| moreid ':' type ';' localvar {$$ = ast_seq_local_var(ast_local_var_instance($1, $3), $5);}
;
local:
"var" localvar {$$ = ast_local(LOCAL_VAR, $2, NULL);}
| "label" moreid ';' {$$ = ast_local(LABEL, $2, NULL);}
| header ';' body ';' {$$ = ast_local(DEFINITION, $1, $3);}
| "forward" header ';' {$$ = ast_local(FORWARD, $2, NULL);}
;
header:
"procedure" t_id '(' ')' {$$ = ast_header(PROCEDURE, $2, NULL, NULL);}
| "procedure" t_id '(' moreformal ')' {$$ = ast_header(PROCEDURE, $2, $4, NULL);}
| "function" t_id '(' ')' ':' type {$$ = ast_header(FUNCTION, $2, NULL, $6);}
| "function" t_id '(' moreformal ')' ':' type {$$ = ast_header(FUNCTION, $2, $4, $7);}
;
moreformal:
formal {$$ = ast_seq_formal($1, NULL);}
| formal ';' moreformal {$$ = ast_seq_formal($1, $3);}
;
formal:
"var" moreid ':' type {$$ = ast_formal(VARREF, $2, $4);}
| moreid ':' type {$$ = ast_formal(VAR, $1, $3);}
;
type:
"integer" {$$ = ast_type(INT, -1, NULL);}
| "real" {$$ = ast_type(REAL, -1, NULL);}
| "boolean" {$$ = ast_type(BOOL, -1, NULL);}
| "char" {$$ = ast_type(CHAR, -1, NULL);}
| "array" "of" type {$$ = ast_type(IARRAY, 0, $3);}
| "array" '[' t_int_const ']' "of" type {$$ = ast_type(ARRAY, $3, $6);}
| '^' type {$$ = ast_type(POINTER, -1, $2);}
;
block:
"begin" morestmt "end" {$$ = ast_begin($2);}
;
morestmt:
stmt {$$ = ast_seq_stmt($1, NULL);}
| stmt ';' morestmt {$$ = ast_seq_stmt($1, $3);}
;
stmt:
/* nothing */ {$$ = NULL;}
| lvalue ":=" expr {$$ = ast_assign($1, $3);}
| block {$$ = $1;}
| call {$$ = $1;}
| "if" expr "then" stmt {$$ = ast_if($2, $4, NULL);}
| "if" expr "then" stmt "else" stmt {$$ = ast_if($2, $4, $6);}
| "while" expr "do" stmt {$$ = ast_while($2, $4);}
| t_id ':' stmt {$$ = ast_stmt($1, $3);}
| "goto" t_id {$$ = ast_goto($2);}
| "return" {$$ = ast_return();}
| "new" lvalue {$$ = ast_new(NULL, $2);}
| "new" '[' expr ']' lvalue {$$ = ast_new($3, $5);}
| "dispose" lvalue {$$ = ast_dispose($2);}
| "dispose" '[' ']' lvalue {$$ = ast_dispose_array($4);}
;
moreexpr:
expr {$$ = ast_seq_expr($1, NULL);}
| expr ',' moreexpr {$$ = ast_seq_expr($1, $3);}
;
expr:
lvalue {$$ = $1;}
| rvalue {$$ = $1;}
;
lvalue:
lvalue '[' expr ']' {$$ = ast_index($1, $3);}
| lvalue '^' {$$ = ast_op($1, DEREF, NULL);}
| t_string_const {$$ = ast_const(STR_CONST, 0, 0, '\0', 0.0, make_string($1));}
| '(' lvalue ')' {$$ = $2;}
| t_id {$$ = ast_id($1);}
| "result" {$$ = ast_result();}
;
rvalue:
t_int_const {$$ = ast_const(INT_CONST, 0, $1, '\0', 0.0, NULL);}
| "true" {$$ = ast_const(BOOL_CONST, 1, 0, '\0', 0.0, NULL);}
| "false" {$$ = ast_const(BOOL_CONST, 0, 0, '\0', 0.0, NULL);}
| t_real_const {$$ = ast_const(REAL_CONST, 0, 0, '\0', $1, NULL);}
| t_char_const {$$ = ast_const(CHAR_CONST, 0, 0, $1, 0.0, NULL);}
| '(' rvalue ')' {$$ = $2;}
| "nil" {$$ = ast_const(NIL, 0, 0, '\0', 0.0, NULL);}
| call {$$ = $1;}
| '@' lvalue {$$ = ast_op($2, REF, NULL);}
| unop expr %prec unary {$$ = ast_op($2, $1, NULL);}
| expr '+' expr {$$ = ast_op($1, PLUS, $3);}
| expr '-' expr {$$ = ast_op($1, MINUS, $3);}
| expr '*' expr {$$ = ast_op($1, TIMES, $3);}
| expr '/' expr {$$ = ast_op($1, DIVIDE, $3);}
| expr "div" expr {$$ = ast_op($1, DIV, $3);}
| expr "mod" expr {$$ = ast_op($1, MOD, $3);}
| expr "or" expr {$$ = ast_op($1, OR, $3);}
| expr "and" expr {$$ = ast_op($1, AND, $3);}
| expr '=' expr {$$ = ast_op($1, EQ, $3);}
| expr "<>" expr {$$ = ast_op($1, NEQ, $3);}
| expr '<' expr {$$ = ast_op($1, LESS, $3);}
| expr "<=" expr {$$ = ast_op($1, LEQ, $3);}
| expr '>' expr {$$ = ast_op($1, GREATER, $3);}
| expr ">=" expr {$$ = ast_op($1, GEQ, $3);}
;
call:
t_id '(' ')' {$$ = ast_call($1, NULL);}
| t_id '(' moreexpr ')' {$$ = ast_call($1, $3);}
;
unop:
"not" {$$ = NOT;}
| '+' {$$ = PLUS;}
| '-' {$$ = MINUS;}
;
/*
binop:
'+'
| '-'
| '*'
| '/'
| "div"
| "mod"
| "or"
| "and"
| '='
| "<>"
| '<'
| "<="
| '>'
| ">="
;
*/
%%
int optimize_code;
int main(int argc, char* argv[]) {
int result = yyparse();
if (result == 0) {
if(type_checking(t)) {
error("Type Checking Failed!");
return 1;
}
while (make_parameters(t));
optimize_code = argc > 1;
generate_code(t);
}
return result;
}