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
|
%{
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
%}
%union {
int intval;
char *strval;
struct CNode *cnode;
}
%error-verbose
%token IDENTIFIER "identifier" INT_CONST "integer constant" CHAR_CONST "character constant" STR_CONST "string constant"
%token KW_VOID "void" KW_CHAR "char" KW_INT "int" KW_STRUCT "struct" KW_UNION "union" KW_IF "if" KW_ELSE "else" KW_WHILE "while"
%token KW_FOR "for" KW_CONT "continue" KW_BREAK "break" KW_RET "ret" KW_SIZEOF "sizeof"
%token OPT_OR "||" OPT_AND "&&" OPT_EQ "==" OPT_NE "!=" OPT_LE "<=" OPT_GE ">=" OPT_SHL "<<" OPT_SHR ">>" OPT_INC "++" OPT_DEC "--" OPT_PTR "->"
%token ASS_MUL "*=" ASS_DIV "/=" ASS_MOD "%=" ASS_ADD "+=" ASS_SUB "-=" ASS_SHL "<<=" ASS_SHR ">>=" ASS_AND "&=" ASS_XOR "^=" ASS_OR "|="
%token UNKNOWN "stray character"
%token END 0 "end of file"
%type<intval> INT_CONST
%type<strval> IDENTIFIER STR_CONST CHAR_CONST
%type<intval> additive_operator assignment_operator equality_operator multiplicative_operator relational_operator shift_operator struct_or_union unary_operator
%type<cnode> additive_expression and_expression arguments array_initializer assignment_expression cast_expression comp_decls compound_statement comp_stmts constant_expression declaration declarator declarator_array declarators equality_expression exclusive_or_expression expression expression_statement function_definition identifier inclusive_or_expression init_declarator init_declarators initializer iteration_statement jump_statement logical_and_expression logical_or_expression multiplicative_expression optional_exp parameters plain_declaration plain_declarator postfix postfix_expression primary_expression prog_list program relational_expression selection_statement shift_expression statement struct_field struct_fields type_name type_specifier unary_expression
%start program
%%
program
: prog_list { ast_root = cnode_create_ast(cnode_list_wrap(PROG, $1)); }
prog_list
: declaration
| function_definition
| prog_list declaration { $$ = cnode_list_append($1, $2); }
| prog_list function_definition { $$ = cnode_list_append($1, $2); }
declaration
: type_specifier ';' {
$$ = cnode_add_loc(cnode_create_decl(
$1,
cnode_list_wrap(INIT_DECLRS, cnode_create_nop())), @$);
}
| type_specifier init_declarators ';' {
$$ = cnode_add_loc(cnode_create_decl(
$1,
cnode_add_loc(cnode_list_wrap(INIT_DECLRS, $2), @2)), @$);
}
function_definition
: type_specifier plain_declarator '(' parameters ')' compound_statement {
$$ = cnode_add_loc(cnode_create_func(
$1, $2, cnode_add_loc(
cnode_list_wrap(PARAMS, $4), @4), $6), @$);
}
| type_specifier plain_declarator '(' ')' compound_statement {
$$ = cnode_add_loc(cnode_create_func(
$1, $2,
cnode_list_wrap(PARAMS, cnode_create_nop()), $5), @$);
}
parameters
: plain_declaration
| parameters ',' plain_declaration { $$ = cnode_list_append($1, $3); }
declarators
: declarator
| declarators ',' declarator { $$ = cnode_list_append($1, $3); }
init_declarators
: init_declarator
| init_declarators ',' init_declarator { $$ = cnode_list_append($1, $3); }
init_declarator
: declarator { $$ = cnode_add_loc(cnode_create_init_declr($1, cnode_create_nop()), @$); }
| declarator '=' initializer { $$ = cnode_add_loc(cnode_create_init_declr($1, $3), @$); }
initializer
: assignment_expression { $$ = cnode_add_loc(cnode_create_initr(INITR_NORM, $1), @$); }
| '{' array_initializer '}' { $$ = cnode_add_loc(cnode_create_initr(INITR_ARR, $2), @$); }
array_initializer
: initializer
| array_initializer ',' initializer {
$$ = cnode_list_append(cnode_add_loc($1, @1), $3); }
type_specifier
: KW_VOID { $$ = cnode_add_loc(cnode_create_type_spec(KW_VOID, 0), @$); }
| KW_CHAR { $$ = cnode_add_loc(cnode_create_type_spec(KW_CHAR, 0), @$); }
| KW_INT { $$ = cnode_add_loc(cnode_create_type_spec(KW_INT, 0), @$); }
| struct_or_union identifier '{' struct_fields '}' {
$$ = cnode_add_loc(cnode_create_type_spec($1, 2, $2, cnode_add_loc(cnode_list_wrap(FIELDS, $4), @4)), @$);
}
| struct_or_union '{' struct_fields '}' {
$$ = cnode_add_loc(cnode_create_type_spec($1, 2, cnode_create_nop(), cnode_add_loc(cnode_list_wrap(FIELDS, $3), @3)), @$);
}
| struct_or_union identifier {
$$ = cnode_add_loc(cnode_create_type_spec($1, 2, $2, cnode_create_nop()), @$);
}
struct_fields
: struct_field
| struct_fields struct_field { $$ = cnode_list_append($1, $2); }
struct_field
: type_specifier declarators ';' {
$$ = cnode_add_loc(
cnode_create_struct_field($1,
cnode_add_loc(cnode_list_wrap(DECLRS, $2), @2)), @$);
}
struct_or_union
: KW_STRUCT { $$ = KW_STRUCT; }
| KW_UNION { $$ = KW_UNION; }
plain_declaration
: type_specifier declarator { $$ = cnode_add_loc(cnode_create_plain_decl($1, $2), @$); }
declarator
: plain_declarator '(' ')' {
$$ = cnode_add_loc(cnode_create_declr(
DECLR_FUNC, 2, $1,
cnode_list_wrap(PARAMS, cnode_create_nop())), @$);
}
| plain_declarator '(' parameters ')' {
$$ = cnode_add_loc(cnode_create_declr(
DECLR_FUNC, 2, $1,
cnode_add_loc(cnode_list_wrap(PARAMS, $3), @3)), @$);
}
| declarator_array
declarator_array
: plain_declarator
| declarator_array '[' constant_expression ']' {
$$ = cnode_add_loc(cnode_create_declr(DECLR_ARR, 2, $1, $3), @$);
}
plain_declarator
: identifier
| '*' plain_declarator {
$$ = cnode_add_loc(cnode_create_declr('*', 1, $2), @$); }
statement
: expression_statement
| compound_statement
| selection_statement
| iteration_statement
| jump_statement
expression_statement
: ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_EXP, 1, cnode_create_nop()), @$); }
| expression ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_EXP, 1, $1), @$); }
compound_statement
: '{' comp_decls comp_stmts '}' {
$$ = cnode_add_loc(
cnode_create_stmt(STMT_COMP, 2, cnode_add_loc(cnode_list_wrap(COMP_DECLS, $2), @2),
cnode_add_loc(cnode_list_wrap(COMP_STMTS, $3), @3)), @$);
}
comp_decls
: { $$ = cnode_create_nop(); }
| comp_decls declaration { $$ = cnode_list_append($1, $2); }
comp_stmts
: { $$ = cnode_create_nop(); }
| comp_stmts statement { $$ = cnode_list_append($1, $2); }
selection_statement
: KW_IF '(' expression ')' statement {
$$ = cnode_add_loc(
cnode_create_stmt(STMT_IF, 3, $3, $5, cnode_create_nop()), @$);
}
| KW_IF '(' expression ')' statement KW_ELSE statement {
$$ = cnode_add_loc(
cnode_create_stmt(STMT_IF, 3, $3, $5, $7), @$);
}
iteration_statement
: KW_WHILE '(' expression ')' statement {
$$ = cnode_add_loc(cnode_create_stmt(STMT_WHILE, 2, $3, $5), @$);
}
| KW_FOR '(' optional_exp ';' optional_exp ';' optional_exp ')' statement {
$$ = cnode_add_loc(cnode_create_stmt(STMT_FOR, 4, $3, $5, $7, $9), @$);
}
optional_exp
: { $$ = cnode_create_nop(); }
| expression
jump_statement
: KW_CONT ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_CONT, 0), @$); }
| KW_BREAK ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_BREAK, 0), @$); }
| KW_RET optional_exp ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_RET, 1, $2), @$); }
expression
: assignment_expression
| expression ',' assignment_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp(',', 2, $1, $3), @2); }
assignment_expression
: logical_or_expression
| unary_expression assignment_operator assignment_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); }
assignment_operator
: '=' { $$ = '='; }
| ASS_MUL { $$ = ASS_MUL; }
| ASS_DIV { $$ = ASS_DIV; }
| ASS_MOD { $$ = ASS_MOD; }
| ASS_ADD { $$ = ASS_ADD; }
| ASS_SUB { $$ = ASS_SUB; }
| ASS_SHL { $$ = ASS_SHL; }
| ASS_SHR { $$ = ASS_SHR; }
| ASS_AND { $$ = ASS_AND; }
| ASS_XOR { $$ = ASS_XOR; }
| ASS_OR { $$ = ASS_OR; }
constant_expression: logical_or_expression
logical_or_expression
: logical_and_expression
| logical_or_expression OPT_OR logical_and_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp(OPT_OR, 2, $1, $3), @2); }
logical_and_expression
: inclusive_or_expression
| logical_and_expression OPT_AND inclusive_or_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp(OPT_AND, 2, $1, $3), @2); }
inclusive_or_expression
: exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp('|', 2, $1, $3), @2); }
exclusive_or_expression
: and_expression
| exclusive_or_expression '^' and_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp('^', 2, $1, $3), @2); }
and_expression
: equality_expression
| and_expression '&' equality_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp('&', 2, $1, $3), @2); }
equality_expression
: relational_expression
| equality_expression equality_operator relational_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); }
equality_operator
: OPT_EQ { $$ = OPT_EQ; }
| OPT_NE { $$ = OPT_NE; }
relational_expression
: shift_expression
| relational_expression relational_operator shift_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); }
relational_operator
: '<' { $$ = '<'; }
| '>' { $$ = '>'; }
| OPT_LE { $$ = OPT_LE; }
| OPT_GE { $$ = OPT_GE; }
shift_expression
: additive_expression
| shift_expression shift_operator additive_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); }
shift_operator
: OPT_SHL { $$ = OPT_SHL; }
| OPT_SHR { $$ = OPT_SHR; }
additive_expression
: multiplicative_expression
| additive_expression additive_operator multiplicative_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); }
additive_operator
: '+' { $$ = '+'; }
| '-' { $$ = '-'; }
multiplicative_expression
: cast_expression
| multiplicative_expression multiplicative_operator cast_expression {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); }
multiplicative_operator
: '*' { $$ = '*'; }
| '/' { $$ = '/'; }
| '%' { $$ = '%'; }
cast_expression
: unary_expression
| '(' type_name ')' cast_expression {
$$ = cnode_add_loc(cnode_create_exp(EXP_CAST, 2, $2, $4), @$); }
type_name
: type_specifier
| type_name '*' { $$ = cnode_add_loc(cnode_create_exp('*', 1, $1), @$); }
unary_expression
: postfix_expression
| OPT_INC unary_expression { $$ = cnode_add_loc(cnode_create_exp(OPT_INC, 1, $2), @$); }
| OPT_DEC unary_expression { $$ = cnode_add_loc(cnode_create_exp(OPT_DEC, 1, $2), @$); }
| unary_operator cast_expression { $$ = cnode_add_loc(cnode_create_exp($1, 1, $2), @$); }
| KW_SIZEOF unary_expression { $$ = cnode_add_loc(cnode_create_exp(KW_SIZEOF, 1, $2), @$); }
| KW_SIZEOF '(' type_name ')' { $$ = cnode_add_loc(cnode_create_exp(KW_SIZEOF, 1, $3), @$); }
unary_operator
: '&' { $$ = '&'; }
| '*' { $$ = '*'; }
| '+' { $$ = '+'; }
| '-' { $$ = '-'; }
| '~' { $$ = '~'; }
| '!' { $$ = '!'; }
postfix_expression
: primary_expression
| postfix_expression postfix {
@$ = @2;
$$ = cnode_add_loc(cnode_create_exp(EXP_POSTFIX, 2, $1, $2), @2); }
postfix
: '[' expression ']' { $$ = cnode_add_loc(cnode_create_exp(POSTFIX_ARR, 1, $2), @$); }
| '(' arguments ')' {
$$ = cnode_add_loc(cnode_create_exp(
POSTFIX_CALL, 1,
cnode_add_loc(cnode_list_wrap(ARGS, $2), @2)), @$); }
| '(' ')' {
$$ = cnode_add_loc(cnode_create_exp(
POSTFIX_CALL, 1, cnode_list_wrap(ARGS, NULL)), @$); }
| '.' identifier { $$ = cnode_add_loc(cnode_create_exp(POSTFIX_DOT, 1, $2), @$); }
| OPT_PTR identifier { $$ = cnode_add_loc(cnode_create_exp(POSTFIX_PTR, 1, $2), @$); }
| OPT_INC { $$ = cnode_add_loc(cnode_create_exp(OPT_INC, 0), @$); }
| OPT_DEC { $$ = cnode_add_loc(cnode_create_exp(OPT_DEC, 0), @$); }
arguments
: assignment_expression
| arguments ',' assignment_expression { $$ = cnode_list_append($1, $3); }
primary_expression
: identifier
| INT_CONST { $$ = cnode_add_loc(cnode_create_int_const($1), @$); }
| CHAR_CONST { $$ = cnode_add_loc(cnode_create_char_const($1), @$); }
| STR_CONST { $$ = cnode_add_loc(cnode_create_str_const($1), @$); }
| '(' expression ')' { $$ = cnode_add_loc($2, @$); }
identifier
: IDENTIFIER { $$ = cnode_add_loc(cnode_create_identifier($1), @$); }
%%
|