From 75142d452b7f6bc3b6be3d8a08c0f602fd157ec5 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 27 Dec 2001 18:13:31 +0000 Subject: [PATCH] Try to make the use of the eoftoken valid. Given that its value is 0 which was also used as a sentinel in ritem, (i) make sure >= 0 is used instead of > 0 where appropriate, (ii), depend upon nritems instead of the 0-sentinel. * src/gram.h, src/gram.c (nritems): New. Expected to be duplication of nitems, but for the time being... * src/reader.c (packgram): Assert nritems and nitems are equal. * src/LR0.c (allocate_itemsets, new_itemsets): Adjust. * src/closure.c (print_closure, print_fderives): Likewise. * src/gram.c (ritem_print): Likewise. * src/print.c (print_core, print_grammar): Likewise. * src/print_graph.c: Likewise. --- ChangeLog | 16 ++++++++++++++++ src/LR0.c | 6 +++--- src/closure.c | 4 ++-- src/gram.c | 6 ++++-- src/gram.h | 1 + src/output.c | 2 +- src/print.c | 14 +++++++------- src/print_graph.c | 4 ++-- src/reader.c | 2 ++ 9 files changed, 38 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4fd250f0..5c2bd8f4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2001-12-27 Akim Demaille + + Try to make the use of the eoftoken valid. Given that its value + is 0 which was also used as a sentinel in ritem, (i) make sure >= 0 + is used instead of > 0 where appropriate, (ii), depend upon nritems + instead of the 0-sentinel. + + * src/gram.h, src/gram.c (nritems): New. + Expected to be duplication of nitems, but for the time being... + * src/reader.c (packgram): Assert nritems and nitems are equal. + * src/LR0.c (allocate_itemsets, new_itemsets): Adjust. + * src/closure.c (print_closure, print_fderives): Likewise. + * src/gram.c (ritem_print): Likewise. + * src/print.c (print_core, print_grammar): Likewise. + * src/print_graph.c: Likewise. + 2001-12-27 Akim Demaille * src/main.c (main): If there are complains after grammar diff --git a/src/LR0.c b/src/LR0.c index a9a69655..9f019223 100644 --- a/src/LR0.c +++ b/src/LR0.c @@ -68,8 +68,8 @@ allocate_itemsets (void) int count = 0; short *symbol_count = XCALLOC (short, nsyms + nuseless_nonterminals); - for (i = 0; ritem[i]; ++i) - if (ritem[i] > 0) + for (i = 0; i < nritems; ++i) + if (ritem[i] >= 0) { count++; symbol_count[ritem[i]]++; @@ -152,7 +152,7 @@ new_itemsets (void) for (i = 0; i < nitemset; ++i) { int symbol = ritem[itemset[i]]; - if (symbol > 0) + if (symbol >= 0) { if (!kernel_size[symbol]) { diff --git a/src/closure.c b/src/closure.c index 12bb5817..45b85c68 100644 --- a/src/closure.c +++ b/src/closure.c @@ -60,7 +60,7 @@ print_closure (const char *title, short *array, size_t size) { short *rp; fprintf (stderr, " %2d: .", array[i]); - for (rp = &ritem[array[i]]; *rp > 0; ++rp) + for (rp = &ritem[array[i]]; *rp >= 0; ++rp) fprintf (stderr, " %s", tags[*rp]); fprintf (stderr, " (rule %d)\n", -*rp); } @@ -101,7 +101,7 @@ print_fderives (void) { short *rhsp; fprintf (stderr, "\t\t%d:", j); - for (rhsp = ritem + rule_table[j].rhs; *rhsp > 0; ++rhsp) + for (rhsp = ritem + rule_table[j].rhs; *rhsp >= 0; ++rhsp) fprintf (stderr, " %s", tags[*rhsp]); fputc ('\n', stderr); } diff --git a/src/gram.c b/src/gram.c index 9db02d8b..219bbee5 100644 --- a/src/gram.c +++ b/src/gram.c @@ -32,6 +32,8 @@ int ntokens; int nvars; short *ritem = NULL; +int nritems; + rule_t *rule_table = NULL; short *rprec = NULL; short *rprecsym = NULL; @@ -60,8 +62,8 @@ ritem_print (FILE *out) { int i; fputs ("RITEM\n", out); - for (i = 0; ritem[i]; ++i) - if (ritem[i] > 0) + for (i = 0; i < nritems; ++i) + if (ritem[i] >= 0) fprintf (out, " %s", tags[ritem[i]]); else fprintf (out, " (rule %d)\n", -ritem[i]); diff --git a/src/gram.h b/src/gram.h index 021061d2..336c38ed 100644 --- a/src/gram.h +++ b/src/gram.h @@ -98,6 +98,7 @@ extern int ntokens; extern int nvars; extern short *ritem; +extern int nritems; extern short *sprec; extern short *sassoc; diff --git a/src/output.c b/src/output.c index 34ff7d36..49e63944 100644 --- a/src/output.c +++ b/src/output.c @@ -204,7 +204,7 @@ output_gram (void) yyrhs = XMALLOC (short, yyrhs_size); for (sp = ritem + 1, i = 1; *sp; ++sp, ++i) - yyrhs[i] = *sp > 0 ? *sp : 0; + yyrhs[i] = *sp >= 0 ? *sp : 0; output_table_data (&format_obstack, yyrhs, ritem[0], 1, yyrhs_size); diff --git a/src/print.c b/src/print.c index 6c0002cd..dea92aca 100644 --- a/src/print.c +++ b/src/print.c @@ -88,7 +88,7 @@ print_core (FILE *out, state_t *state) sp1 = sp = ritem + sitems[i]; - while (*sp > 0) + while (*sp >= 0) sp++; rule = -(*sp); @@ -99,7 +99,7 @@ print_core (FILE *out, state_t *state) fputc ('.', out); - for (/* Nothing */; *sp > 0; ++sp) + for (/* Nothing */; *sp >= 0; ++sp) fprintf (out, " %s", escape (tags[*sp])); fprintf (out, _(" (rule %d)"), rule); @@ -377,8 +377,8 @@ print_grammar (FILE *out) fprintf (out, _(" %3d %3d %s ->"), i, rule_table[i].line, escape (tags[rule_table[i].lhs])); rule = &ritem[rule_table[i].rhs]; - if (*rule > 0) - while (*rule > 0) + if (*rule >= 0) + while (*rule >= 0) fprintf (out, " %s", escape (tags[*rule++])); else fprintf (out, " /* %s */", _("empty")); @@ -399,7 +399,7 @@ print_grammar (FILE *out) sprintf (buffer, " (%d)", i); for (j = 1; j <= nrules; j++) - for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++) + for (rule = &ritem[rule_table[j].rhs]; *rule >= 0; rule++) if (*rule == token_translations[i]) { END_TEST (65); @@ -420,7 +420,7 @@ print_grammar (FILE *out) { if (rule_table[j].lhs == i) left_count++; - for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++) + for (rule = &ritem[rule_table[j].rhs]; *rule >= 0; rule++) if (*rule == i) { right_count++; @@ -455,7 +455,7 @@ print_grammar (FILE *out) sprintf (buffer + strlen (buffer), _(" on right:")); for (j = 1; j <= nrules; j++) { - for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++) + for (rule = &ritem[rule_table[j].rhs]; *rule >= 0; rule++) if (*rule == i) { END_TEST (65); diff --git a/src/print_graph.c b/src/print_graph.c index 2c7d94c2..55e4b379 100644 --- a/src/print_graph.c +++ b/src/print_graph.c @@ -66,7 +66,7 @@ print_core (state_t *state, struct obstack *node_obstack) sp1 = sp = ritem + sitems[i]; - while (*sp > 0) + while (*sp >= 0) sp++; rule = -(*sp); @@ -81,7 +81,7 @@ print_core (state_t *state, struct obstack *node_obstack) obstack_1grow (node_obstack, '.'); - for (/* Nothing */; *sp > 0; ++sp) + for (/* Nothing */; *sp >= 0; ++sp) obstack_fgrow1 (node_obstack, " %s", escape (tags[*sp])); } } diff --git a/src/reader.c b/src/reader.c index c5f3b11e..2f583f1e 100644 --- a/src/reader.c +++ b/src/reader.c @@ -1764,6 +1764,8 @@ packgram (void) } ritem[itemno] = 0; + nritems = itemno; + assert (nritems == nitems); if (trace_flag) ritem_print (stderr); -- 2.45.2