1 |
2 |
jsauermann |
|
2 |
|
|
%{
|
3 |
|
|
|
4 |
|
|
#include
|
5 |
|
|
#include "Node.hh"
|
6 |
|
|
#include "Name.hh"
|
7 |
|
|
|
8 |
|
|
extern int yylex();
|
9 |
|
|
extern int yyparse();
|
10 |
|
|
extern int yyerror(const char *);
|
11 |
|
|
extern FILE * out;
|
12 |
|
|
|
13 |
|
|
%}
|
14 |
|
|
|
15 |
|
|
%token EOFILE ERROR
|
16 |
|
|
%token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF ASM
|
17 |
|
|
%token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
|
18 |
|
|
%token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
|
19 |
|
|
%token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
|
20 |
|
|
%token XOR_ASSIGN OR_ASSIGN TYPE_NAME
|
21 |
|
|
|
22 |
|
|
%token TYPEDEF EXTERN STATIC AUTO REGISTER
|
23 |
|
|
%token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
|
24 |
|
|
%token STRUCT UNION ENUM ELLIPSIS
|
25 |
|
|
|
26 |
|
|
%token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
|
27 |
|
|
|
28 |
|
|
%expect 1
|
29 |
|
|
|
30 |
|
|
%start all
|
31 |
|
|
|
32 |
|
|
%union { NumericConstant * _num;
|
33 |
|
|
const char *_name;
|
34 |
|
|
StringConstant *_string_constant;
|
35 |
|
|
BinExprType _bin_expr_type;
|
36 |
|
|
UnaExprType _una_expr_type;
|
37 |
|
|
Specifier _specifier;
|
38 |
|
|
Expression *_expr;
|
39 |
|
|
Pointer *_pointer;
|
40 |
|
|
IdentifierList *_identifier_list;
|
41 |
|
|
Initializer *_initializer;
|
42 |
|
|
InitializerList *_initializer_list;
|
43 |
|
|
ParameterDeclaration *_parameter_declaration;
|
44 |
|
|
ParameterDeclarationList *_parameter_declaration_list;
|
45 |
|
|
Declarator *_declarator;
|
46 |
|
|
Enumerator *_enumerator;
|
47 |
|
|
EnumeratorList *_enumerator_list;
|
48 |
|
|
StructDeclarator *_struct_declarator;
|
49 |
|
|
StructDeclaratorList *_struct_declarator_list;
|
50 |
|
|
StructDeclaration *_struct_declaration;
|
51 |
|
|
StructDeclarationList *_struct_declaration_list;
|
52 |
|
|
TypeSpecifier *_type_specifier;
|
53 |
|
|
InitDeclarator *_init_declarator;
|
54 |
|
|
InitDeclaratorList *_init_declarator_list;
|
55 |
|
|
Declaration *_declaration;
|
56 |
|
|
DeclarationList *_declaration_list;
|
57 |
|
|
TypeName *_type_name;
|
58 |
|
|
Statement *_statement;
|
59 |
|
|
ExpressionStatement *_expression_statement;
|
60 |
|
|
CompoundStatement *_compound_statement;
|
61 |
|
|
StatementList *_statement_list;
|
62 |
|
|
FunctionDefinition *_function_definition;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
%type <_function_definition> function_definition
|
66 |
|
|
function_head
|
67 |
|
|
%type <_statement_list> statement_list
|
68 |
|
|
%type <_statement> statement
|
69 |
|
|
labeled_statement
|
70 |
|
|
selection_statement
|
71 |
|
|
iteration_statement
|
72 |
|
|
jump_statement
|
73 |
|
|
%type <_expression_statement> expression_statement
|
74 |
|
|
%type <_compound_statement> compound_statement
|
75 |
|
|
%type <_type_name> type_name
|
76 |
|
|
%type <_declaration_list> declaration_list
|
77 |
|
|
%type <_declaration> declaration
|
78 |
|
|
%type <_init_declarator> init_declarator
|
79 |
|
|
%type <_init_declarator_list> init_declarator_list
|
80 |
|
|
%type <_type_specifier> struct_or_union_specifier
|
81 |
|
|
type_specifier
|
82 |
|
|
enum_specifier
|
83 |
|
|
declaration_specifiers
|
84 |
|
|
type_qualifier_list
|
85 |
|
|
specifier_qualifier_list
|
86 |
|
|
type_qualifier
|
87 |
|
|
%type <_struct_declaration_list> struct_declaration_list
|
88 |
|
|
%type <_struct_declaration> struct_declaration
|
89 |
|
|
%type <_struct_declarator_list> struct_declarator_list
|
90 |
|
|
%type <_struct_declarator> struct_declarator
|
91 |
|
|
%type <_enumerator> enumerator
|
92 |
|
|
%type <_enumerator_list> enumerator_list
|
93 |
|
|
%type <_declarator> declarator
|
94 |
|
|
direct_declarator
|
95 |
|
|
abstract_declarator
|
96 |
|
|
direct_abstract_declarator
|
97 |
|
|
%type <_parameter_declaration> parameter_declaration
|
98 |
|
|
%type <_parameter_declaration_list> parameter_list
|
99 |
|
|
%type <_initializer> initializer
|
100 |
|
|
%type <_initializer_list> initializer_list
|
101 |
|
|
%type <_identifier_list> identifier_list
|
102 |
|
|
%type <_pointer> pointer
|
103 |
|
|
%type <_specifier> struct_or_union
|
104 |
|
|
%type <_una_expr_type> unary_operator
|
105 |
|
|
%type <_bin_expr_type> assignment_operator
|
106 |
|
|
%type <_name> IDENTIFIER TYPE_NAME
|
107 |
|
|
%type <_string_constant> STRING_LITERAL
|
108 |
|
|
string_sequence
|
109 |
|
|
%type <_num> CONSTANT
|
110 |
|
|
%type <_expr> expression
|
111 |
|
|
primary_expression
|
112 |
|
|
postfix_expression
|
113 |
|
|
argument_expression_list
|
114 |
|
|
unary_expression
|
115 |
|
|
cast_expression
|
116 |
|
|
multiplicative_expression
|
117 |
|
|
additive_expression
|
118 |
|
|
shift_expression
|
119 |
|
|
relational_expression
|
120 |
|
|
equality_expression
|
121 |
|
|
and_expression
|
122 |
|
|
exclusive_or_expression
|
123 |
|
|
inclusive_or_expression
|
124 |
|
|
logical_and_expression
|
125 |
|
|
logical_or_expression
|
126 |
|
|
conditional_expression
|
127 |
|
|
assignment_expression
|
128 |
|
|
constant_expression
|
129 |
|
|
|
130 |
|
|
%%
|
131 |
|
|
|
132 |
|
|
all : translation_unit EOFILE
|
133 |
|
|
{ StringConstant::EmitAll(out); return 0; }
|
134 |
|
|
;
|
135 |
|
|
|
136 |
|
|
primary_expression
|
137 |
|
|
: IDENTIFIER { $$ = IdentifierExpression::New($1); }
|
138 |
|
|
| CONSTANT { $$ = new NumericExpression($1); }
|
139 |
|
|
| string_sequence { $$ = new StringExpression($1); }
|
140 |
|
|
| '(' expression ')' { $$ = $2; }
|
141 |
|
|
| ASM '(' string_sequence ')' { $$ = new AsmExpression($3); }
|
142 |
|
|
;
|
143 |
|
|
|
144 |
|
|
string_sequence
|
145 |
|
|
: STRING_LITERAL { $$ = $1; }
|
146 |
|
|
| string_sequence STRING_LITERAL { $$ = *$1 & $2; }
|
147 |
|
|
;
|
148 |
|
|
|
149 |
|
|
postfix_expression
|
150 |
|
|
: primary_expression
|
151 |
|
|
{ $$ = $1; }
|
152 |
|
|
| postfix_expression '[' expression ']'
|
153 |
|
|
{ $$ = BinaryExpression::New(ET_ELEMENT, $1, $3); }
|
154 |
|
|
| postfix_expression '(' ')'
|
155 |
|
|
{ $$ = BinaryExpression::New(ET_FUNCALL, $1, 0); }
|
156 |
|
|
| postfix_expression '(' argument_expression_list ')'
|
157 |
|
|
{ $$ = BinaryExpression::New(ET_FUNCALL, $1, $3); }
|
158 |
|
|
| postfix_expression '.' IDENTIFIER
|
159 |
|
|
{ $$ = new MemberExpression(false, $1, $3); }
|
160 |
|
|
| postfix_expression PTR_OP IDENTIFIER
|
161 |
|
|
{ $$ = new MemberExpression(true, $1, $3); }
|
162 |
|
|
| postfix_expression INC_OP
|
163 |
|
|
{ $$ = UnaryExpression::New(ET_POSTINC, $1); }
|
164 |
|
|
| postfix_expression DEC_OP
|
165 |
|
|
{ $$ = UnaryExpression::New(ET_POSTDEC, $1); }
|
166 |
|
|
;
|
167 |
|
|
|
168 |
|
|
argument_expression_list
|
169 |
|
|
: assignment_expression
|
170 |
|
|
{ $$ = $1; }
|
171 |
|
|
| argument_expression_list ',' assignment_expression
|
172 |
|
|
{ $$ = new ArgListExpression($1, $3); }
|
173 |
|
|
;
|
174 |
|
|
|
175 |
|
|
unary_expression
|
176 |
|
|
: postfix_expression
|
177 |
|
|
{ $$ = $1; }
|
178 |
|
|
| INC_OP unary_expression
|
179 |
|
|
{ $$ = UnaryExpression::New(ET_PREINC, $2); }
|
180 |
|
|
| DEC_OP unary_expression
|
181 |
|
|
{ $$ = UnaryExpression::New(ET_PREDEC, $2); }
|
182 |
|
|
| unary_operator cast_expression
|
183 |
|
|
{ $$ = UnaryExpression::New($1, $2); }
|
184 |
|
|
| SIZEOF unary_expression
|
185 |
|
|
{ $$ = new NumericExpression($2); }
|
186 |
|
|
| SIZEOF '(' type_name ')'
|
187 |
|
|
{ $$ = new NumericExpression($3); }
|
188 |
|
|
;
|
189 |
|
|
|
190 |
|
|
unary_operator
|
191 |
|
|
: '&' { $$ = ET_ADDRESS; }
|
192 |
|
|
| '*' { $$ = ET_CONTENT; }
|
193 |
|
|
| '+' { $$ = ET_CONJUGATE; }
|
194 |
|
|
| '-' { $$ = ET_NEGATE; }
|
195 |
|
|
| '~' { $$ = ET_COMPLEMENT; }
|
196 |
|
|
| '!' { $$ = ET_LOG_NOT; }
|
197 |
|
|
;
|
198 |
|
|
|
199 |
|
|
cast_expression
|
200 |
|
|
: unary_expression
|
201 |
|
|
{ $$ = $1; }
|
202 |
|
|
| '(' type_name ')' cast_expression
|
203 |
|
|
{ $$ = new UnaryExpression($2, $4); }
|
204 |
|
|
;
|
205 |
|
|
|
206 |
|
|
multiplicative_expression
|
207 |
|
|
: cast_expression
|
208 |
|
|
{ $$ = $1; }
|
209 |
|
|
| multiplicative_expression '*' cast_expression
|
210 |
|
|
{ $$ = BinaryExpression::New(ET_MULT, $1, $3); }
|
211 |
|
|
| multiplicative_expression '/' cast_expression
|
212 |
|
|
{ $$ = BinaryExpression::New(ET_DIV, $1, $3); }
|
213 |
|
|
| multiplicative_expression '%' cast_expression
|
214 |
|
|
{ $$ = BinaryExpression::New(ET_MOD, $1, $3); }
|
215 |
|
|
;
|
216 |
|
|
|
217 |
|
|
additive_expression
|
218 |
|
|
: multiplicative_expression
|
219 |
|
|
{ $$ = $1; }
|
220 |
|
|
| additive_expression '+' multiplicative_expression
|
221 |
|
|
{ $$ = AdditionExpression::New($1, $3); }
|
222 |
|
|
| additive_expression '-' multiplicative_expression
|
223 |
|
|
{ $$ = SubtractionExpression::New($1, $3); }
|
224 |
|
|
;
|
225 |
|
|
|
226 |
|
|
shift_expression
|
227 |
|
|
: additive_expression
|
228 |
|
|
{ $$ = $1; }
|
229 |
|
|
| shift_expression LEFT_OP additive_expression
|
230 |
|
|
{ $$ = BinaryExpression::New(ET_LEFT, $1, $3); }
|
231 |
|
|
| shift_expression RIGHT_OP additive_expression
|
232 |
|
|
{ $$ = BinaryExpression::New(ET_RIGHT, $1, $3); }
|
233 |
|
|
;
|
234 |
|
|
|
235 |
|
|
relational_expression
|
236 |
|
|
: shift_expression
|
237 |
|
|
{ $$ = $1; }
|
238 |
|
|
| relational_expression '<' shift_expression
|
239 |
|
|
{ $$ = BinaryExpression::New(ET_LESS, $1, $3); }
|
240 |
|
|
| relational_expression '>' shift_expression
|
241 |
|
|
{ $$ = BinaryExpression::New(ET_GREATER, $1, $3); }
|
242 |
|
|
| relational_expression LE_OP shift_expression
|
243 |
|
|
{ $$ = BinaryExpression::New(ET_LESS_EQUAL, $1, $3); }
|
244 |
|
|
| relational_expression GE_OP shift_expression
|
245 |
|
|
{ $$ = BinaryExpression::New(ET_GREATER_EQUAL, $1, $3); }
|
246 |
|
|
;
|
247 |
|
|
|
248 |
|
|
equality_expression
|
249 |
|
|
: relational_expression
|
250 |
|
|
{ $$ = $1; }
|
251 |
|
|
| equality_expression EQ_OP relational_expression
|
252 |
|
|
{ $$ = BinaryExpression::New(ET_EQUAL, $1, $3); }
|
253 |
|
|
| equality_expression NE_OP relational_expression
|
254 |
|
|
{ $$ = BinaryExpression::New(ET_NOT_EQUAL, $1, $3); }
|
255 |
|
|
;
|
256 |
|
|
|
257 |
|
|
and_expression
|
258 |
|
|
: equality_expression
|
259 |
|
|
{ $$ = $1; }
|
260 |
|
|
| and_expression '&' equality_expression
|
261 |
|
|
{ $$ = BinaryExpression::New(ET_BIT_AND, $1, $3); }
|
262 |
|
|
;
|
263 |
|
|
|
264 |
|
|
exclusive_or_expression
|
265 |
|
|
: and_expression
|
266 |
|
|
{ $$ = $1; }
|
267 |
|
|
| exclusive_or_expression '^' and_expression
|
268 |
|
|
{ $$ = BinaryExpression::New(ET_BIT_XOR, $1, $3); }
|
269 |
|
|
;
|
270 |
|
|
|
271 |
|
|
inclusive_or_expression
|
272 |
|
|
: exclusive_or_expression
|
273 |
|
|
{ $$ = $1; }
|
274 |
|
|
| inclusive_or_expression '|' exclusive_or_expression
|
275 |
|
|
{ $$ = BinaryExpression::New(ET_BIT_OR, $1, $3); }
|
276 |
|
|
;
|
277 |
|
|
|
278 |
|
|
logical_and_expression
|
279 |
|
|
: inclusive_or_expression
|
280 |
|
|
{ $$ = $1; }
|
281 |
|
|
| logical_and_expression AND_OP inclusive_or_expression
|
282 |
|
|
{ $$ = BinaryExpression::New(ET_LOG_AND, $1, $3); }
|
283 |
|
|
;
|
284 |
|
|
|
285 |
|
|
logical_or_expression
|
286 |
|
|
: logical_and_expression
|
287 |
|
|
{ $$ = $1; }
|
288 |
|
|
| logical_or_expression OR_OP logical_and_expression
|
289 |
|
|
{ $$ = BinaryExpression::New(ET_LOG_OR, $1, $3); }
|
290 |
|
|
;
|
291 |
|
|
|
292 |
|
|
conditional_expression
|
293 |
|
|
: logical_or_expression
|
294 |
|
|
{ $$ = $1; }
|
295 |
|
|
| logical_or_expression '?' expression ':' conditional_expression
|
296 |
|
|
{ $$ = new CondExpression($1, $3, $5); }
|
297 |
|
|
;
|
298 |
|
|
|
299 |
|
|
assignment_expression
|
300 |
|
|
: conditional_expression
|
301 |
|
|
{ $$ = $1; }
|
302 |
|
|
| unary_expression assignment_operator assignment_expression
|
303 |
|
|
{ $$ = BinaryExpression::New($2, $1, $3); }
|
304 |
|
|
;
|
305 |
|
|
|
306 |
|
|
assignment_operator
|
307 |
|
|
: '=' { $$ = ET_ASSIGN; }
|
308 |
|
|
| MUL_ASSIGN { $$ = ET_MULT_ASSIGN; }
|
309 |
|
|
| DIV_ASSIGN { $$ = ET_DIV_ASSIGN; }
|
310 |
|
|
| MOD_ASSIGN { $$ = ET_MOD_ASSIGN; }
|
311 |
|
|
| ADD_ASSIGN { $$ = ET_ADD_ASSIGN; }
|
312 |
|
|
| SUB_ASSIGN { $$ = ET_SUB_ASSIGN; }
|
313 |
|
|
| LEFT_ASSIGN { $$ = ET_LEFT_ASSIGN; }
|
314 |
|
|
| RIGHT_ASSIGN { $$ = ET_RIGHT_ASSIGN; }
|
315 |
|
|
| AND_ASSIGN { $$ = ET_AND_ASSIGN; }
|
316 |
|
|
| XOR_ASSIGN { $$ = ET_XOR_ASSIGN; }
|
317 |
|
|
| OR_ASSIGN { $$ = ET_OR_ASSIGN; }
|
318 |
|
|
;
|
319 |
|
|
|
320 |
|
|
expression
|
321 |
|
|
: assignment_expression
|
322 |
|
|
{ $$ = $1; }
|
323 |
|
|
| expression ',' assignment_expression
|
324 |
|
|
{ $$ = BinaryExpression::New(ET_LIST, $1, $3); }
|
325 |
|
|
;
|
326 |
|
|
|
327 |
|
|
constant_expression
|
328 |
|
|
: conditional_expression
|
329 |
|
|
{ $$ = $1; }
|
330 |
|
|
;
|
331 |
|
|
|
332 |
|
|
declaration
|
333 |
|
|
: declaration_specifiers ';'
|
334 |
|
|
{ $$ = new Declaration($1, 0); }
|
335 |
|
|
| declaration_specifiers init_declarator_list ';'
|
336 |
|
|
{ $$ = new Declaration($1, $2); }
|
337 |
|
|
;
|
338 |
|
|
|
339 |
|
|
declaration_specifiers
|
340 |
|
|
: type_specifier { $$ = $1; }
|
341 |
|
|
| type_qualifier { $$ = $1; }
|
342 |
|
|
| type_specifier declaration_specifiers { $$ = *$2 + *$1; }
|
343 |
|
|
| type_qualifier declaration_specifiers { $$ = *$2 + *$1; }
|
344 |
|
|
;
|
345 |
|
|
|
346 |
|
|
init_declarator_list
|
347 |
|
|
: init_declarator
|
348 |
|
|
{ $$ = new InitDeclaratorList($1, 0); }
|
349 |
|
|
| init_declarator ',' init_declarator_list
|
350 |
|
|
{ $$ = new InitDeclaratorList($1, $3); }
|
351 |
|
|
;
|
352 |
|
|
|
353 |
|
|
init_declarator
|
354 |
|
|
: declarator
|
355 |
|
|
{ $$ = new InitDeclarator($1, 0); }
|
356 |
|
|
| declarator '=' initializer
|
357 |
|
|
{ $$ = new InitDeclarator($1, $3); }
|
358 |
|
|
;
|
359 |
|
|
|
360 |
|
|
type_specifier
|
361 |
|
|
// storage class
|
362 |
|
|
: TYPEDEF { $$ = new TypeSpecifier(SC_TYPEDEF); }
|
363 |
|
|
| EXTERN { $$ = new TypeSpecifier(SC_EXTERN); }
|
364 |
|
|
| STATIC { $$ = new TypeSpecifier(SC_STATIC); }
|
365 |
|
|
| AUTO { $$ = new TypeSpecifier(SC_AUTO); }
|
366 |
|
|
| REGISTER { $$ = new TypeSpecifier(SC_REGISTER); }
|
367 |
|
|
// type
|
368 |
|
|
| VOID { $$ = new TypeSpecifier(TS_VOID); }
|
369 |
|
|
| CHAR { $$ = new TypeSpecifier(TS_CHAR); }
|
370 |
|
|
| SHORT { $$ = new TypeSpecifier(TS_SHORT); }
|
371 |
|
|
| INT { $$ = new TypeSpecifier(TS_INT); }
|
372 |
|
|
| LONG { $$ = new TypeSpecifier(TS_LONG); }
|
373 |
|
|
| FLOAT { $$ = new TypeSpecifier(TS_FLOAT); }
|
374 |
|
|
| DOUBLE { $$ = new TypeSpecifier(TS_DOUBLE); }
|
375 |
|
|
| SIGNED { $$ = new TypeSpecifier(TS_SIGNED); }
|
376 |
|
|
| UNSIGNED { $$ = new TypeSpecifier(TS_UNSIGNED); }
|
377 |
|
|
| struct_or_union_specifier { $$ = $1; }
|
378 |
|
|
| enum_specifier { $$ = $1; }
|
379 |
|
|
| TYPE_NAME { $$ = new TypeSpecifier(TS_TYPE_NAME, $1, 0); }
|
380 |
|
|
;
|
381 |
|
|
|
382 |
|
|
struct_or_union_specifier
|
383 |
|
|
: struct_or_union IDENTIFIER '{' struct_declaration_list '}'
|
384 |
|
|
{ $$ = new TypeSpecifier($1, $2, $4); }
|
385 |
|
|
| struct_or_union '{' struct_declaration_list '}'
|
386 |
|
|
{ $$ = new TypeSpecifier($1, 0, $3); }
|
387 |
|
|
| struct_or_union IDENTIFIER
|
388 |
|
|
{ $$ = new TypeSpecifier($1, $2, 0); }
|
389 |
|
|
;
|
390 |
|
|
|
391 |
|
|
struct_or_union
|
392 |
|
|
: STRUCT { $$ = TS_STRUCT; }
|
393 |
|
|
| UNION { $$ = TS_UNION; }
|
394 |
|
|
;
|
395 |
|
|
|
396 |
|
|
struct_declaration_list
|
397 |
|
|
: struct_declaration
|
398 |
|
|
{ $$ = new StructDeclarationList($1, 0); }
|
399 |
|
|
| struct_declaration struct_declaration_list
|
400 |
|
|
{ $$ = new StructDeclarationList($1, $2); }
|
401 |
|
|
;
|
402 |
|
|
|
403 |
|
|
struct_declaration
|
404 |
|
|
: specifier_qualifier_list struct_declarator_list ';'
|
405 |
|
|
{ $$ = new StructDeclaration($1, $2); }
|
406 |
|
|
;
|
407 |
|
|
|
408 |
|
|
specifier_qualifier_list
|
409 |
|
|
: type_specifier { $$ = $1; }
|
410 |
|
|
| type_qualifier { $$ = $1; }
|
411 |
|
|
| type_specifier specifier_qualifier_list { $$=*$1 + *$2; }
|
412 |
|
|
| type_qualifier specifier_qualifier_list { $$=*$1 + *$2; }
|
413 |
|
|
;
|
414 |
|
|
|
415 |
|
|
struct_declarator_list
|
416 |
|
|
: struct_declarator
|
417 |
|
|
{ $$ = new StructDeclaratorList($1, 0); }
|
418 |
|
|
| struct_declarator ',' struct_declarator_list
|
419 |
|
|
{ $$ = new StructDeclaratorList($1, $3); }
|
420 |
|
|
;
|
421 |
|
|
|
422 |
|
|
struct_declarator
|
423 |
|
|
: declarator
|
424 |
|
|
{ $$ = new StructDeclarator($1, 0); }
|
425 |
|
|
| ':' constant_expression
|
426 |
|
|
{ $$ = new StructDeclarator( 0, $2); }
|
427 |
|
|
| declarator ':' constant_expression
|
428 |
|
|
{ $$ = new StructDeclarator($1, $3); }
|
429 |
|
|
;
|
430 |
|
|
|
431 |
|
|
enum_specifier
|
432 |
|
|
: ENUM '{' enumerator_list '}'
|
433 |
|
|
{ $$ = new TypeSpecifier( 0, $3); }
|
434 |
|
|
| ENUM IDENTIFIER '{' enumerator_list '}'
|
435 |
|
|
{ $$ = new TypeSpecifier($2, $4); }
|
436 |
|
|
| ENUM IDENTIFIER
|
437 |
|
|
{ $$ = new TypeSpecifier(TS_ENUM); }
|
438 |
|
|
;
|
439 |
|
|
|
440 |
|
|
enumerator_list
|
441 |
|
|
: enumerator
|
442 |
|
|
{ $$ = new EnumeratorList($1, 0); }
|
443 |
|
|
| enumerator ',' enumerator_list
|
444 |
|
|
{ $$ = new EnumeratorList($1, $3); }
|
445 |
|
|
;
|
446 |
|
|
|
447 |
|
|
enumerator
|
448 |
|
|
: IDENTIFIER { $$ = new Enumerator($1, 0); }
|
449 |
|
|
| IDENTIFIER '=' constant_expression
|
450 |
|
|
{ $$ = new Enumerator($1, $3); }
|
451 |
|
|
;
|
452 |
|
|
|
453 |
|
|
type_qualifier
|
454 |
|
|
: CONST { $$ = new TypeSpecifier(TQ_CONST); }
|
455 |
|
|
| VOLATILE { $$ = new TypeSpecifier(TQ_VOLATILE); }
|
456 |
|
|
;
|
457 |
|
|
|
458 |
|
|
declarator
|
459 |
|
|
: pointer direct_declarator
|
460 |
|
|
{ $$ = new Declarator(new DeclItem($1), $2->Reverse()); }
|
461 |
|
|
| direct_declarator
|
462 |
|
|
{ $$ = $1->Reverse(); }
|
463 |
|
|
;
|
464 |
|
|
|
465 |
|
|
direct_declarator
|
466 |
|
|
: IDENTIFIER
|
467 |
|
|
{ $$ = new Declarator(new DeclItem($1), 0); }
|
468 |
|
|
| '(' declarator ')'
|
469 |
|
|
{ $$ = new Declarator(new DeclItem($2), 0); }
|
470 |
|
|
| direct_declarator '[' constant_expression ']'
|
471 |
|
|
{ $$ = new Declarator(new DeclItem($3), $1); }
|
472 |
|
|
| direct_declarator '[' ']'
|
473 |
|
|
{ $$ = new Declarator(new DeclItem(DECL_ARRAY), $1); }
|
474 |
|
|
| direct_declarator '(' parameter_list ')'
|
475 |
|
|
{ $$ = new Declarator(new DeclItem($3), $1); }
|
476 |
|
|
| direct_declarator '(' identifier_list ')'
|
477 |
|
|
{ $$ = new Declarator(new DeclItem($3), $1); }
|
478 |
|
|
| direct_declarator '(' ')'
|
479 |
|
|
{ $$ = new Declarator(new DeclItem(DECL_FUN), $1); }
|
480 |
|
|
;
|
481 |
|
|
|
482 |
|
|
pointer
|
483 |
|
|
: '*'
|
484 |
|
|
{ $$ = new Pointer(new Ptr(0), 0); }
|
485 |
|
|
| '*' type_qualifier_list
|
486 |
|
|
{ $$ = new Pointer(new Ptr($2), 0); }
|
487 |
|
|
| '*' pointer
|
488 |
|
|
{ $$ = new Pointer(new Ptr(0), $2); }
|
489 |
|
|
| '*' type_qualifier_list pointer
|
490 |
|
|
{ $$ = new Pointer(new Ptr($2), $3); }
|
491 |
|
|
;
|
492 |
|
|
|
493 |
|
|
type_qualifier_list
|
494 |
|
|
: type_qualifier { $$ = $1 }
|
495 |
|
|
| type_qualifier type_qualifier_list { $$ = *$1 + *$2; }
|
496 |
|
|
;
|
497 |
|
|
|
498 |
|
|
parameter_list
|
499 |
|
|
: parameter_declaration
|
500 |
|
|
{ $$ = new ParameterDeclarationList($1, 0); }
|
501 |
|
|
| ELLIPSIS
|
502 |
|
|
{ $$ = new ParameterDeclarationList( 0, 0); }
|
503 |
|
|
| parameter_declaration ',' parameter_list
|
504 |
|
|
{
|
505 |
|
|
if ($3->Head()) $$ = new ParameterDeclarationList($1, $3);
|
506 |
|
|
else $$ = $3->SetHead($1->SetEllipsis());
|
507 |
|
|
}
|
508 |
|
|
;
|
509 |
|
|
|
510 |
|
|
parameter_declaration
|
511 |
|
|
: declaration_specifiers declarator
|
512 |
|
|
{ $$ = new ParameterDeclaration($1, $2); }
|
513 |
|
|
| declaration_specifiers abstract_declarator
|
514 |
|
|
{ $$ = new ParameterDeclaration($1, $2); }
|
515 |
|
|
| declaration_specifiers
|
516 |
|
|
{ $$ = new ParameterDeclaration($1, 0); }
|
517 |
|
|
;
|
518 |
|
|
|
519 |
|
|
identifier_list
|
520 |
|
|
: IDENTIFIER
|
521 |
|
|
{ $$ = new IdentifierList(new Identifier($1), 0); }
|
522 |
|
|
| IDENTIFIER ',' identifier_list
|
523 |
|
|
{ $$ = new IdentifierList(new Identifier($1), $3); }
|
524 |
|
|
;
|
525 |
|
|
|
526 |
|
|
type_name
|
527 |
|
|
: specifier_qualifier_list
|
528 |
|
|
{ assert($1); $$ = new TypeName($1, 0); }
|
529 |
|
|
| specifier_qualifier_list abstract_declarator
|
530 |
|
|
{ assert($1); $$ = new TypeName($1, $2); }
|
531 |
|
|
;
|
532 |
|
|
|
533 |
|
|
abstract_declarator
|
534 |
|
|
: pointer
|
535 |
|
|
{ $$ = new Declarator(new DeclItem($1), 0); }
|
536 |
|
|
| direct_abstract_declarator
|
537 |
|
|
{ $$ = $1->Reverse(); }
|
538 |
|
|
| pointer direct_abstract_declarator
|
539 |
|
|
{ $$ = new Declarator(new DeclItem($1), $2->Reverse()); }
|
540 |
|
|
;
|
541 |
|
|
|
542 |
|
|
direct_abstract_declarator
|
543 |
|
|
: '(' abstract_declarator ')'
|
544 |
|
|
{ $$ = new Declarator(new DeclItem($2), 0); }
|
545 |
|
|
| '[' ']'
|
546 |
|
|
{ $$ = new Declarator(new DeclItem(DECL_ARRAY), 0); }
|
547 |
|
|
| '[' constant_expression ']'
|
548 |
|
|
{ $$ = new Declarator(new DeclItem($2), 0); }
|
549 |
|
|
| '(' ')'
|
550 |
|
|
{ $$ = new Declarator(new DeclItem(DECL_FUN), 0); }
|
551 |
|
|
| '(' parameter_list ')'
|
552 |
|
|
{ $$ = new Declarator(new DeclItem($2), 0); }
|
553 |
|
|
| direct_abstract_declarator '[' ']'
|
554 |
|
|
{ $$ = new Declarator(new DeclItem(DECL_ARRAY), $1); }
|
555 |
|
|
| direct_abstract_declarator '[' constant_expression ']'
|
556 |
|
|
{ $$ = new Declarator(new DeclItem($3), $1); }
|
557 |
|
|
| direct_abstract_declarator '(' ')'
|
558 |
|
|
{ $$ = new Declarator(new DeclItem(DECL_FUN), $1); }
|
559 |
|
|
| direct_abstract_declarator '(' parameter_list ')'
|
560 |
|
|
{ $$ = new Declarator(new DeclItem($3), $1); }
|
561 |
|
|
;
|
562 |
|
|
|
563 |
|
|
initializer
|
564 |
|
|
: assignment_expression
|
565 |
|
|
{ $$ = new Initializer($1); }
|
566 |
|
|
| '{' initializer_list '}'
|
567 |
|
|
{ $$ = new Initializer($2->Reverse()); }
|
568 |
|
|
| '{' initializer_list ',' '}'
|
569 |
|
|
{ $$ = new Initializer($2->Reverse()); }
|
570 |
|
|
;
|
571 |
|
|
|
572 |
|
|
initializer_list
|
573 |
|
|
: initializer
|
574 |
|
|
{ $$ = new InitializerList($1, 0); }
|
575 |
|
|
| initializer_list ',' initializer
|
576 |
|
|
{ $$ = new InitializerList($3, $1); }
|
577 |
|
|
;
|
578 |
|
|
|
579 |
|
|
statement
|
580 |
|
|
: labeled_statement { $$ = $1; }
|
581 |
|
|
| compound_statement { $$ = $1; }
|
582 |
|
|
| expression_statement { $$ = $1; }
|
583 |
|
|
| selection_statement { $$ = $1; }
|
584 |
|
|
| iteration_statement { $$ = $1; }
|
585 |
|
|
| jump_statement { $$ = $1; }
|
586 |
|
|
;
|
587 |
|
|
|
588 |
|
|
labeled_statement
|
589 |
|
|
: IDENTIFIER ':' statement
|
590 |
|
|
{ $$ = new LabelStatement($1, $3); }
|
591 |
|
|
| CASE constant_expression ':' statement
|
592 |
|
|
{ $$ = new CaseStatement($2, $4); }
|
593 |
|
|
| DEFAULT ':' statement
|
594 |
|
|
{ $$ = new CaseStatement(0, $3); }
|
595 |
|
|
;
|
596 |
|
|
|
597 |
|
|
compound_statement
|
598 |
|
|
: '{' '}'
|
599 |
|
|
{ $$ = new CompoundStatement( 0, 0); }
|
600 |
|
|
| '{' statement_list '}'
|
601 |
|
|
{ $$ = new CompoundStatement( 0, $2); }
|
602 |
|
|
| '{' declaration_list '}'
|
603 |
|
|
{ $$ = new CompoundStatement($2, 0); }
|
604 |
|
|
| '{' declaration_list statement_list '}'
|
605 |
|
|
{ $$ = new CompoundStatement($2, $3); }
|
606 |
|
|
;
|
607 |
|
|
|
608 |
|
|
declaration_list
|
609 |
|
|
: declaration
|
610 |
|
|
{ $$ = new DeclarationList($1, 0); }
|
611 |
|
|
| declaration declaration_list
|
612 |
|
|
{ $$ = new DeclarationList($1, $2); }
|
613 |
|
|
;
|
614 |
|
|
|
615 |
|
|
statement_list
|
616 |
|
|
: statement
|
617 |
|
|
{ $$ = new StatementList($1, 0); }
|
618 |
|
|
| statement statement_list
|
619 |
|
|
{ $$ = new StatementList($1, $2); }
|
620 |
|
|
;
|
621 |
|
|
|
622 |
|
|
expression_statement
|
623 |
|
|
: ';' { $$ = new ExpressionStatement(0); }
|
624 |
|
|
| expression ';' { $$ = new ExpressionStatement($1); }
|
625 |
|
|
| error ';' { $$ = new ExpressionStatement(0);
|
626 |
|
|
Node::Error(); }
|
627 |
|
|
;
|
628 |
|
|
|
629 |
|
|
selection_statement
|
630 |
|
|
: IF '(' expression ')' statement
|
631 |
|
|
{ $$ = new IfElseStatement($3, $5, 0); }
|
632 |
|
|
| IF '(' expression ')' statement ELSE statement
|
633 |
|
|
{ $$ = new IfElseStatement($3, $5, $7); }
|
634 |
|
|
| SWITCH '(' expression ')' compound_statement
|
635 |
|
|
{ $$ = new SwitchStatement($3, $5); }
|
636 |
|
|
;
|
637 |
|
|
|
638 |
|
|
iteration_statement
|
639 |
|
|
: WHILE '(' expression ')' statement
|
640 |
|
|
{ $$ = new WhileStatement($3, $5); }
|
641 |
|
|
| DO statement WHILE '(' expression ')' ';'
|
642 |
|
|
{ $$ = new DoWhileStatement($2, $5); }
|
643 |
|
|
| FOR '(' expression_statement expression_statement ')' statement
|
644 |
|
|
{ $$ = new ForStatement($3, $4, 0, $6); }
|
645 |
|
|
| FOR '(' expression_statement expression_statement
|
646 |
|
|
expression ')' statement
|
647 |
|
|
{ $$ = new ForStatement($3, $4, $5, $7); }
|
648 |
|
|
;
|
649 |
|
|
|
650 |
|
|
jump_statement
|
651 |
|
|
: GOTO IDENTIFIER ';' { $$ = new GotoStatement($2); }
|
652 |
|
|
| CONTINUE ';' { $$ = new ContStatement(false); }
|
653 |
|
|
| BREAK ';' { $$ = new ContStatement(true); }
|
654 |
|
|
| RETURN ';' { $$ = new ReturnStatement(0); }
|
655 |
|
|
| RETURN expression ';' { $$ = new ReturnStatement($2); }
|
656 |
|
|
;
|
657 |
|
|
|
658 |
|
|
translation_unit
|
659 |
|
|
: external_declaration { }
|
660 |
|
|
| external_declaration translation_unit { }
|
661 |
|
|
;
|
662 |
|
|
|
663 |
|
|
external_declaration
|
664 |
|
|
: function_definition
|
665 |
|
|
{ $1->Emit(out);
|
666 |
|
|
fprintf(out,
|
667 |
|
|
";;; ------------------------------------;\n"); }
|
668 |
|
|
| declaration
|
669 |
|
|
{ if ($1) $1->Emit(out);
|
670 |
|
|
fprintf(out,
|
671 |
|
|
";;; ------------------------------------;\n"); }
|
672 |
|
|
| error
|
673 |
|
|
{ Node::Error();
|
674 |
|
|
fprintf(out,
|
675 |
|
|
";;; SYNTAX ERROR\n"
|
676 |
|
|
";;; ------------------------------------;\n"); }
|
677 |
|
|
;
|
678 |
|
|
|
679 |
|
|
function_head
|
680 |
|
|
: declaration_specifiers declarator declaration_list
|
681 |
|
|
{ $$ = new FunctionDefinition($1, $2, $3); }
|
682 |
|
|
| declaration_specifiers declarator
|
683 |
|
|
{ $$ = new FunctionDefinition($1, $2, 0); }
|
684 |
|
|
| declarator declaration_list
|
685 |
|
|
{ $$ = new FunctionDefinition( 0, $1, $2); }
|
686 |
|
|
| declarator
|
687 |
|
|
{ $$ = new FunctionDefinition( 0, $1, 0); }
|
688 |
|
|
;
|
689 |
|
|
|
690 |
|
|
function_definition
|
691 |
|
|
: function_head compound_statement
|
692 |
|
|
{ $$ = $1->SetBody($2); }
|
693 |
|
|
;
|
694 |
|
|
%%
|