forked from mukundsood1996/stfu
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAST.y
323 lines (264 loc) · 7.49 KB
/
AST.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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "header.h"
void yyerror(const char*);
int yylex();
/* prototypes */
int ex (nodeType *p, int flag);
/* function prototype to create a node for an operation */
nodeType *opr(int oper, int nops, ...);
/* function prototype to create a node for an identifier */
nodeType *id(char *identifier);
/* function prototype to create a node for a constat */
nodeType *con(char *value);
int if_assign = 1;
%}
%union
{
int ival;
nodeType *nPtr;
char string[128];
}
%token HASH INCLUDE DEFINE STDIO STDLIB MATH STRING TIME
%token IDENTIFIER INTEGER_LITERAL FLOAT_LITERAL STRING_LITERAL HEADER_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
%type <string> IDENTIFIER INTEGER_LITERAL FLOAT_LITERAL STRING_LITERAL
%type <nPtr> primary_expression postfix_expression multiplicative_expression
%type <nPtr> unary_expression additive_expression relational_expression
%type <nPtr> equality_expression conditional_expression assignment_expression
%type <nPtr> statement compound_statement expression_statement block_item
%type <nPtr> expression init_declarator init_declarator_list
%type <nPtr> iteration_statement block_item_list translation_unit
%type <nPtr> external_declaration declaration
%start translation_unit
%%
headers
: HASH INCLUDE HEADER_LITERAL
| HASH INCLUDE '<' libraries '>'
;
libraries
: STDIO
| STDLIB
| MATH
| STRING
| TIME
;
primary_expression
: IDENTIFIER {$$ = id($1);}
| INTEGER_LITERAL {$$ = con($1);}
| FLOAT_LITERAL {$$ = con($1);}
| STRING_LITERAL {$$ = con($1);}
| '(' expression ')' {$$ = $2;}
;
postfix_expression
: primary_expression {$$ = $1;}
| postfix_expression '(' ')'
| postfix_expression '.' IDENTIFIER {
char *tmp = strcat($1->id.name,".");
tmp = strcat(tmp, $3);
printf(" %s\n", tmp);
$$ = id(tmp);
}
| postfix_expression INC_OP {
$$ = opr('=', 2, $1, opr('+', 2, $1, con("1") ) );
}
| postfix_expression DEC_OP {
$$ = opr('=', 2, $1, opr('-', 2, $1, con("1") ) );
}
| INC_OP primary_expression {
$$ = opr('=', 2, $2, opr('+', 2, $2, con("1") ) );
}
| DEC_OP primary_expression {
$$ = opr('=', 2, $2, opr('-', 2, $2, con("1") ) );
}
;
unary_expression
: postfix_expression {$$ = $1;}
| '+' unary_expression {$$ = opr('+', 1, $2);}
| '-' unary_expression {$$ = opr('+', 1, $2);}
;
multiplicative_expression
: unary_expression {$$ = $1;}
| multiplicative_expression '*' unary_expression {$$ = opr('*', 2, $1, $3);}
| multiplicative_expression '/' unary_expression {$$ = opr('/', 2, $1, $3);}
| multiplicative_expression '%' unary_expression {$$ = opr('%', 2, $1, $3);}
;
additive_expression
: multiplicative_expression {$$ = $1;}
| additive_expression '+' multiplicative_expression {$$ = opr('+', 2, $1, $3);}
| additive_expression '-' multiplicative_expression {$$ = opr('-', 2, $1, $3);}
;
relational_expression
: additive_expression
| relational_expression '<' additive_expression {$$ = opr('<', 2, $1, $3);}
| relational_expression '>' additive_expression {$$ = opr('>', 2, $1, $3);}
| relational_expression LE_OP additive_expression {$$ = opr(LE_OP, 2, $1, $3);}
| relational_expression GE_OP additive_expression {$$ = opr(GE_OP, 2, $1, $3);}
;
equality_expression
: relational_expression {$$ = $1;}
| equality_expression EQ_OP relational_expression {$$ = opr(EQ_OP, 2, $1, $3);}
| equality_expression NE_OP relational_expression {$$ = opr(NE_OP, 2, $1, $3);}
;
conditional_expression
: equality_expression {$$ = $1;}
| equality_expression '?' expression ':' conditional_expression {$$ = opr('?', 2, $1, opr(':', 2, $3, $5) );}
;
assignment_expression
: conditional_expression {$$ = $1;}
| unary_expression '=' assignment_expression {$$ = opr('=', 2, $1, $3);}
| unary_expression ADD_ASSIGN assignment_expression {$$ = opr('=', 2, $1, opr('+', 2, $1, $3) );}
| unary_expression SUB_ASSIGN assignment_expression {$$ = opr('=', 2, $1, opr('-', 2, $1, $3) );}
;
expression
: assignment_expression {$$ = $1;}
| expression ',' assignment_expression
;
constant_expression
: conditional_expression {}
;
declaration
: type_specifier ';' //{$$ = opr(';', 1, $1);}
| type_specifier init_declarator_list ';' {$$ = opr(';', 1, $2);}
;
init_declarator_list
: init_declarator {$$ = $1;}
| init_declarator_list ',' init_declarator {$$ = opr(',', 2, $1, $3);}
;
init_declarator
: IDENTIFIER '=' assignment_expression {$$ = opr('=', 2, id($1), $3);}
| IDENTIFIER {$$ = id($1);}
;
type_specifier
: VOID
| CHAR
| INT
| FLOAT
| struct_specifier {if_assign = 0;}
;
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 ';' /* for anonymous struct/union */
| 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 {$$ = $1;}
| expression_statement {$$ = $1;}
| iteration_statement {$$ = $1;}
;
compound_statement
: '{' '}'
| '{' block_item_list '}' {$$ = $2;}
;
block_item_list
: block_item {$$ = $1;}
| block_item_list block_item {$$ = opr(';', 2, $1, $2);}
;
block_item
: declaration {$$ = $1;}
| statement {$$ = $1;}
;
expression_statement
: ';'
| expression ';' {$$ = $1;}
;
iteration_statement
: FOR '(' expression_statement expression_statement expression ')' statement {$$ = opr(FOR, 4, $3, $4, $5, $7);}
;
translation_unit
: external_declaration
| translation_unit external_declaration ;
external_declaration
: INT MAIN '(' ')' compound_statement {ex($5, 0); /*freeNode($2);*/}
| declaration {
if(if_assign)
{
ex($1, 2); /*freeNode($2);*/
}
}
| headers
;
%%
void yyerror(const char *str)
{
fflush(stdout);
fprintf(stderr, "*** %s\n", str);
}
int main(){
if(!yyparse())
{
printf("Successful\n");
}
else
printf("Unsuccessful\n");
return 0;
}
nodeType *con(char *value)
{
nodeType *p;
/* allocate node */
if ((p = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
/* copy information */
p->type = typeCon;
strcpy(p->con.value, value);
return p;
}
nodeType *id(char *identifier) {
nodeType *p;
/* allocate node */
if ((p = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
/* copy information */
p->type = typeId;
strcpy(p->id.name,identifier);
// printf("The copied identifier %s\n", p->id.name);
return p;
}
nodeType *opr(int oper, int nops, ...)
{
va_list ap;
nodeType *p;
int i;
/* allocate node, extending op array */
if ((p = malloc(sizeof(nodeType) +(nops-1) * sizeof(nodeType *))) == NULL)
yyerror("out of memory");
/* copy information */
p->type = typeOpr;
p->opr.oper = oper;
p->opr.nops = nops;
va_start(ap, nops);
for (i = 0; i < nops; i++)
p->opr.op[i] = va_arg(ap, nodeType*);
va_end(ap);
return p;
}