-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyParser.y
400 lines (335 loc) · 9.22 KB
/
myParser.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int yylex();
void yyerror (char *);
extern FILE * yyin;
extern int * yylineno;
extern void substring(char*, char*, int, int);
// identifiers
char ids[256][256] = {0};
void addId(char *);
int idExists(char *);
// functions
char funcs[256][256] = {0};
void addFunc(char *);
int funcExists(char *);
// structs
char structs[256][256] = {0};
void addStruct(char *);
int structExists(char *);
// type names
char typeNames[256][256] = {0};
void addTypeName(char *);
int typeNameExists(char *);
void undeclaredError(char * id);
%}
%union
{
int intValue;
char *stringValue;
}
%token DIGITS
%token CHARACTER
%token PROGRAM
%token FUNCTION
%token VARS
%token CHAR
%token INTEGER
%token END_FUNCTION
%token RETURN
%token STARTMAIN
%token ENDMAIN
%token IF
%token THEN
%token ELSEIF
%token ENDIF
%token WHILE
%token ENDWHILE
%token FOR
%token TO
%token STEP
%token ENDFOR
%token BREAK
%token PRINT
%token SWITCH
%token CASE
%token DEFAULT
%token ENDSWITCH
%token AND
%token OR
%token VARNAME
%token SEMICOLON
%token STRING
%token COMP
%token ELSE
%token STRUCT
%token ENDSTRUCT
%token TYPEDEF
%start file
%%
file: PROGRAM structs functions main {;}
| PROGRAM functions main {;}
| PROGRAM structs main {;}
| PROGRAM main {;}
;
structs: struct
| struct structs
;
declarations: VARS declaration
| declarations declaration
;
struct: STRUCT declarations ENDSTRUCT {char newId[256]; substring(newId, $<stringValue>1, 7, strlen($<stringValue>1)-1); addStruct(newId);}
| TYPEDEF STRUCT declarations VARNAME ENDSTRUCT {char newId[256]; substring(newId, $<stringValue>2, 7, strlen($<stringValue>2)-1);if(strcmp(newId, $<stringValue>4) == 0){addStruct(newId);addTypeName(newId);}else{yyerror("TYPEDEF STRUCT name different than ENDSTRUCT name!");YYERROR;}}
;
main: STARTMAIN declarations block ENDMAIN {;}
| STARTMAIN declarations ENDMAIN {;}
| STARTMAIN block ENDMAIN {;}
| STARTMAIN ENDMAIN {;}
;
block: statement {$<intValue>$ = $<intValue>1;}
| statement block {$<intValue>$ = $<intValue>1 || $<intValue>2;}
;
statement: print {$<intValue>$ = 0;}
| assignment {$<intValue>$ = 0;}
| if {$<intValue>$ = 0;}
| for {$<intValue>$ = 0;}
| while {$<intValue>$ = 0;}
| switch {$<intValue>$ = 0;}
| break {$<intValue>$ = 0;}
| invocation SEMICOLON {$<intValue>$ = 0;}
| return {$<intValue>$ = 1;}
;
print: PRINT '(' STRING ')' SEMICOLON {;}
| PRINT '(' STRING ',' print_var_list ')' SEMICOLON {;}
;
invocation: VARNAME '(' parameter_list ')' {if(funcExists($<stringValue>1) == 0){undeclaredError($<stringValue>1);YYERROR;}}
parameter_list: expression {;}
| expression ',' parameter_list {;}
;
declaration: var_type var_list SEMICOLON {;}
assignment: VARNAME '=' expression SEMICOLON {if(idExists($<stringValue>1) == 0){undeclaredError($<stringValue>1);YYERROR;}}
| array '=' expression SEMICOLON {if(idExists($<stringValue>1) == 0){undeclaredError($<stringValue>1);YYERROR;}}
;
expression: DIGITS {;}
| array {if(idExists($<stringValue>1) == 0){undeclaredError($<stringValue>1);YYERROR;}}
| CHARACTER {;}
| VARNAME {if(idExists($<stringValue>1) == 0){undeclaredError($<stringValue>1);YYERROR;}}
| expression operator expression {;}
| '(' expression ')' {;}
| invocation {;}
;
var_list: VARNAME {addId($<stringValue>1);}
| array {addId($<stringValue>1);}
| var_list ',' VARNAME {addId($<stringValue>3);}
| var_list ',' array {addId($<stringValue>3);}
;
print_var_list: VARNAME {if(idExists($<stringValue>1) == 0){undeclaredError($<stringValue>1);YYERROR;}}
| array {if(idExists($<stringValue>1) == 0){undeclaredError($<stringValue>1);YYERROR;}}
| var_list ',' VARNAME {if(idExists($<stringValue>3) == 0){undeclaredError($<stringValue>3);YYERROR;}}
| var_list ',' array {if(idExists($<stringValue>3) == 0){undeclaredError($<stringValue>3);YYERROR;}}
;
operator: '+' {;}
| '-' {;}
| '*' {;}
| '/' {;}
| '^' {;}
;
array: VARNAME '[' DIGITS ']' {$<stringValue>$ = $<stringValue>1;}
| VARNAME '[' VARNAME ']' {$<stringValue>$ = $<stringValue>1; if(idExists($<stringValue>3) == 0){undeclaredError($<stringValue>3);YYERROR;}}
;
functions: function {;}
| function functions {;}
;
function: FUNCTION VARNAME '(' param_declaration ')' declarations block END_FUNCTION {if($<intValue>7 == 0){yyerror("function does not return!");YYERROR;}addFunc($<stringValue>2);}
| FUNCTION VARNAME '(' param_declaration ')' declarations END_FUNCTION {yyerror("function does not return!");YYERROR;}
| FUNCTION VARNAME '(' param_declaration ')' END_FUNCTION {yyerror("empty functions are not allowed!");YYERROR;}
| FUNCTION VARNAME '(' param_declaration ')' block END_FUNCTION {if($<intValue>6 == 0){yyerror("function does not return!");YYERROR;}addFunc($<stringValue>2);}
;
param_declaration: var_type VARNAME {addId($<stringValue>2);}
| var_type array {addId($<stringValue>2);}
| var_type array ',' param_declaration {addId($<stringValue>2);}
| var_type VARNAME ',' param_declaration {addId($<stringValue>2);}
;
var_type: CHAR {;}
| INTEGER {;}
| VARNAME {if(typeNameExists($<stringValue>1) == 0){undeclaredError($<stringValue>1);YYERROR;}}
;
return: RETURN expression SEMICOLON {;}
;
break: BREAK SEMICOLON {;}
;
bool_op: AND {;}
| OR {;}
;
condition: expression COMP expression {;}
| condition bool_op condition {;}
;
while: WHILE '(' condition ')' block ENDWHILE {;}
;
if: IF '(' condition ')' THEN block ENDIF {;}
| IF '(' condition ')' THEN block ELSE block ENDIF {;}
| IF '(' condition ')' THEN block elseifs ENDIF {;}
| IF '(' condition ')' THEN block elseifs ELSE block ENDIF {;}
;
elseifs: ELSEIF '(' condition ')' block {;}
| ELSEIF '(' condition ')' block elseifs {;}
;
cases: CASE '(' expression ')' ':' block {;}
| CASE '(' expression ')' ':' block cases {;}
;
switch: SWITCH '(' expression ')' cases ENDSWITCH
| SWITCH '(' expression ')' cases DEFAULT ':' block ENDSWITCH
;
for: FOR VARNAME ':' '=' DIGITS TO DIGITS STEP DIGITS block ENDFOR
| FOR VARNAME ':' '=' DIGITS TO DIGITS STEP DIGITS ENDFOR
;
%%
void yyerror(char *s) {
fprintf(stderr, "at line %d: %s\n", yylineno, s);
}
void catFile(char* filename){
FILE * fp = fopen(filename, "r");
char next = fgetc(fp);
while (next != EOF)
{
printf ("%c", next);
next = fgetc(fp);
}
fclose(fp);
printf("\n");
}
int main(int argc, char **argv){
// print file contents
catFile(argv[1]);
// redirect input to file
yyin = fopen(argv[1],"r");
yyparse();
return 0;
}
void addId(char * newId){
int exists = 0;
// find first empty id
int i = 0;
while(ids[i][0] != 0){
i++;
if(strcmp(ids[i], newId) == 0){
exists = 1;
}
}
// only add it if it doesnt already exist
if(exists==0){
// add id
int j = 0;
while(newId[j] != '\0'){
ids[i][j] = newId[j];
j++;
}
}
}
int idExists(char * id){
int i = 0;
while(ids[i][0] != 0){
if(strcmp(ids[i], id) == 0){
return 1;
}
i++;
}
return 0;
}
void addFunc(char * newFunc){
int exists = 0;
// find first empty id
int i = 0;
while(funcs[i][0] != 0){
i++;
if(strcmp(funcs[i], newFunc) == 0){
exists = 1;
}
}
// only add it if it doesnt already exist
if(exists==0){
// add id
int j = 0;
while(newFunc[j] != '\0'){
funcs[i][j] = newFunc[j];
j++;
}
}
}
int funcExists(char * func){
int i = 0;
while(funcs[i][0] != 0){
if(strcmp(funcs[i], func) == 0){
return 1;
}
i++;
}
return 0;
}
void addStruct(char * newStruct){
int exists = 0;
// find first empty id
int i = 0;
while(structs[i][0] != 0){
i++;
if(strcmp(structs[i], newStruct) == 0){
exists = 1;
}
}
// only add it if it doesnt already exist
if(exists==0){
// add id
int j = 0;
while(newStruct[j] != '\0'){
structs[i][j] = newStruct[j];
j++;
}
}
}
int structExists(char * struc){
int i = 0;
while(structs[i][0] != 0){
if(strcmp(structs[i], struc) == 0){
return 1;
}
i++;
}
return 0;
}
void addTypeName(char * newTypeName){
int exists = 0;
// find first empty id
int i = 0;
while(typeNames[i][0] != 0){
i++;
if(strcmp(typeNames[i], newTypeName) == 0){
exists = 1;
}
}
// only add it if it doesnt already exist
if(exists==0){
// add id
int j = 0;
while(newTypeName[j] != '\0'){
typeNames[i][j] = newTypeName[j];
j++;
}
}
}
int typeNameExists(char * typeName){
int i = 0;
while(typeNames[i][0] != 0){
if(strcmp(typeNames[i], typeName) == 0){
return 1;
}
i++;
}
return 0;
}
void undeclaredError(char * id){
char message[270] = "undeclared identifier: ";
yyerror(strcat(message, id));
}