From 5064a7ebce75a26d0405c92040f1a40187fcc7e3 Mon Sep 17 00:00:00 2001 From: kartofen Date: Wed, 2 Jul 2025 22:55:08 +0300 Subject: turn clr into lalr and first steps for generating a parser --- util-tables.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'util-tables.c') diff --git a/util-tables.c b/util-tables.c index 83a015c..09df914 100644 --- a/util-tables.c +++ b/util-tables.c @@ -27,20 +27,20 @@ void util_tables_fill() follow[i] = xcalloc(total_symbols, sizeof(*follow[i])); first[i] = xcalloc(total_symbols, sizeof(*follow[i])); } - + for(size_t sym = 0; sym < total_symbols; sym++) { first[sym][sym] = 1; } - + for(size_t i = 0; i < total_productions; i++) { struct production *p = &grammar[i]; - + first[p->LHS][p->RHS[0]] = 1; - + for(size_t j = 1; j < p->nRHS; j++) follow[p->RHS[j-1]][p->RHS[j]] = 1; } - + #define set(e) if((e) != 1) changed = (e) = 1 int changed; @@ -66,7 +66,7 @@ void util_tables_free() free(follow[i]); free(first[i]); } - + free(follow); free(first); } @@ -75,7 +75,7 @@ void util_tables_print() { char *s1 = "-- FIRST --"; printf(" %*s%s\n", (int)((total_symbols*3) - strlen(s1))/2, "", s1); - + printf(" "); for(size_t i = 0; i < total_symbols; i++) printf("%2zu ", i); printf("\n"); @@ -88,7 +88,7 @@ void util_tables_print() } printf("\n"); - + char *s2 = "-- FOLLOW --"; printf(" %*s%s\n", (int)((total_symbols*3) - strlen(s2))/2, "", s2); @@ -119,7 +119,7 @@ enum symbol { SYMBOLS_END, }; -const size_t total_symbols = SYMBOLS_END; +size_t total_symbols = SYMBOLS_END; int symbol_is_terminal(symbol s) { return s < E; } int symbol_is_nonterminal(symbol s) { return s >= E; } -- cgit v1.2.3