-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc.y
339 lines (275 loc) · 11.5 KB
/
c.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
%{
#include <cstdio>
#include <iostream>
#include "ast.hpp"
using namespace std;
// stuff from flex that bison needs to know about:
extern "C" int yylex();
int yyparse();
extern "C" FILE *yyin;
void yyerror(const char *s);
// #define AST struct AST
AST* ast_root;
%}
%union{
AST* a;
char* s;
}
%token <s> IDENTIFIER I_CONSTANT F_CONSTANT STRING_LITERAL FUNC_NAME SIZEOF
%token <s> PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
%token <s> AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
%token <s> SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
%token <s> XOR_ASSIGN OR_ASSIGN
%token <s> TYPEDEF_NAME
%token <s> TYPEDEF EXTERN STATIC AUTO REGISTER INLINE
%token <s> CONST RESTRICT VOLATILE
%token <s> BOOL CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE VOID
%token <s> COMPLEX IMAGINARY
%token <s> STRUCT UNION ENUM ELLIPSIS
%token <s> CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
%token <s> ALIGNAS ALIGNOF ATOMIC GENERIC NORETURN STATIC_ASSERT THREAD_LOCAL
%type <a> primary_expression constant postfix_expression
%type <a> argument_expression_list unary_expression unary_operator multiplicative_expression
%type <a> additive_expression shift_expression relational_expression equality_expression and_expression
%type <a> exclusive_or_expression inclusive_or_expression logical_and_expression logical_or_expression
%type <a> conditional_expression assignment_expression expression
%type <a> cast_expression type_name
%type <a> declaration declaration_specifiers init_declarator_list init_declarator type_specifier type_qualifier
%type <a> declarator direct_declarator pointer type_qualifier_list parameter_type_list parameter_list
%type <a> parameter_declaration statement
%type <a> labeled_statement compound_statement block_item_list block_item expression_statement selection_statement
%type <a> iteration_statement jump_statement new_start translation_unit external_declaration function_definition
// %start translation_unit
%start new_start
%%
primary_expression
: IDENTIFIER { $$ = createAST(0, "ID", {}, $1); }
| constant
| STRING_LITERAL { $$ = createAST(0, "STR_LITERAL", {}, $1); }
| '(' expression ')' { $$ = $2; }
;
constant
: I_CONSTANT { $$ = createAST(0, "I_CONSTANT", {}, $1); } /* includes character_constant */
| F_CONSTANT { $$ = createAST(0, "F_CONSTANT", {}, $1); }
;
postfix_expression
: primary_expression
| postfix_expression '(' ')' { $$ = createAST(1, "fn_call", {$1, createAST(3, "argument_expression")}); }
| postfix_expression '(' argument_expression_list ')' { $$ = createAST(1, "fn_call", {$1, $3}); }
| postfix_expression INC_OP { AST* inc = createAST(0, "INC_OP"); $$ = createAST(1, "postfix_expression", {$1, inc}); }
| postfix_expression DEC_OP { AST* dec = createAST(0, "DEC_OP"); $$ = createAST(1, "postfix_expression", {$1, dec}); }
;
argument_expression_list
: assignment_expression { $$ = createAST(3, "argument_expression", {$1}); }
| argument_expression_list ',' assignment_expression { push($1, {$3}); $$ = $1; }
;
unary_expression
: postfix_expression
| INC_OP unary_expression { AST* inc = createAST(0, "INC_OP"); $$ = createAST(1, "unary_expression", {inc, $2}); }
| DEC_OP unary_expression { AST* dec = createAST(0, "DEC_OP"); $$ = createAST(1, "unary_expression", {dec, $2}); }
| unary_operator unary_expression { $$ = createAST(1, "unary_expression", {$1, $2}); }
;
unary_operator
: '&' {$$ = createAST(0, "&");}
| '*' {$$ = createAST(0, "*");}
| '+' {$$ = createAST(0, "+");}
| '-' {$$ = createAST(0, "-");}
| '~' {$$ = createAST(0, "~");}
| '!' {$$ = createAST(0, "!");}
;
multiplicative_expression
: unary_expression
| multiplicative_expression '*' unary_expression { $$ = createAST(1, "*", {$1, $3}); }
| multiplicative_expression '/' unary_expression { $$ = createAST(1, "/", {$1, $3}); }
| multiplicative_expression '%' unary_expression { $$ = createAST(1, "%", {$1, $3}); }
;
additive_expression
: multiplicative_expression
| additive_expression '+' multiplicative_expression { $$ = createAST(1, "+", {$1, $3}); }
| additive_expression '-' multiplicative_expression { $$ = createAST(1, "-", {$1, $3}); }
;
shift_expression
: additive_expression
| shift_expression LEFT_OP additive_expression { $$ = createAST(1, "LEFT_OP", {$1, $3}); }
| shift_expression RIGHT_OP additive_expression { $$ = createAST(1, "RIGHT_OP", {$1, $3}); }
;
relational_expression
: shift_expression
| relational_expression '<' shift_expression { $$ = createAST(1, "<", {$1, $3}); }
| relational_expression '>' shift_expression { $$ = createAST(1, ">", {$1, $3}); }
| relational_expression LE_OP shift_expression { $$ = createAST(1, "LE_OP", {$1, $3}); }
| relational_expression GE_OP shift_expression { $$ = createAST(1, "GE_OP", {$1, $3}); }
;
equality_expression
: relational_expression
| equality_expression EQ_OP relational_expression { $$ = createAST(1, "EQ_OP", {$1, $3}); }
| equality_expression NE_OP relational_expression { $$ = createAST(1, "NE_OP", {$1, $3}); }
;
and_expression
: equality_expression
| and_expression '&' equality_expression { $$ = createAST(1, "&", {$1, $3}); }
;
exclusive_or_expression
: and_expression
| exclusive_or_expression '^' and_expression { $$ = createAST(1, "^", {$1, $3}); }
;
inclusive_or_expression
: exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression { $$ = createAST(1, "|", {$1, $3}); }
;
logical_and_expression
: inclusive_or_expression
| logical_and_expression AND_OP inclusive_or_expression { $$ = createAST(1, "AND_OP", {$1, $3}); }
;
logical_or_expression
: logical_and_expression
| logical_or_expression OR_OP logical_and_expression { $$ = createAST(1, "OR_OP", {$1, $3}); }
;
conditional_expression
: logical_or_expression
| logical_or_expression '?' expression ':' conditional_expression { $$ = createAST(1, "?:", {$1, $3, $5}); }
;
cast_expression
: conditional_expression
| '(' type_name ')' cast_expression { $$ = createAST(1, "cast_expression", {$2, $4}); }
;
type_name
: declaration_specifiers pointer { $$ = createAST(1, "type_name", {$1, $2}); }
| declaration_specifiers { $$ = createAST(1, "type_name", {$1}); }
;
assignment_expression
// : conditional_expression
: cast_expression
| unary_expression '=' assignment_expression { $$ = createAST(1, "assignment", {$1, $3}); }
;
expression
: assignment_expression { $$ = createAST(3, "expression", {$1}); }
| expression ',' assignment_expression { push($1, {$3}); $$ = $1; }
;
declaration
: declaration_specifiers ';' { $$ = createAST(1, "declaration", {$1}); }
| declaration_specifiers init_declarator_list ';' { $$ = createAST(1, "declaration", {$1, $2}); }
;
declaration_specifiers
: declaration_specifiers type_specifier { if($1->node_string=="CONST") {$$ = createAST(3, "type", {$2});} else {push($1, {$2}); $$ = $1;} }
| type_specifier { $$ = createAST(3, "type", {$1}); }
| declaration_specifiers type_qualifier { $$ = $1; }
| type_qualifier
;
init_declarator_list
: init_declarator { $$ = createAST(3, "init_declarator", {$1}); }
| init_declarator_list ',' init_declarator { push($1, {$3}); $$ = $1; }
;
init_declarator
: declarator '=' assignment_expression { $$ = createAST(1, "=", {$1, $3}); }
| declarator
;
type_specifier
: VOID {$$ = createAST(0, "VOID");}
| CHAR {$$ = createAST(0, "CHAR");}
| SHORT {$$ = createAST(0, "SHORT");}
| INT {$$ = createAST(0, "INT");}
| LONG {$$ = createAST(0, "LONG");}
| FLOAT {$$ = createAST(0, "FLOAT");}
| DOUBLE {$$ = createAST(0, "DOUBLE");}
| SIGNED {$$ = createAST(0, "SIGNED");}
| UNSIGNED {$$ = createAST(0, "UNSIGNED");}
| BOOL {$$ = createAST(0, "BOOL");}
;
type_qualifier
: CONST { $$ = createAST(0, "CONST"); }
;
declarator
: pointer direct_declarator { if($2->node_string=="declarator") {push($2->children[0], $1->children); $$ = $2;} else {$$ = createAST(0, "declarator", {$1, $2});} }
| direct_declarator { if($1->node_string=="declarator") {$$ = $1;} else {$$ = createAST(0, "declarator", {createAST(0, "pointer"), $1});} }
;
direct_declarator
: IDENTIFIER { $$ = createAST(0, "ID", {}, $1); }
| '(' declarator ')' { $$ = $2; }
| direct_declarator '(' parameter_type_list ')' { $$ = createAST(1, "fn_declaration", {$1, $3}); }
| direct_declarator '(' ')' { $$ = createAST(1, "fn_declaration", {$1, createAST(3, "parameters")}); }
;
pointer
: '*' type_qualifier_list pointer { AST* star = createAST(0, "*"); push($3, {star}); $$ = $3; }
| '*' type_qualifier_list { AST* star = createAST(0, "*"); $$ = createAST(0, "pointer", {star}); }
| '*' pointer { AST* star = createAST(0, "*"); push($2, {star}); $$ = $2; }
| '*' { AST* star = createAST(0, "*"); $$ = createAST(0, "pointer", {star}); }
;
type_qualifier_list
: type_qualifier
| type_qualifier_list type_qualifier { $$ = createAST(3, "type_qualifier_list", {$1, $2}); }
; // ignored. not included in ast
parameter_type_list
: parameter_list ',' ELLIPSIS { AST* temp = createAST(0, "ELLIPSIS"); push($1, {temp}); $$ = $1; }
| parameter_list
;
parameter_list
: parameter_declaration { $$ = createAST(3, "parameters", {$1}); }
| parameter_list ',' parameter_declaration { push($1, {$3}); $$ = $1; }
;
parameter_declaration
: declaration_specifiers declarator { $$ = createAST(1, "parameter_declaration", {$1, $2}); }
| declaration_specifiers
;
statement
: labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
;
labeled_statement
: IDENTIFIER ':' statement { AST* temp = createAST(0, "ID", {}, $1); $$ = createAST(1, "labeled_statement", {temp, $3}); }
;
compound_statement
: '{' '}' { $$ = createAST(3, "compound_statement"); }
| '{' block_item_list '}' { $$ = $2; }
;
block_item_list
: block_item { $$ = createAST(3, "compound_statement", {$1}); }
| block_item_list block_item { push($1, {$2}); $$ = $1; }
;
block_item
: declaration
| statement
;
expression_statement
: ';' { $$ = createAST(0, ";"); }
| expression ';' { $$ = $1; }
;
selection_statement
: IF '(' expression ')' statement ELSE statement { $$ = createAST(1, "ifte", {$3, $5, $7}); }
| IF '(' expression ')' statement { $$ = createAST(1, "ifte", {$3, $5}); }
;
iteration_statement
: WHILE '(' expression ')' statement { $$ = createAST(1, "while", {$3, $5}); }
;
jump_statement
: GOTO IDENTIFIER ';' { AST* temp1 = createAST(0, "GOTO"); AST* temp2 = createAST(0, "ID", {}, $2); $$ = createAST(1, "jump", {temp1, temp2});}
| CONTINUE ';' { AST* temp1 = createAST(0, "CONTINUE"); $$ = createAST(1, "jump", {temp1});}
| BREAK ';' { AST* temp1 = createAST(0, "BREAK"); $$ = createAST(1, "jump", {temp1});}
| RETURN ';' { AST* temp1 = createAST(0, "RETURN"); $$ = createAST(1, "jump", {temp1});}
| RETURN expression ';' { AST* temp1 = createAST(0, "RETURN"); $$ = createAST(1, "jump", {temp1, $2});}
;
new_start
: translation_unit { ast_root = $1; $$ = $1; }
;
translation_unit
: external_declaration { $$ = createAST(1, "PROGRAM", {$1}); }
| translation_unit external_declaration { AST* root = $1; push(root, {$2}); $$ = root; }
;
external_declaration
: function_definition
| declaration { $$ = createAST(1, "global", {$1}); }
;
function_definition
: declaration_specifiers declarator compound_statement { $$ = createAST(1, "FUNC", {$1, $2, $3}); }
;
%%
#include <stdio.h>
void yyerror(const char *s)
{
fflush(stdout);
fprintf(stderr, "*** %s\n", s);
}