forked from fengcc/ProgramSlicing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbison_c.y
367 lines (314 loc) · 17.6 KB
/
bison_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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
%{
#include <stdio.h>
#include <stdlib.h>
#include "ast_node.h"
void yyerror(char *s);
%}
%union {
struct AstNode *a;
double d;
char s[32];
}
%token AUTO DEFINE EXTERN REGISTER STATIC TYPEDEF
%token CHAR DOUBLE FLOAT INT LONG SHORT SIGNED UNSIGNED VOID VOLATILE
%token BREAK CASE CONTINUE DEFAULT DO ELSE FOR GOTO IF RETURN SWITCH WHILE
%token ENUM STRUCT UNION SIZEOF
%token <d> NUMBER
%token <s> IDENTIFIER CHARACTER STRING_CONSTANT
%token ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN INC_OP DEC_OP AND_OP OR_OP LE_OP GE_OP EQ_OP NE_OP
%type <a> primary_expression postfix_expression argument_expression_list unary_expression unary_operator cast_expression
%type <a> type_name multiplicative_expression additive_expression relational_expression equality_expression and_expression
%type <a> exclusive_or_expression inclusive_or_expression logical_and_expression logical_or_expression expression
%type <a> declaration init_declarator_list init_declarator declarator initializer
%type <a> statement assignment_statement expression_statement selection_statement iteration_statement jump_statement
%type <a> translation_unit function_definition compound_statement block_item_list block_item
%start translation_unit
%nonassoc LOWER_THAN_ELSE /* 定义优先级,解决二义性 */
%nonassoc ELSE
%%
primary_expression
: IDENTIFIER /*标识符*/ { $$ = newAstNode(Identifier, newNodeValue(0, $1), linesno); }
| NUMBER /*数字*/ { $$ = newAstNode(Number, newNodeValue(1, $1), linesno); }
| CHARACTER /*字符*/ { $$ = newAstNode(String, newNodeValue(0, $1), linesno); }
| STRING_CONSTANT /*字符串常量*/ { $$ = newAstNode(String, newNodeValue(0, $1), linesno); }
| '(' expression ')' { $$ = newAstNode(Expression, newNodeValue(0, "primary_expression"), linesno);
AstNode *left_bracket = newAstNode(Operator, newNodeValue(0, "("), linesno);
AstNode *right_bracket = newAstNode(Operator, newNodeValue(0, ")"), linesno);
linkAstNode($$, left_bracket, $2, right_bracket, NULL); }
;
postfix_expression /*数组,自定义函数 均未考虑*/
: primary_expression { $$ = $1; }
| postfix_expression INC_OP { $$ = newAstNode(Expression, newNodeValue(0, "postfix_expression"), linesno);
AstNode *inc_op = newAstNode(Operator, newNodeValue(0, "++"), linesno);
linkAstNode($$, $1, inc_op, NULL); }
| postfix_expression DEC_OP { $$ = newAstNode(Expression, newNodeValue(0, "postfix_expression"), linesno);
AstNode *dec_op = newAstNode(Operator, newNodeValue(0, "--"), linesno);
linkAstNode($$, $1, dec_op, NULL); }
| postfix_expression '(' ')' /*无参库函数调用*/
{ $$ = newAstNode(Expression, newNodeValue(0, "postfix_expression"), linesno);
AstNode *left_bracket = newAstNode(Operator, newNodeValue(0, "("), linesno);
AstNode *right_bracket = newAstNode(Operator, newNodeValue(0, ")"), linesno);
linkAstNode($$, $1, left_bracket, right_bracket, NULL); }
| postfix_expression '(' argument_expression_list ')' /*有参库函数调用*/
{ $$ = newAstNode(Expression, newNodeValue(0, "postfix_expression"), linesno);
AstNode *left_bracket = newAstNode(Operator, newNodeValue(0, "("), linesno);
AstNode *right_bracket = newAstNode(Operator, newNodeValue(0, ")"), linesno);
linkAstNode($$, $1, left_bracket, $3, right_bracket, NULL); }
;
unary_expression
: postfix_expression { $$ = $1; }
| INC_OP unary_expression { $$ = newAstNode(Expression, newNodeValue(0, "unary_expression"), linesno);
AstNode *inc_op = newAstNode(Operator, newNodeValue(0, "++"), linesno);
linkAstNode($$, inc_op, $2, NULL); }
| DEC_OP unary_expression { $$ = newAstNode(Expression, newNodeValue(0, "unary_expression"), linesno);
AstNode *dec_op = newAstNode(Operator, newNodeValue(0, "--"), linesno);
linkAstNode($$, dec_op, $2, NULL); }
| unary_operator cast_expression { $$ = newAstNode(Expression, newNodeValue(0, "unary_expression"), linesno);
linkAstNode($$, $1, $2, NULL); }
;
unary_operator /*未考虑指针和取地址符*/
: '+' { $$ = newAstNode(Operator, newNodeValue(0, "+"), linesno); }
| '-' { $$ = newAstNode(Operator, newNodeValue(0, "-"), linesno); }
| '~' { $$ = newAstNode(Operator, newNodeValue(0, "~"), linesno); }
| '!' { $$ = newAstNode(Operator, newNodeValue(0, "!"), linesno); }
| '&' { $$ = newAstNode(Operator, newNodeValue(0, "&"), linesno); }
;
cast_expression
: unary_expression { $$ = $1; }
| '(' type_name ')' cast_expression /*强制类型转换*/
{ $$ = newAstNode(Expression, newNodeValue(0, "cast_expression"), linesno);
AstNode *left_bracket = newAstNode(Operator, newNodeValue(0, "("), linesno);
AstNode *right_bracket = newAstNode(Operator, newNodeValue(0, ")"), linesno);
linkAstNode($$, left_bracket, $2, right_bracket, $4, NULL); }
;
multiplicative_expression
: cast_expression { $$ = $1; }
| multiplicative_expression '*' cast_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "multiplicative_expression"), linesno);
AstNode *multiply = newAstNode(Operator, newNodeValue(0, "*"), linesno);
linkAstNode($$, $1, multiply, $3, NULL); }
| multiplicative_expression '/' cast_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "multiplicative_expression"), linesno);
AstNode *divide = newAstNode(Operator, newNodeValue(0, "/"), linesno);
linkAstNode($$, $1, divide, $3, NULL); }
| multiplicative_expression '%' cast_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "multiplicative_expression"), linesno);
AstNode *divide = newAstNode(Operator, newNodeValue(0, "%"), linesno);
linkAstNode($$, $1, divide, $3, NULL); }
;
additive_expression
: multiplicative_expression { $$ = $1; }
| additive_expression '+' multiplicative_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "additive_expression"), linesno);
AstNode *add = newAstNode(Operator, newNodeValue(0, "+"), linesno);
linkAstNode($$, $1, add, $3, NULL); }
| additive_expression '-' multiplicative_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "additive_expression"), linesno);
AstNode *sub = newAstNode(Operator, newNodeValue(0, "-"), linesno);
linkAstNode($$, $1, sub, $3, NULL); }
;
/*没有考虑移位运算*/
relational_expression
: additive_expression { $$ = $1; }
| relational_expression '<' additive_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "relational_expression"), linesno);
AstNode *less = newAstNode(Operator, newNodeValue(0, "<"), linesno);
linkAstNode($$, $1, less, $3, NULL); }
| relational_expression '>' additive_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "relational_expression"), linesno);
AstNode *greater = newAstNode(Operator, newNodeValue(0, ">"), linesno);
linkAstNode($$, $1, greater, $3, NULL); }
| relational_expression LE_OP additive_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "relational_expression"), linesno);
AstNode *le_op = newAstNode(Operator, newNodeValue(0, "<="), linesno);
linkAstNode($$, $1, le_op, $3, NULL); }
| relational_expression GE_OP additive_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "relational_expression"), linesno);
AstNode *ge_op = newAstNode(Operator, newNodeValue(0, ">="), linesno);
linkAstNode($$, $1, ge_op, $3, NULL); }
;
equality_expression
: relational_expression { $$ = $1; }
| equality_expression EQ_OP relational_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "equality_expression"), linesno);
AstNode *eq_op = newAstNode(Operator, newNodeValue(0, "=="), linesno);
linkAstNode($$, $1, eq_op, $3, NULL); }
| equality_expression NE_OP relational_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "equality_expression"), linesno);
AstNode *ne_op = newAstNode(Operator, newNodeValue(0, "!="), linesno);
linkAstNode($$, $1, ne_op, $3, NULL); }
;
and_expression
: equality_expression { $$ = $1; }
| and_expression '&' equality_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "and_expression"), linesno);
AstNode *and = newAstNode(Operator, newNodeValue(0, "&"), linesno);
linkAstNode($$, $1, and, $3, NULL); }
;
exclusive_or_expression
: and_expression { $$ = $1; }
| exclusive_or_expression '^' and_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "exclusive_or_expression"), linesno);
AstNode *e_or = newAstNode(Operator, newNodeValue(0, "^"), linesno);
linkAstNode($$, $1, e_or, $3, NULL); }
;
inclusive_or_expression
: exclusive_or_expression { $$ = $1; }
| inclusive_or_expression '|' exclusive_or_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "inclusive_or_expression"), linesno);
AstNode *i_or = newAstNode(Operator, newNodeValue(0, "|"), linesno);
linkAstNode($$, $1, i_or, $3, NULL); }
;
logical_and_expression
: inclusive_or_expression { $$ = $1; }
| logical_and_expression AND_OP inclusive_or_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "logical_and_expression"), linesno);
AstNode *l_and = newAstNode(Operator, newNodeValue(0, "&&"), linesno);
linkAstNode($$, $1, l_and, $3, NULL); }
;
logical_or_expression
: logical_and_expression { $$ = $1; }
| logical_or_expression OR_OP logical_and_expression
{ $$ = newAstNode(Expression, newNodeValue(0, "logical_or_expression"), linesno);
AstNode *l_or = newAstNode(Operator, newNodeValue(0, "||"), linesno);
linkAstNode($$, $1, l_or, $3, NULL); }
;
expression
: logical_or_expression { $$ = $1; }
;
argument_expression_list
: expression { $$ = $1; }
| argument_expression_list ',' expression
{ $$ = newAstNode(Expression, newNodeValue(0, "argument_expression_list"), linesno);
AstNode *comma = newAstNode(Operator, newNodeValue(0, ","), linesno);
linkAstNode($$, $1, comma, $3, NULL); }
;
declaration /* 暂时只考虑了简单的变量声明 */
: type_name init_declarator_list ';'
{ $$ = newAstNode(Declaration, newNodeValue(0, "declaration"), linesno);
linkAstNode($$, $1, $2, NULL); }
;
type_name /* 类型名 */
: CHAR { $$ = newAstNode(Keyword, newNodeValue(0, "char"), linesno); }
| DOUBLE { $$ = newAstNode(Keyword, newNodeValue(0, "double"), linesno); }
| FLOAT { $$ = newAstNode(Keyword, newNodeValue(0, "float"), linesno); }
| INT { $$ = newAstNode(Keyword, newNodeValue(0, "int"), linesno); }
| LONG { $$ = newAstNode(Keyword, newNodeValue(0, "long"), linesno); }
| SHORT { $$ = newAstNode(Keyword, newNodeValue(0, "short"), linesno); }
| SIGNED { $$ = newAstNode(Keyword, newNodeValue(0, "signed"), linesno); }
| UNSIGNED { $$ = newAstNode(Keyword, newNodeValue(0, "unsigned"), linesno); }
;
init_declarator_list /* 初始化列表 */
: init_declarator { $$ = $1; }
| init_declarator_list ',' init_declarator
{ $$ = newAstNode(Expression, newNodeValue(0, "init_declarator_list"), linesno);
AstNode *comma = newAstNode(Operator, newNodeValue(0, ","), linesno);
linkAstNode($$, $1, comma, $3, NULL); }
;
init_declarator
: declarator { $$ = $1; }
| declarator '=' initializer { $$ = newAstNode(Expression, newNodeValue(0, "init_declarator"), linesno);
AstNode *assign = newAstNode(Operator, newNodeValue(0, "="), linesno);
linkAstNode($$, $1, assign, $3, NULL); }
;
declarator /* 暂时只考虑了简单的变量声明 */
: IDENTIFIER { $$ = newAstNode(Identifier, newNodeValue(0, $1), linesno); }
;
initializer
: NUMBER { $$ = newAstNode(Number, newNodeValue(1, $1), linesno); }
| IDENTIFIER { $$ = newAstNode(Identifier, newNodeValue(0, $1), linesno); }
| CHARACTER { $$ = newAstNode(String, newNodeValue(0, $1), linesno); }
;
statement
: assignment_statement /* 赋值语句 */ { $$ = $1; }
| expression_statement /* 表达式语句 */ { $$ = $1; }
| selection_statement /* 选择语句 */ { $$ = $1; }
| iteration_statement /* 循环语句 */ { $$ = $1; }
| jump_statement /* 跳转语句 */ { $$ = $1; }
| compound_statement { $$ = $1; }
;
assignment_statement
: IDENTIFIER '=' expression ';' { $$ = newAstNode(Statement, newNodeValue(0, "assignment_statement"), linesno);
AstNode *identifier = newAstNode(Identifier, newNodeValue(0, $1), linesno);
AstNode *assign = newAstNode(Operator, newNodeValue(0, "="), linesno);
AstNode *semicolon = newAstNode(Operator, newNodeValue(0, ";"), linesno);
linkAstNode($$, identifier, assign, $3, semicolon, NULL); }
;
expression_statement
: expression ';' { $$ = newAstNode(Statement, newNodeValue(0, "expression_statement"), linesno);
AstNode *semicolon = newAstNode(Operator, newNodeValue(0, ";"), linesno);
linkAstNode($$, $1, semicolon, NULL); }
;
selection_statement
: IF '(' expression ')' compound_statement %prec LOWER_THAN_ELSE
{ $$ = newAstNode(Statement, newNodeValue(0, "selection_statement_no_else"), $3->linenumber);
$$->endlinenumber = $5->firstchild->linenumber;
AstNode *if_keyword = newAstNode(Keyword, newNodeValue(0, "if"), $3->linenumber);
AstNode *left_bracket = newAstNode(Operator, newNodeValue(0, "("), $3->linenumber);
AstNode *right_bracket = newAstNode(Operator, newNodeValue(0, ")"), $3->linenumber);
linkAstNode($$, if_keyword, left_bracket, $3, right_bracket, $5, NULL); }
| IF '(' expression ')' compound_statement ELSE compound_statement
{ $$ = newAstNode(Statement, newNodeValue(0, "selection_statement_with_else"), $3->linenumber);
$$->endlinenumber = $7->firstchild->linenumber;
AstNode *if_keyword = newAstNode(Keyword, newNodeValue(0, "if"), $3->linenumber);
AstNode *else_keyword = newAstNode(Keyword, newNodeValue(0, "else"), $3->linenumber);
AstNode *left_bracket = newAstNode(Operator, newNodeValue(0, "("), $3->linenumber);
AstNode *right_bracket = newAstNode(Operator, newNodeValue(0, ")"), $3->linenumber);
linkAstNode($$, if_keyword, left_bracket, $3, right_bracket, $5, else_keyword, $7, NULL); }
;
iteration_statement
: WHILE '(' expression ')' compound_statement
{ $$ = newAstNode(Statement, newNodeValue(0, "iteration_statement"), $3->linenumber);
$$->endlinenumber = $5->firstchild->linenumber;
AstNode *while_keyword = newAstNode(Keyword, newNodeValue(0, "while"), $3->linenumber);
AstNode *left_bracket = newAstNode(Operator, newNodeValue(0, "("), $3->linenumber);
AstNode *right_bracket = newAstNode(Operator, newNodeValue(0, ")"), $3->linenumber);
linkAstNode($$, while_keyword, left_bracket, $3, right_bracket, $5, NULL); }
;
jump_statement
: RETURN ';' { $$ = newAstNode(Statement, newNodeValue(0, "jump_statement"), linesno);
AstNode *return_keyword = newAstNode(Keyword, newNodeValue(0, "return"), linesno);
AstNode *semicolon = newAstNode(Operator, newNodeValue(0, ";"), linesno);
linkAstNode($$, return_keyword, semicolon, NULL); }
| RETURN expression ';' { $$ = newAstNode(Statement, newNodeValue(0, "jump_statement"), linesno);
AstNode *return_keyword = newAstNode(Keyword, newNodeValue(0, "return"), linesno);
AstNode *semicolon = newAstNode(Operator, newNodeValue(0, ";"), linesno);
linkAstNode($$, return_keyword, $2, semicolon, NULL); }
;
translation_unit
: function_definition /*仅仅用来考虑无参main函数的,其他函数情况暂时没有考虑*/
{ ast_root = $1; }
;
function_definition
: type_name IDENTIFIER '(' ')' compound_statement
{ $$ = newAstNode(FunctionDefinition, newNodeValue(0, "function_definition"), $1->linenumber);
AstNode *identifier = newAstNode(Identifier, newNodeValue(0, $2), $1->linenumber);
AstNode *left_bracket = newAstNode(Operator, newNodeValue(0, "("), $1->linenumber);
AstNode *right_bracket = newAstNode(Operator, newNodeValue(0, ")"), $1->linenumber);
linkAstNode($$, $1, identifier, left_bracket, right_bracket, $5, NULL); }
;
compound_statement
: '{' '}' { $$ = newAstNode(FunctionDefinition, newNodeValue(0, "compound_statement"), linesno);
AstNode *big_left_bracket = newAstNode(Operator, newNodeValue(0, "{"), linesno);
AstNode *big_right_bracket = newAstNode(Operator, newNodeValue(0, "}"), linesno);
linkAstNode($$, big_left_bracket, big_right_bracket, NULL); }
| '{' block_item_list '}' { $$ = newAstNode(FunctionDefinition, newNodeValue(0, "compound_statement"), $2->linenumber-1);
AstNode *big_left_bracket = newAstNode(Operator, newNodeValue(0, "{"), linesno);
AstNode *big_right_bracket = newAstNode(Operator, newNodeValue(0, "}"), linesno);
linkAstNode($$, big_left_bracket, $2, big_right_bracket, NULL); }
;
block_item_list
: block_item { $$ = $1; }
| block_item_list block_item { $$ = newAstNode(FunctionDefinition, newNodeValue(0, "block_item_list"), $1->linenumber);
linkAstNode($$, $1, $2, NULL); }
;
block_item
: declaration { $$ = $1; }
| statement { $$ = $1; }
;
%%
void yyerror(char *s)
{
fflush(stdout);
fprintf(stderr, "Error: lines %d: %s\n", linesno, s);
}