diff options
author | Matthew Sotoudeh <matthew@masot.net> | 2024-02-19 16:41:13 -0800 |
---|---|---|
committer | Matthew Sotoudeh <matthew@masot.net> | 2024-02-19 16:41:13 -0800 |
commit | 26a42b4a7ba077659f791208a2a7989bfdfb3663 (patch) | |
tree | 02069692c4d629d3108bbed43c4eb6eddfd2fbc7 /grammars/c/disambiguate.c | |
parent | e133f250761c67b4465181f41909e78c272901d3 (diff) |
playing with the C grammar
Diffstat (limited to 'grammars/c/disambiguate.c')
-rw-r--r-- | grammars/c/disambiguate.c | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/grammars/c/disambiguate.c b/grammars/c/disambiguate.c index a2d69d1..403d65f 100644 --- a/grammars/c/disambiguate.c +++ b/grammars/c/disambiguate.c @@ -1,14 +1,37 @@ +struct token *TYPE_NAMES[1024]; +size_t N_TYPE_NAMES; + +void alert_parse(struct state *state) { + if (PRODUCTION_ID_TO_SYMBOL[state->production_id] == SYMBOL_TYPEDEF) { + for (struct token *t = find_token(state, 2); t->symbol != DONE_SYMBOL; t++) { + if (t->symbol == SYMBOL_IDENT) { + TYPE_NAMES[N_TYPE_NAMES++] = t; + break; + } + } + } +} + +int is_typename(struct token *token) { + if (!strcmp("int", token->string)) return 1; + for (size_t i = 0; i < N_TYPE_NAMES; i++) + if (!strcmp(TYPE_NAMES[i]->string, token->string)) + return 1; + return 0; +} + int disambiguator(struct state *old, struct state *new) { // printf("Old tree:\n"); // print_parse_tree(old, 4); // printf("New tree:\n"); // print_parse_tree(new, 4); - if (old->start_idx != new->start_idx) { - // printf("\t\tIGNORING "); print_parse_tree2(old); - // printf("\t\tVS: "); print_parse_tree2(new); - return 2; - } + if (old->production_id == PRODUCTION_DECL_STMT) + if (!is_typename(find_token(old->reasons[0], 0))) + return 1; + if (new->production_id == PRODUCTION_DECL_STMT) + if (!is_typename(find_token(new->reasons[0], 0))) + return 0; // Prefer the earlier parsings in the grammar when two entirely different // productions are taken. |