10 #include "type_hash.h"
12 #include "adt/bitfiddle.h"
13 #include "adt/error.h"
14 #include "adt/array.h"
16 //#define PRINT_TOKENS
17 //#define ABORT_ON_ERROR
18 #define MAX_LOOKAHEAD 2
20 struct environment_entry_t {
22 declaration_t *old_declaration;
23 const void *old_context;
27 static token_t lookahead_buffer[MAX_LOOKAHEAD];
28 static int lookahead_bufpos;
29 static struct obstack environment_obstack;
30 static environment_entry_t **environment_stack = NULL;
31 static context_t *context = NULL;
32 static declaration_t *last_declaration = NULL;
33 static struct obstack temp_obst;
36 statement_t *parse_compound_statement(void);
38 statement_t *parse_statement(void);
41 expression_t *parse_sub_expression(unsigned precedence);
43 expression_t *parse_expression(void);
46 void *allocate_ast_zero(size_t size)
48 void *res = allocate_ast(size);
54 void *allocate_type_zero(size_t size)
56 void *res = obstack_alloc(type_obst, size);
62 * returns the top element of the environment stack
65 size_t environment_top(void)
67 return ARR_LEN(environment_stack);
75 token = lookahead_buffer[lookahead_bufpos];
76 lookahead_buffer[lookahead_bufpos] = lexer_token;
79 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
82 print_token(stderr, &token);
83 fprintf(stderr, "\n");
88 const token_t *la(int num)
90 assert(num > 0 && num <= MAX_LOOKAHEAD);
91 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
92 return & lookahead_buffer[pos];
96 void eat(token_type_t type)
98 assert(token.type == type);
102 void parser_print_error_prefix_pos(const source_position_t source_position)
104 fputs(source_position.input_name, stderr);
106 fprintf(stderr, "%d", source_position.linenr);
107 fputs(": error: ", stderr);
108 #ifdef ABORT_ON_ERROR
113 void parser_print_error_prefix(void)
115 parser_print_error_prefix_pos(token.source_position);
119 void parse_error(const char *message)
121 parser_print_error_prefix();
122 fprintf(stderr, "parse error: %s\n", message);
126 void parse_error_expected(const char *message, ...)
131 if(message != NULL) {
132 parser_print_error_prefix();
133 fprintf(stderr, "%s\n", message);
135 parser_print_error_prefix();
136 fputs("Parse error: got ", stderr);
137 print_token(stderr, &token);
138 fputs(", expected ", stderr);
140 va_start(args, message);
141 token_type_t token_type = va_arg(args, token_type_t);
142 while(token_type != 0) {
146 fprintf(stderr, ", ");
148 print_token_type(stderr, token_type);
149 token_type = va_arg(args, token_type_t);
152 fprintf(stderr, "\n");
156 void eat_until(int token_type)
158 while(token.type != token_type) {
159 if(token.type == T_EOF)
166 #define expect(expected) \
167 if(UNLIKELY(token.type != (expected))) { \
168 parse_error_expected(NULL, (expected), 0); \
174 #define expect_void(expected) \
175 if(UNLIKELY(token.type != (expected))) { \
176 parse_error_expected(NULL, (expected), 0); \
182 static void set_context(context_t *new_context)
184 context = new_context;
186 declaration_t *declaration = new_context->declarations;
187 if(declaration != NULL) {
189 if(declaration->next == NULL)
191 declaration = declaration->next;
195 last_declaration = declaration;
199 * called when we find a 2nd declarator for an identifier we already have a
202 static int is_compatible_declaration (declaration_t *declaration,
203 declaration_t *previous)
205 /* TODO: not correct yet */
206 return declaration->type == previous->type;
210 * pushs an environment_entry on the environment stack and links the
211 * corresponding symbol to the new entry
214 declaration_t *environment_push(declaration_t *declaration, const void *context)
216 environment_entry_t *entry
217 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
218 memset(entry, 0, sizeof(entry[0]));
220 int top = ARR_LEN(environment_stack);
221 ARR_RESIZE(environment_stack, top + 1);
222 environment_stack[top] = entry;
224 assert(declaration->source_position.input_name != NULL);
226 symbol_t *symbol = declaration->symbol;
227 assert(declaration != symbol->declaration);
229 if(symbol->context == context) {
230 declaration_t *previous_declaration = symbol->declaration;
231 if(symbol->declaration != NULL) {
232 if(!is_compatible_declaration(declaration, previous_declaration)) {
233 parser_print_error_prefix_pos(declaration->source_position);
234 fprintf(stderr, "definition of symbol '%s' with type ",
235 declaration->symbol->string);
236 print_type(declaration->type, NULL);
238 parser_print_error_prefix_pos(
239 previous_declaration->source_position);
240 fprintf(stderr, "is incompatible with previous declaration "
242 print_type(previous_declaration->type, NULL);
245 return previous_declaration;
249 entry->old_declaration = symbol->declaration;
250 entry->old_context = symbol->context;
251 entry->symbol = symbol;
252 symbol->declaration = declaration;
253 symbol->context = context;
259 * pops symbols from the environment stack until @p new_top is the top element
262 void environment_pop_to(size_t new_top)
264 environment_entry_t *entry = NULL;
265 size_t top = ARR_LEN(environment_stack);
271 assert(new_top < top);
274 entry = environment_stack[i - 1];
276 symbol_t *symbol = entry->symbol;
278 symbol->declaration = entry->old_declaration;
279 symbol->context = entry->old_context;
282 } while(i != new_top);
283 obstack_free(&environment_obstack, entry);
285 ARR_SHRINKLEN(environment_stack, (int) new_top);
290 static expression_t *parse_constant_expression(void)
292 /* TODO: not correct yet */
293 return parse_expression();
296 static expression_t *parse_assignment_expression(void)
298 /* TODO: not correct yet */
299 return parse_expression();
302 static void parse_compound_type_entries(void);
303 static void parse_declarator(declaration_t *declaration,
304 storage_class_t storage_class, type_t *type,
305 int may_be_abstract);
306 static declaration_t *record_declaration(declaration_t *declaration);
308 typedef struct declaration_specifiers_t declaration_specifiers_t;
309 struct declaration_specifiers_t {
310 storage_class_t storage_class;
314 static compound_type_t *find_compound_type(compound_type_t *types,
315 const symbol_t *symbol)
317 compound_type_t *type = types;
318 for( ; type != NULL; type = type->next) {
319 if(type->symbol == symbol)
326 static type_t *parse_compound_type_specifier(int is_struct)
334 symbol_t *symbol = NULL;
335 compound_type_t *compound_type = NULL;
337 if(token.type == T_IDENTIFIER) {
338 symbol = token.v.symbol;
341 if(context != NULL) {
343 compound_type = find_compound_type(context->structs, symbol);
345 compound_type = find_compound_type(context->unions, symbol);
348 } else if(token.type != '{') {
350 parse_error_expected("problem while parsing struct type specifiers",
351 T_IDENTIFIER, '{', 0);
353 parse_error_expected("problem while parsing union type specifiers",
354 T_IDENTIFIER, '{', 0);
360 if(compound_type == NULL) {
361 compound_type = allocate_type_zero(sizeof(compound_type[0]));
364 compound_type->type.type = TYPE_COMPOUND_STRUCT;
366 compound_type->type.type = TYPE_COMPOUND_UNION;
368 compound_type->source_position = token.source_position;
369 compound_type->symbol = symbol;
372 if(token.type == '{') {
373 if(compound_type->defined) {
374 parser_print_error_prefix();
375 fprintf(stderr, "multiple definition of %s %s\n",
376 is_struct ? "struct" : "union", symbol->string);
377 compound_type->context.declarations = NULL;
379 compound_type->defined = 1;
381 int top = environment_top();
382 context_t *last_context = context;
383 set_context(&compound_type->context);
385 parse_compound_type_entries();
387 assert(context == &compound_type->context);
388 set_context(last_context);
389 environment_pop_to(top);
392 return (type_t*) compound_type;
395 static enum_entry_t *parse_enum_type_entries(void)
399 if(token.type == '}') {
401 parse_error("empty enum not allowed");
405 enum_entry_t *result = NULL;
406 enum_entry_t *last_entry = NULL;
408 enum_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
409 if(token.type != T_IDENTIFIER) {
410 parse_error_expected("problem while parsing enum entry",
415 entry->symbol = token.v.symbol;
418 if(token.type == '=') {
420 entry->value = parse_constant_expression();
423 if(last_entry != NULL) {
424 last_entry->next = entry;
430 if(token.type != ',')
433 } while(token.type != '}');
439 static type_t *parse_enum_specifier(void)
443 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
444 enum_type->type.type = TYPE_ENUM;
445 enum_type->source_position = token.source_position;
447 /* TODO: rewrite to the same style as struct/union above to handle
448 * type identities correctly
451 if(token.type == T_IDENTIFIER) {
452 enum_type->symbol = token.v.symbol;
454 if(token.type == '{') {
455 enum_type->entries = parse_enum_type_entries();
457 } else if(token.type == '{') {
458 enum_type->entries = parse_enum_type_entries();
460 parse_error_expected("problem while parsing enum type specifiers",
464 return (type_t*) enum_type;
468 const char *parse_string_literals(void)
470 assert(token.type == T_STRING_LITERAL);
471 const char *result = token.v.string;
475 while(token.type == T_STRING_LITERAL) {
476 result = concat_strings(result, token.v.string);
484 void parse_attributes(void)
488 case T___attribute__:
496 parse_error("EOF while parsing attribute");
514 if(token.type != T_STRING_LITERAL) {
515 parse_error_expected("while parsing assembler attribute",
520 parse_string_literals();
525 goto attributes_finished;
534 SPECIFIER_SIGNED = 1 << 0,
535 SPECIFIER_UNSIGNED = 1 << 1,
536 SPECIFIER_LONG = 1 << 2,
537 SPECIFIER_INT = 1 << 3,
538 SPECIFIER_DOUBLE = 1 << 4,
539 SPECIFIER_CHAR = 1 << 5,
540 SPECIFIER_SHORT = 1 << 6,
541 SPECIFIER_LONG_LONG = 1 << 7,
542 SPECIFIER_FLOAT = 1 << 8,
543 SPECIFIER_BOOL = 1 << 9,
544 SPECIFIER_VOID = 1 << 10,
545 #ifdef PROVIDE_COMPLEX
546 SPECIFIER_COMPLEX = 1 << 11,
548 #ifdef PROVIDE_IMAGINARY
549 SPECIFIER_IMAGINARY = 1 << 12,
553 #define STORAGE_CLASSES \
560 #define TYPE_QUALIFIERS \
565 case T___extension__:
567 #ifdef PROVIDE_COMPLEX
568 #define COMPLEX_SPECIFIERS \
571 #define COMPLEX_SPECIFIERS
574 #ifdef PROVIDE_IMAGINARY
575 #define IMAGINARY_SPECIFIERS \
578 #define IMAGINARY_SPECIFIERS
581 #define TYPE_SPECIFIERS \
598 #define DECLARATION_START \
604 type_t *create_builtin_type(symbol_t *symbol)
606 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
607 type->type.type = TYPE_BUILTIN;
608 type->symbol = symbol;
610 type_t *result = typehash_insert((type_t*) type);
611 if(result != (type_t*) type) {
612 obstack_free(type_obst, type);
619 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
621 declaration_t *declaration;
623 unsigned type_qualifiers = 0;
624 unsigned type_specifiers = 0;
631 #define MATCH_STORAGE_CLASS(token, class) \
633 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
634 parse_error("multiple storage classes in declaration " \
637 specifiers->storage_class = class; \
641 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
642 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
643 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
644 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
645 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
647 /* type qualifiers */
648 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
650 type_qualifiers |= qualifier; \
654 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
655 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
656 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
657 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
659 case T___extension__:
664 /* type specifiers */
665 #define MATCH_SPECIFIER(token, specifier, name) \
668 if(type_specifiers & specifier) { \
669 parse_error("multiple " name " type specifiers given"); \
671 type_specifiers |= specifier; \
675 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
676 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
677 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
678 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
679 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
680 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
681 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
682 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
683 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
684 #ifdef PROVIDE_COMPLEX
685 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
687 #ifdef PROVIDE_IMAGINARY
688 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
692 if(type_specifiers & SPECIFIER_LONG_LONG) {
693 parse_error("multiple type specifiers given");
694 } else if(type_specifiers & SPECIFIER_LONG) {
695 type_specifiers |= SPECIFIER_LONG_LONG;
697 type_specifiers |= SPECIFIER_LONG;
701 /* TODO: if type != NULL for the following rules issue an error */
703 type = parse_compound_type_specifier(1);
706 type = parse_compound_type_specifier(0);
709 type = parse_enum_specifier();
711 case T___builtin_va_list:
712 type = create_builtin_type(token.v.symbol);
716 case T___attribute__:
722 declaration = token.v.symbol->declaration;
723 if(declaration == NULL ||
724 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
725 goto finish_specifiers;
728 type = declaration->type;
729 assert(type != NULL);
733 /* function specifier */
735 goto finish_specifiers;
742 atomic_type_type_t atomic_type;
744 /* match valid basic types */
745 switch(type_specifiers) {
747 atomic_type = ATOMIC_TYPE_VOID;
750 atomic_type = ATOMIC_TYPE_CHAR;
752 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
753 atomic_type = ATOMIC_TYPE_SCHAR;
755 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
756 atomic_type = ATOMIC_TYPE_UCHAR;
758 case SPECIFIER_SHORT:
759 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
760 case SPECIFIER_SHORT | SPECIFIER_INT:
761 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
762 atomic_type = ATOMIC_TYPE_SHORT;
764 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
765 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
766 atomic_type = ATOMIC_TYPE_USHORT;
769 case SPECIFIER_SIGNED:
770 case SPECIFIER_SIGNED | SPECIFIER_INT:
771 atomic_type = ATOMIC_TYPE_INT;
773 case SPECIFIER_UNSIGNED:
774 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
775 atomic_type = ATOMIC_TYPE_UINT;
778 case SPECIFIER_SIGNED | SPECIFIER_LONG:
779 case SPECIFIER_LONG | SPECIFIER_INT:
780 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
781 atomic_type = ATOMIC_TYPE_LONG;
783 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
784 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
785 atomic_type = ATOMIC_TYPE_ULONG;
787 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
788 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
789 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
790 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
792 atomic_type = ATOMIC_TYPE_LONGLONG;
794 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
795 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
797 atomic_type = ATOMIC_TYPE_ULONGLONG;
799 case SPECIFIER_FLOAT:
800 atomic_type = ATOMIC_TYPE_FLOAT;
802 case SPECIFIER_DOUBLE:
803 atomic_type = ATOMIC_TYPE_DOUBLE;
805 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
806 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
809 atomic_type = ATOMIC_TYPE_BOOL;
811 #ifdef PROVIDE_COMPLEX
812 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
813 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
815 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
816 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
818 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
819 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
822 #ifdef PROVIDE_IMAGINARY
823 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
824 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
826 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
827 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
829 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
830 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
834 /* invalid specifier combination, give an error message */
835 if(type_specifiers == 0) {
836 parse_error("no type specifiers given in declaration");
837 } else if((type_specifiers & SPECIFIER_SIGNED) &&
838 (type_specifiers & SPECIFIER_UNSIGNED)) {
839 parse_error("signed and unsigned specifiers gives");
840 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
841 parse_error("only integer types can be signed or unsigned");
843 parse_error("multiple datatypes in declaration");
845 atomic_type = ATOMIC_TYPE_INVALID;
848 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
849 atype->type.type = TYPE_ATOMIC;
850 atype->atype = atomic_type;
853 type = (type_t*) atype;
855 if(type_specifiers != 0) {
856 parse_error("multiple datatypes in declaration");
860 type->qualifiers = type_qualifiers;
862 type_t *result = typehash_insert(type);
863 if(newtype && result != (type_t*) type) {
864 obstack_free(type_obst, type);
867 specifiers->type = result;
871 unsigned parse_type_qualifiers(void)
873 unsigned type_qualifiers = 0;
877 /* type qualifiers */
878 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
879 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
880 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
881 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
884 return type_qualifiers;
889 typedef struct parsed_pointer_t parsed_pointer_t;
890 struct parsed_pointer_t {
891 unsigned type_qualifiers;
892 parsed_pointer_t *next;
896 parsed_pointer_t *parse_pointers(void)
898 parsed_pointer_t *result = NULL;
899 parsed_pointer_t *last_pointer = NULL;
901 while(token.type == '*') {
903 parsed_pointer_t *pointer
904 = obstack_alloc(&temp_obst, sizeof(pointer[0]));
905 pointer->type_qualifiers = parse_type_qualifiers();
907 if(last_pointer != NULL) {
908 last_pointer->next = pointer;
912 last_pointer = pointer;
919 type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
921 for( ; pointer != NULL; pointer = pointer->next) {
922 pointer_type_t *pointer_type
923 = allocate_type_zero(sizeof(pointer_type[0]));
924 pointer_type->type.type = TYPE_POINTER;
925 pointer_type->points_to = type;
926 pointer_type->type.qualifiers = pointer->type_qualifiers;
928 type_t *result = typehash_insert((type_t*) pointer_type);
929 if(result != (type_t*) pointer_type) {
930 obstack_free(type_obst, pointer_type);
940 void parse_identifier_list(void)
943 if(token.type != T_IDENTIFIER) {
944 parse_error_expected("problem while parsing parameter identifier "
945 "list", T_IDENTIFIER, 0);
949 if(token.type != ',')
956 declaration_t *parse_parameter(void)
958 declaration_specifiers_t specifiers;
959 memset(&specifiers, 0, sizeof(specifiers));
961 parse_declaration_specifiers(&specifiers);
963 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
964 parse_declarator(declaration, specifiers.storage_class,
971 declaration_t *parse_parameters(method_type_t *type)
973 if(token.type == T_IDENTIFIER) {
974 symbol_t *symbol = token.v.symbol;
975 declaration_t *declaration = symbol->declaration;
976 if(declaration == NULL
977 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
979 parse_identifier_list();
984 if(token.type == ')') {
985 type->unspecified_parameters = 1;
988 if(token.type == T_void && la(1)->type == ')') {
993 declaration_t *declarations = NULL;
994 declaration_t *declaration;
995 declaration_t *last_declaration = NULL;
996 method_parameter_t *parameter;
997 method_parameter_t *last_parameter = NULL;
1000 switch(token.type) {
1004 return declarations;
1008 declaration = parse_parameter();
1010 parameter = allocate_type_zero(sizeof(parameter[0]));
1011 parameter->type = declaration->type;
1013 if(last_parameter != NULL) {
1014 last_declaration->next = declaration;
1015 last_parameter->next = parameter;
1017 type->parameters = parameter;
1018 declarations = declaration;
1020 last_parameter = parameter;
1021 last_declaration = declaration;
1025 return declarations;
1027 if(token.type != ',')
1028 return declarations;
1033 typedef struct declarator_part declarator_part;
1034 struct declarator_part {
1035 parsed_pointer_t *pointers;
1036 method_type_t *method_type;
1037 declarator_part *inner;
1042 declarator_part *parse_inner_declarator(declaration_t *declaration,
1043 int may_be_abstract)
1045 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
1046 memset(part, 0, sizeof(part[0]));
1048 part->pointers = parse_pointers();
1050 /* TODO: find out if this is correct */
1053 switch(token.type) {
1055 if(declaration == NULL) {
1056 parse_error("no identifier expected in typename");
1058 declaration->symbol = token.v.symbol;
1059 declaration->source_position = token.source_position;
1065 part->inner = parse_inner_declarator(declaration, may_be_abstract);
1071 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1076 switch(token.type) {
1080 method_type_t *method_type
1081 = allocate_type_zero(sizeof(method_type[0]));
1082 method_type->type.type = TYPE_METHOD;
1084 declaration->context.declarations = parse_parameters(method_type);
1086 part->method_type = method_type;
1093 if(token.type == T_static) {
1097 unsigned type_qualifiers = parse_type_qualifiers();
1098 if(type_qualifiers != 0) {
1099 if(token.type == T_static) {
1106 if(token.type == '*' && la(1)->type == ']') {
1108 } else if(token.type != ']') {
1109 parse_assignment_expression();
1115 goto declarator_finished;
1119 declarator_finished:
1126 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1129 type = make_pointers(type, part->pointers);
1131 method_type_t *method_type = part->method_type;
1132 if(method_type != NULL) {
1133 method_type->result_type = type;
1135 type_t *result = typehash_insert((type_t*) method_type);
1136 if(result != (type_t*) method_type) {
1137 obstack_free(type_obst, method_type);
1143 } while(part != NULL);
1149 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1150 type_t *type, int may_be_abstract)
1152 declarator_part *part
1153 = parse_inner_declarator(declaration, may_be_abstract);
1156 declaration->type = construct_declarator_type(part, type);
1157 declaration->storage_class = storage_class;
1158 obstack_free(&temp_obst, part);
1163 type_t *parse_abstract_declarator(type_t *base_type)
1165 declarator_part *part = parse_inner_declarator(NULL, 1);
1167 type_t *result = construct_declarator_type(part, base_type);
1168 obstack_free(&temp_obst, part);
1173 static declaration_t *record_declaration(declaration_t *declaration)
1178 symbol_t *symbol = declaration->symbol;
1179 if(symbol != NULL) {
1180 declaration_t *alias = environment_push(declaration, context);
1181 if(alias != declaration)
1185 if(last_declaration != NULL) {
1186 last_declaration->next = declaration;
1188 context->declarations = declaration;
1190 last_declaration = declaration;
1196 void parser_error_multiple_definition(declaration_t *previous,
1197 declaration_t *declaration)
1199 parser_print_error_prefix_pos(declaration->source_position);
1200 fprintf(stderr, "multiple definition of symbol '%s'\n",
1201 declaration->symbol->string);
1202 parser_print_error_prefix_pos(previous->source_position);
1203 fprintf(stderr, "this is the location of the previous "
1208 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1211 declaration_t *ndeclaration
1212 = allocate_ast_zero(sizeof(ndeclaration[0]));
1214 parse_declarator(ndeclaration, specifiers->storage_class,
1215 specifiers->type, 0);
1216 declaration_t *declaration = record_declaration(ndeclaration);
1217 if(token.type == '=') {
1220 /* TODO: check that this is an allowed type (esp. no method type) */
1222 if(declaration->initializer != NULL) {
1223 parser_error_multiple_definition(declaration, ndeclaration);
1226 if(token.type == '{') {
1230 declaration->initializer = parse_assignment_expression();
1232 } else if(token.type == '{') {
1233 if(declaration->type->type != TYPE_METHOD) {
1234 parser_print_error_prefix();
1235 fprintf(stderr, "Declarator ");
1236 print_type(declaration->type, declaration->symbol);
1237 fprintf(stderr, " is not a method type.\n");
1240 if(declaration->initializer != NULL) {
1241 parser_error_multiple_definition(declaration, ndeclaration);
1244 statement_t *statement = parse_compound_statement();
1245 declaration->statement = statement;
1249 if(token.type != ',')
1257 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1260 if(token.type == ':') {
1262 parse_constant_expression();
1263 /* TODO (bitfields) */
1265 declaration_t *declaration
1266 = allocate_ast_zero(sizeof(declaration[0]));
1267 parse_declarator(declaration, specifiers->storage_class,
1268 specifiers->type, 0);
1270 /* TODO: check for doubled fields */
1271 record_declaration(declaration);
1273 if(token.type == ':') {
1275 parse_constant_expression();
1276 /* TODO (bitfields) */
1280 if(token.type != ',')
1287 static void parse_compound_type_entries(void)
1291 while(token.type != '}' && token.type != T_EOF) {
1292 declaration_specifiers_t specifiers;
1293 memset(&specifiers, 0, sizeof(specifiers));
1294 /* TODO not correct as this allows storage class stuff... but only
1295 * specifiers and qualifiers sould be allowed here */
1296 parse_declaration_specifiers(&specifiers);
1298 parse_struct_declarators(&specifiers);
1300 if(token.type == T_EOF) {
1301 parse_error("unexpected error while parsing struct");
1306 void parse_declaration(void)
1308 declaration_specifiers_t specifiers;
1309 memset(&specifiers, 0, sizeof(specifiers));
1310 parse_declaration_specifiers(&specifiers);
1312 if(token.type == ';') {
1316 parse_init_declarators(&specifiers);
1319 type_t *parse_typename(void)
1321 declaration_specifiers_t specifiers;
1322 memset(&specifiers, 0, sizeof(specifiers));
1323 parse_declaration_specifiers(&specifiers);
1324 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1325 /* TODO: improve error message, user does probably not know what a
1326 * storage class is...
1328 parse_error("typename may not have a storage class");
1331 type_t *result = parse_abstract_declarator(specifiers.type);
1339 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1340 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1341 expression_t *left);
1343 typedef struct expression_parser_function_t expression_parser_function_t;
1344 struct expression_parser_function_t {
1345 unsigned precedence;
1346 parse_expression_function parser;
1347 unsigned infix_precedence;
1348 parse_expression_infix_function infix_parser;
1351 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1354 expression_t *expected_expression_error(void)
1356 parser_print_error_prefix();
1357 fprintf(stderr, "expected expression, got token ");
1358 print_token(stderr, & token);
1359 fprintf(stderr, "\n");
1361 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1362 expression->type = EXPR_INVALID;
1369 expression_t *parse_string_const(void)
1371 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1373 cnst->expression.type = EXPR_STRING_LITERAL;
1374 cnst->value = parse_string_literals();
1376 return (expression_t*) cnst;
1380 expression_t *parse_int_const(void)
1382 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1384 cnst->expression.type = EXPR_CONST;
1385 cnst->value = token.v.intvalue;
1389 return (expression_t*) cnst;
1393 expression_t *parse_reference(void)
1395 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1397 ref->expression.type = EXPR_REFERENCE;
1398 ref->symbol = token.v.symbol;
1402 return (expression_t*) ref;
1406 expression_t *parse_cast(void)
1408 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1410 cast->expression.type = EXPR_UNARY;
1411 cast->type = UNEXPR_CAST;
1412 cast->expression.source_position = token.source_position;
1414 type_t *type = parse_typename();
1417 expression_t *value = parse_sub_expression(20);
1419 cast->expression.datatype = type;
1420 cast->value = value;
1422 return (expression_t*) cast;
1426 expression_t *parse_brace_expression(void)
1430 declaration_t *declaration;
1431 switch(token.type) {
1434 return parse_cast();
1436 declaration = token.v.symbol->declaration;
1437 if(declaration != NULL &&
1438 (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
1439 return parse_cast();
1443 expression_t *result = parse_expression();
1450 expression_t *parse_primary_expression(void)
1452 switch(token.type) {
1454 return parse_int_const();
1455 case T_STRING_LITERAL:
1456 return parse_string_const();
1458 return parse_reference();
1460 return parse_brace_expression();
1463 /* TODO: error message */
1468 expression_t *parse_array_expression(unsigned precedence,
1469 expression_t *array_ref)
1475 array_access_expression_t *array_access
1476 = allocate_ast_zero(sizeof(array_access[0]));
1478 array_access->expression.type = EXPR_ARRAY_ACCESS;
1479 array_access->array_ref = array_ref;
1480 array_access->index = parse_expression();
1482 if(token.type != ']') {
1483 parse_error_expected("Problem while parsing array access", ']', 0);
1488 return (expression_t*) array_access;
1492 type_t *get_expression_type(const expression_t *expression)
1500 expression_t *parse_sizeof(unsigned precedence)
1504 sizeof_expression_t *sizeof_expression
1505 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1506 sizeof_expression->expression.type = EXPR_SIZEOF;
1508 if(token.type == '(' /* && LA1 is type_specifier */) {
1510 sizeof_expression->type = parse_typename();
1513 expression_t *expression = parse_sub_expression(precedence);
1514 sizeof_expression->type = get_expression_type(expression);
1517 return (expression_t*) sizeof_expression;
1521 expression_t *parse_select_expression(unsigned precedence,
1522 expression_t *compound)
1526 assert(token.type == '.' || token.type == T_SELECT);
1529 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1531 select->expression.type = EXPR_SELECT;
1532 select->compound = compound;
1534 if(token.type != T_IDENTIFIER) {
1535 parse_error_expected("Problem while parsing compound select",
1539 select->symbol = token.v.symbol;
1542 return (expression_t*) select;
1546 expression_t *parse_call_expression(unsigned precedence,
1547 expression_t *expression)
1550 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1552 call->expression.type = EXPR_CALL;
1553 call->method = expression;
1555 /* parse arguments */
1558 if(token.type != ')') {
1559 call_argument_t *last_argument = NULL;
1562 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1564 argument->expression = parse_expression();
1565 if(last_argument == NULL) {
1566 call->arguments = argument;
1568 last_argument->next = argument;
1570 last_argument = argument;
1572 if(token.type != ',')
1579 return (expression_t*) call;
1583 expression_t *parse_conditional_expression(unsigned precedence,
1584 expression_t *expression)
1588 conditional_expression_t *conditional
1589 = allocate_ast_zero(sizeof(conditional[0]));
1590 conditional->condition = expression;
1592 conditional->true_expression = parse_expression();
1594 conditional->false_expression = parse_sub_expression(precedence);
1596 return (expression_t*) conditional;
1599 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1601 expression_t *parse_##unexpression_type(unsigned precedence) \
1605 unary_expression_t *unary_expression \
1606 = allocate_ast_zero(sizeof(unary_expression[0])); \
1607 unary_expression->expression.type = EXPR_UNARY; \
1608 unary_expression->type = unexpression_type; \
1609 unary_expression->value = parse_sub_expression(precedence); \
1611 return (expression_t*) unary_expression; \
1614 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1615 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1616 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1617 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1618 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1619 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1620 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1621 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1623 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1625 expression_t *parse_##unexpression_type(unsigned precedence, \
1626 expression_t *left) \
1628 (void) precedence; \
1631 unary_expression_t *unary_expression \
1632 = allocate_ast_zero(sizeof(unary_expression[0])); \
1633 unary_expression->expression.type = EXPR_UNARY; \
1634 unary_expression->type = unexpression_type; \
1635 unary_expression->value = left; \
1637 return (expression_t*) unary_expression; \
1640 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1641 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1643 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1645 expression_t *parse_##binexpression_type(unsigned precedence, \
1646 expression_t *left) \
1650 expression_t *right = parse_sub_expression(precedence); \
1652 binary_expression_t *binexpr \
1653 = allocate_ast_zero(sizeof(binexpr[0])); \
1654 binexpr->expression.type = EXPR_BINARY; \
1655 binexpr->type = binexpression_type; \
1656 binexpr->left = left; \
1657 binexpr->right = right; \
1659 return (expression_t*) binexpr; \
1662 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1663 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1664 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1665 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1666 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1667 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1668 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1669 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1670 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1671 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1672 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1673 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1674 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1675 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1676 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1677 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1678 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1679 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1682 expression_t *parse_sub_expression(unsigned precedence)
1684 if(token.type < 0) {
1685 return expected_expression_error();
1688 expression_parser_function_t *parser
1689 = &expression_parsers[token.type];
1690 source_position_t source_position = token.source_position;
1693 if(parser->parser != NULL) {
1694 left = parser->parser(parser->precedence);
1696 left = parse_primary_expression();
1699 left->source_position = source_position;
1702 if(token.type < 0) {
1703 return expected_expression_error();
1706 parser = &expression_parsers[token.type];
1707 if(parser->infix_parser == NULL)
1709 if(parser->infix_precedence < precedence)
1712 left = parser->infix_parser(parser->infix_precedence, left);
1714 left->source_position = source_position;
1721 expression_t *parse_expression(void)
1723 return parse_sub_expression(1);
1728 void register_expression_parser(parse_expression_function parser,
1729 int token_type, unsigned precedence)
1731 expression_parser_function_t *entry = &expression_parsers[token_type];
1733 if(entry->parser != NULL) {
1734 fprintf(stderr, "for token ");
1735 print_token_type(stderr, token_type);
1736 fprintf(stderr, "\n");
1737 panic("trying to register multiple expression parsers for a token");
1739 entry->parser = parser;
1740 entry->precedence = precedence;
1743 void register_expression_infix_parser(parse_expression_infix_function parser,
1744 int token_type, unsigned precedence)
1746 expression_parser_function_t *entry = &expression_parsers[token_type];
1748 if(entry->infix_parser != NULL) {
1749 fprintf(stderr, "for token ");
1750 print_token_type(stderr, token_type);
1751 fprintf(stderr, "\n");
1752 panic("trying to register multiple infix expression parsers for a "
1755 entry->infix_parser = parser;
1756 entry->infix_precedence = precedence;
1760 void init_expression_parsers(void)
1762 memset(&expression_parsers, 0, sizeof(expression_parsers));
1764 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1765 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1766 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1768 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1769 T_GREATERGREATER, 16);
1770 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1771 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1772 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1773 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1774 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1775 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1776 T_GREATEREQUAL, 14);
1777 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1778 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1779 T_EXCLAMATIONMARKEQUAL, 13);
1780 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1781 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1782 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1783 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
1784 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
1785 register_expression_infix_parser(parse_conditional_expression, '?', 7);
1786 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1788 register_expression_infix_parser(parse_array_expression, '[', 30);
1789 register_expression_infix_parser(parse_call_expression, '(', 30);
1790 register_expression_infix_parser(parse_select_expression, '.', 30);
1791 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1792 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1794 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1797 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1798 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1799 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1800 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1801 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1802 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1803 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1804 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1805 register_expression_parser(parse_sizeof, T_sizeof, 25);
1810 statement_t *parse_case_statement(void)
1821 statement_t *parse_default_statement(void)
1831 statement_t *parse_label_statement(void)
1841 statement_t *parse_if(void)
1849 if(token.type == T_else) {
1858 statement_t *parse_switch(void)
1870 statement_t *parse_while(void)
1882 statement_t *parse_do(void)
1895 statement_t *parse_for(void)
1899 if(token.type != ';') {
1900 /* TODO not correct... this could also be a declaration */
1904 if(token.type != ';') {
1908 if(token.type != ')') {
1918 statement_t *parse_goto(void)
1921 expect(T_IDENTIFIER);
1928 statement_t *parse_continue(void)
1933 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1934 statement->source_position = token.source_position;
1935 statement->type = STATEMENT_CONTINUE;
1941 statement_t *parse_break(void)
1950 statement_t *parse_return(void)
1954 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1955 statement->statement.type = STATEMENT_RETURN;
1956 if(token.type != ';') {
1957 statement->return_value = parse_expression();
1961 return (statement_t*) statement;
1965 statement_t *parse_declaration_statement(void)
1967 parse_declaration();
1972 statement_t *parse_expression_statement(void)
1979 statement_t *parse_statement(void)
1981 declaration_t *declaration;
1982 statement_t *statement = NULL;
1984 /* declaration or statement */
1985 switch(token.type) {
1987 statement = parse_case_statement();
1991 statement = parse_default_statement();
1995 statement = parse_compound_statement();
1999 statement = parse_if();
2003 statement = parse_switch();
2007 statement = parse_while();
2011 statement = parse_do();
2015 statement = parse_for();
2019 statement = parse_goto();
2023 statement = parse_continue();
2027 statement = parse_break();
2031 statement = parse_return();
2039 if(la(1)->type == ':') {
2040 statement = parse_label_statement();
2044 declaration = token.v.symbol->declaration;
2045 if(declaration != NULL &&
2046 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2047 statement = parse_declaration_statement();
2051 statement = parse_expression_statement();
2055 statement = parse_declaration_statement();
2063 statement_t *parse_compound_statement(void)
2067 compound_statement_t *compound_statement
2068 = allocate_ast_zero(sizeof(compound_statement[0]));
2069 compound_statement->statement.type = STATEMENT_COMPOUND;
2071 int top = environment_top();
2072 context_t *last_context = context;
2073 set_context(&compound_statement->context);
2075 statement_t *last_statement = NULL;
2077 while(token.type != '}' && token.type != T_EOF) {
2078 statement_t *statement = parse_statement();
2080 if(last_statement != NULL) {
2081 last_statement->next = statement;
2083 compound_statement->statements = statement;
2085 last_statement = statement;
2088 assert(context == &compound_statement->context);
2089 set_context(last_context);
2090 environment_pop_to(top);
2094 return (statement_t*) compound_statement;
2098 translation_unit_t *parse_translation_unit(void)
2100 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2102 assert(context == NULL);
2103 set_context(&unit->context);
2105 while(token.type != T_EOF) {
2106 parse_declaration();
2109 assert(context == &unit->context);
2111 last_declaration = NULL;
2116 translation_unit_t *parse(void)
2118 obstack_init(&environment_obstack);
2119 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2121 type_set_output(stderr);
2123 lookahead_bufpos = 0;
2124 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2127 translation_unit_t *unit = parse_translation_unit();
2129 DEL_ARR_F(environment_stack);
2130 obstack_free(&environment_obstack, NULL);
2135 void init_parser(void)
2137 init_expression_parsers();
2138 obstack_init(&temp_obst);
2141 void exit_parser(void)
2143 obstack_free(&temp_obst, NULL);