-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcminus.y
319 lines (267 loc) · 11.9 KB
/
cminus.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
/* C-Minus BNF Grammar */
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "asm.h"
#include "symboltable.h"
extern int yylineno;
int yylex();
void yyerror(const char *s);
//for symTable.h
extern struct symType parsedsymType;
extern struct symTable globalSymTable;
extern struct symTable *CurrentScope;
//for asm.h
extern int count;
extern int params;
extern int vaList[3];
extern int vaNum;
extern FILE *x86;
int TextSection = 0; //if textsection = 0 then create it
//prevents multiple text sections to be created
%}
%union {
int n;
char s[15];
}
%token ELSE
%token IF
%token INT
%token RETURN
%token VOID
%token WHILE
%token ID
%token NUM
%token LTE
%token GTE
%token EQUAL
%token NOTEQUAL
%type<n> NUM call factor term expression type_specifier simple_expression additive_expression addop mulop relop iteration_stmt selection_stmt if_stmt
%type<s> ID var var_declaration
%%
program : declaration_list {endScope();}; //once the rule is met need to close the golobal scope
declaration_list : declaration_list declaration
| declaration
;
declaration : var_declaration
| fun_declaration
;
var_declaration : type_specifier ID ';' {
insertSym($2, parsedsymType, VAR);
strcpy($$, $2);
resetparsedsymType();
}
| type_specifier ID '[' NUM ']' ';' {
parsedsymType.array = 1;
parsedsymType.arrSize = $4;
insertSym($2, parsedsymType, VAR);
strcpy($$, $2);
resetparsedsymType();
}
;
type_specifier : INT {$$ = 0; parsedsymType.type = 0;}
| VOID {$$ = 1; parsedsymType.type = 1;}
;
fun_declaration : type_specifier ID '(' {
if(!TextSection++) fprintf(x86, "\nSECTION .text\n");
//decalre .text if has not been declared yet
declaration(FUNC, $2);
startScope();
}
params ')' {
parsedsymType.type = $1;
parsedsymType.parameters = params;
insertGlobalSym($2, parsedsymType, FUNC);
resetparsedsymType();
params=0;
}
compound_stmt {
endScope();
if($1 == 1)
epi();
freeAllReg();
}
;
params : param_list | VOID ;
param_list : param_list ',' param {params++; }
| param {params++; }
;
param : type_specifier ID {
parsedsymType.parameters = params + 1;
parsedsymType.initialized = 1;
insertSym($2, parsedsymType, VAR);
resetparsedsymType();
}
| type_specifier ID '[' ']' {
parsedsymType.parameters = params + 1;
parsedsymType.array = 1;
parsedsymType.initialized = 1;
insertSym($2, parsedsymType, VAR);
resetparsedsymType();
}
;
compound_stmt : '{' { //start scope b4 pararms read
if(!inFunctionBody())
startScope();
}
local_declarations statement_list '}' {if(!inFunctionBody())
endScope();}
;
local_declarations : local_declarations var_declaration {declaration(VAR, $2);}
| /* empty */ ;
statement_list : statement_list statement
| /* empty */ ;
statement : expression_stmt
| compound_stmt
| selection_stmt
| iteration_stmt
| return_stmt ;
expression_stmt : expression ';'
| ';'
;
selection_stmt : if_stmt statement {fprintf(x86, "EndIf%i:\n", $1);}
| if_stmt statement ELSE {fprintf(x86, "jmp EndIfElse%i\n", $1);
fprintf(x86, "EndIf%i:\n", $1);
}
statement {fprintf(x86, "EndIfElse%i:\n", $1);}
;
/*
this was the oriinal code but bison gave a error so "rule useless in parser due to conflicts", this was
due to bison only looks ahead one production rule. But from searching people had a similar proble and the issues was
"two productions for statement both have code blocks before the parser has read any tokens to know which production is going to apply."
hence a new production rule was added
IF '(' expression ')' {
$<n>$ = count; count++;
fprintf(x86, "cmp %s, 1\n", stringify($3));
fprintf(x86, "jne EndIf%i\n", $<n>$);
freeReg();
}
statement {fprintf(x86, "EndIf%i:\n", $<n>1);}
| IF '(' expression ')' {
$<n>$ = count; count++;
fprintf(x86, "cmp %s, 1\n", stringify($3));
fprintf(x86, "jne EndIf%i\n", $<n>$);
freeReg();
}
statement ELSE {
fprintf(x86, "jmp EndIfElse%i\n", $<n>1);
fprintf(x86, "EndIf%i:\n", $<n>1);
}
statement {fprintf(x86, "EndIfElse%i:\n", $<n>1);}
;
*/
if_stmt : IF '(' expression ')' {
$$ = count; count++;
fprintf(x86, "cmp %s, 1\n", stringify($3));
fprintf(x86, "jne EndIf%i\n", $$);
freeReg();
}
;
iteration_stmt : WHILE {
$<n>$ = count; count++; //$<n>$ used beacsue its a midrule
fprintf(x86, "While%i:\n", $<n>1);
}
'(' expression ')' {
fprintf(x86, "cmp %s, 1\n", stringify($<n>3));
fprintf(x86, "jne EndWhile%i\n", $<n>1);
freeReg();
}
statement {
fprintf(x86, "jmp While%i\n", $<n>1);
fprintf(x86, "EndWhile%i:\n", $<n>1);
}
;
/*If a similar production rule to if_stmt was made, then the code should have no issue running a nested
while loop, but when compiling bison can only look one ahead, this causes a usless rule to be formed when
makeing a nestled while loop and causes a conflic in parser*/
return_stmt : RETURN ';' {epi();}
| RETURN expression ';' {
if($2 == EAX){
epi();
}
else{
fprintf(x86, "mov eax, %s\n", stringify($2));
epi();
}
freeReg();
}
;
expression : var '=' expression {$$=9;
memOp(STORE,$1,$3);
freeReg();
}
| simple_expression {$$ = $1;}
;
var : ID {findSym($1)->attr.references++; strcpy($$, $1);}
| ID '[' expression ']' {
struct symbolEntry *tmp = findSym($1);
if(!tmp->attr.array)
printf("error - %s is not an array", tmp->id);
tmp->attr.references++;
tmp->attr.regContainingArrIndex = $3;
strcpy($$, $1);
}
;
simple_expression : additive_expression relop additive_expression {
$$=$1;
boolOp($2,$1,$3);
freeReg();
}
| additive_expression {$$ = $1;
}
;
relop : LTE {$$=LTEQU;}| '<'{$$=LESS;} | '>' {$$=GTR;}| GTE{$$=GTEQU;} | EQUAL{$$=EQU;} | NOTEQUAL {$$=NEQU;};
additive_expression : additive_expression addop term {
$$ = $1;
mathOp($2,$1,$3);
freeReg();
}
| term {$$ = $1;}
;
addop : '+' {$$ = ADD;}
| '-' {$$ = SUB;}
;
term : term mulop factor {$$ = $1;
mathOp($2,$1,$3);
freeReg();
}
| factor {$$ = $1;}
;
mulop : '*' {$$ = MULT;}
| '/' {$$ = DIV;}
;
factor : '(' expression ')' {$$ = $2;}
| var {
$$ = availReg();
memOp(LOAD,$1,$$);
}
| call {$$ = $1;}
| NUM {$$=availReg();
storeVar($$, $1);
}
;
call : ID '(' args ')' {
call($1, vaList);
$$=availReg();
params=0;
}
;
args : arg_list | /* empty */ ;
arg_list : arg_list ',' expression {vaList[params++] = $3;}
| expression {vaList[params++] = $1;}
;
%%
int main(){
x86 = fopen ("output.asm", "w");
if(!yyparse())
printf("\nParsing complete\n");
else
printf("\nParsing failed\n");
fclose(x86);
return 0;
}
void yyerror (char const *s)
{
fprintf (stderr, "%s, line:%i\n", s, yylineno);
}