#include "reduce.h"
#include "nullable.h"
-/* Linked list of rule numbers. */
+/* Linked list of rules. */
typedef struct rule_list_s
{
struct rule_list_s *next;
- rule_number_t value;
+ rule_t *value;
} rule_list_t;
-char *nullable = NULL;
+bool *nullable = NULL;
static void
nullable_print (FILE *out)
}
void
-set_nullable (void)
+nullable_compute (void)
{
rule_number_t ruleno;
symbol_number_t *s1;
rule_list_t *p;
symbol_number_t *squeue = XCALLOC (symbol_number_t, nvars);
- short *rcount = XCALLOC (short, nrules + 1);
+ short *rcount = XCALLOC (short, nrules);
/* RITEM contains all the rules, including useless productions.
Hence we must allocate room for useless nonterminals too. */
rule_list_t **rsets = XCALLOC (rule_list_t *, nvars) - ntokens;
Supposedly NRITEMS - NRULES is enough. But why take the risk? */
rule_list_t *relts = XCALLOC (rule_list_t, nritems + nvars + 1);
- if (trace_flag)
- fprintf (stderr, "Entering set_nullable\n");
-
- nullable = XCALLOC (char, nvars) - ntokens;
+ nullable = XCALLOC (bool, nvars) - ntokens;
s1 = s2 = squeue;
p = relts;
- for (ruleno = 1; ruleno < nrules + 1; ++ruleno)
+ for (ruleno = 0; ruleno < nrules; ++ruleno)
if (rules[ruleno].useful)
{
rule_t *rule = &rules[ruleno];
{
rcount[ruleno]++;
p->next = rsets[*r];
- p->value = ruleno;
+ p->value = rule;
rsets[*r] = p;
p++;
}
else
{
/* This rule has an empty RHS. */
- assert (rule_number_of_item_number (rule->rhs[0]) == ruleno);
+ assert (item_number_as_rule_number (rule->rhs[0]) == ruleno);
if (rule->useful && !nullable[rule->lhs->number])
{
nullable[rule->lhs->number] = 1;
while (s1 < s2)
for (p = rsets[*s1++]; p; p = p->next)
{
- ruleno = p->value;
- if (--rcount[ruleno] == 0)
- if (rules[ruleno].useful && !nullable[rules[ruleno].lhs->number])
+ rule_t *rule = p->value;
+ if (--rcount[rule->number] == 0)
+ 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;
}
}
XFREE (rsets + ntokens);
XFREE (relts);
- if (trace_flag)
+ if (trace_flag & trace_sets)
nullable_print (stderr);
}
void
-free_nullable (void)
+nullable_free (void)
{
XFREE (nullable + ntokens);
}