diff options
author | David Walter Seikel | 2012-01-09 08:35:38 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-09 08:35:38 +1000 |
commit | a94fa72317334d05501923cfae51d555fef43583 (patch) | |
tree | 6409bb0902eb63c39483dd95940a737484733d65 /LuaSL | |
parent | One of these days I'll get spaces to work. Another hack at it. (diff) | |
download | SledjHamr-a94fa72317334d05501923cfae51d555fef43583.zip SledjHamr-a94fa72317334d05501923cfae51d555fef43583.tar.gz SledjHamr-a94fa72317334d05501923cfae51d555fef43583.tar.bz2 SledjHamr-a94fa72317334d05501923cfae51d555fef43583.tar.xz |
Added ANSI C flex and yacc files, just for reference.
Diffstat (limited to '')
-rw-r--r-- | LuaSL/src/ANSI_C.l | 182 | ||||
-rw-r--r-- | LuaSL/src/ANSI_C.y | 471 |
2 files changed, 653 insertions, 0 deletions
diff --git a/LuaSL/src/ANSI_C.l b/LuaSL/src/ANSI_C.l new file mode 100644 index 0000000..8a6b435 --- /dev/null +++ b/LuaSL/src/ANSI_C.l | |||
@@ -0,0 +1,182 @@ | |||
1 | D [0-9] | ||
2 | L [a-zA-Z_] | ||
3 | H [a-fA-F0-9] | ||
4 | E [Ee][+-]?{D}+ | ||
5 | P [Pp][+-]?{D}+ | ||
6 | FS (f|F|l|L) | ||
7 | IS ((u|U)|(u|U)?(l|L|ll|LL)|(l|L|ll|LL)(u|U)) | ||
8 | |||
9 | %{ | ||
10 | #include <stdio.h> | ||
11 | #include "y.tab.h" | ||
12 | |||
13 | void count(void); | ||
14 | %} | ||
15 | |||
16 | %% | ||
17 | "/*" { comment(); } | ||
18 | "//"[^\n]* { /* consume //-comment */ } | ||
19 | |||
20 | |||
21 | "auto" { count(); return(AUTO); } | ||
22 | "_Bool" { count(); return(BOOL); } | ||
23 | "break" { count(); return(BREAK); } | ||
24 | "case" { count(); return(CASE); } | ||
25 | "char" { count(); return(CHAR); } | ||
26 | "_Complex" { count(); return(COMPLEX); } | ||
27 | "const" { count(); return(CONST); } | ||
28 | "continue" { count(); return(CONTINUE); } | ||
29 | "default" { count(); return(DEFAULT); } | ||
30 | "do" { count(); return(DO); } | ||
31 | "double" { count(); return(DOUBLE); } | ||
32 | "else" { count(); return(ELSE); } | ||
33 | "enum" { count(); return(ENUM); } | ||
34 | "extern" { count(); return(EXTERN); } | ||
35 | "float" { count(); return(FLOAT); } | ||
36 | "for" { count(); return(FOR); } | ||
37 | "goto" { count(); return(GOTO); } | ||
38 | "if" { count(); return(IF); } | ||
39 | "_Imaginary" { count(); return(IMAGINARY); } | ||
40 | "inline" { count(); return(INLINE); } | ||
41 | "int" { count(); return(INT); } | ||
42 | "long" { count(); return(LONG); } | ||
43 | "register" { count(); return(REGISTER); } | ||
44 | "restrict" { count(); return(RESTRICT); } | ||
45 | "return" { count(); return(RETURN); } | ||
46 | "short" { count(); return(SHORT); } | ||
47 | "signed" { count(); return(SIGNED); } | ||
48 | "sizeof" { count(); return(SIZEOF); } | ||
49 | "static" { count(); return(STATIC); } | ||
50 | "struct" { count(); return(STRUCT); } | ||
51 | "switch" { count(); return(SWITCH); } | ||
52 | "typedef" { count(); return(TYPEDEF); } | ||
53 | "union" { count(); return(UNION); } | ||
54 | "unsigned" { count(); return(UNSIGNED); } | ||
55 | "void" { count(); return(VOID); } | ||
56 | "volatile" { count(); return(VOLATILE); } | ||
57 | "while" { count(); return(WHILE); } | ||
58 | |||
59 | {L}({L}|{D})* { count(); return(check_type()); } | ||
60 | |||
61 | 0[xX]{H}+{IS}? { count(); return(CONSTANT); } | ||
62 | 0{D}+{IS}? { count(); return(CONSTANT); } | ||
63 | {D}+{IS}? { count(); return(CONSTANT); } | ||
64 | L?'(\\.|[^\\'\n])+' { count(); return(CONSTANT); } | ||
65 | |||
66 | {D}+{E}{FS}? { count(); return(CONSTANT); } | ||
67 | {D}*"."{D}+({E})?{FS}? { count(); return(CONSTANT); } | ||
68 | {D}+"."{D}*({E})?{FS}? { count(); return(CONSTANT); } | ||
69 | 0[xX]{H}+{P}{FS}? { count(); return(CONSTANT); } | ||
70 | 0[xX]{H}*"."{H}+({P})?{FS}? { count(); return(CONSTANT); } | ||
71 | 0[xX]{H}+"."{H}*({P})?{FS}? { count(); return(CONSTANT); } | ||
72 | |||
73 | |||
74 | L?\"(\\.|[^\\"\n])*\" { count(); return(STRING_LITERAL); } | ||
75 | |||
76 | "..." { count(); return(ELLIPSIS); } | ||
77 | ">>=" { count(); return(RIGHT_ASSIGN); } | ||
78 | "<<=" { count(); return(LEFT_ASSIGN); } | ||
79 | "+=" { count(); return(ADD_ASSIGN); } | ||
80 | "-=" { count(); return(SUB_ASSIGN); } | ||
81 | "*=" { count(); return(MUL_ASSIGN); } | ||
82 | "/=" { count(); return(DIV_ASSIGN); } | ||
83 | "%=" { count(); return(MOD_ASSIGN); } | ||
84 | "&=" { count(); return(AND_ASSIGN); } | ||
85 | "^=" { count(); return(XOR_ASSIGN); } | ||
86 | "|=" { count(); return(OR_ASSIGN); } | ||
87 | ">>" { count(); return(RIGHT_OP); } | ||
88 | "<<" { count(); return(LEFT_OP); } | ||
89 | "++" { count(); return(INC_OP); } | ||
90 | "--" { count(); return(DEC_OP); } | ||
91 | "->" { count(); return(PTR_OP); } | ||
92 | "&&" { count(); return(AND_OP); } | ||
93 | "||" { count(); return(OR_OP); } | ||
94 | "<=" { count(); return(LE_OP); } | ||
95 | ">=" { count(); return(GE_OP); } | ||
96 | "==" { count(); return(EQ_OP); } | ||
97 | "!=" { count(); return(NE_OP); } | ||
98 | ";" { count(); return(';'); } | ||
99 | ("{"|"<%") { count(); return('{'); } | ||
100 | ("}"|"%>") { count(); return('}'); } | ||
101 | "," { count(); return(','); } | ||
102 | ":" { count(); return(':'); } | ||
103 | "=" { count(); return('='); } | ||
104 | "(" { count(); return('('); } | ||
105 | ")" { count(); return(')'); } | ||
106 | ("["|"<:") { count(); return('['); } | ||
107 | ("]"|":>") { count(); return(']'); } | ||
108 | "." { count(); return('.'); } | ||
109 | "&" { count(); return('&'); } | ||
110 | "!" { count(); return('!'); } | ||
111 | "~" { count(); return('~'); } | ||
112 | "-" { count(); return('-'); } | ||
113 | "+" { count(); return('+'); } | ||
114 | "*" { count(); return('*'); } | ||
115 | "/" { count(); return('/'); } | ||
116 | "%" { count(); return('%'); } | ||
117 | "<" { count(); return('<'); } | ||
118 | ">" { count(); return('>'); } | ||
119 | "^" { count(); return('^'); } | ||
120 | "|" { count(); return('|'); } | ||
121 | "?" { count(); return('?'); } | ||
122 | |||
123 | [ \t\v\n\f] { count(); } | ||
124 | . { /* Add code to complain about unmatched characters */ } | ||
125 | |||
126 | %% | ||
127 | |||
128 | int yywrap(void) | ||
129 | { | ||
130 | return 1; | ||
131 | } | ||
132 | |||
133 | |||
134 | void comment(void) | ||
135 | { | ||
136 | char c, prev = 0; | ||
137 | |||
138 | while ((c = input()) != 0) /* (EOF maps to 0) */ | ||
139 | { | ||
140 | if (c == '/' && prev == '*') | ||
141 | return; | ||
142 | prev = c; | ||
143 | } | ||
144 | error("unterminated comment"); | ||
145 | } | ||
146 | |||
147 | |||
148 | int column = 0; | ||
149 | |||
150 | void count(void) | ||
151 | { | ||
152 | int i; | ||
153 | |||
154 | for (i = 0; yytext[i] != '\0'; i++) | ||
155 | if (yytext[i] == '\n') | ||
156 | column = 0; | ||
157 | else if (yytext[i] == '\t') | ||
158 | column += 8 - (column % 8); | ||
159 | else | ||
160 | column++; | ||
161 | |||
162 | ECHO; | ||
163 | } | ||
164 | |||
165 | |||
166 | int check_type(void) | ||
167 | { | ||
168 | /* | ||
169 | * pseudo code --- this is what it should check | ||
170 | * | ||
171 | * if (yytext == type_name) | ||
172 | * return TYPE_NAME; | ||
173 | * | ||
174 | * return IDENTIFIER; | ||
175 | */ | ||
176 | |||
177 | /* | ||
178 | * it actually will only return IDENTIFIER | ||
179 | */ | ||
180 | |||
181 | return IDENTIFIER; | ||
182 | } | ||
diff --git a/LuaSL/src/ANSI_C.y b/LuaSL/src/ANSI_C.y new file mode 100644 index 0000000..28e2ca2 --- /dev/null +++ b/LuaSL/src/ANSI_C.y | |||
@@ -0,0 +1,471 @@ | |||
1 | %token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF | ||
2 | %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP | ||
3 | %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN | ||
4 | %token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN | ||
5 | %token XOR_ASSIGN OR_ASSIGN TYPE_NAME | ||
6 | |||
7 | %token TYPEDEF EXTERN STATIC AUTO REGISTER INLINE RESTRICT | ||
8 | %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID | ||
9 | %token BOOL COMPLEX IMAGINARY | ||
10 | %token STRUCT UNION ENUM ELLIPSIS | ||
11 | |||
12 | %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN | ||
13 | |||
14 | %start translation_unit | ||
15 | %% | ||
16 | |||
17 | primary_expression | ||
18 | : IDENTIFIER | ||
19 | | CONSTANT | ||
20 | | STRING_LITERAL | ||
21 | | '(' expression ')' | ||
22 | ; | ||
23 | |||
24 | postfix_expression | ||
25 | : primary_expression | ||
26 | | postfix_expression '[' expression ']' | ||
27 | | postfix_expression '(' ')' | ||
28 | | postfix_expression '(' argument_expression_list ')' | ||
29 | | postfix_expression '.' IDENTIFIER | ||
30 | | postfix_expression PTR_OP IDENTIFIER | ||
31 | | postfix_expression INC_OP | ||
32 | | postfix_expression DEC_OP | ||
33 | | '(' type_name ')' '{' initializer_list '}' | ||
34 | | '(' type_name ')' '{' initializer_list ',' '}' | ||
35 | ; | ||
36 | |||
37 | argument_expression_list | ||
38 | : assignment_expression | ||
39 | | argument_expression_list ',' assignment_expression | ||
40 | ; | ||
41 | |||
42 | unary_expression | ||
43 | : postfix_expression | ||
44 | | INC_OP unary_expression | ||
45 | | DEC_OP unary_expression | ||
46 | | unary_operator cast_expression | ||
47 | | SIZEOF unary_expression | ||
48 | | SIZEOF '(' type_name ')' | ||
49 | ; | ||
50 | |||
51 | unary_operator | ||
52 | : '&' | ||
53 | | '*' | ||
54 | | '+' | ||
55 | | '-' | ||
56 | | '~' | ||
57 | | '!' | ||
58 | ; | ||
59 | |||
60 | cast_expression | ||
61 | : unary_expression | ||
62 | | '(' type_name ')' cast_expression | ||
63 | ; | ||
64 | |||
65 | multiplicative_expression | ||
66 | : cast_expression | ||
67 | | multiplicative_expression '*' cast_expression | ||
68 | | multiplicative_expression '/' cast_expression | ||
69 | | multiplicative_expression '%' cast_expression | ||
70 | ; | ||
71 | |||
72 | additive_expression | ||
73 | : multiplicative_expression | ||
74 | | additive_expression '+' multiplicative_expression | ||
75 | | additive_expression '-' multiplicative_expression | ||
76 | ; | ||
77 | |||
78 | shift_expression | ||
79 | : additive_expression | ||
80 | | shift_expression LEFT_OP additive_expression | ||
81 | | shift_expression RIGHT_OP additive_expression | ||
82 | ; | ||
83 | |||
84 | relational_expression | ||
85 | : shift_expression | ||
86 | | relational_expression '<' shift_expression | ||
87 | | relational_expression '>' shift_expression | ||
88 | | relational_expression LE_OP shift_expression | ||
89 | | relational_expression GE_OP shift_expression | ||
90 | ; | ||
91 | |||
92 | equality_expression | ||
93 | : relational_expression | ||
94 | | equality_expression EQ_OP relational_expression | ||
95 | | equality_expression NE_OP relational_expression | ||
96 | ; | ||
97 | |||
98 | and_expression | ||
99 | : equality_expression | ||
100 | | and_expression '&' equality_expression | ||
101 | ; | ||
102 | |||
103 | exclusive_or_expression | ||
104 | : and_expression | ||
105 | | exclusive_or_expression '^' and_expression | ||
106 | ; | ||
107 | |||
108 | inclusive_or_expression | ||
109 | : exclusive_or_expression | ||
110 | | inclusive_or_expression '|' exclusive_or_expression | ||
111 | ; | ||
112 | |||
113 | logical_and_expression | ||
114 | : inclusive_or_expression | ||
115 | | logical_and_expression AND_OP inclusive_or_expression | ||
116 | ; | ||
117 | |||
118 | logical_or_expression | ||
119 | : logical_and_expression | ||
120 | | logical_or_expression OR_OP logical_and_expression | ||
121 | ; | ||
122 | |||
123 | conditional_expression | ||
124 | : logical_or_expression | ||
125 | | logical_or_expression '?' expression ':' conditional_expression | ||
126 | ; | ||
127 | |||
128 | assignment_expression | ||
129 | : conditional_expression | ||
130 | | unary_expression assignment_operator assignment_expression | ||
131 | ; | ||
132 | |||
133 | assignment_operator | ||
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 | ; | ||
146 | |||
147 | expression | ||
148 | : assignment_expression | ||
149 | | expression ',' assignment_expression | ||
150 | ; | ||
151 | |||
152 | constant_expression | ||
153 | : conditional_expression | ||
154 | ; | ||
155 | |||
156 | declaration | ||
157 | : declaration_specifiers ';' | ||
158 | | declaration_specifiers init_declarator_list ';' | ||
159 | ; | ||
160 | |||
161 | declaration_specifiers | ||
162 | : storage_class_specifier | ||
163 | | storage_class_specifier declaration_specifiers | ||
164 | | type_specifier | ||
165 | | type_specifier declaration_specifiers | ||
166 | | type_qualifier | ||
167 | | type_qualifier declaration_specifiers | ||
168 | | function_specifier | ||
169 | | function_specifier declaration_specifiers | ||
170 | ; | ||
171 | |||
172 | init_declarator_list | ||
173 | : init_declarator | ||
174 | | init_declarator_list ',' init_declarator | ||
175 | ; | ||
176 | |||
177 | init_declarator | ||
178 | : declarator | ||
179 | | declarator '=' initializer | ||
180 | ; | ||
181 | |||
182 | storage_class_specifier | ||
183 | : TYPEDEF | ||
184 | | EXTERN | ||
185 | | STATIC | ||
186 | | AUTO | ||
187 | | REGISTER | ||
188 | ; | ||
189 | |||
190 | type_specifier | ||
191 | : VOID | ||
192 | | CHAR | ||
193 | | SHORT | ||
194 | | INT | ||
195 | | LONG | ||
196 | | FLOAT | ||
197 | | DOUBLE | ||
198 | | SIGNED | ||
199 | | UNSIGNED | ||
200 | | BOOL | ||
201 | | COMPLEX | ||
202 | | IMAGINARY | ||
203 | | struct_or_union_specifier | ||
204 | | enum_specifier | ||
205 | | TYPE_NAME | ||
206 | ; | ||
207 | |||
208 | struct_or_union_specifier | ||
209 | : struct_or_union IDENTIFIER '{' struct_declaration_list '}' | ||
210 | | struct_or_union '{' struct_declaration_list '}' | ||
211 | | struct_or_union IDENTIFIER | ||
212 | ; | ||
213 | |||
214 | struct_or_union | ||
215 | : STRUCT | ||
216 | | UNION | ||
217 | ; | ||
218 | |||
219 | struct_declaration_list | ||
220 | : struct_declaration | ||
221 | | struct_declaration_list struct_declaration | ||
222 | ; | ||
223 | |||
224 | struct_declaration | ||
225 | : specifier_qualifier_list struct_declarator_list ';' | ||
226 | ; | ||
227 | |||
228 | specifier_qualifier_list | ||
229 | : type_specifier specifier_qualifier_list | ||
230 | | type_specifier | ||
231 | | type_qualifier specifier_qualifier_list | ||
232 | | type_qualifier | ||
233 | ; | ||
234 | |||
235 | struct_declarator_list | ||
236 | : struct_declarator | ||
237 | | struct_declarator_list ',' struct_declarator | ||
238 | ; | ||
239 | |||
240 | struct_declarator | ||
241 | : declarator | ||
242 | | ':' constant_expression | ||
243 | | declarator ':' constant_expression | ||
244 | ; | ||
245 | |||
246 | enum_specifier | ||
247 | : ENUM '{' enumerator_list '}' | ||
248 | | ENUM IDENTIFIER '{' enumerator_list '}' | ||
249 | | ENUM '{' enumerator_list ',' '}' | ||
250 | | ENUM IDENTIFIER '{' enumerator_list ',' '}' | ||
251 | | ENUM IDENTIFIER | ||
252 | ; | ||
253 | |||
254 | enumerator_list | ||
255 | : enumerator | ||
256 | | enumerator_list ',' enumerator | ||
257 | ; | ||
258 | |||
259 | enumerator | ||
260 | : IDENTIFIER | ||
261 | | IDENTIFIER '=' constant_expression | ||
262 | ; | ||
263 | |||
264 | type_qualifier | ||
265 | : CONST | ||
266 | | RESTRICT | ||
267 | | VOLATILE | ||
268 | ; | ||
269 | |||
270 | function_specifier | ||
271 | : INLINE | ||
272 | ; | ||
273 | |||
274 | declarator | ||
275 | : pointer direct_declarator | ||
276 | | direct_declarator | ||
277 | ; | ||
278 | |||
279 | |||
280 | direct_declarator | ||
281 | : IDENTIFIER | ||
282 | | '(' declarator ')' | ||
283 | | direct_declarator '[' type_qualifier_list assignment_expression ']' | ||
284 | | direct_declarator '[' type_qualifier_list ']' | ||
285 | | direct_declarator '[' assignment_expression ']' | ||
286 | | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']' | ||
287 | | direct_declarator '[' type_qualifier_list STATIC assignment_expression ']' | ||
288 | | direct_declarator '[' type_qualifier_list '*' ']' | ||
289 | | direct_declarator '[' '*' ']' | ||
290 | | direct_declarator '[' ']' | ||
291 | | direct_declarator '(' parameter_type_list ')' | ||
292 | | direct_declarator '(' identifier_list ')' | ||
293 | | direct_declarator '(' ')' | ||
294 | ; | ||
295 | |||
296 | pointer | ||
297 | : '*' | ||
298 | | '*' type_qualifier_list | ||
299 | | '*' pointer | ||
300 | | '*' type_qualifier_list pointer | ||
301 | ; | ||
302 | |||
303 | type_qualifier_list | ||
304 | : type_qualifier | ||
305 | | type_qualifier_list type_qualifier | ||
306 | ; | ||
307 | |||
308 | |||
309 | parameter_type_list | ||
310 | : parameter_list | ||
311 | | parameter_list ',' ELLIPSIS | ||
312 | ; | ||
313 | |||
314 | parameter_list | ||
315 | : parameter_declaration | ||
316 | | parameter_list ',' parameter_declaration | ||
317 | ; | ||
318 | |||
319 | parameter_declaration | ||
320 | : declaration_specifiers declarator | ||
321 | | declaration_specifiers abstract_declarator | ||
322 | | declaration_specifiers | ||
323 | ; | ||
324 | |||
325 | identifier_list | ||
326 | : IDENTIFIER | ||
327 | | identifier_list ',' IDENTIFIER | ||
328 | ; | ||
329 | |||
330 | type_name | ||
331 | : specifier_qualifier_list | ||
332 | | specifier_qualifier_list abstract_declarator | ||
333 | ; | ||
334 | |||
335 | abstract_declarator | ||
336 | : pointer | ||
337 | | direct_abstract_declarator | ||
338 | | pointer direct_abstract_declarator | ||
339 | ; | ||
340 | |||
341 | direct_abstract_declarator | ||
342 | : '(' abstract_declarator ')' | ||
343 | | '[' ']' | ||
344 | | '[' assignment_expression ']' | ||
345 | | direct_abstract_declarator '[' ']' | ||
346 | | direct_abstract_declarator '[' assignment_expression ']' | ||
347 | | '[' '*' ']' | ||
348 | | direct_abstract_declarator '[' '*' ']' | ||
349 | | '(' ')' | ||
350 | | '(' parameter_type_list ')' | ||
351 | | direct_abstract_declarator '(' ')' | ||
352 | | direct_abstract_declarator '(' parameter_type_list ')' | ||
353 | ; | ||
354 | |||
355 | initializer | ||
356 | : assignment_expression | ||
357 | | '{' initializer_list '}' | ||
358 | | '{' initializer_list ',' '}' | ||
359 | ; | ||
360 | |||
361 | initializer_list | ||
362 | : initializer | ||
363 | | designation initializer | ||
364 | | initializer_list ',' initializer | ||
365 | | initializer_list ',' designation initializer | ||
366 | ; | ||
367 | |||
368 | designation | ||
369 | : designator_list '=' | ||
370 | ; | ||
371 | |||
372 | designator_list | ||
373 | : designator | ||
374 | | designator_list designator | ||
375 | ; | ||
376 | |||
377 | designator | ||
378 | : '[' constant_expression ']' | ||
379 | | '.' IDENTIFIER | ||
380 | ; | ||
381 | |||
382 | statement | ||
383 | : labeled_statement | ||
384 | | compound_statement | ||
385 | | expression_statement | ||
386 | | selection_statement | ||
387 | | iteration_statement | ||
388 | | jump_statement | ||
389 | ; | ||
390 | |||
391 | labeled_statement | ||
392 | : IDENTIFIER ':' statement | ||
393 | | CASE constant_expression ':' statement | ||
394 | | DEFAULT ':' statement | ||
395 | ; | ||
396 | |||
397 | compound_statement | ||
398 | : '{' '}' | ||
399 | | '{' block_item_list '}' | ||
400 | ; | ||
401 | |||
402 | block_item_list | ||
403 | : block_item | ||
404 | | block_item_list block_item | ||
405 | ; | ||
406 | |||
407 | block_item | ||
408 | : declaration | ||
409 | | statement | ||
410 | ; | ||
411 | |||
412 | expression_statement | ||
413 | : ';' | ||
414 | | expression ';' | ||
415 | ; | ||
416 | |||
417 | selection_statement | ||
418 | : IF '(' expression ')' statement | ||
419 | | IF '(' expression ')' statement ELSE statement | ||
420 | | SWITCH '(' expression ')' statement | ||
421 | ; | ||
422 | |||
423 | iteration_statement | ||
424 | : WHILE '(' expression ')' statement | ||
425 | | DO statement WHILE '(' expression ')' ';' | ||
426 | | FOR '(' expression_statement expression_statement ')' statement | ||
427 | | FOR '(' expression_statement expression_statement expression ')' statement | ||
428 | | FOR '(' declaration expression_statement ')' statement | ||
429 | | FOR '(' declaration expression_statement expression ')' statement | ||
430 | ; | ||
431 | |||
432 | jump_statement | ||
433 | : GOTO IDENTIFIER ';' | ||
434 | | CONTINUE ';' | ||
435 | | BREAK ';' | ||
436 | | RETURN ';' | ||
437 | | RETURN expression ';' | ||
438 | ; | ||
439 | |||
440 | translation_unit | ||
441 | : external_declaration | ||
442 | | translation_unit external_declaration | ||
443 | ; | ||
444 | |||
445 | external_declaration | ||
446 | : function_definition | ||
447 | | declaration | ||
448 | ; | ||
449 | |||
450 | function_definition | ||
451 | : declaration_specifiers declarator declaration_list compound_statement | ||
452 | | declaration_specifiers declarator compound_statement | ||
453 | ; | ||
454 | |||
455 | declaration_list | ||
456 | : declaration | ||
457 | | declaration_list declaration | ||
458 | ; | ||
459 | |||
460 | |||
461 | %% | ||
462 | #include <stdio.h> | ||
463 | |||
464 | extern char yytext[]; | ||
465 | extern int column; | ||
466 | |||
467 | void yyerror(char const *s) | ||
468 | { | ||
469 | fflush(stdout); | ||
470 | printf("\n%*s\n%*s\n", column, "^", column, s); | ||
471 | } | ||