From e1a4f3a428d3b721b1a63cc0cb77369e8efe7ed8 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 3 Jul 2002 10:36:38 +0000 Subject: [PATCH] * src/closure.c (print_fderives): Use rule_rhs_print. * src/derives.c (print_derives): Use rule_rhs_print. (rule_list_t): New, replaces `shorts'. (set_derives): Add comments. * tests/sets.at (Nullable, Firsts): Adjust. --- ChangeLog | 10 ++++++++- src/closure.c | 7 ++---- src/derives.c | 44 ++++++++++++++++++++++-------------- tests/sets.at | 62 +++++++++++++++++++++++++-------------------------- 4 files changed, 69 insertions(+), 54 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3ff54419..606481ab 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2002-07-03 Akim Demaille + + * src/closure.c (print_fderives): Use rule_rhs_print. + * src/derives.c (print_derives): Use rule_rhs_print. + (rule_list_t): New, replaces `shorts'. + (set_derives): Add comments. + * tests/sets.at (Nullable, Firsts): Adjust. + 2002-07-03 Akim Demaille * src/output.c (prepare_actions): Free `tally' and `width'. @@ -8,7 +16,7 @@ * src/scan-skel.l: here. (<>): Close yyout, and free its name. - + 2002-07-03 Akim Demaille Fix some memory leaks, and fix a bug: state 0 was examined twice. diff --git a/src/closure.c b/src/closure.c index 02d275a7..33dd9b7a 100644 --- a/src/closure.c +++ b/src/closure.c @@ -99,11 +99,8 @@ print_fderives (void) fprintf (stderr, "\t%s derives\n", symbols[i]->tag); BITSET_FOR_EACH (iter, FDERIVES (i), r, 0) { - item_number_t *rhsp = NULL; - fprintf (stderr, "\t\t%d:", r - 1); - for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) - fprintf (stderr, " %s", symbols[*rhsp]->tag); - fputc ('\n', stderr); + fprintf (stderr, "\t\t%3d ", r); + rule_rhs_print (&rules[r], stderr); } } fprintf (stderr, "\n\n"); diff --git a/src/derives.c b/src/derives.c index 0f89833f..87b15f2d 100644 --- a/src/derives.c +++ b/src/derives.c @@ -27,6 +27,13 @@ #include "gram.h" #include "derives.h" +/* Linked list of rule numbers. */ +typedef struct rule_list_s +{ + struct rule_list_s *next; + rule_number_t value; +} rule_list_t; + rule_number_t **derives = NULL; static void @@ -42,12 +49,8 @@ print_derives (void) fprintf (stderr, "\t%s derives\n", symbols[i]->tag); for (rp = derives[i]; *rp > 0; rp++) { - item_number_t *rhsp; - fprintf (stderr, "\t\t%d:", *rp); - for (rhsp = rules[*rp].rhs; *rhsp >= 0; ++rhsp) - fprintf (stderr, " %s", symbols[*rhsp]->tag); - fprintf (stderr, " (rule %d)\n", - rule_number_of_item_number (*rhsp) - 1); + fprintf (stderr, "\t\t%3d ", *rp - 1); + rule_rhs_print (&rules[*rp], stderr); } } @@ -60,31 +63,37 @@ set_derives (void) { symbol_number_t i; rule_number_t r; - shorts *p; rule_number_t *q; - shorts **dset; - shorts *delts; - dset = XCALLOC (shorts *, nvars) - ntokens; - delts = XCALLOC (shorts, nrules + 1); + /* DSET[NTERM] -- A linked list of the numbers of the rules whose + LHS is NTERM. */ + rule_list_t **dset = XCALLOC (rule_list_t *, nvars) - ntokens; + + /* DELTS[RULE] -- There are NRULES rule number to attach to nterms. + Instead of performing NRULES allocations for each, have an array + indexed by rule numbers. */ + rule_list_t *delts = XCALLOC (rule_list_t, nrules + 1); - p = delts; for (r = nrules; r > 0; r--) { symbol_number_t lhs = rules[r].lhs->number; + rule_list_t *p = &delts[r]; + /* A new LHS is found. */ p->next = dset[lhs]; p->value = r; dset[lhs] = p; - p++; } + /* DSET contains what we need under the form of a linked list. Make + it a single array. */ + derives = XCALLOC (rule_number_t *, nvars) - ntokens; - q = XCALLOC (short, nvars + int_of_rule_number (nrules)); + q = XCALLOC (rule_number_t, nvars + int_of_rule_number (nrules)); for (i = ntokens; i < nsyms; i++) { + rule_list_t *p = dset[i]; derives[i] = q; - p = dset[i]; while (p) { *q++ = p->value; @@ -96,10 +105,11 @@ set_derives (void) if (trace_flag) print_derives (); - XFREE (dset + ntokens); - XFREE (delts); + free (dset + ntokens); + free (delts); } + void free_derives (void) { diff --git a/tests/sets.at b/tests/sets.at index 0cbc3da6..e1cf3ff6 100644 --- a/tests/sets.at +++ b/tests/sets.at @@ -83,10 +83,10 @@ AT_EXTRACT_SETS([stderr], [sets]) AT_CHECK([[cat sets]], [], [[DERIVES $axiom derives - 1: e $ (rule 0) + 0 e $ e derives - 2: 'e' (rule 1) - 3: (rule 2) + 1 'e' + 2 /* empty */ NULLABLE $axiom: no e: yes @@ -98,12 +98,12 @@ FIRSTS e FDERIVES $axiom derives - 0: e $ - 1: 'e' - 2: + 1 e $ + 2 'e' + 3 /* empty */ e derives - 1: 'e' - 2: + 2 'e' + 3 /* empty */ ]]) AT_CLEANUP @@ -213,15 +213,15 @@ AT_EXTRACT_SETS([stderr], [sets]) AT_CHECK([[cat sets]], [], [[DERIVES $axiom derives - 1: exp $ (rule 0) + 0 exp $ exp derives - 2: exp '<' exp (rule 1) - 3: exp '>' exp (rule 2) - 4: exp '+' exp (rule 3) - 5: exp '-' exp (rule 4) - 6: exp '^' exp (rule 5) - 7: exp '=' exp (rule 6) - 8: "exp" (rule 7) + 1 exp '<' exp + 2 exp '>' exp + 3 exp '+' exp + 4 exp '-' exp + 5 exp '^' exp + 6 exp '=' exp + 7 "exp" NULLABLE $axiom: no exp: no @@ -233,22 +233,22 @@ FIRSTS exp FDERIVES $axiom derives - 0: exp $ - 1: exp '<' exp - 2: exp '>' exp - 3: exp '+' exp - 4: exp '-' exp - 5: exp '^' exp - 6: exp '=' exp - 7: "exp" + 1 exp $ + 2 exp '<' exp + 3 exp '>' exp + 4 exp '+' exp + 5 exp '-' exp + 6 exp '^' exp + 7 exp '=' exp + 8 "exp" exp derives - 1: exp '<' exp - 2: exp '>' exp - 3: exp '+' exp - 4: exp '-' exp - 5: exp '^' exp - 6: exp '=' exp - 7: "exp" + 2 exp '<' exp + 3 exp '>' exp + 4 exp '+' exp + 5 exp '-' exp + 6 exp '^' exp + 7 exp '=' exp + 8 "exp" ]]) AT_CLEANUP -- 2.45.2