1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2021 Tobias Kortkamp <tobik@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "config.h"
30
31 #include <sys/types.h>
32 #include <inttypes.h>
33 #include <stdbool.h>
34
35 #include "peg.h"
36 #include "peg/grammar.h"
37
38 // Reference: https://cs.wmich.edu/~gupta/teaching/cs4850/sumII06/The%20syntax%20of%20C%20in%20Backus_Naur%20form.htm
39
40 // Prototypes
41 static RULE(abstract_declarator);
42 static RULE(abstract_declarator_0);
43 static RULE(additive_expression);
44 static RULE(additive_expression_0);
45 static RULE(additive_expression_1);
46 static RULE(and_expression);
47 static RULE(and_expression_0);
48 static RULE(assignment_expression);
49 static RULE(assignment_expression_0);
50 static RULE(assignment_operator);
51 static RULE(assignment_operator_0);
52 static RULE(cast_expression);
53 static RULE(cast_expression_0);
54 static RULE(character_constant);
55 static RULE(compound_statement);
56 static RULE(conditional_expression);
57 static RULE(conditional_expression_0);
58 static RULE(constant);
59 static RULE(constant_expression);
60 static RULE(declaration);
61 static RULE(declaration_specifier);
62 static RULE(declarator);
63 static RULE(direct_abstract_declarator);
64 static RULE(direct_abstract_declarator_0);
65 static RULE(direct_abstract_declarator_1);
66 static RULE(direct_abstract_declarator_2);
67 static RULE(direct_declarator);
68 static RULE(direct_declarator_0);
69 static RULE(direct_declarator_1);
70 static RULE(direct_declarator_2);
71 static RULE(direct_declarator_3);
72 static RULE(enum_specifier);
73 static RULE(enum_specifier_0);
74 static RULE(enum_specifier_1);
75 static RULE(enum_specifier_2);
76 static RULE(enumeration_constant);
77 static RULE(enumerator);
78 static RULE(enumerator_0);
79 static RULE(enumerator_list);
80 static RULE(enumerator_list_0);
81 static RULE(equality_expression);
82 static RULE(equality_expression_0);
83 static RULE(equality_expression_1);
84 static RULE(exclusive_or_expression);
85 static RULE(exclusive_or_expression_0);
86 static RULE(expression);
87 static RULE(expression_0);
88 static RULE(expression_statement);
89 static RULE(external_declaration);
90 static RULE(floating_constant);
91 static RULE(function_definition);
92 static RULE(identifier);
93 static RULE(inclusive_or_expression);
94 static RULE(init_declarator);
95 static RULE(init_declarator_0);
96 static RULE(initializer);
97 static RULE(initializer_0);
98 static RULE(initializer_1);
99 static RULE(initializer_list);
100 static RULE(initializer_list_0);
101 static RULE(integer_constant);
102 static RULE(iteration_statement);
103 static RULE(iteration_statement_0);
104 static RULE(iteration_statement_1);
105 static RULE(iteration_statement_2);
106 static RULE(jump_statement);
107 static RULE(jump_statement_0);
108 static RULE(jump_statement_1);
109 static RULE(jump_statement_2);
110 static RULE(jump_statement_3);
111 static RULE(labeled_statement);
112 static RULE(labeled_statement_0);
113 static RULE(labeled_statement_1);
114 static RULE(labeled_statement_2);
115 static RULE(logical_and_expression);
116 static RULE(logical_and_expression_0);
117 static RULE(logical_or_expression);
118 static RULE(logical_or_expression_0);
119 static RULE(multiplicative_expression);
120 static RULE(multiplicative_expression_0);
121 static RULE(multiplicative_expression_1);
122 static RULE(multiplicative_expression_2);
123 static RULE(parameter_declaration);
124 static RULE(parameter_declaration_0);
125 static RULE(parameter_declaration_1);
126 static RULE(parameter_list);
127 static RULE(parameter_list_0);
128 static RULE(parameter_type_list);
129 static RULE(parameter_type_list_0);
130 static RULE(pointer);
131 static RULE(postfix_expression);
132 static RULE(postfix_expression_0);
133 static RULE(postfix_expression_1);
134 static RULE(postfix_expression_2);
135 static RULE(postfix_expression_3);
136 static RULE(postfix_expression_4);
137 static RULE(postfix_expression_5);
138 static RULE(primary_expression);
139 static RULE(primary_expression_0);
140 static RULE(relational_expression);
141 static RULE(relational_expression_0);
142 static RULE(relational_expression_1);
143 static RULE(relational_expression_2);
144 static RULE(relational_expression_3);
145 static RULE(selection_statement);
146 static RULE(selection_statement_0);
147 static RULE(selection_statement_1);
148 static RULE(selection_statement_2);
149 static RULE(shift_expression);
150 static RULE(shift_expression_0);
151 static RULE(shift_expression_1);
152 static RULE(specifier_qualifier);
153 static RULE(statement);
154 static RULE(storage_class_specifier);
155 static RULE(storage_class_specifier_0);
156 static RULE(string);
157 static RULE(string_character);
158 static RULE(string_character_escaped);
159 static RULE(string_character_unescaped);
160 static RULE(struct_declaration);
161 static RULE(struct_declarator);
162 static RULE(struct_declarator_0);
163 static RULE(struct_declarator_1);
164 static RULE(struct_declarator_list);
165 static RULE(struct_declarator_list_0);
166 static RULE(struct_or_union);
167 static RULE(struct_or_union_0);
168 static RULE(struct_or_union_specifier);
169 static RULE(type_name);
170 static RULE(type_qualifier);
171 static RULE(type_qualifier_0);
172 static RULE(type_specifier);
173 static RULE(type_specifier_0);
174 static RULE(typedef_name);
175 static RULE(unary_expression);
176 static RULE(unary_expression_0);
177 static RULE(unary_expression_1);
178 static RULE(unary_expression_2);
179 static RULE(unary_expression_3);
180 static RULE(unary_expression_4);
181 static RULE(unary_operator);
182 static RULE(unary_operator_0);
183 static RULE(ws);
184
185 RULE(translation_unit);
186
RULE(translation_unit)187 RULE(translation_unit) {
188 return ANY(external_declaration);
189 }
190
RULE(external_declaration)191 RULE(external_declaration) {
192 if (!MATCH(function_definition))
193 if (!MATCH(declaration))
194 return 0;
195 return 1;
196 }
197
RULE(function_definition)198 RULE(function_definition) {
199 if (ANY(declaration_specifier))
200 if (MATCH(declarator))
201 if (ANY(declaration))
202 if (MATCH(compound_statement))
203 return 1;
204 return 0;
205 }
206
RULE(declaration_specifier)207 RULE(declaration_specifier) {
208 if (!MATCH(storage_class_specifier))
209 if (!MATCH(type_specifier))
210 if (!MATCH(type_qualifier))
211 return 0;
212 return 1;
213 }
214
RULE(storage_class_specifier_0)215 RULE(storage_class_specifier_0) {
216 return STRING("auto", "register", "static", "extern", "typedef");
217 }
218
RULE(storage_class_specifier)219 RULE(storage_class_specifier) {
220 if (ANY(ws))
221 if (MATCH(storage_class_specifier_0))
222 if (SOME(ws))
223 return 1;
224 return 0;
225 }
226
RULE(type_specifier_0)227 RULE(type_specifier_0) {
228 if (!STRING("void", "char", "short", "int", "long", "float", "double", "signed", "unsigned"))
229 if (!MATCH(struct_or_union_specifier))
230 if (!MATCH(enum_specifier))
231 if (!MATCH(typedef_name))
232 return 0;
233 return 1;
234 }
235
RULE(type_specifier)236 RULE(type_specifier) {
237 if (ANY(ws))
238 if (MATCH(type_specifier_0))
239 if (SOME(ws))
240 return 1;
241 return 0;
242 }
243
RULE(struct_or_union_specifier)244 RULE(struct_or_union_specifier) {
245 if (MATCH(struct_or_union))
246 if (OPT(MATCH(identifier)))
247 if (OPT(SOME(struct_declaration)))
248 return 1;
249 return 0;
250 }
251
RULE(struct_or_union_0)252 RULE(struct_or_union_0) {
253 if (!STRING("struct", "union"))
254 return 0;
255 return 1;
256 }
257
RULE(struct_or_union)258 RULE(struct_or_union) {
259 if (ANY(ws))
260 if (MATCH(struct_or_union_0))
261 if (ANY(ws))
262 return 1;
263 return 0;
264 }
265
RULE(struct_declaration)266 RULE(struct_declaration) {
267 if (ANY(specifier_qualifier))
268 if (MATCH(struct_declarator_list))
269 return 1;
270 return 0;
271 }
272
RULE(specifier_qualifier)273 RULE(specifier_qualifier) {
274 if (!MATCH(type_specifier))
275 if (!MATCH(type_qualifier))
276 return 0;
277 return 1;
278 }
279
280
RULE(struct_declarator_list_0)281 RULE(struct_declarator_list_0) {
282 if (MATCH(struct_declarator_list))
283 if (ANY(ws))
284 if (CHAR(','))
285 if (ANY(ws))
286 if (MATCH(struct_declarator))
287 return 1;
288 return 0;
289 }
290
RULE(struct_declarator_list)291 RULE(struct_declarator_list) {
292 if (!MATCH(struct_declarator_list_0))
293 if (!MATCH(struct_declarator))
294 return 0;
295 return 1;
296 }
297
RULE(struct_declarator_0)298 RULE(struct_declarator_0) {
299 if (MATCH(declarator))
300 if (ANY(ws))
301 if (CHAR(':'))
302 if (ANY(ws))
303 if (MATCH(constant_expression))
304 return 1;
305 return 0;
306 }
307
RULE(struct_declarator_1)308 RULE(struct_declarator_1) {
309 if (ANY(ws))
310 if (CHAR(':'))
311 if (ANY(ws))
312 if (MATCH(constant_expression))
313 return 1;
314 return 0;
315 }
316
RULE(struct_declarator)317 RULE(struct_declarator) {
318 if (!MATCH(struct_declarator_0))
319 if (!MATCH(declarator))
320 if (!MATCH(struct_declarator_1))
321 return 0;
322 return 1;
323 }
324
RULE(declarator)325 RULE(declarator) {
326 if (OPT(MATCH(pointer)))
327 if (MATCH(direct_declarator))
328 return 1;
329 return 0;
330 }
331
RULE(pointer)332 RULE(pointer) {
333 if (ANY(ws))
334 if (CHAR('*'))
335 if (ANY(ws))
336 if (ANY(type_qualifier))
337 if (OPT(MATCH(pointer)))
338 return 1;
339 return 0;
340 }
341
RULE(type_qualifier_0)342 RULE(type_qualifier_0) {
343 return STRING("const", "volatile");
344 }
345
RULE(type_qualifier)346 RULE(type_qualifier) {
347 if (ANY(ws))
348 if (MATCH(type_qualifier_0))
349 if (SOME(ws))
350 return 1;
351 return 0;
352 }
353
RULE(direct_declarator_0)354 RULE(direct_declarator_0) {
355 if (ANY(ws))
356 if (CHAR('('))
357 if (ANY(ws))
358 if (MATCH(declarator))
359 if (ANY(ws))
360 if (CHAR(')'))
361 if (ANY(ws))
362 return 1;
363 return 0;
364 }
365
RULE(direct_declarator_1)366 RULE(direct_declarator_1) {
367 if (MATCH(direct_declarator))
368 if (ANY(ws))
369 if (CHAR('['))
370 if (ANY(ws))
371 if (OPT(MATCH(constant_expression)))
372 if (ANY(ws))
373 if (CHAR(']'))
374 if (ANY(ws))
375 return 1;
376 return 0;
377 }
378
RULE(direct_declarator_2)379 RULE(direct_declarator_2) {
380 if (MATCH(direct_declarator))
381 if (ANY(ws))
382 if (CHAR('('))
383 if (ANY(ws))
384 if (MATCH(parameter_type_list))
385 if (ANY(ws))
386 if (CHAR(')'))
387 if (ANY(ws))
388 return 1;
389 return 0;
390 }
391
RULE(direct_declarator_3)392 RULE(direct_declarator_3) {
393 if (MATCH(direct_declarator))
394 if (ANY(ws))
395 if (CHAR('('))
396 if (ANY(ws))
397 if (ANY(identifier))
398 if (ANY(ws))
399 if (CHAR(')'))
400 if (ANY(ws))
401 return 1;
402 return 0;
403 }
404
RULE(direct_declarator)405 RULE(direct_declarator) {
406 if (!MATCH(identifier))
407 if (!MATCH(direct_declarator_0))
408 if (!MATCH(direct_declarator_1))
409 if (!MATCH(direct_declarator_2))
410 if (!MATCH(direct_declarator_3))
411 return 0;
412 return 1;
413 }
414
RULE(constant_expression)415 RULE(constant_expression) { return MATCH(conditional_expression); }
416
RULE(conditional_expression_0)417 RULE(conditional_expression_0) {
418 if (MATCH(logical_or_expression))
419 if (ANY(ws))
420 if (CHAR('?'))
421 if (ANY(ws))
422 if (MATCH(expression))
423 if (ANY(ws))
424 if (CHAR(':'))
425 if (ANY(ws))
426 if (MATCH(conditional_expression))
427 return 1;
428 return 0;
429 }
430
RULE(conditional_expression)431 RULE(conditional_expression) {
432 if (!MATCH(logical_or_expression))
433 if (!MATCH(conditional_expression_0))
434 return 0;
435 return 1;
436 }
437
RULE(logical_or_expression_0)438 RULE(logical_or_expression_0) {
439 if (MATCH(logical_or_expression))
440 if (ANY(ws))
441 if (STRING("||"))
442 if (ANY(ws))
443 if (MATCH(logical_and_expression))
444 return 1;
445 return 0;
446 }
447
RULE(logical_or_expression)448 RULE(logical_or_expression) {
449 if (!MATCH(logical_and_expression))
450 if (!MATCH(logical_or_expression_0))
451 return 0;
452 return 1;
453 }
454
RULE(logical_and_expression_0)455 RULE(logical_and_expression_0) {
456 if (MATCH(logical_and_expression))
457 if (ANY(ws))
458 if (STRING("&&"))
459 if (ANY(ws))
460 if (MATCH(inclusive_or_expression))
461 return 1;
462 return 0;
463 }
464
RULE(logical_and_expression)465 RULE(logical_and_expression) {
466 if (!MATCH(inclusive_or_expression))
467 if (!MATCH(logical_and_expression_0))
468 return 0;
469 return 1;
470 }
471
RULE(inclusive_or_expression)472 RULE(inclusive_or_expression) {
473 if (!MATCH(exclusive_or_expression))
474 if (!MATCH(inclusive_or_expression))
475 if (!MATCH(exclusive_or_expression))
476 return 0;
477 return 1;
478 }
479
RULE(exclusive_or_expression_0)480 RULE(exclusive_or_expression_0) {
481 if (MATCH(exclusive_or_expression))
482 if (ANY(ws))
483 if (CHAR('^'))
484 if (ANY(ws))
485 if (MATCH(and_expression))
486 return 1;
487 return 0;
488 }
489
RULE(exclusive_or_expression)490 RULE(exclusive_or_expression) {
491 if (!MATCH(and_expression))
492 if (!MATCH(exclusive_or_expression_0))
493 return 0;
494 return 1;
495 }
496
RULE(and_expression_0)497 RULE(and_expression_0) {
498 if (MATCH(and_expression))
499 if (ANY(ws))
500 if (CHAR('&'))
501 if (ANY(ws))
502 if (MATCH(equality_expression))
503 return 1;
504 return 0;
505 }
506
RULE(and_expression)507 RULE(and_expression) {
508 if (!MATCH(equality_expression))
509 if (!MATCH(and_expression_0))
510 return 0;
511 return 1;
512 }
513
RULE(equality_expression_0)514 RULE(equality_expression_0) {
515 if (MATCH(equality_expression))
516 if (ANY(ws))
517 if (STRING("=="))
518 if (ANY(ws))
519 if (MATCH(relational_expression))
520 return 1;
521 return 0;
522 }
523
RULE(equality_expression_1)524 RULE(equality_expression_1) {
525 if (MATCH(equality_expression))
526 if (ANY(ws))
527 if (STRING("!="))
528 if (ANY(ws))
529 if (MATCH(relational_expression))
530 return 1;
531 return 0;
532 }
533
RULE(equality_expression)534 RULE(equality_expression) {
535 if (!MATCH(relational_expression))
536 if (!MATCH(equality_expression_0))
537 if (!MATCH(equality_expression_1))
538 return 0;
539 return 1;
540 }
541
RULE(relational_expression_0)542 RULE(relational_expression_0) {
543 if (MATCH(relational_expression))
544 if (ANY(ws))
545 if (STRING("<="))
546 if (ANY(ws))
547 if (MATCH(shift_expression))
548 return 1;
549 return 0;
550 }
551
RULE(relational_expression_1)552 RULE(relational_expression_1) {
553 if (MATCH(relational_expression))
554 if (ANY(ws))
555 if (STRING(">="))
556 if (ANY(ws))
557 if (MATCH(shift_expression))
558 return 1;
559 return 0;
560 }
561
RULE(relational_expression_2)562 RULE(relational_expression_2) {
563 if (MATCH(relational_expression))
564 if (ANY(ws))
565 if (CHAR('<'))
566 if (ANY(ws))
567 if (MATCH(shift_expression))
568 return 1;
569 return 0;
570 }
571
RULE(relational_expression_3)572 RULE(relational_expression_3) {
573 if (MATCH(relational_expression))
574 if (ANY(ws))
575 if (CHAR('>'))
576 if (ANY(ws))
577 if (MATCH(shift_expression))
578 return 1;
579 return 0;
580 }
581
RULE(relational_expression)582 RULE(relational_expression) {
583 if (!MATCH(shift_expression))
584 if (!MATCH(relational_expression_0))
585 if (!MATCH(relational_expression_1))
586 if (!MATCH(relational_expression_2))
587 if (!MATCH(relational_expression_3))
588 return 0;
589 return 1;
590 }
591
RULE(shift_expression_0)592 RULE(shift_expression_0) {
593 if (MATCH(shift_expression))
594 if (ANY(ws))
595 if (STRING("<<"))
596 if (ANY(ws))
597 if (MATCH(additive_expression))
598 return 1;
599 return 0;
600 }
601
RULE(shift_expression_1)602 RULE(shift_expression_1) {
603 if (MATCH(shift_expression))
604 if (ANY(ws))
605 if (STRING(">>"))
606 if (ANY(ws))
607 if (MATCH(additive_expression))
608 return 1;
609 return 0;
610 }
611
RULE(shift_expression)612 RULE(shift_expression) {
613 if (!MATCH(additive_expression))
614 if (!MATCH(shift_expression_0))
615 if (!MATCH(shift_expression_1))
616 return 0;
617 return 1;
618 }
619
RULE(additive_expression_0)620 RULE(additive_expression_0) {
621 if (MATCH(additive_expression))
622 if (ANY(ws))
623 if (CHAR('+'))
624 if (ANY(ws))
625 if (MATCH(multiplicative_expression))
626 return 1;
627 return 0;
628 }
629
RULE(additive_expression_1)630 RULE(additive_expression_1) {
631 if (MATCH(additive_expression))
632 if (ANY(ws))
633 if (CHAR('-'))
634 if (ANY(ws))
635 if (MATCH(multiplicative_expression))
636 return 1;
637 return 0;
638 }
639
RULE(additive_expression)640 RULE(additive_expression) {
641 if (!MATCH(multiplicative_expression))
642 if (!MATCH(additive_expression_0))
643 if (!MATCH(additive_expression_1))
644 return 0;
645 return 1;
646 }
647
RULE(multiplicative_expression_0)648 RULE(multiplicative_expression_0) {
649 if (MATCH(multiplicative_expression))
650 if (ANY(ws))
651 if (CHAR('*'))
652 if (ANY(ws))
653 if (MATCH(cast_expression))
654 return 1;
655 return 0;
656 }
657
RULE(multiplicative_expression_1)658 RULE(multiplicative_expression_1) {
659 if (MATCH(multiplicative_expression))
660 if (ANY(ws))
661 if (CHAR('/'))
662 if (ANY(ws))
663 if (MATCH(cast_expression))
664 return 1;
665 return 0;
666 }
667
RULE(multiplicative_expression_2)668 RULE(multiplicative_expression_2) {
669 if (MATCH(multiplicative_expression))
670 if (ANY(ws))
671 if (CHAR('%'))
672 if (ANY(ws))
673 if (MATCH(cast_expression))
674 return 1;
675 return 0;
676 }
677
RULE(multiplicative_expression)678 RULE(multiplicative_expression) {
679 if (!MATCH(cast_expression))
680 if (!MATCH(multiplicative_expression_0))
681 if (!MATCH(multiplicative_expression_1))
682 if (!MATCH(multiplicative_expression_2))
683 return 0;
684 return 1;
685 }
686
RULE(cast_expression_0)687 RULE(cast_expression_0) {
688 if (ANY(ws))
689 if (CHAR('('))
690 if (ANY(ws))
691 if (MATCH(type_name))
692 if (ANY(ws))
693 if (CHAR(')'))
694 if (ANY(ws))
695 if (MATCH(cast_expression))
696 return 1;
697 return 0;
698 }
699
RULE(cast_expression)700 RULE(cast_expression) {
701 if (!MATCH(unary_expression))
702 if (!MATCH(cast_expression_0))
703 return 0;
704 return 1;
705 }
706
RULE(unary_expression_0)707 RULE(unary_expression_0) {
708 if (ANY(ws))
709 if (STRING("++"))
710 if (ANY(ws))
711 if (MATCH(unary_expression))
712 return 1;
713 return 0;
714 }
715
RULE(unary_expression_1)716 RULE(unary_expression_1) {
717 if (ANY(ws))
718 if (STRING("--"))
719 if (ANY(ws))
720 if (MATCH(unary_expression))
721 return 1;
722 return 0;
723 }
724
RULE(unary_expression_2)725 RULE(unary_expression_2) {
726 if (MATCH(unary_operator))
727 if (MATCH(cast_expression))
728 return 1;
729 return 0;
730 }
731
RULE(unary_expression_3)732 RULE(unary_expression_3) {
733 if (ANY(ws))
734 if (STRING("sizeof"))
735 if (ANY(ws))
736 if (MATCH(unary_expression))
737 return 1;
738 return 0;
739 }
740
RULE(unary_expression_4)741 RULE(unary_expression_4) {
742 if (ANY(ws))
743 if (STRING("sizeof"))
744 if (ANY(ws))
745 if (MATCH(type_name))
746 return 1;
747 return 0;
748 }
749
RULE(unary_expression)750 RULE(unary_expression) {
751 if (!MATCH(postfix_expression))
752 if (!MATCH(unary_expression_0))
753 if (!MATCH(unary_expression_1))
754 if (!MATCH(unary_expression_2))
755 if (!MATCH(unary_expression_3))
756 if (!MATCH(unary_expression_4))
757 return 0;
758 return 1;
759 }
760
RULE(postfix_expression_0)761 RULE(postfix_expression_0) {
762 if (MATCH(postfix_expression))
763 if (ANY(ws))
764 if (CHAR('['))
765 if (ANY(ws))
766 if (MATCH(expression))
767 if (ANY(ws))
768 if (CHAR(']'))
769 if (ANY(ws))
770 return 1;
771 return 0;
772 }
773
RULE(postfix_expression_1)774 RULE(postfix_expression_1) {
775 if (MATCH(postfix_expression))
776 if (ANY(ws))
777 if (CHAR('('))
778 if (ANY(ws))
779 if (ANY(assignment_expression))
780 if (ANY(ws))
781 if (CHAR(')'))
782 if (ANY(ws))
783 return 1;
784 return 0;
785 }
786
RULE(postfix_expression_2)787 RULE(postfix_expression_2) {
788 if (MATCH(postfix_expression))
789 if (ANY(ws))
790 if (CHAR('.'))
791 if (ANY(ws))
792 if (MATCH(identifier))
793 return 1;
794 return 0;
795 }
796
RULE(postfix_expression_3)797 RULE(postfix_expression_3) {
798 if (MATCH(postfix_expression))
799 if (ANY(ws))
800 if (STRING("->"))
801 if (ANY(ws))
802 if (MATCH(identifier))
803 return 1;
804 return 0;
805 }
806
RULE(postfix_expression_4)807 RULE(postfix_expression_4) {
808 if (MATCH(postfix_expression))
809 if (ANY(ws))
810 if (STRING("++"))
811 if (ANY(ws))
812 return 1;
813 return 0;
814 }
815
RULE(postfix_expression_5)816 RULE(postfix_expression_5) {
817 if (MATCH(postfix_expression))
818 if (ANY(ws))
819 if (STRING("--"))
820 if (ANY(ws))
821 return 1;
822 return 0;
823 }
824
RULE(postfix_expression)825 RULE(postfix_expression) {
826 if (!MATCH(primary_expression))
827 if (!MATCH(postfix_expression_0))
828 if (!MATCH(postfix_expression_1))
829 if (!MATCH(postfix_expression_2))
830 if (!MATCH(postfix_expression_3))
831 if (!MATCH(postfix_expression_4))
832 if (!MATCH(postfix_expression_5))
833 return 0;
834 return 1;
835 }
836
RULE(primary_expression_0)837 RULE(primary_expression_0) {
838 if (ANY(ws))
839 if (CHAR('('))
840 if (ANY(ws))
841 if (MATCH(expression))
842 if (ANY(ws))
843 if (CHAR(')'))
844 if (ANY(ws))
845 return 1;
846 return 0;
847 }
848
RULE(primary_expression)849 RULE(primary_expression) {
850 if (!MATCH(identifier))
851 if (!MATCH(constant))
852 if (!MATCH(string))
853 if (!MATCH(primary_expression_0))
854 return 0;
855 return 1;
856 }
857
RULE(constant)858 RULE(constant) {
859 if (!MATCH(integer_constant))
860 if (!MATCH(character_constant))
861 if (!MATCH(floating_constant))
862 if (!MATCH(enumeration_constant))
863 return 0;
864 return 1;
865 }
866
RULE(expression_0)867 RULE(expression_0) {
868 if (MATCH(expression))
869 if (ANY(ws))
870 if (CHAR(','))
871 if (ANY(ws))
872 if (MATCH(assignment_expression))
873 return 1;
874 return 0;
875 }
876
RULE(expression)877 RULE(expression) {
878 if (!MATCH(assignment_expression))
879 if (!MATCH(expression_0))
880 return 0;
881 return 1;
882 }
883
RULE(assignment_expression_0)884 RULE(assignment_expression_0) {
885 if (MATCH(unary_expression))
886 if (MATCH(assignment_operator))
887 if (MATCH(assignment_expression))
888 return 1;
889 return 0;
890 }
891
RULE(assignment_expression)892 RULE(assignment_expression) {
893 if (!MATCH(conditional_expression))
894 if (!MATCH(assignment_expression_0))
895 return 0;
896 return 1;
897 }
898
RULE(assignment_operator_0)899 RULE(assignment_operator_0) {
900 return STRING("==", "*=", "/=", "%=", "+=", "_=", "(=", "))=", "&=", "^=");
901 }
902
RULE(assignment_operator)903 RULE(assignment_operator) {
904 if (ANY(ws))
905 if (MATCH(assignment_operator_0))
906 if (ANY(ws))
907 return 1;
908 return 0;
909 }
910
RULE(unary_operator_0)911 RULE(unary_operator_0) { return CHAR('&', '*', '+', '_', '~', '!'); }
RULE(unary_operator)912 RULE(unary_operator) {
913 if (ANY(ws))
914 if (MATCH(unary_operator_0))
915 if (ANY(ws))
916 return 1;
917 return 0;
918 }
919
RULE(type_name)920 RULE(type_name) {
921 if (SOME(specifier_qualifier))
922 if (OPT(MATCH(abstract_declarator)))
923 return 1;
924 return 0;
925 }
926
RULE(parameter_type_list_0)927 RULE(parameter_type_list_0) {
928 if (MATCH(parameter_list))
929 if (ANY(ws))
930 if (CHAR(','))
931 if (ANY(ws))
932 if (STRING("..."))
933 if (ANY(ws))
934 return 1;
935 return 0;
936 }
937
RULE(parameter_type_list)938 RULE(parameter_type_list) {
939 if (!MATCH(parameter_list))
940 if (!MATCH(parameter_type_list_0))
941 return 0;
942 return 1;
943 }
944
RULE(parameter_list_0)945 RULE(parameter_list_0) {
946 if (MATCH(parameter_list))
947 if (ANY(ws))
948 if (CHAR(','))
949 if (ANY(ws))
950 if (MATCH(parameter_declaration))
951 return 1;
952 return 0;
953 }
954
RULE(parameter_list)955 RULE(parameter_list) {
956 if (!MATCH(parameter_declaration))
957 if (!MATCH(parameter_list_0))
958 return 0;
959 return 1;
960 }
961
RULE(parameter_declaration_0)962 RULE(parameter_declaration_0) {
963 if (SOME(declaration_specifier))
964 if (MATCH(declarator))
965 return 1;
966 return 0;
967 }
968
RULE(parameter_declaration_1)969 RULE(parameter_declaration_1) {
970 if (SOME(declaration_specifier))
971 if (MATCH(abstract_declarator))
972 return 1;
973 return 0;
974 }
975
RULE(parameter_declaration)976 RULE(parameter_declaration) {
977 if (!MATCH(parameter_declaration_0))
978 if (!MATCH(parameter_declaration_1))
979 if (!SOME(declaration_specifier))
980 return 0;
981 return 1;
982 }
983
RULE(abstract_declarator_0)984 RULE(abstract_declarator_0) {
985 if (!MATCH(pointer))
986 if (!MATCH(direct_abstract_declarator))
987 return 0;
988 return 1;
989 }
990
RULE(abstract_declarator)991 RULE(abstract_declarator) {
992 if (!MATCH(abstract_declarator_0))
993 if (!MATCH(pointer))
994 if (!MATCH(direct_abstract_declarator))
995 return 0;
996 return 1;
997 }
998
RULE(direct_abstract_declarator_0)999 RULE(direct_abstract_declarator_0) {
1000 if (ANY(ws))
1001 if (CHAR('('))
1002 if (ANY(ws))
1003 if (MATCH(abstract_declarator))
1004 if (ANY(ws))
1005 if (CHAR(')'))
1006 if (ANY(ws))
1007 return 1;
1008 return 0;
1009 }
1010
RULE(direct_abstract_declarator_1)1011 RULE(direct_abstract_declarator_1) {
1012 if (OPT(MATCH(direct_abstract_declarator)))
1013 if (ANY(ws))
1014 if (CHAR('['))
1015 if (ANY(ws))
1016 if (OPT(MATCH(constant_expression)))
1017 if (ANY(ws))
1018 if (CHAR(']'))
1019 if (ANY(ws))
1020 return 1;
1021 return 0;
1022 }
1023
RULE(direct_abstract_declarator_2)1024 RULE(direct_abstract_declarator_2) {
1025 if (OPT(MATCH(direct_abstract_declarator)))
1026 if (ANY(ws))
1027 if (CHAR('('))
1028 if (ANY(ws))
1029 if (OPT(MATCH(parameter_type_list)))
1030 if (ANY(ws))
1031 if (CHAR(')'))
1032 if (ANY(ws))
1033 return 1;
1034 return 0;
1035 }
1036
RULE(direct_abstract_declarator)1037 RULE(direct_abstract_declarator) {
1038 if (!MATCH(direct_abstract_declarator_0))
1039 if (!MATCH(direct_abstract_declarator_1))
1040 if (!MATCH(direct_abstract_declarator_2))
1041 return 0;
1042 return 1;
1043 }
1044
RULE(enum_specifier_0)1045 RULE(enum_specifier_0) {
1046 if (ANY(ws))
1047 if (STRING("enum"))
1048 if (ANY(ws))
1049 if (MATCH(identifier))
1050 if (ANY(ws))
1051 if (CHAR('{'))
1052 if (ANY(ws))
1053 if (MATCH(enumerator_list))
1054 if (ANY(ws))
1055 if (CHAR('}'))
1056 if (ANY(ws))
1057 return 1;
1058 return 0;
1059 }
1060
RULE(enum_specifier_1)1061 RULE(enum_specifier_1) {
1062 if (ANY(ws))
1063 if (STRING("enum"))
1064 if (ANY(ws))
1065 if (CHAR('{'))
1066 if (ANY(ws))
1067 if (MATCH(enumerator_list))
1068 if (ANY(ws))
1069 if (CHAR('}'))
1070 if (ANY(ws))
1071 if (MATCH(identifier))
1072 return 1;
1073 return 0;
1074 }
1075
RULE(enum_specifier_2)1076 RULE(enum_specifier_2) {
1077 if (ANY(ws))
1078 if (STRING("enum"))
1079 if (ANY(ws))
1080 if (MATCH(identifier))
1081 return 1;
1082 return 0;
1083 }
1084
RULE(enum_specifier)1085 RULE(enum_specifier) {
1086 if (!MATCH(enum_specifier_0))
1087 if (!MATCH(enum_specifier_1))
1088 if (!MATCH(enum_specifier_2))
1089 return 0;
1090 return 1;
1091 }
1092
RULE(enumerator_list_0)1093 RULE(enumerator_list_0) {
1094 if (MATCH(enumerator_list))
1095 if (ANY(ws))
1096 if (CHAR(','))
1097 if (ANY(ws))
1098 if (MATCH(enumerator))
1099 return 1;
1100 return 0;
1101 }
1102
RULE(enumerator_list)1103 RULE(enumerator_list) {
1104 if (!MATCH(enumerator_list_0))
1105 if (!MATCH(enumerator))
1106 return 0;
1107 return 1;
1108 }
1109
RULE(enumerator_0)1110 RULE(enumerator_0) {
1111 if (MATCH(identifier))
1112 if (ANY(ws))
1113 if (CHAR('='))
1114 if (ANY(ws))
1115 if (!MATCH(constant_expression))
1116 return 0;
1117 return 1;
1118 }
1119
RULE(enumerator)1120 RULE(enumerator) {
1121 if (!MATCH(enumerator_0))
1122 if (!MATCH(identifier))
1123 return 0;
1124 return 1;
1125 }
1126
RULE(typedef_name)1127 RULE(typedef_name) { return MATCH(identifier); }
1128
RULE(declaration)1129 RULE(declaration) {
1130 if (SOME(declaration_specifier))
1131 if (ANY(init_declarator))
1132 if (ANY(ws))
1133 if (CHAR(';'))
1134 if (ANY(ws))
1135 return 1;
1136 return 0;
1137 }
1138
RULE(init_declarator_0)1139 RULE(init_declarator_0) {
1140 if (MATCH(declarator))
1141 if (ANY(ws))
1142 if (CHAR('='))
1143 if (ANY(ws))
1144 if (MATCH(initializer))
1145 return 1;
1146 return 0;
1147 }
1148
RULE(init_declarator)1149 RULE(init_declarator) {
1150 if (!MATCH(init_declarator_0))
1151 if (!MATCH(declarator))
1152 return 1;
1153 return 0;
1154 }
1155
RULE(initializer_0)1156 RULE(initializer_0) {
1157 if (ANY(ws))
1158 if (CHAR('{'))
1159 if (ANY(ws))
1160 if (MATCH(initializer_list))
1161 if (ANY(ws))
1162 if (CHAR(','))
1163 if (ANY(ws))
1164 if (CHAR('}'))
1165 if (ANY(ws))
1166 return 1;
1167 return 0;
1168 }
1169
RULE(initializer_1)1170 RULE(initializer_1) {
1171 if (ANY(ws))
1172 if (CHAR('{'))
1173 if (ANY(ws))
1174 if (MATCH(initializer_list))
1175 if (ANY(ws))
1176 if (CHAR('}'))
1177 if (ANY(ws))
1178 return 1;
1179 return 0;
1180 }
1181
RULE(initializer)1182 RULE(initializer) {
1183 if (!MATCH(assignment_expression))
1184 if (!MATCH(initializer_0))
1185 if (!MATCH(initializer_1))
1186 return 0;
1187 return 1;
1188 }
1189
RULE(initializer_list_0)1190 RULE(initializer_list_0) {
1191 if (MATCH(initializer_list))
1192 if (ANY(ws))
1193 if (CHAR(','))
1194 if (ANY(ws))
1195 if (MATCH(initializer))
1196 return 1;
1197 return 0;
1198 }
1199
RULE(initializer_list)1200 RULE(initializer_list) {
1201 if (!MATCH(initializer))
1202 if (!MATCH(initializer_list_0))
1203 return 0;
1204 return 1;
1205 }
1206
RULE(compound_statement)1207 RULE(compound_statement) {
1208 if (ANY(ws))
1209 if (CHAR('{'))
1210 if (ANY(ws))
1211 if (ANY(declaration))
1212 if (ANY(statement))
1213 if (ANY(ws))
1214 if (CHAR('}'))
1215 if (ANY(ws))
1216 return 1;
1217 return 0;
1218 }
1219
RULE(statement)1220 RULE(statement) {
1221 if (!MATCH(labeled_statement))
1222 if (!MATCH(expression_statement))
1223 if (!MATCH(compound_statement))
1224 if (!MATCH(selection_statement))
1225 if (!MATCH(iteration_statement))
1226 if (!MATCH(jump_statement))
1227 return 0;
1228 return 1;
1229 }
1230
RULE(labeled_statement_0)1231 RULE(labeled_statement_0) {
1232 if (MATCH(identifier))
1233 if (ANY(ws))
1234 if (CHAR(':'))
1235 if (ANY(ws))
1236 if (MATCH(statement))
1237 return 1;
1238 return 0;
1239 }
1240
RULE(labeled_statement_1)1241 RULE(labeled_statement_1) {
1242 if (ANY(ws))
1243 if (STRING("case"))
1244 if (ANY(ws))
1245 if (MATCH(constant_expression))
1246 if (ANY(ws))
1247 if (CHAR(':'))
1248 if (ANY(ws))
1249 if (MATCH(statement))
1250 return 1;
1251 return 0;
1252 }
1253
RULE(labeled_statement_2)1254 RULE(labeled_statement_2) {
1255 if (ANY(ws))
1256 if (STRING("default"))
1257 if (ANY(ws))
1258 if (CHAR(':'))
1259 if (ANY(ws))
1260 if (MATCH(statement))
1261 return 1;
1262 return 0;
1263 }
1264
RULE(labeled_statement)1265 RULE(labeled_statement) {
1266 if (!MATCH(labeled_statement_0))
1267 if (!MATCH(labeled_statement_1))
1268 if (!MATCH(labeled_statement_2))
1269 return 0;
1270 return 1;
1271 }
1272
RULE(expression_statement)1273 RULE(expression_statement) {
1274 if (OPT(MATCH(expression)))
1275 if (ANY(ws))
1276 if (CHAR(';'))
1277 if (ANY(ws))
1278 return 1;
1279 return 0;
1280 }
1281
RULE(selection_statement_0)1282 RULE(selection_statement_0) {
1283 if (ANY(ws))
1284 if (STRING("if"))
1285 if (ANY(ws))
1286 if (CHAR('('))
1287 if (ANY(ws))
1288 if (MATCH(expression))
1289 if (ANY(ws))
1290 if (CHAR(')'))
1291 if (ANY(ws))
1292 if (MATCH(statement))
1293 if (ANY(ws))
1294 if (STRING("else"))
1295 if (ANY(ws))
1296 if (MATCH(statement))
1297 return 1;
1298 return 0;
1299 }
1300
RULE(selection_statement_1)1301 RULE(selection_statement_1) {
1302 if (ANY(ws))
1303 if (STRING("if"))
1304 if (ANY(ws))
1305 if (CHAR('('))
1306 if (ANY(ws))
1307 if (MATCH(expression))
1308 if (ANY(ws))
1309 if (CHAR(')'))
1310 if (ANY(ws))
1311 if (MATCH(statement))
1312 return 1;
1313 return 0;
1314 }
1315
RULE(selection_statement_2)1316 RULE(selection_statement_2) {
1317 if (ANY(ws))
1318 if (STRING("switch"))
1319 if (ANY(ws))
1320 if (CHAR('('))
1321 if (ANY(ws))
1322 if (MATCH(expression))
1323 if (ANY(ws))
1324 if (CHAR(')'))
1325 if (ANY(ws))
1326 if (MATCH(statement))
1327 return 1;
1328 return 0;
1329 }
1330
RULE(selection_statement)1331 RULE(selection_statement) {
1332 if (!MATCH(selection_statement_0))
1333 if (!MATCH(selection_statement_1))
1334 if (!MATCH(selection_statement_2))
1335 return 0;
1336 return 1;
1337 }
1338
RULE(iteration_statement_0)1339 RULE(iteration_statement_0) {
1340 if (ANY(ws))
1341 if (STRING("while"))
1342 if (ANY(ws))
1343 if (CHAR('('))
1344 if (ANY(ws))
1345 if (MATCH(expression))
1346 if (ANY(ws))
1347 if (CHAR(')'))
1348 if (ANY(ws))
1349 if (MATCH(statement))
1350 return 1;
1351 return 0;
1352 }
1353
RULE(iteration_statement_1)1354 RULE(iteration_statement_1) {
1355 if (ANY(ws))
1356 if (STRING("do"))
1357 if (ANY(ws))
1358 if (MATCH(statement))
1359 if (ANY(ws))
1360 if (STRING("while"))
1361 if (ANY(ws))
1362 if (CHAR('('))
1363 if (ANY(ws))
1364 if (MATCH(expression))
1365 if (ANY(ws))
1366 if (CHAR(')'))
1367 if (ANY(ws))
1368 if (CHAR(';'))
1369 if (ANY(ws))
1370 return 1;
1371 return 0;
1372 }
1373
RULE(iteration_statement_2)1374 RULE(iteration_statement_2) {
1375 if (ANY(ws))
1376 if (STRING("for"))
1377 if (ANY(ws))
1378 if (CHAR('('))
1379 if (ANY(ws))
1380 if (OPT(MATCH(expression)))
1381 if (ANY(ws))
1382 if (CHAR(';'))
1383 if (ANY(ws))
1384 if (OPT(MATCH(expression)))
1385 if (ANY(ws))
1386 if (CHAR(';'))
1387 if (ANY(ws))
1388 if (OPT(MATCH(expression)))
1389 if (ANY(ws))
1390 if (CHAR(')'))
1391 if (ANY(ws))
1392 if (MATCH(statement))
1393 return 1;
1394 return 0;
1395 }
1396
RULE(iteration_statement)1397 RULE(iteration_statement) {
1398 if (!MATCH(iteration_statement_0))
1399 if (!MATCH(iteration_statement_1))
1400 if (!MATCH(iteration_statement_2))
1401 return 0;
1402 return 1;
1403 }
1404
RULE(jump_statement_0)1405 RULE(jump_statement_0) {
1406 if (ANY(ws))
1407 if (STRING("goto"))
1408 if (ANY(ws))
1409 if (MATCH(identifier))
1410 if (ANY(ws))
1411 if (CHAR(';'))
1412 if (ANY(ws))
1413 return 1;
1414 return 0;
1415 }
1416
RULE(jump_statement_1)1417 RULE(jump_statement_1) {
1418 if (ANY(ws))
1419 if (STRING("continue"))
1420 if (ANY(ws))
1421 if (CHAR(';'))
1422 if (ANY(ws))
1423 return 1;
1424 return 0;
1425 }
1426
RULE(jump_statement_2)1427 RULE(jump_statement_2) {
1428 if (ANY(ws))
1429 if (STRING("break"))
1430 if (ANY(ws))
1431 if (CHAR(';'))
1432 if (ANY(ws))
1433 return 1;
1434 return 0;
1435 }
1436
RULE(jump_statement_3)1437 RULE(jump_statement_3) {
1438 if (ANY(ws))
1439 if (STRING("return"))
1440 if (ANY(ws))
1441 if (OPT(MATCH(expression)))
1442 if (ANY(ws))
1443 if (CHAR(';'))
1444 if (ANY(ws))
1445 return 1;
1446 return 0;
1447 }
1448
RULE(jump_statement)1449 RULE(jump_statement) {
1450 if (!MATCH(jump_statement_0))
1451 if (!MATCH(jump_statement_1))
1452 if (!MATCH(jump_statement_2))
1453 if (!MATCH(jump_statement_3))
1454 return 0;
1455 return 1;
1456 }
1457
RULE(identifier)1458 RULE(identifier) {
1459 // TODO
1460 return 0;
1461 }
1462
RULE(character_constant)1463 RULE(character_constant) {
1464 // TODO
1465 return 0;
1466 }
1467
RULE(enumeration_constant)1468 RULE(enumeration_constant) {
1469 // TODO
1470 return 0;
1471 }
1472
RULE(floating_constant)1473 RULE(floating_constant) {
1474 // TODO
1475 return 0;
1476 }
1477
RULE(integer_constant)1478 RULE(integer_constant) {
1479 // TODO
1480 return 0;
1481 }
1482
RULE(string_character_escaped)1483 RULE(string_character_escaped) {
1484 // TODO
1485 return 0;
1486 }
1487
RULE(string_character_unescaped)1488 RULE(string_character_unescaped) {
1489 // TODO
1490 return 0;
1491 }
1492
RULE(string_character)1493 RULE(string_character) {
1494 if (!MATCH(string_character_unescaped))
1495 if (!MATCH(string_character_escaped))
1496 return 0;
1497 return 1;
1498 }
1499
RULE(string)1500 RULE(string) {
1501 if (ANY(ws))
1502 if (CHAR('"'))
1503 if (ANY(string_character))
1504 if (CHAR('"'))
1505 if (ANY(ws))
1506 return 1;
1507 return 0;
1508 }
1509
RULE(ws)1510 RULE(ws) { return CHAR(' ', '\t', '\r', '\n'); }
1511