-
Notifications
You must be signed in to change notification settings - Fork 0
/
minic_parser.y
385 lines (318 loc) · 6.03 KB
/
minic_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
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
/* C Language Compiler Parser Created by Vincent Lin (https://github.com/vincnttt/) on 2021.06.11 */
%{
#include "lex.yy.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYERROR_VERBOSE 1
extern int yylineno;
int err_count = 0;
int yyerror(char *msg);
%}
%locations
%token IDENTIFIER STRING
%token CHAR_CONST DEC_CONST FLOAT_CONST
%token VOID INT SHORT LONG LLONG CHAR FLOAT DOUBLE SIGNED UNSIGNED
%token IF ELSE FOR WHILE SWITCH CASE DEFAULT BREAK CONTINUE RETURN
%token PRINTF SCANF
%token INCR DECR
%token ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN XOR_ASSIGN
%left '+' '-' '*' '/' '%' '^'
%left '<' '>' ',' LS_EQ GR_EQ
%left LOGIC_AND LOGIC_OR EQ NOT_EQ
%right '=' '!'
%nonassoc UMINUS
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
%start program
%%
program
: program builder
| builder
;
builder
: function
| declaration
;
function
: type IDENTIFIER '(' arg_list ')' compound_stmt
| type IDENTIFIER '(' arg_list error compound_stmt
| type IDENTIFIER error arg_list ')' compound_stmt
| type IDENTIFIER '(' VOID ')' compound_stmt
;
type
: datatype pointer
| datatype
;
pointer
: '*' pointer
| '*'
;
datatype
: sign_spec type_spec
| type_spec
;
sign_spec
: SIGNED
| UNSIGNED
;
type_spec
: VOID
| INT
| SHORT
| SHORT INT
| LONG
| LONG INT
| LLONG
| LLONG INT
| CHAR
| FLOAT
| DOUBLE
;
arg_list
: arguments
| /* Could be empty */
;
arguments
: arguments ',' arg
| arg
;
arg
: type identifier
;
stmt
: compound_stmt
| single_stmt
;
compound_stmt
: '{' statements '}'
;
statements
: statements stmt
| /* Could be empty */
;
output
: PRINTF '(' STRING ')' ';'
| PRINTF error STRING ')' ';'
| PRINTF '(' STRING error ';'
| PRINTF '(' STRING ')' error
| PRINTF '(' STRING ',' out_data ')' ';'
| PRINTF error STRING ',' out_data ')' ';'
| PRINTF '(' STRING error out_data ')' ';'
| PRINTF '(' STRING ',' out_data error ';'
| PRINTF '(' STRING ',' out_data ')' error
;
out_data
: IDENTIFIER
| out_data ',' arr_access
| out_data ',' IDENTIFIER
;
input
: SCANF '(' STRING ',' '&' IDENTIFIER ')' ';'
| SCANF error STRING ',' '&' IDENTIFIER ')' ';'
| SCANF '(' STRING error '&' IDENTIFIER ')' ';'
| SCANF '(' STRING ',' error IDENTIFIER ')' ';'
| SCANF '(' STRING ',' '&' IDENTIFIER error ';'
| SCANF '(' STRING ',' '&' IDENTIFIER ')' error
;
single_stmt
: declaration
| if_stmt
| for_stmt
| while_stmt
| switch_stmt
| output
| input
| func_call ';'
| func_call error
| BREAK ';'
| BREAK error
| CONTINUE ';'
| CONTINUE error
| RETURN ';'
| RETURN error
| RETURN sub_expr ';'
| RETURN sub_expr error
;
if_stmt
: IF '(' expr ')' stmt %prec LOWER_THAN_ELSE
| IF '(' expr ')' stmt ELSE stmt
;
for_stmt
: FOR '(' expr_stmt expr_stmt ')' stmt
| FOR '(' expr_stmt expr_stmt expr ')' stmt
;
while_stmt
: WHILE '(' expr ')' stmt
;
switch_stmt
: SWITCH '(' IDENTIFIER ')' '{' switch_expr '}'
;
switch_expr
: struct_case
;
struct_case
: cases
| CASE constant ':' stmt BREAK ';' struct_case
;
cases
: CASE constant ':' switch_expr
| end_case
;
end_case
: DEFAULT ':' stmt
;
declaration
: type decl_list ';'
| decl_list ';'
| unary_expr ';'
;
decl_list
: decl_list ',' sub_decl
| sub_decl
;
sub_decl
: assignment_expr
| identifier
| arr_access
;
arr_in
: single_arr
| multi_arr
;
single_arr
: '{' sub_arr '}'
;
sub_arr
: arr_index
| sub_arr ',' arr_index
;
multi_arr
: '{' sub_multi_arr '}'
;
sub_multi_arr
: single_arr
| sub_multi_arr ',' single_arr
;
expr_stmt
: expr ';'
| expr error
| ';'
;
expr
: expr ',' sub_expr
| sub_expr
;
sub_expr
: sub_expr '>' sub_expr
| sub_expr '<' sub_expr
| sub_expr LS_EQ sub_expr
| sub_expr GR_EQ sub_expr
| sub_expr EQ sub_expr
| sub_expr NOT_EQ sub_expr
| sub_expr LOGIC_AND sub_expr
| sub_expr LOGIC_OR sub_expr
| '!' sub_expr
| arithmetic_expr
| assignment_expr
| unary_expr
;
/* Left Hand Side of the equation*/
lhs
: identifier
| arr_access
;
identifier
: IDENTIFIER
;
assign_op
: '='
| ADD_ASSIGN
| SUB_ASSIGN
| MUL_ASSIGN
| DIV_ASSIGN
| MOD_ASSIGN
| XOR_ASSIGN
;
arithmetic_expr
: arithmetic_expr '+' arithmetic_expr
| arithmetic_expr '-' arithmetic_expr
| arithmetic_expr '*' arithmetic_expr
| arithmetic_expr '/' arithmetic_expr
| arithmetic_expr '%' arithmetic_expr
| arithmetic_expr '^' arithmetic_expr
| '(' arithmetic_expr ')'
| '-' arithmetic_expr %prec UMINUS
| identifier
| constant
;
assignment_expr
: lhs assign_op arithmetic_expr
| lhs assign_op arr_access
| lhs assign_op arr_in
| lhs assign_op func_call
| lhs assign_op unary_expr
| unary_expr assign_op unary_expr
;
unary_expr
: identifier INCR
| identifier DECR
| INCR identifier
| DECR identifier
;
constant
: CHAR_CONST
| DEC_CONST
| FLOAT_CONST
;
arr_access
: identifier '[' arr_index ']'
| identifier '[' arr_index ']' '[' arr_index ']'
;
arr_index
: constant
| identifier
;
func_call
: identifier '(' param_list ')'
| identifier '(' ')'
;
param_list
: param_list ',' param
| param
;
param
: sub_expr
| STRING
;
%%
int main(int argc, char *argv[]) {
yyin = fopen(argv[1], "r");
/* Display project logo */
printf("===========================================\n");
printf("\t __ __ _ _ _____ \n");
printf("\t | \\/ (_) (_)/ ____|\n");
printf("\t | \\ / |_ _ __ _| | \n");
printf("\t | |\\/| | | '_ \\| | | \n");
printf("\t | | | | | | | | | |____ \n");
printf("\t |_| |_|_|_| |_|_|\\_____|\n");
printf("===========================================\n\n");
if(!parsing_res()) {
printf("[SUCCESS]: No error(s) found.\n");
}
fclose(yyin);
return 0;
}
int parsing_res() {
err_count;
int status = yyparse();
if(status || err_count) {
printf("[FOUND]: %d error(s)\n", err_count);
return 3; /* yyparse will returns 0, 1 or 2 */
}
return 0;
}
int yyerror(char *msg) {
++err_count;
printf("[ERROR]: %s, in line %d\n", msg, yylineno);
}