(item_number_of_rule_number, rule_number_of_item_number): New.
* src/LR0.c, src/closure.c, src/derives.c, src/derives.h,
* src/gram.c, src/lalr.c, src/nullable.c, src/output.c, src/print.c,
* src/print_graph.c, src/reader.c, src/reduce.c, src/reduce.h:
Propagate their use.
Much remains to be done, in particular wrt `shorts' from types.h.
+2002-06-30 Akim Demaille <akim@epita.fr>
+
+ * src/gram.h (rule_number_t, RULE_NUMBER_MAX, int_of_rule_number)
+ (item_number_of_rule_number, rule_number_of_item_number): New.
+ * src/LR0.c, src/closure.c, src/derives.c, src/derives.h,
+ * src/gram.c, src/lalr.c, src/nullable.c, src/output.c, src/print.c,
+ * src/print_graph.c, src/reader.c, src/reduce.c, src/reduce.h:
+ Propagate their use.
+ Much remains to be done, in particular wrt `shorts' from types.h.
+
+
2002-06-30 Akim Demaille <akim@epita.fr>
* src/symtab.c (symbol_new): Initialize the `printer' member.
static void
allocate_itemsets (void)
{
- int i, r;
+ symbol_number_t i;
+ rule_number_t r;
item_number_t *rhsp;
/* Count the number of occurrences of all the symbols in RITEMS.
static void
print_fderives (void)
{
- int i, j;
+ int i;
+ rule_number_t r;
fprintf (stderr, "FDERIVES\n");
for (i = ntokens; i < nsyms; i++)
{
fprintf (stderr, "\t%s derives\n", symbol_tag_get (symbols[i]));
- for (j = 0; j < nrules + 1; j++)
- if (bitset_test (FDERIVES (i), j))
+ for (r = 0; r < nrules + 1; r++)
+ if (bitset_test (FDERIVES (i), r))
{
- item_number_t *rhsp;
- fprintf (stderr, "\t\t%d:", j - 1);
- for (rhsp = rules[j].rhs; *rhsp >= 0; ++rhsp)
+ item_number_t *rhsp = NULL;
+ fprintf (stderr, "\t\t%d:", r - 1);
+ for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
fprintf (stderr, " %s", symbol_tag_get (symbols[*rhsp]));
fputc ('\n', stderr);
}
static void
set_firsts (void)
{
- int i, j;
+ symbol_number_t i, j;
firsts = bitsetv_create (nvars, nvars, BITSET_FIXED);
static void
set_fderives (void)
{
- int i, j, k;
+ symbol_number_t i, j;
+ rule_number_t k;
fderives = bitsetv_create (nvars, nrules + 1, BITSET_FIXED);
int c;
/* A bit index over RULESET. */
- int ruleno;
+ rule_number_t ruleno;
if (trace_flag)
print_closure ("input", core, n);
/* Match rules with nonterminals for bison,
- Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
+ Copyright 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
#include "gram.h"
#include "derives.h"
-short **derives = NULL;
+rule_number_t **derives = NULL;
static void
print_derives (void)
for (i = ntokens; i < nsyms; i++)
{
- short *sp;
+ rule_number_t *rp;
fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
- for (sp = derives[i]; *sp > 0; sp++)
+ for (rp = derives[i]; *rp > 0; rp++)
{
item_number_t *rhsp;
- fprintf (stderr, "\t\t%d:", *sp);
- for (rhsp = rules[*sp].rhs; *rhsp >= 0; ++rhsp)
- fprintf (stderr, " %s", symbols[*rhsp]->tag);
- fprintf (stderr, " (rule %d)\n", -*rhsp - 1);
+ fprintf (stderr, "\t\t%d:", *rp);
+ for (rhsp = rules[*rp].rhs; *rhsp >= 0; ++rhsp)
+ fprintf (stderr, " %s", symbol_tag_get (symbols[*rhsp]));
+ fprintf (stderr, " (rule %d)\n",
+ rule_number_of_item_number (*rhsp) - 1);
}
}
void
set_derives (void)
{
- int i;
+ symbol_number_t i;
+ rule_number_t r;
shorts *p;
- short *q;
+ rule_number_t *q;
shorts **dset;
shorts *delts;
delts = XCALLOC (shorts, nrules + 1);
p = delts;
- for (i = nrules; i > 0; i--)
+ for (r = nrules; r > 0; r--)
{
- symbol_number_t lhs = rules[i].lhs->number;
+ symbol_number_t lhs = rules[r].lhs->number;
p->next = dset[lhs];
- p->value = i;
+ p->value = r;
dset[lhs] = p;
p++;
}
- derives = XCALLOC (short *, nvars) - ntokens;
- q = XCALLOC (short, nvars + nrules);
+ derives = XCALLOC (rule_number_t *, nvars) - ntokens;
+ q = XCALLOC (short, nvars + int_of_rule_number (nrules));
for (i = ntokens; i < nsyms; i++)
{
/* DERIVES[SYMBOL - NTOKENS] points to a vector of the number of the
rules that SYMBOL derives, terminated with -1. */
-extern short **derives;
+extern rule_number_t **derives;
/* Compute DERIVES. */
unsigned int nritems = 0;
rule_t *rules = NULL;
-int nrules = 0;
+rule_number_t nrules = 0;
symbol_t **symbols = NULL;
int nsyms = 0;
ritem_longest_rhs (void)
{
int max = 0;
- int i;
+ rule_number_t r;
- for (i = 1; i < nrules + 1; ++i)
+ for (r = 1; r < nrules + 1; ++r)
{
- int length = rule_rhs_length (&rules[i]);
+ int length = rule_rhs_length (&rules[r]);
if (length > max)
max = length;
}
void
grammar_rules_partial_print (FILE *out, const char *title,
- int begin, int end)
+ rule_number_t begin, rule_number_t end)
{
int r;
symbol_t *last_lhs = NULL;
void
grammar_dump (FILE *out, const char *title)
{
- int i;
- item_number_t *r;
-
fprintf (out, "%s\n\n", title);
fprintf (out,
"ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
ntokens, nvars, nsyms, nrules, nritems);
+
+
fprintf (out, "Variables\n---------\n\n");
- fprintf (out, "Value Sprec Sassoc Tag\n");
- for (i = ntokens; i < nsyms; i++)
- fprintf (out, "%5d %5d %5d %s\n",
- i,
- symbols[i]->prec, symbols[i]->assoc,
- symbol_tag_get (symbols[i]));
- fprintf (out, "\n\n");
+ {
+ symbol_number_t i;
+ fprintf (out, "Value Sprec Sassoc Tag\n");
+
+ for (i = ntokens; i < nsyms; i++)
+ fprintf (out, "%5d %5d %5d %s\n",
+ i,
+ symbols[i]->prec, symbols[i]->assoc,
+ symbol_tag_get (symbols[i]));
+ fprintf (out, "\n\n");
+ }
+
fprintf (out, "Rules\n-----\n\n");
- fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
- for (i = 1; i < nrules + nuseless_productions + 1; i++)
- {
- int rhs_count = 0;
- /* Find the last RHS index in ritems. */
- for (r = rules[i].rhs; *r >= 0; ++r)
- ++rhs_count;
- fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
- i - 1,
- rules[i].prec ? rules[i].prec->prec : 0,
- rules[i].prec ? rules[i].prec->assoc : 0,
- rules[i].useful,
- rules[i].rhs - ritem,
- rules[i].rhs - ritem + rhs_count - 1,
- rules[i].lhs->number);
- /* Dumped the RHS. */
- for (r = rules[i].rhs; *r >= 0; r++)
- fprintf (out, " %3d", *r);
- fprintf (out, " [%d]\n", -(*r) - 1);
- }
+ {
+ rule_number_t i;
+ fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
+ for (i = 1; i < nrules + nuseless_productions + 1; i++)
+ {
+ rule_t *rule = &rules[i];
+ item_number_t *r = NULL;
+ int rhs_count = 0;
+ /* Find the last RHS index in ritems. */
+ for (r = rule->rhs; *r >= 0; ++r)
+ ++rhs_count;
+ fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
+ i - 1,
+ rule->prec ? rule->prec->prec : 0,
+ rule->prec ? rule->prec->assoc : 0,
+ rule->useful,
+ rule->rhs - ritem,
+ rule->rhs - ritem + rhs_count - 1,
+ rule->lhs->number);
+ /* Dumped the RHS. */
+ for (r = rule->rhs; *r >= 0; r++)
+ fprintf (out, " %3d", *r);
+ fprintf (out, " [%d]\n", -(*r) - 1);
+ }
+ }
fprintf (out, "\n\n");
+
fprintf (out, "Rules interpreted\n-----------------\n\n");
- for (i = 1; i < nrules + nuseless_productions + 1; i++)
- {
- fprintf (out, "%-5d ", i);
- rule_print (&rules[i], out);
- }
+ {
+ rule_number_t r;
+ for (r = 1; r < nrules + nuseless_productions + 1; r++)
+ {
+ fprintf (out, "%-5d ", r);
+ rule_print (&rules[r], out);
+ }
+ }
fprintf (out, "\n\n");
}
# define ISTOKEN(s) ((s) < ntokens)
# define ISVAR(s) ((s) >= ntokens)
-extern int nrules;
extern int nsyms;
extern int ntokens;
extern int nvars;
-# define ITEM_NUMBER_MAX INT_MAX
typedef int item_number_t;
+# define ITEM_NUMBER_MAX ((item_number_t) INT_MAX)
extern item_number_t *ritem;
extern unsigned int nritems;
-/* There is weird relationship between item_number_t and
- symbol_number_t: we store symbol_number_t in item_number_t, but in
- the latter we also store, as negative numbers, the rule numbers.
+/* There is weird relationship between OT1H item_number_t and OTOH
+ symbol_number_t and rule_number_t: we store the latter in
+ item_number_t. symbol_number_t are stored as are, while
+ the negation of rule_number_t are stored.
Therefore, an symbol_number_t must be a valid item_number_t, and we
sometimes have to perform the converse transformation. */
extern symbol_number_t start_symbol;
+/* Rules numbers. */
+typedef short rule_number_t;
+# define RULE_NUMBER_MAX ((rule_number_t) SHRT_MAX)
+extern rule_number_t nrules;
+# define int_of_rule_number(RNum) ((int) (RNum))
+# define item_number_of_rule_number(RNum) ((item_number_t) (- RNum))
+# define rule_number_of_item_number(INum) ((rule_number_t) (- INum))
+
+
+/*--------.
+| Rules. |
+`--------*/
typedef struct rule_s
{
/* The number of the rule in the source. It is usually the index in
RULES too, except if there are useless rules. */
- short user_number;
+ rule_number_t user_number;
/* The index in RULES. Usually the rule number in the source,
except if some rules are useless. */
- short number;
+ rule_number_t number;
symbol_t *lhs;
item_number_t *rhs;
/* Print the grammar's rules numbers from BEGIN (inclusive) to END
(exclusive) on OUT under TITLE. */
void grammar_rules_partial_print PARAMS ((FILE *out, const char *title,
- int begin, int end));
+ rule_number_t begin,
+ rule_number_t end));
/* Print the grammar's rules on OUT. */
void grammar_rules_print PARAMS ((FILE *out));
static void
-add_lookback_edge (state_t *state, int ruleno, int gotono)
+add_lookback_edge (state_t *state, rule_number_t ruleno, int gotono)
{
int i;
shorts *sp;
{
int nedges = 0;
symbol_number_t symbol1 = states[to_state[i]]->accessing_symbol;
- short *rulep;
+ rule_number_t *rulep;
for (rulep = derives[symbol1]; *rulep > 0; rulep++)
{
void
set_nullable (void)
{
- int ruleno;
+ rule_number_t ruleno;
symbol_number_t *s1;
symbol_number_t *s2;
shorts *p;
for (ruleno = 1; ruleno < nrules + 1; ++ruleno)
if (rules[ruleno].useful)
{
- if (rules[ruleno].rhs[0] >= 0)
+ rule_t *rule = &rules[ruleno];
+ if (rule->rhs[0] >= 0)
{
/* This rule has a non empty RHS. */
- item_number_t *r;
+ item_number_t *r = NULL;
int any_tokens = 0;
- for (r = rules[ruleno].rhs; *r >= 0; ++r)
+ for (r = rule->rhs; *r >= 0; ++r)
if (ISTOKEN (*r))
any_tokens = 1;
/* This rule has only nonterminals: schedule it for the second
pass. */
if (!any_tokens)
- for (r = rules[ruleno].rhs; *r >= 0; ++r)
+ for (r = rule->rhs; *r >= 0; ++r)
{
rcount[ruleno]++;
p->next = rsets[*r];
else
{
/* This rule has an empty RHS. */
- assert (rules[ruleno].rhs[0] == -ruleno);
- if (rules[ruleno].useful && !nullable[rules[ruleno].lhs->number])
+ assert (rule_number_of_item_number (rule->rhs[0]) == ruleno);
+ if (rule->useful && !nullable[rule->lhs->number])
{
- nullable[rules[ruleno].lhs->number] = 1;
- *s2++ = rules[ruleno].lhs->number;
+ nullable[rule->lhs->number] = 1;
+ *s2++ = rule->lhs->number;
}
}
}
static void
prepare_rules (void)
{
- int r;
+ rule_number_t r;
unsigned int i = 0;
item_number_t *rhs = XMALLOC (item_number_t, nritems);
unsigned int *prhs = XMALLOC (unsigned int, nrules + 1);
for (r = 1; r < nrules + 1; ++r)
{
- item_number_t *rhsp;
+ item_number_t *rhsp = NULL;
/* Index of rule R in RHS. */
prhs[r] = i;
/* RHS of the rule R. */
action_row (state_t *state)
{
int i;
- int default_rule = 0;
+ rule_number_t default_rule = 0;
reductions_t *redp = state->reductions;
shifts_t *shiftp = state->shifts;
errs_t *errp = state->errs;
for (i = 0; i < state->nlookaheads; i++)
{
int count = 0;
- int rule = -state->lookaheads_rule[i]->number;
- int j;
+ rule_number_t rule = -state->lookaheads_rule[i]->number;
+ symbol_number_t j;
for (j = 0; j < ntokens; j++)
if (actrow[j] == rule)
void
actions_output (FILE *out)
{
- int rule;
+ rule_number_t r;
fputs ("m4_define([b4_actions], \n[[", out);
- for (rule = 1; rule < nrules + 1; ++rule)
- if (rules[rule].action)
+ for (r = 1; r < nrules + 1; ++r)
+ if (rules[r].action)
{
- fprintf (out, " case %d:\n", rule);
+ fprintf (out, " case %d:\n", r);
if (!no_lines_flag)
fprintf (out, muscle_find ("linef"),
- rules[rule].action_location.first_line,
+ rules[r].action_location.first_line,
quotearg_style (c_quoting_style,
muscle_find ("filename")));
fprintf (out, " %s\n break;\n\n",
- rules[rule].action);
+ rules[r].action);
}
fputs ("]])\n\n", out);
}
print_grammar (FILE *out)
{
symbol_number_t i;
- item_number_t *rule;
char buffer[90];
int column = 0;
if (token_translations[i] != undeftoken->number)
{
const char *tag = symbol_tag_get (symbols[token_translations[i]]);
- int r;
+ rule_number_t r;
+ item_number_t *rhsp;
+
buffer[0] = 0;
column = strlen (tag);
fputs (tag, out);
sprintf (buffer, " (%d)", i);
for (r = 1; r < nrules + 1; r++)
- for (rule = rules[r].rhs; *rule >= 0; rule++)
- if (item_number_as_symbol_number (*rule) == token_translations[i])
+ for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
+ if (item_number_as_symbol_number (*rhsp) == token_translations[i])
{
END_TEST (65);
sprintf (buffer + strlen (buffer), " %d", r - 1);
for (i = ntokens; i < nsyms; i++)
{
int left_count = 0, right_count = 0;
- int r;
+ rule_number_t r;
const char *tag = symbol_tag_get (symbols[i]);
for (r = 1; r < nrules + 1; r++)
{
+ item_number_t *rhsp;
if (rules[r].lhs->number == i)
left_count++;
- for (rule = rules[r].rhs; *rule >= 0; rule++)
- if (item_number_as_symbol_number (*rule) == i)
+ for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
+ if (item_number_as_symbol_number (*rhsp) == i)
{
right_count++;
break;
sprintf (buffer + strlen (buffer), _(" on right:"));
for (r = 1; r < nrules + 1; r++)
{
- for (rule = rules[r].rhs; *rule >= 0; rule++)
- if (item_number_as_symbol_number (*rule) == i)
+ item_number_t *rhsp;
+ for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
+ if (item_number_as_symbol_number (*rhsp) == i)
{
END_TEST (65);
sprintf (buffer + strlen (buffer), " %d", r - 1);
{
item_number_t *sp;
item_number_t *sp1;
- int rule;
+ rule_number_t rule;
sp1 = sp = ritem + sitems[i];
while (*sp >= 0)
sp++;
- rule = -(*sp);
+ rule = rule_number_of_item_number (*sp);
if (i)
obstack_1grow (oout, '\n');
static void
packgram (void)
{
- unsigned int itemno;
- int ruleno;
- symbol_list_t *p;
+ unsigned int itemno = 0;
+ rule_number_t ruleno = 1;
+ symbol_list_t *p = grammar;
ritem = XCALLOC (item_number_t, nritems);
rules = XCALLOC (rule_t, nrules) - 1;
- itemno = 0;
- ruleno = 1;
-
- p = grammar;
while (p)
{
symbol_t *ruleprec = p->ruleprec;
`useless', but no warning should be issued). */
static bitset V1;
-static int nuseful_productions;
-int nuseless_productions;
+static rule_number_t nuseful_productions;
+rule_number_t nuseless_productions;
static int nuseful_nonterminals;
-int nuseless_nonterminals;
+symbol_number_t nuseless_nonterminals;
\f
/*-------------------------------------------------------------------.
| Another way to do this would be with a set for each production and |
`-------------------------------------------------------------------*/
static bool
-useful_production (int i, bitset N0)
+useful_production (rule_number_t r, bitset N0)
{
- item_number_t *r;
- short n;
+ item_number_t *rhsp;
/* A production is useful if all of the nonterminals in its appear
in the set of useful nonterminals. */
- for (r = rules[i].rhs; *r >= 0; r++)
- if (ISVAR (n = *r) && !bitset_test (N0, n - ntokens))
+ for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
+ if (ISVAR (*rhsp) && !bitset_test (N0, *rhsp - ntokens))
return FALSE;
return TRUE;
}
useless_nonterminals (void)
{
bitset Np, Ns;
- int i;
+ rule_number_t r;
/* N is set as built. Np is set being built this iteration. P is
set of all productions which have a RHS all in N. */
while (1)
{
bitset_copy (Np, N);
- for (i = 1; i < nrules + 1; i++)
- if (!bitset_test (P, i)
- && useful_production (i, N))
+ for (r = 1; r < nrules + 1; r++)
+ if (!bitset_test (P, r)
+ && useful_production (r, N))
{
- bitset_set (Np, rules[i].lhs->number - ntokens);
- bitset_set (P, i);
+ bitset_set (Np, rules[r].lhs->number - ntokens);
+ bitset_set (P, r);
}
if (bitset_equal_p (N, Np))
break;
inaccessable_symbols (void)
{
bitset Vp, Vs, Pp;
- int i;
- short t;
- item_number_t *r;
/* Find out which productions are reachable and which symbols are
used. Starting with an empty set of productions and a set of
while (1)
{
+ rule_number_t r;
bitset_copy (Vp, V);
- for (i = 1; i < nrules + 1; i++)
+ for (r = 1; r < nrules + 1; r++)
{
- if (!bitset_test (Pp, i)
- && bitset_test (P, i)
- && bitset_test (V, rules[i].lhs->number))
+ if (!bitset_test (Pp, r)
+ && bitset_test (P, r)
+ && bitset_test (V, rules[r].lhs->number))
{
- for (r = rules[i].rhs; *r >= 0; r++)
- if (ISTOKEN (t = *r) || bitset_test (N, t - ntokens))
- bitset_set (Vp, t);
- bitset_set (Pp, i);
+ item_number_t *rhsp;
+ for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
+ if (ISTOKEN (*rhsp) || bitset_test (N, *rhsp - ntokens))
+ bitset_set (Vp, *rhsp);
+ bitset_set (Pp, r);
}
}
if (bitset_equal_p (V, Vp))
nuseless_productions = nrules - nuseful_productions;
nuseful_nonterminals = 0;
- for (i = ntokens; i < nsyms; i++)
- if (bitset_test (V, i))
- nuseful_nonterminals++;
+ {
+ symbol_number_t i;
+ for (i = ntokens; i < nsyms; i++)
+ if (bitset_test (V, i))
+ nuseful_nonterminals++;
+ }
nuseless_nonterminals = nvars - nuseful_nonterminals;
/* A token that was used in %prec should not be warned about. */
- for (i = 1; i < nrules + 1; i++)
- if (rules[i].precsym != 0)
- bitset_set (V1, rules[i].precsym->number);
+ {
+ rule_number_t i;
+ for (i = 1; i < nrules + 1; i++)
+ if (rules[i].precsym != 0)
+ bitset_set (V1, rules[i].precsym->number);
+ }
}
{
/* Report and flag useless productions. */
{
- int r;
+ rule_number_t r;
for (r = 1; r < nrules + 1; r++)
{
rules[r].useful = bitset_test (P, r);
int useful = 1;
int useless = nrules + 1 - nuseless_productions;
rule_t *rules_sorted = XMALLOC (rule_t, nrules + 1) - 1;
- int i;
- for (i = 1; i < nrules + 1; ++i)
- rules_sorted[rules[i].useful ? useful++ : useless++] = rules[i];
+ rule_number_t r;
+ for (r = 1; r < nrules + 1; ++r)
+ rules_sorted[rules[r].useful ? useful++ : useless++] = rules[r];
free (rules + 1);
rules = rules_sorted;
/* Renumber the rules markers in RITEMS. */
- for (i = 1; i < nrules + 1; ++i)
+ for (r = 1; r < nrules + 1; ++r)
{
- item_number_t *rhsp = rules[i].rhs;
+ item_number_t *rhsp = rules[r].rhs;
for (/* Nothing. */; *rhsp >= 0; ++rhsp)
/* Nothing. */;
- *rhsp = -i;
- rules[i].number = i;
+ *rhsp = item_number_of_rule_number (r);
+ rules[r].number = r;
}
nrules -= nuseless_productions;
}
}
{
- int r;
+ rule_number_t r;
for (r = 1; r < nrules + 1; ++r)
{
item_number_t *rhsp;
void reduce_output PARAMS ((FILE *out));
void reduce_free PARAMS ((void));
-extern int nuseless_nonterminals;
-extern int nuseless_productions;
+extern symbol_number_t nuseless_nonterminals;
+extern rule_number_t nuseless_productions;
#endif /* !REDUCE_H_ */