-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_parser.y
More file actions
385 lines (330 loc) · 8.14 KB
/
compiler_parser.y
File metadata and controls
385 lines (330 loc) · 8.14 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
%{
#include <stdio.h>
#include <stdint.h>
int yylex();
extern FILE *yyin;
int yyerror(char*);
extern int yylineno;
static FILE *compiler_out;
#include "parser_func/declarations.h"
#include "parser_func/expressions.h"
#include "parser_func/loops.h"
#include "instruction_graph/i_graph.h"
%}
%union {
char *id;
int64_t value;
};
/*//////////////////////////////////////////////
//
// TOKENS
//
*///////////////////////////////////////////////
/*
// DECLARE
*/
%token DECLARE
/*
// BODY
*/
%token BEGIN_P
%token END
/*
// IF
*/
%token IF
%token THEN
%token ELSE
%token ENDIF
/*
// WHILE
*/
%token WHILE
%token ENDWHILE
/*
// REPEAT
*/
%token REPEAT
%token UNTIL
/*
// FOR
*/
%token FOR
%token FROM
%left TO
%left DOWNTO
%token ENDFOR
/*
// UNIVERSAL
*/
%token DO
/*
// ENDL
*/
%token ENDL
/*
// READ & WRITE
*/
%token READ
%token WRITE
/*
// OPERATORS & Extra Symbols
*/
%left ADD // +
%left SUB // -
%left MUL // *
%left DIV // /
%left MOD // %
%left ASSIGN // :=
%token END_EXPR // ;
%left IS_EQUAL // =
%left IS_N_EQUAL // !=
%left LESS // <
%left GREATER // >
%left LESS_EQ // <=
%left GREATER_EQ // >=
%token L_BRACE // (
%token R_BRACE // )
%token ARRAY_IND // :
%token COMA // ,
/*
// SYMBOL & CONST VALUE
*/
%token <id> pidentifier
%token <value> num
/*//////////////////////////////////////////////
//
// RULES
//
*//////////////////////////////////////////////
%%
line: %empty
| any ENDL line
;
any: DECLARE declarations
| BEGIN_P
| assignment ASSIGN expression END_EXPR {
expression_t *expr = malloc(sizeof(expression_t));
expression_get(expr);
// for debug purpose
print_expression(expr, stdout);
fprintf(stdout, ";\n");
////////////////////
i_graph_add_instruction(expr, i_EXPR);
}
| IF condition THEN {
expression_t *expr = malloc(sizeof(expression_t));
expression_get(expr);
// for debug purpose
fprintf(stdout, "IF ");
print_expression(expr, stdout);
fprintf(stdout, " THEN\n");
////////////////////
i_graph_add_instruction(expr, i_IF);
}
| ELSE {
// for debug purpose
fprintf(stdout, "ELSE\n");
////////////////////
i_graph_add_instruction(NULL, i_ELSE);
}
| ENDIF {
// for debug purpose
fprintf(stdout, "ENDIF\n");
////////////////////
i_graph_add_instruction(NULL, i_ENDIF);
}
| WHILE condition DO {
expression_t *expr = malloc(sizeof(expression_t));
expression_get(expr);
// for debug purpose
fprintf(stdout, "WHILE ");
print_expression(expr, stdout);
fprintf(stdout, " DO\n");
////////////////////
i_graph_add_instruction(expr, i_WHILE);
}
| ENDWHILE {
// for debug purpose
fprintf(stdout, "ENDWHILE\n");
////////////////////
i_graph_add_instruction(NULL, i_ENDWHILE);
}
| REPEAT {
// for debug purpose
fprintf(stdout, "REPEAT\n");
////////////////////
i_graph_add_instruction(NULL, i_REPEAT);
}
| UNTIL condition END_EXPR {
expression_t *expr = malloc(sizeof(expression_t));
expression_get(expr);
// for debug purpose
fprintf(stdout, "UNTIL ");
print_expression(expr, stdout);
fprintf(stdout, ";\n");
////////////////////
i_graph_add_instruction(expr, i_UNTIL);
}
| FOR pidentifier FROM value TO value DO {
for_loop_t *loop = malloc(sizeof(for_loop_t));
loop_get($2, loop);
loop->type = loop_TO;
// for debug purpose
fprintf(stdout, "FOR %s ", $2);
print_expression(&(loop->range_vars), stdout);
fprintf(stdout, " DO (TO LOOP)\n");
////////////////////
i_graph_add_instruction(loop, i_FOR);
}
| FOR pidentifier FROM value DOWNTO value DO {
for_loop_t *loop = malloc(sizeof(for_loop_t));
loop_get($2, loop);
loop->type = loop_DOWNTO;
// for debug purpose
fprintf(stdout, "FOR %s ", $2);
print_expression(&(loop->range_vars), stdout);
fprintf(stdout, " DO (DOWNTO LOOP)\n");
////////////////////
i_graph_add_instruction(loop, i_FOR);
}
| ENDFOR {
// for debug purpose
fprintf(stdout, "ENDFOR\n");
////////////////////
i_graph_add_instruction(NULL, i_ENDFOR);
}
| READ assignment END_EXPR {
expression_t *expr = malloc(sizeof(expression_t));
expression_get(expr);
// for debug purpose
expr->type = expr_READ;
fprintf(stdout, "READ ");
print_expression(expr, stdout);
fprintf(stdout, ";\n");
////////////////////
i_graph_add_instruction(expr, i_READ);
}
| WRITE value END_EXPR {
expression_t *expr = malloc(sizeof(expression_t));
expression_get(expr);
// for debug purpose
expr->type = expr_WRITE;
fprintf(stdout, "WRITE ");
print_expression(expr, stdout);
fprintf(stdout, ";\n");
////////////////////
i_graph_add_instruction(expr, i_WRITE);
}
| END {
i_graph_set_and_check(compiler_out);
i_graph_execute(NULL);
i_graph_free_all();
}
;
assignment: pidentifier {
expression_spin_reduce();
expression_set_var($1);
}
| pidentifier L_BRACE pidentifier R_BRACE {
expression_spin_reduce();
expression_set_var_arr_var($1, $3);
}
| pidentifier L_BRACE num R_BRACE {
expression_spin_reduce();
expression_set_var_arr_num($1, $3);
}
;
declarations: declarations COMA pidentifier {
declare_var($3);
}
| declarations COMA pidentifier L_BRACE num ARRAY_IND num R_BRACE {
declare_array($3, $5, $7);
}
| pidentifier {
declare_var($1);
}
| pidentifier L_BRACE num ARRAY_IND num R_BRACE {
declare_array($1, $3, $5);
}
;
expression: value {
expression_set_type((expr_type)expr_VALUE);
}
| value ADD value {
expression_set_type((expr_type)expr_ADD);
}
| value SUB value {
expression_set_type((expr_type)expr_SUB);
}
| value MUL value {
expression_set_type((expr_type)expr_MUL);
}
| value DIV value {
expression_set_type((expr_type)expr_DIV);
}
| value MOD value {
expression_set_type((expr_type)expr_MOD);
}
;
condition: value IS_EQUAL value {
expression_set_type((expr_type)cond_IS_EQUAL);
}
| value IS_N_EQUAL value {
expression_set_type((expr_type)cond_IS_N_EQUAL);
}
| value LESS value {
expression_set_type((expr_type)cond_LESS);
}
| value GREATER value {
expression_set_type((expr_type)cond_GREATER);
}
| value LESS_EQ value {
expression_set_type((expr_type)cond_LESS_EQ);
}
| value GREATER_EQ value {
expression_set_type((expr_type)cond_GREATER_EQ);
}
;
value: num {
expression_set_num($1);
}
| identifier
;
identifier: pidentifier {
expression_set_var($1);
}
| pidentifier L_BRACE pidentifier R_BRACE {
expression_set_var_arr_var($1, $3);
}
| pidentifier L_BRACE num R_BRACE {
expression_set_var_arr_num($1, $3);
}
;
%%
int yyerror(char *s) {
printf("%s\n", s);
rewind(yyin);
int line_ctr = 1;
char c;
while (line_ctr != yylineno && (c = fgetc(yyin)) != EOF) {
if (c == '\n') {
++line_ctr;
}
}
if (line_ctr == yylineno) {
fprintf(stderr, "Error in line: %d!\n", line_ctr);
while ((c = fgetc(yyin)) != '\n') {
fprintf(stderr, "\033[0;32m%c", c);
}
fprintf(stderr, "\n\033[0m");
} else {
fprintf(stderr, "[PARSER]: Something went wrong, line_ctr = %d!\n", line_ctr);
}
return 0;
}
void parse(FILE *in, FILE *out) {
yyin = in;
compiler_out = out;
yyparse();
}