forked from mukundsood1996/stfu
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ICG.y
296 lines (285 loc) · 8.19 KB
/
ICG.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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LIMIT 1024
// extern int lineno = 0;
void yyerror(const char*);
int yylex();
int temp_no = 0;
int label = 0;
FILE *outfile;
void arithmetic_gen(char op[5]);
void display_stack();
void push(char *);
char *pop();
void label_push(int);
char *label_pop();
typedef struct Stack {
char *items[LIMIT];
int top;
}Stack;
Stack stack;
Stack label_stack;
%}
%union
{
char string[128];
}
%token HASH INCLUDE DEFINE STDIO STDLIB MATH STRING TIME
%token IDENTIFIER INTEGER_LITERAL STRING_LITERAL HEADER_LITERAL FLOAT_LITERAL
%token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
%token ADD_ASSIGN SUB_ASSIGN
%token CHAR INT FLOAT VOID MAIN
%token STRUCT
%token FOR
%start translation_unit
%%
headers
: HASH INCLUDE HEADER_LITERAL
| HASH INCLUDE '<' libraries '>'
;
libraries
: STDIO
| STDLIB
| MATH
| STRING
| TIME
;
primary_expression
: IDENTIFIER {push($1.string); $$ = $1;}
| INTEGER_LITERAL {push($1.string); $$ = $1;}
| FLOAT_LITERAL {push($1.string); $$ = $1;}
| STRING_LITERAL {push($1.string); $$ = $1;}
| '(' expression ')' {$$=$1;}
;
postfix_expression
: primary_expression {$$=$1;}
| postfix_expression '(' ')'
| postfix_expression '.' IDENTIFIER
| postfix_expression INC_OP {push($1.string); push("1"); arithmetic_gen("+"); fprintf(outfile, "%s = %s\n", pop(), pop());}
| postfix_expression DEC_OP {push($1.string); push("1"); arithmetic_gen("-"); fprintf(outfile, "%s = %s\n", pop(), pop());}
| INC_OP primary_expression {push($2.string); push("1"); arithmetic_gen("+"); fprintf(outfile, "%s = %s\n", pop(), pop());}
| DEC_OP primary_expression {push($2.string); push("1"); arithmetic_gen("-"); fprintf(outfile, "%s = %s\n", pop(), pop());}
;
unary_expression
: postfix_expression {$$=$1;}
| '+' unary_expression {char temp[5]; strcpy(temp, pop()); push("0"); push(temp); arithmetic_gen("+");}
| '-' unary_expression {char temp[5]; strcpy(temp, pop()); push("0"); push(temp); arithmetic_gen("-");}
;
multiplicative_expression
: unary_expression
| multiplicative_expression '*' unary_expression {arithmetic_gen("*");}
| multiplicative_expression '/' unary_expression {arithmetic_gen("/");}
| multiplicative_expression '%' unary_expression {arithmetic_gen("%");}
;
additive_expression
: multiplicative_expression
| additive_expression '+' multiplicative_expression {arithmetic_gen("+");}
| additive_expression '-' multiplicative_expression {arithmetic_gen("-");}
;
relational_expression
: additive_expression
| relational_expression '<' additive_expression {arithmetic_gen("<");}
| relational_expression '>' additive_expression {arithmetic_gen(">");}
| relational_expression LE_OP additive_expression {arithmetic_gen("<=");}
| relational_expression GE_OP additive_expression {arithmetic_gen(">=");}
;
equality_expression
: relational_expression
| equality_expression EQ_OP relational_expression {arithmetic_gen("==");}
| equality_expression NE_OP relational_expression {arithmetic_gen("!=");}
;
conditional_expression
: equality_expression
| equality_expression {fprintf(outfile, "ifFalse %s goto L%d\n", pop(), ++label); char temp[5]; sprintf(temp, "t%d", temp_no++); push(temp);} '?' expression {gen_true_code();} ':' conditional_expression {gen_false_code();}
;
assignment_expression
: conditional_expression
| unary_expression '=' assignment_expression {fprintf(outfile, "%s = %s\n", pop(), pop());}
| unary_expression ADD_ASSIGN assignment_expression {arithmetic_gen("+"); fprintf(outfile, "%s = %s\n", $1.string, pop());}
| unary_expression SUB_ASSIGN assignment_expression {arithmetic_gen("-"); fprintf(outfile, "%s = %s\n", $1.string, pop());}
;
expression
: assignment_expression
| expression ',' assignment_expression
;
constant_expression
: conditional_expression /* with constraints */
;
declaration
: type_specifier ';'
| type_specifier init_declarator_list ';'
;
init_declarator_list
: init_declarator
| init_declarator_list ',' init_declarator
;
init_declarator
: IDENTIFIER {push($1.string); $$=$1;} '=' assignment_expression {fprintf(outfile, "%s = %s\n", $1.string, pop());}
// | IDENTIFIER '=' equality_expression {fprintf(outfile, "if not %s goto L%d\n", pop(), ++label);} '?' expression {fprintf(outfile, "%s = %s\n", $1.string, pop());fprintf(outfile, "goto L%d\n", ++label);} ':' conditional_expression {fprintf(outfile, "L%d :\n%s = %s\n", label-1, $1.string, pop()); pop(); fprintf(outfile, "goto L%d\n", label);}
| IDENTIFIER {push($1.string); $$=$1;}
;
type_specifier
: VOID
| CHAR
| INT
| FLOAT
| struct_specifier
;
struct_specifier
: STRUCT '{' struct_declaration_list '}'
| STRUCT IDENTIFIER '{' struct_declaration_list '}'
| STRUCT IDENTIFIER
;
struct_declaration_list
: struct_declaration
| struct_declaration_list struct_declaration
;
struct_declaration
: specifier_qualifier_list ';'
| specifier_qualifier_list struct_declarator_list ';'
;
specifier_qualifier_list
: type_specifier specifier_qualifier_list
| type_specifier
;
struct_declarator_list
: struct_declarator
| struct_declarator_list ',' struct_declarator
;
struct_declarator
: ':' constant_expression
| IDENTIFIER ':' constant_expression
| IDENTIFIER
;
statement
: compound_statement
| expression_statement
| iteration_statement
;
compound_statement
: '{' '}'
| '{' block_item_list '}'
;
block_item_list
: block_item
| block_item_list block_item
;
block_item
: declaration
| statement
;
expression_statement
: ';'
| expression ';'
;
iteration_statement
// FOR '(' expression_statement {fprintf(outfile, "L%d :\n", ++label);} expression_statement {fprintf(outfile, "ifFalse %s goto L%d\n", pop(), ++label);} ')' statement {fprintf(outfile, "goto L%d\nL%d", label-1, label);}
: FOR '(' expression_statement {fprintf(outfile, "L%d :\n", ++label);} expression_statement {fprintf(outfile, "ifFalse %s goto L%d\ngoto L%d\nL%d : \n", pop(), label+3, label+1, label+2); label_push(label+2); label_push(label+3); label_push(label+1); label_push(label);} expression {fprintf(outfile, "goto %s\n", label_pop()); label += 3;}')' {fprintf(outfile, "%s :\n", label_pop());} statement {fprintf(outfile, "goto %s\n%s : \n", label_pop(), label_pop());}
;
translation_unit
: external_declaration
| translation_unit external_declaration
;
external_declaration
: INT MAIN '(' ')' compound_statement
| declaration
| headers
;
%%
void yyerror(const char *str)
{
fflush(stdout);
fprintf(stderr, "%s at line\n", str);
}
int main(){
stack.top = -1;
push("$");
outfile = fopen("output_file.txt", "w");
if (yyparse() != 0)
{
printf("Parse failed\n");
exit(0);
}
printf("success\n");
int i = 0;
fclose(outfile);
system("cat output_file.txt");
return 0;
}
void push(char *str)
{
stack.items[++stack.top] = (char*)malloc(LIMIT);
strcpy(stack.items[stack.top], str);
}
char *pop()
{
if (stack.top <= -1) {
printf("\nError in evaluating expression\n");
exit(0);
}
char *str = (char*)malloc(LIMIT);
strcpy(str, stack.items[stack.top--]);
free(stack.items[stack.top+1]);
return str;
}
char *top(int off)
{
return stack.items[stack.top-off];
}
void arithmetic_gen(char op[5])
{
char temp[5];
sprintf(temp,"t%d",temp_no++);
fprintf(outfile,"%s = %s %s %s\n",temp,top(1),op,top(0));
pop(); pop(); push(temp);
}
void display_stack()
{
int i;
for(i=0; i<=stack.top; i++)
printf("%s ", stack.items[i]);
printf("\n");
}
void gen_true_code()
{
if (stack.top > -1)
{
fprintf(outfile, "%s = %s\ngoto L%d\n", top(0), pop(), label+1);
label_push(label+1);
fprintf(outfile, "L%d :\n", label);
label_push(label+1);
}
else
fprintf(outfile, "%s\ngoto L%d\n", pop(), ++label);
}
void gen_false_code()
{
if (stack.top > -1)
{
fprintf(outfile, "%s = %s\ngoto %s\n", top(0), pop(), label_pop());
fprintf(outfile, "%s :\n", label_pop());
label++;
}
else
fprintf(outfile, "%s\ngoto L%d\n", pop(), label-1);
}
void label_push(int label)
{
char temp[5];
sprintf(temp, "L%d", label);
label_stack.items[++label_stack.top] = malloc(LIMIT);
strcpy(label_stack.items[label_stack.top], temp);
}
char *label_pop()
{
if (label_stack.top <= -1) {
printf("\nError in evaluating expression\n");
exit(0);
}
char *str = (char*)malloc(LIMIT);
strcpy(str, label_stack.items[label_stack.top--]);
free(label_stack.items[label_stack.top+1]);
return str;
}