37
|
1 |
%{
|
39
|
2 |
#include "prototypes.h"
|
37
|
3 |
%}
|
|
4 |
%token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF
|
|
5 |
%token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
|
|
6 |
%token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
|
|
7 |
%token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
|
|
8 |
%token XOR_ASSIGN OR_ASSIGN TYPE_NAME
|
|
9 |
|
|
10 |
%token TYPEDEF EXTERN STATIC AUTO REGISTER
|
|
11 |
%token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
|
|
12 |
%token STRUCT UNION ENUM ELIPSIS RANGE
|
|
13 |
|
|
14 |
%token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
|
|
15 |
|
|
16 |
%start file
|
|
17 |
%%
|
|
18 |
|
|
19 |
primary_expr
|
39
|
20 |
: identifier
|
|
21 |
| CONSTANT
|
|
22 |
| STRING_LITERAL
|
|
23 |
| '(' expr ')'
|
|
24 |
;
|
37
|
25 |
|
|
26 |
postfix_expr
|
39
|
27 |
: primary_expr
|
|
28 |
| postfix_expr '[' expr ']'
|
|
29 |
| postfix_expr '(' ')'
|
|
30 |
| postfix_expr '(' argument_expr_list ')'
|
|
31 |
| postfix_expr '.' identifier
|
|
32 |
| postfix_expr PTR_OP identifier
|
|
33 |
| postfix_expr INC_OP
|
|
34 |
| postfix_expr DEC_OP
|
|
35 |
;
|
37
|
36 |
|
|
37 |
argument_expr_list
|
39
|
38 |
: assignment_expr
|
|
39 |
| argument_expr_list ',' assignment_expr
|
|
40 |
;
|
37
|
41 |
|
|
42 |
unary_expr
|
39
|
43 |
: postfix_expr
|
|
44 |
| INC_OP unary_expr
|
|
45 |
| DEC_OP unary_expr
|
|
46 |
| unary_operator cast_expr
|
|
47 |
| SIZEOF unary_expr
|
|
48 |
| SIZEOF '(' type_name ')'
|
|
49 |
;
|
37
|
50 |
|
|
51 |
unary_operator
|
39
|
52 |
: '&'
|
|
53 |
| '*'
|
|
54 |
| '+'
|
|
55 |
| '-'
|
|
56 |
| '~'
|
|
57 |
| '!'
|
|
58 |
;
|
37
|
59 |
|
|
60 |
cast_expr
|
39
|
61 |
: unary_expr
|
|
62 |
| '(' type_name ')' cast_expr
|
|
63 |
;
|
37
|
64 |
|
|
65 |
multiplicative_expr
|
39
|
66 |
: cast_expr
|
|
67 |
| multiplicative_expr '*' cast_expr
|
|
68 |
| multiplicative_expr '/' cast_expr
|
|
69 |
| multiplicative_expr '%' cast_expr
|
|
70 |
;
|
37
|
71 |
|
|
72 |
additive_expr
|
39
|
73 |
: multiplicative_expr
|
|
74 |
| additive_expr '+' multiplicative_expr
|
|
75 |
| additive_expr '-' multiplicative_expr
|
|
76 |
;
|
37
|
77 |
|
|
78 |
shift_expr
|
39
|
79 |
: additive_expr
|
|
80 |
| shift_expr LEFT_OP additive_expr
|
|
81 |
| shift_expr RIGHT_OP additive_expr
|
|
82 |
;
|
37
|
83 |
|
|
84 |
relational_expr
|
39
|
85 |
: shift_expr
|
|
86 |
| relational_expr '<' shift_expr
|
|
87 |
| relational_expr '>' shift_expr
|
|
88 |
| relational_expr LE_OP shift_expr
|
|
89 |
| relational_expr GE_OP shift_expr
|
|
90 |
;
|
37
|
91 |
|
|
92 |
equality_expr
|
39
|
93 |
: relational_expr
|
|
94 |
| equality_expr EQ_OP relational_expr
|
|
95 |
| equality_expr NE_OP relational_expr
|
|
96 |
;
|
37
|
97 |
|
|
98 |
and_expr
|
39
|
99 |
: equality_expr
|
|
100 |
| and_expr '&' equality_expr
|
|
101 |
;
|
37
|
102 |
|
|
103 |
exclusive_or_expr
|
39
|
104 |
: and_expr
|
|
105 |
| exclusive_or_expr '^' and_expr
|
|
106 |
;
|
37
|
107 |
|
|
108 |
inclusive_or_expr
|
39
|
109 |
: exclusive_or_expr
|
|
110 |
| inclusive_or_expr '|' exclusive_or_expr
|
|
111 |
;
|
37
|
112 |
|
|
113 |
logical_and_expr
|
39
|
114 |
: inclusive_or_expr
|
|
115 |
| logical_and_expr AND_OP inclusive_or_expr
|
|
116 |
;
|
37
|
117 |
|
|
118 |
logical_or_expr
|
39
|
119 |
: logical_and_expr
|
|
120 |
| logical_or_expr OR_OP logical_and_expr
|
|
121 |
;
|
37
|
122 |
|
|
123 |
conditional_expr
|
39
|
124 |
: logical_or_expr
|
|
125 |
| logical_or_expr '?' logical_or_expr ':' conditional_expr
|
|
126 |
;
|
37
|
127 |
|
|
128 |
assignment_expr
|
39
|
129 |
: conditional_expr
|
|
130 |
| unary_expr assignment_operator assignment_expr
|
|
131 |
;
|
37
|
132 |
|
|
133 |
assignment_operator
|
39
|
134 |
: '='
|
|
135 |
| MUL_ASSIGN
|
|
136 |
| DIV_ASSIGN
|
|
137 |
| MOD_ASSIGN
|
|
138 |
| ADD_ASSIGN
|
|
139 |
| SUB_ASSIGN
|
|
140 |
| LEFT_ASSIGN
|
|
141 |
| RIGHT_ASSIGN
|
|
142 |
| AND_ASSIGN
|
|
143 |
| XOR_ASSIGN
|
|
144 |
| OR_ASSIGN
|
|
145 |
;
|
37
|
146 |
|
|
147 |
expr
|
39
|
148 |
: assignment_expr
|
|
149 |
| expr ',' assignment_expr
|
|
150 |
;
|
37
|
151 |
|
|
152 |
constant_expr
|
39
|
153 |
: conditional_expr
|
|
154 |
;
|
37
|
155 |
|
|
156 |
declaration
|
39
|
157 |
: declaration_specifiers ';'
|
|
158 |
| declaration_specifiers init_declarator_list ';'
|
|
159 |
;
|
37
|
160 |
|
|
161 |
declaration_specifiers
|
39
|
162 |
: storage_class_specifier
|
|
163 |
| storage_class_specifier declaration_specifiers
|
|
164 |
| type_specifier
|
|
165 |
| type_specifier declaration_specifiers
|
|
166 |
;
|
37
|
167 |
|
|
168 |
init_declarator_list
|
39
|
169 |
: init_declarator
|
|
170 |
| init_declarator_list ',' init_declarator
|
|
171 |
;
|
37
|
172 |
|
|
173 |
init_declarator
|
39
|
174 |
: declarator
|
|
175 |
| declarator '=' initializer
|
|
176 |
;
|
37
|
177 |
|
|
178 |
storage_class_specifier
|
39
|
179 |
: TYPEDEF
|
|
180 |
| EXTERN
|
|
181 |
| STATIC
|
|
182 |
| AUTO
|
|
183 |
| REGISTER
|
|
184 |
;
|
37
|
185 |
|
|
186 |
type_specifier
|
39
|
187 |
: CHAR
|
|
188 |
| SHORT
|
|
189 |
| INT
|
|
190 |
| LONG
|
|
191 |
| SIGNED
|
|
192 |
| UNSIGNED
|
|
193 |
| FLOAT
|
|
194 |
| DOUBLE
|
|
195 |
| CONST
|
|
196 |
| VOLATILE
|
|
197 |
| VOID
|
|
198 |
| struct_or_union_specifier
|
|
199 |
| enum_specifier
|
|
200 |
| TYPE_NAME
|
|
201 |
;
|
37
|
202 |
|
|
203 |
struct_or_union_specifier
|
39
|
204 |
: struct_or_union identifier '{' struct_declaration_list '}'
|
|
205 |
| struct_or_union '{' struct_declaration_list '}'
|
|
206 |
| struct_or_union identifier
|
|
207 |
;
|
37
|
208 |
|
|
209 |
struct_or_union
|
39
|
210 |
: STRUCT
|
|
211 |
| UNION
|
|
212 |
;
|
37
|
213 |
|
|
214 |
struct_declaration_list
|
39
|
215 |
: struct_declaration
|
|
216 |
| struct_declaration_list struct_declaration
|
|
217 |
;
|
37
|
218 |
|
|
219 |
struct_declaration
|
39
|
220 |
: type_specifier_list struct_declarator_list ';'
|
|
221 |
;
|
37
|
222 |
|
|
223 |
struct_declarator_list
|
39
|
224 |
: struct_declarator
|
|
225 |
| struct_declarator_list ',' struct_declarator
|
|
226 |
;
|
37
|
227 |
|
|
228 |
struct_declarator
|
39
|
229 |
: declarator
|
|
230 |
| ':' constant_expr
|
|
231 |
| declarator ':' constant_expr
|
|
232 |
;
|
37
|
233 |
|
|
234 |
enum_specifier
|
39
|
235 |
: ENUM '{' enumerator_list '}'
|
|
236 |
| ENUM identifier '{' enumerator_list '}'
|
|
237 |
| ENUM identifier
|
|
238 |
;
|
37
|
239 |
|
|
240 |
enumerator_list
|
39
|
241 |
: enumerator
|
|
242 |
| enumerator_list ',' enumerator
|
|
243 |
;
|
37
|
244 |
|
|
245 |
enumerator
|
39
|
246 |
: identifier
|
|
247 |
| identifier '=' constant_expr
|
|
248 |
;
|
37
|
249 |
|
|
250 |
declarator
|
39
|
251 |
: declarator2
|
|
252 |
| pointer declarator2
|
|
253 |
;
|
37
|
254 |
|
|
255 |
declarator2
|
39
|
256 |
: identifier
|
|
257 |
| '(' declarator ')'
|
|
258 |
| declarator2 '[' ']'
|
|
259 |
| declarator2 '[' constant_expr ']'
|
|
260 |
| declarator2 '(' ')'
|
|
261 |
| declarator2 '(' parameter_type_list ')'
|
|
262 |
| declarator2 '(' parameter_identifier_list ')'
|
|
263 |
;
|
37
|
264 |
|
|
265 |
pointer
|
39
|
266 |
: '*'
|
|
267 |
| '*' type_specifier_list
|
|
268 |
| '*' pointer
|
|
269 |
| '*' type_specifier_list pointer
|
|
270 |
;
|
37
|
271 |
|
|
272 |
type_specifier_list
|
39
|
273 |
: type_specifier
|
|
274 |
| type_specifier_list type_specifier
|
|
275 |
;
|
37
|
276 |
|
|
277 |
parameter_identifier_list
|
39
|
278 |
: identifier_list
|
|
279 |
| identifier_list ',' ELIPSIS
|
|
280 |
;
|
37
|
281 |
|
|
282 |
identifier_list
|
39
|
283 |
: identifier
|
|
284 |
| identifier_list ',' identifier
|
|
285 |
;
|
37
|
286 |
|
|
287 |
parameter_type_list
|
39
|
288 |
: parameter_list
|
|
289 |
| parameter_list ',' ELIPSIS
|
|
290 |
;
|
37
|
291 |
|
|
292 |
parameter_list
|
39
|
293 |
: parameter_declaration
|
|
294 |
| parameter_list ',' parameter_declaration
|
|
295 |
;
|
37
|
296 |
|
|
297 |
parameter_declaration
|
39
|
298 |
: type_specifier_list declarator
|
|
299 |
| type_name
|
|
300 |
;
|
37
|
301 |
|
|
302 |
type_name
|
39
|
303 |
: type_specifier_list
|
|
304 |
| type_specifier_list abstract_declarator
|
|
305 |
;
|
37
|
306 |
|
|
307 |
abstract_declarator
|
39
|
308 |
: pointer
|
|
309 |
| abstract_declarator2
|
|
310 |
| pointer abstract_declarator2
|
|
311 |
;
|
37
|
312 |
|
|
313 |
abstract_declarator2
|
39
|
314 |
: '(' abstract_declarator ')'
|
|
315 |
| '[' ']'
|
|
316 |
| '[' constant_expr ']'
|
|
317 |
| abstract_declarator2 '[' ']'
|
|
318 |
| abstract_declarator2 '[' constant_expr ']'
|
|
319 |
| '(' ')'
|
|
320 |
| '(' parameter_type_list ')'
|
|
321 |
| abstract_declarator2 '(' ')'
|
|
322 |
| abstract_declarator2 '(' parameter_type_list ')'
|
|
323 |
;
|
37
|
324 |
|
|
325 |
initializer
|
39
|
326 |
: assignment_expr
|
|
327 |
| '{' initializer_list '}'
|
|
328 |
| '{' initializer_list ',' '}'
|
|
329 |
;
|
37
|
330 |
|
|
331 |
initializer_list
|
39
|
332 |
: initializer
|
|
333 |
| initializer_list ',' initializer
|
|
334 |
;
|
37
|
335 |
|
|
336 |
statement
|
39
|
337 |
: labeled_statement
|
|
338 |
| compound_statement
|
|
339 |
| expression_statement
|
|
340 |
| selection_statement
|
|
341 |
| iteration_statement
|
|
342 |
| jump_statement
|
|
343 |
;
|
37
|
344 |
|
|
345 |
labeled_statement
|
39
|
346 |
: identifier ':' statement
|
|
347 |
| CASE constant_expr ':' statement
|
|
348 |
| DEFAULT ':' statement
|
|
349 |
;
|
37
|
350 |
|
|
351 |
compound_statement
|
39
|
352 |
: '{' '}'
|
|
353 |
| '{' statement_list '}'
|
|
354 |
| '{' declaration_list '}'
|
|
355 |
| '{' declaration_list statement_list '}'
|
|
356 |
;
|
37
|
357 |
|
|
358 |
declaration_list
|
39
|
359 |
: declaration
|
|
360 |
| declaration_list declaration
|
|
361 |
;
|
37
|
362 |
|
|
363 |
statement_list
|
39
|
364 |
: statement
|
|
365 |
| statement_list statement
|
|
366 |
;
|
37
|
367 |
|
|
368 |
expression_statement
|
39
|
369 |
: ';'
|
|
370 |
| expr ';'
|
|
371 |
;
|
37
|
372 |
|
|
373 |
selection_statement
|
39
|
374 |
: IF '(' expr ')' statement
|
|
375 |
| IF '(' expr ')' statement ELSE statement
|
|
376 |
| SWITCH '(' expr ')' statement
|
|
377 |
;
|
37
|
378 |
|
|
379 |
iteration_statement
|
39
|
380 |
: WHILE '(' expr ')' statement
|
|
381 |
| DO statement WHILE '(' expr ')' ';'
|
|
382 |
| FOR '(' ';' ';' ')' statement
|
|
383 |
| FOR '(' ';' ';' expr ')' statement
|
|
384 |
| FOR '(' ';' expr ';' ')' statement
|
|
385 |
| FOR '(' ';' expr ';' expr ')' statement
|
|
386 |
| FOR '(' expr ';' ';' ')' statement
|
|
387 |
| FOR '(' expr ';' ';' expr ')' statement
|
|
388 |
| FOR '(' expr ';' expr ';' ')' statement
|
|
389 |
| FOR '(' expr ';' expr ';' expr ')' statement
|
|
390 |
;
|
37
|
391 |
|
|
392 |
jump_statement
|
39
|
393 |
: GOTO identifier ';'
|
|
394 |
| CONTINUE ';'
|
|
395 |
| BREAK ';'
|
|
396 |
| RETURN ';'
|
|
397 |
| RETURN expr ';'
|
|
398 |
;
|
37
|
399 |
|
|
400 |
file
|
39
|
401 |
: external_definition
|
|
402 |
| file external_definition
|
|
403 |
;
|
37
|
404 |
|
|
405 |
external_definition
|
39
|
406 |
: function_definition
|
|
407 |
| declaration
|
|
408 |
;
|
37
|
409 |
|
|
410 |
function_definition
|
39
|
411 |
: declarator function_body
|
|
412 |
| declaration_specifiers declarator function_body
|
|
413 |
;
|
37
|
414 |
|
|
415 |
function_body
|
39
|
416 |
: compound_statement
|
|
417 |
| declaration_list compound_statement
|
|
418 |
;
|
37
|
419 |
|
|
420 |
identifier
|
39
|
421 |
: IDENTIFIER
|
|
422 |
;
|
37
|
423 |
%%
|
|
424 |
|
|
425 |
#include <stdio.h>
|
|
426 |
|
|
427 |
extern YYSTYPE yytext;
|
|
428 |
extern int column;
|
|
429 |
|
|
430 |
void yyerror(char *s)
|
|
431 |
{
|
39
|
432 |
fflush(stdout);
|
|
433 |
printf("[%4d]: %s\n", column, yytext);
|
|
434 |
printf("\n%*s\n%*s\n", column, "^", column, s);
|
37
|
435 |
}
|