* src/gram.h (token_number_as_item_number)
(item_number_as_token_number): New.
* src/output.c (GENERATE_OUTPUT_TABLE): New.
Use it to create output_item_number_table and
output_token_number_table.
* src/LR0.c, src/derives.c, src/gram.c, src/gram.h, src/lalr.c,
* src/lex.c, src/nullable.c, src/output.c, src/print.c,
* src/print_graph.c, src/reader.c, src/reduce.c, src/state.h,
* src/symtab.c, src/symtab.h: Use token_number_t instead of shorts.
16 files changed:
+2002-04-22 Akim Demaille <akim@epita.fr>
+
+ Propagate more token_number_t.
+
+ * src/gram.h (token_number_as_item_number)
+ (item_number_as_token_number): New.
+ * src/output.c (GENERATE_OUTPUT_TABLE): New.
+ Use it to create output_item_number_table and
+ output_token_number_table.
+ * src/LR0.c, src/derives.c, src/gram.c, src/gram.h, src/lalr.c,
+ * src/lex.c, src/nullable.c, src/output.c, src/print.c,
+ * src/print_graph.c, src/reader.c, src/reduce.c, src/state.h,
+ * src/symtab.c, src/symtab.h: Use token_number_t instead of shorts.
+
+
2002-04-22 Akim Demaille <akim@epita.fr>
* src/output.h, src/output.c (get_lines_number): Remove.
2002-04-22 Akim Demaille <akim@epita.fr>
* src/output.h, src/output.c (get_lines_number): Remove.
#include "bitset.h"
#include "quotearg.h"
#include "symtab.h"
#include "bitset.h"
#include "quotearg.h"
#include "symtab.h"
#include "getargs.h"
#include "reader.h"
#include "gram.h"
#include "getargs.h"
#include "reader.h"
#include "gram.h"
static state_t *last_state = NULL;
static int nshifts;
static state_t *last_state = NULL;
static int nshifts;
-static short *shift_symbol = NULL;
+static token_number_t *shift_symbol = NULL;
static short *redset = NULL;
static short *shiftset = NULL;
static short *redset = NULL;
static short *shiftset = NULL;
shiftset = XCALLOC (short, nsyms);
redset = XCALLOC (short, nrules + 1);
state_hash = XCALLOC (state_t *, STATE_HASH_SIZE);
shiftset = XCALLOC (short, nsyms);
redset = XCALLOC (short, nrules + 1);
state_hash = XCALLOC (state_t *, STATE_HASH_SIZE);
- shift_symbol = XCALLOC (short, nsyms);
+ shift_symbol = XCALLOC (token_number_t, nsyms);
nshifts = 0;
for (i = 0; i < nritemset; ++i)
nshifts = 0;
for (i = 0; i < nritemset; ++i)
- {
- int symbol = ritem[itemset[i]];
- if (symbol >= 0)
- {
- if (!kernel_size[symbol])
- {
- shift_symbol[nshifts] = symbol;
- nshifts++;
- }
-
- kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1;
- kernel_size[symbol]++;
- }
- }
+ if (ritem[itemset[i]] >= 0)
+ {
+ token_number_t symbol
+ = item_number_as_token_number (ritem[itemset[i]]);
+ if (!kernel_size[symbol])
+ {
+ shift_symbol[nshifts] = symbol;
+ nshifts++;
+ }
+
+ kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1;
+ kernel_size[symbol]++;
+ }
`-----------------------------------------------------------------*/
static state_t *
`-----------------------------------------------------------------*/
static state_t *
+new_state (token_number_t symbol)
`--------------------------------------------------------------*/
static int
`--------------------------------------------------------------*/
static int
+get_state (token_number_t symbol)
if (trace_flag)
fprintf (stderr, "Entering append_states, state = %d\n",
if (trace_flag)
fprintf (stderr, "Entering append_states, state = %d\n",
p = delts;
for (i = nrules; i > 0; i--)
{
p = delts;
for (i = nrules; i > 0; i--)
{
- int lhs = rules[i].lhs->number;
+ token_number_t lhs = rules[i].lhs->number;
p->next = dset[lhs];
p->value = i;
dset[lhs] = p;
p->next = dset[lhs];
p->value = i;
dset[lhs] = p;
token_number_t *token_translations = NULL;
token_number_t *token_translations = NULL;
+token_number_t start_symbol = 0;
int max_user_token_number = 256;
int max_user_token_number = 256;
extern item_number_t *ritem;
extern int nritems;
extern item_number_t *ritem;
extern int nritems;
-extern int start_symbol;
+/* There is weird relationship between item_number_t and
+ token_number_t: we store token_number_t in item_number_t, but in
+ the latter we also store, as negative numbers, the rule numbers.
+
+ Therefore, an token_number_t must be a valid item_number_t, and we
+ sometimes have to perform the converse transformation. */
+#define token_number_as_item_number(Tok) ((item_number_t) (Tok))
+#define item_number_as_token_number(Ite) ((token_number_t) (Ite))
+
+extern token_number_t start_symbol;
`----------------------------------------------------------*/
static int
`----------------------------------------------------------*/
static int
-map_goto (int state, int symbol)
+map_goto (int state, token_number_t symbol)
for (; j < sp->nshifts; j++)
{
for (; j < sp->nshifts; j++)
{
- int symbol = SHIFT_SYMBOL (sp, j);
+ token_number_t symbol = SHIFT_SYMBOL (sp, j);
if (nullable[symbol])
edge[nedges++] = map_goto (stateno, symbol);
}
if (nullable[symbol])
edge[nedges++] = map_goto (stateno, symbol);
}
for (i = 0; i < ngotos; i++)
{
int nedges = 0;
for (i = 0; i < ngotos; i++)
{
int nedges = 0;
- int symbol1 = states[to_state[i]]->accessing_symbol;
+ token_number_t symbol1 = states[to_state[i]]->accessing_symbol;
short *rulep;
for (rulep = derives[symbol1]; *rulep > 0; rulep++)
short *rulep;
for (rulep = derives[symbol1]; *rulep > 0; rulep++)
for (j = 0; j < sp->nshifts; j++)
{
state = states[sp->shifts[j]];
for (j = 0; j < sp->nshifts; j++)
{
state = states[sp->shifts[j]];
- if (state->accessing_symbol == *rp)
+ if (state->accessing_symbol
+ == item_number_as_token_number (*rp))
/* JF added rp>=ritem && I hope to god its right! */
if (rp >= ritem && ISVAR (*rp))
{
/* JF added rp>=ritem && I hope to god its right! */
if (rp >= ritem && ISVAR (*rp))
{
- edge[nedges++] = map_goto (states1[--length], *rp);
+ /* Downcasting from item_number_t to token_number_t. */
+ edge[nedges++] = map_goto (states1[--length],
+ item_number_as_token_number (*rp));
if (nullable[*rp])
done = 0;
}
if (nullable[*rp])
done = 0;
}
obstack_1grow (&token_obstack, '\0');
token_buffer = obstack_finish (&token_obstack);
symval = getsym (token_buffer);
obstack_1grow (&token_obstack, '\0');
token_buffer = obstack_finish (&token_obstack);
symval = getsym (token_buffer);
- if (symval->number == -1)
+ if (symval->number == NUMBER_UNDEFINED)
{
symval->number = ntokens++;
symval->class = token_sym;
{
symval->number = ntokens++;
symval->class = token_sym;
token_buffer = obstack_finish (&token_obstack);
symval = getsym (token_buffer);
token_buffer = obstack_finish (&token_obstack);
symval = getsym (token_buffer);
- if (symval->number == -1)
+ if (symval->number == NUMBER_UNDEFINED)
{
symval->number = ntokens++;
symval->class = token_sym;
{
symval->number = ntokens++;
symval->class = token_sym;
set_nullable (void)
{
int ruleno;
set_nullable (void)
{
int ruleno;
- short *s1;
- short *s2;
+ token_number_t *s1;
+ token_number_t *s2;
- short *squeue = XCALLOC (short, nvars);
+ token_number_t *squeue = XCALLOC (token_number_t, nvars);
short *rcount = XCALLOC (short, nrules + 1);
/* RITEM contains all the rules, including useless productions.
Hence we must allocate room for useless nonterminals too. */
short *rcount = XCALLOC (short, nrules + 1);
/* RITEM contains all the rules, including useless productions.
Hence we must allocate room for useless nonterminals too. */
-/*----------------------------------------------------------------.
-| Format the FIRST and then TABLE_DATA[BEGIN..END[ into OOUT, and |
-| return the number of bits needed for its longuest value. |
-`----------------------------------------------------------------*/
-
-static inline long int
-output_short_table (struct obstack *oout,
- short *table_data,
- short first,
- int begin,
- int end)
-{
- long int max = first;
- int i;
- int j = 1;
+/*------------------------------------------------------------------.
+| Create a function NAME which Format the FIRST and then |
+| TABLE_DATA[BEGIN..END[ (of TYPE) into OOUT, and return the number |
+| of bits needed for its longuest value. |
+`------------------------------------------------------------------*/
- obstack_fgrow1 (oout, "%6d", first);
- for (i = begin; i < end; ++i)
- {
- obstack_1grow (oout, ',');
- if (j >= 10)
- {
- obstack_sgrow (oout, "\n ");
- j = 1;
- }
- else
- ++j;
- obstack_fgrow1 (oout, "%6d", table_data[i]);
- if (table_data[i] > max)
- max = table_data[i];
- }
- obstack_1grow (oout, 0);
+#define GENERATE_OUTPUT_TABLE(Name, Type) \
+ \
+static inline long int \
+Name (struct obstack *oout, \
+ Type *table_data, \
+ Type first, \
+ int begin, \
+ int end) \
+{ \
+ long int max = first; \
+ int i; \
+ int j = 1; \
+ \
+ obstack_fgrow1 (oout, "%6d", first); \
+ for (i = begin; i < end; ++i) \
+ { \
+ obstack_1grow (oout, ','); \
+ if (j >= 10) \
+ { \
+ obstack_sgrow (oout, "\n "); \
+ j = 1; \
+ } \
+ else \
+ ++j; \
+ obstack_fgrow1 (oout, "%6d", table_data[i]); \
+ if (table_data[i] > max) \
+ max = table_data[i]; \
+ } \
+ obstack_1grow (oout, 0); \
+ \
+ return max; \
-
-/*--------------------.
-| Similar, for ints. |
-`--------------------*/
-
-static inline long int
-output_int_table (struct obstack *oout,
- int *table_data,
- int first,
- int begin,
- int end)
-{
- long int max = first;
- int i;
- int j = 1;
-
- obstack_fgrow1 (oout, "%6d", first);
- for (i = begin; i < end; ++i)
- {
- obstack_1grow (oout, ',');
- if (j >= 10)
- {
- obstack_sgrow (oout, "\n ");
- j = 1;
- }
- else
- ++j;
- obstack_fgrow1 (oout, "%6d", table_data[i]);
- if (table_data[i] > max)
- max = table_data[i];
- }
- obstack_1grow (oout, 0);
-
- return max;
-}
+GENERATE_OUTPUT_TABLE(output_int_table, int)
+GENERATE_OUTPUT_TABLE(output_short_table, short)
+GENERATE_OUTPUT_TABLE(output_token_number_table, token_number_t)
+GENERATE_OUTPUT_TABLE(output_item_number_table, item_number_t)
/*-----------------------------------------------------------------.
/*-----------------------------------------------------------------.
static void
prepare_tokens (void)
{
static void
prepare_tokens (void)
{
- long int max = output_short_table (&format_obstack, token_translations,
- 0, 1, max_user_token_number + 1);
+ long int max = output_token_number_table (&format_obstack,
+ token_translations,
+ 0, 1, max_user_token_number + 1);
muscle_insert ("translate", obstack_finish (&format_obstack));
MUSCLE_INSERT_LONG_INT ("token_number_max", max);
XFREE (token_translations);
muscle_insert ("translate", obstack_finish (&format_obstack));
MUSCLE_INSERT_LONG_INT ("token_number_max", max);
XFREE (token_translations);
int i = 0;
item_number_t *rhs = XMALLOC (item_number_t, nritems);
short *prhs = XMALLOC (short, nrules + 1);
int i = 0;
item_number_t *rhs = XMALLOC (item_number_t, nritems);
short *prhs = XMALLOC (short, nrules + 1);
- short *r1 = XMALLOC (short, nrules + 1);
+ token_number_t *r1 = XMALLOC (token_number_t, nrules + 1);
short *r2 = XMALLOC (short, nrules + 1);
short *rline = XMALLOC (short, nrules + 1);
short *r2 = XMALLOC (short, nrules + 1);
short *rline = XMALLOC (short, nrules + 1);
output_short_table (&format_obstack, rline, 0, 1, nrules + 1);
muscle_insert ("rline", obstack_finish (&format_obstack));
output_short_table (&format_obstack, rline, 0, 1, nrules + 1);
muscle_insert ("rline", obstack_finish (&format_obstack));
- output_short_table (&format_obstack, r1, 0, 1, nrules + 1);
+ output_token_number_table (&format_obstack, r1, 0, 1, nrules + 1);
muscle_insert ("r1", obstack_finish (&format_obstack));
output_short_table (&format_obstack, r2, 0, 1, nrules + 1);
muscle_insert ("r1", obstack_finish (&format_obstack));
output_short_table (&format_obstack, r2, 0, 1, nrules + 1);
prepare_states (void)
{
size_t i;
prepare_states (void)
{
size_t i;
- short *values = (short *) alloca (sizeof (short) * nstates);
+ token_number_t *values =
+ (token_number_t *) alloca (sizeof (token_number_t) * nstates);
for (i = 0; i < nstates; ++i)
values[i] = states[i]->accessing_symbol;
for (i = 0; i < nstates; ++i)
values[i] = states[i]->accessing_symbol;
- output_short_table (&format_obstack, values,
- 0, 1, nstates);
+ output_token_number_table (&format_obstack, values,
+ 0, 1, nstates);
muscle_insert ("stos", obstack_finish (&format_obstack));
}
muscle_insert ("stos", obstack_finish (&format_obstack));
}
to reduce. */
for (i = 0; i < shiftp->nshifts; i++)
{
to reduce. */
for (i = 0; i < shiftp->nshifts; i++)
{
int shift_state = shiftp->shifts[i];
if (!shift_state)
continue;
int shift_state = shiftp->shifts[i];
if (!shift_state)
continue;
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
- int symbol = states[state1]->accessing_symbol;
+ token_number_t symbol = states[state1]->accessing_symbol;
fprintf (out,
_(" %-4s\tshift, and go to state %d\n"),
escape (symbols[symbol]->tag), state1);
fprintf (out,
_(" %-4s\tshift, and go to state %d\n"),
escape (symbols[symbol]->tag), state1);
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
- int symbol = states[state1]->accessing_symbol;
+ token_number_t symbol = states[state1]->accessing_symbol;
fprintf (out, _(" %-4s\tgo to state %d\n"),
escape (symbols[symbol]->tag), state1);
}
fprintf (out, _(" %-4s\tgo to state %d\n"),
escape (symbols[symbol]->tag), state1);
}
if (state->consistent)
{
int rule = redp->rules[0];
if (state->consistent)
{
int rule = redp->rules[0];
- int symbol = rules[rule].lhs->number;
+ token_number_t symbol = rules[rule].lhs->number;
fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"),
rule - 1, escape (symbols[symbol]->tag));
return;
fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"),
rule - 1, escape (symbols[symbol]->tag));
return;
static void
print_grammar (FILE *out)
{
static void
print_grammar (FILE *out)
{
+ token_number_t i;
+ int j;
item_number_t *rule;
char buffer[90];
int column = 0;
item_number_t *rule;
char buffer[90];
int column = 0;
/* rule # : LHS -> RHS */
fprintf (out, "%s\n\n", _("Grammar"));
fprintf (out, " %s\n", _("Number, Line, Rule"));
/* rule # : LHS -> RHS */
fprintf (out, "%s\n\n", _("Grammar"));
fprintf (out, " %s\n", _("Number, Line, Rule"));
- for (i = 1; i < nrules + 1; i++)
+ for (j = 1; j < nrules + 1; j++)
{
fprintf (out, _(" %3d %3d %s ->"),
{
fprintf (out, _(" %3d %3d %s ->"),
- i - 1, rules[i].line, escape (rules[i].lhs->tag));
- rule = rules[i].rhs;
+ j - 1, rules[j].line, escape (rules[j].lhs->tag));
+ rule = rules[j].rhs;
if (*rule >= 0)
while (*rule >= 0)
fprintf (out, " %s", escape (symbols[*rule++]->tag));
if (*rule >= 0)
while (*rule >= 0)
fprintf (out, " %s", escape (symbols[*rule++]->tag));
for (j = 1; j < nrules + 1; j++)
for (rule = rules[j].rhs; *rule >= 0; rule++)
for (j = 1; j < nrules + 1; j++)
for (rule = rules[j].rhs; *rule >= 0; rule++)
- if (*rule == token_translations[i])
+ if (item_number_as_token_number (*rule) == token_translations[i])
{
END_TEST (65);
sprintf (buffer + strlen (buffer), " %d", j - 1);
{
END_TEST (65);
sprintf (buffer + strlen (buffer), " %d", j - 1);
if (rules[j].lhs->number == i)
left_count++;
for (rule = rules[j].rhs; *rule >= 0; rule++)
if (rules[j].lhs->number == i)
left_count++;
for (rule = rules[j].rhs; *rule >= 0; rule++)
+ if (item_number_as_token_number (*rule) == i)
for (j = 1; j < nrules + 1; j++)
{
for (rule = rules[j].rhs; *rule >= 0; rule++)
for (j = 1; j < nrules + 1; j++)
{
for (rule = rules[j].rhs; *rule >= 0; rule++)
+ if (item_number_as_token_number (*rule) == i)
{
END_TEST (65);
sprintf (buffer + strlen (buffer), " %d", j - 1);
{
END_TEST (65);
sprintf (buffer + strlen (buffer), " %d", j - 1);
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
- int symbol = states[state1]->accessing_symbol;
+ token_number_t symbol = states[state1]->accessing_symbol;
/* This symbol and its alias are a single token defn.
Allocate a tokno, and assign to both check agreement of
prec and assoc fields and make both the same */
/* This symbol and its alias are a single token defn.
Allocate a tokno, and assign to both check agreement of
prec and assoc fields and make both the same */
- if (this->number == -1)
+ if (this->number == NUMBER_UNDEFINED)
{
if (this == eoftoken || this->alias == eoftoken)
this->number = this->alias->number = 0;
else
{
{
if (this == eoftoken || this->alias == eoftoken)
this->number = this->alias->number = 0;
else
{
- assert (this->alias->number != -1);
+ assert (this->alias->number != NUMBER_UNDEFINED);
this->number = this->alias->number;
}
}
this->number = this->alias->number;
}
}
}
else /* this->class == token_sym */
{
}
else /* this->class == token_sym */
{
- assert (this->number != -1);
+ assert (this->number != NUMBER_UNDEFINED);
}
symbols[this->number] = this;
}
symbols[this->number] = this;
symbol->class = what_is;
if (what_is == nterm_sym && oldclass != nterm_sym)
symbol->number = nvars++;
symbol->class = what_is;
if (what_is == nterm_sym && oldclass != nterm_sym)
symbol->number = nvars++;
- if (what_is == token_sym && symbol->number == -1)
+ if (what_is == token_sym && symbol->number == NUMBER_UNDEFINED)
symbol->number = ntokens++;
if (typename)
symbol->number = ntokens++;
if (typename)
symval->assoc = assoc;
if (symval->class == nterm_sym)
complain (_("symbol %s redefined"), symval->tag);
symval->assoc = assoc;
if (symval->class == nterm_sym)
complain (_("symbol %s redefined"), symval->tag);
- if (symval->number == -1)
+ if (symval->number == NUMBER_UNDEFINED)
{
symval->number = ntokens++;
symval->class = token_sym;
{
symval->number = ntokens++;
symval->class = token_sym;
p = p->next;
while (p && p->sym)
{
p = p->next;
while (p && p->sym)
{
- ritem[itemno++] = p->sym->number;
+ /* item_number_t = token_number_t.
+ But the former needs to contain more: negative rule numbers. */
+ ritem[itemno++] = token_number_as_item_number (p->sym->number);
/* A rule gets by default the precedence and associativity
of the last token in it. */
if (p->sym->class == token_sym)
/* A rule gets by default the precedence and associativity
of the last token in it. */
if (p->sym->class == token_sym)
static void
nonterminals_reduce (void)
{
static void
nonterminals_reduce (void)
{
/* Map the nonterminals to their new index: useful first, useless
afterwards. Kept for later report. */
/* Map the nonterminals to their new index: useful first, useless
afterwards. Kept for later report. */
- short *nontermmap = XCALLOC (short, nvars) - ntokens;
+ token_number_t *nontermmap = XCALLOC (token_number_t, nvars) - ntokens;
n = ntokens;
for (i = ntokens; i < nsyms; i++)
if (bitset_test (V, i))
n = ntokens;
for (i = ntokens; i < nsyms; i++)
if (bitset_test (V, i))
for (i = 0; i < nritems; ++i)
if (ISVAR (ritem[i]))
for (i = 0; i < nritems; ++i)
if (ISVAR (ritem[i]))
- ritem[i] = nontermmap[ritem[i]];
+ ritem[i] = token_number_as_item_number (nontermmap[ritem[i]]);
start_symbol = nontermmap[start_symbol];
start_symbol = nontermmap[start_symbol];
struct state_s *link;
short number;
struct state_s *link;
short number;
- short accessing_symbol;
+ token_number_t accessing_symbol;
shifts *shifts;
reductions *reductions;
errs *errs;
shifts *shifts;
reductions *reductions;
errs *errs;
res->tag = xstrdup (tag);
res->type_name = NULL;
res->tag = xstrdup (tag);
res->type_name = NULL;
+ res->number = NUMBER_UNDEFINED;
res->prec = 0;
res->assoc = right_assoc;
res->user_token_number = SUNDEF;
res->prec = 0;
res->assoc = right_assoc;
res->user_token_number = SUNDEF;
/* Internal token numbers. */
typedef short token_number_t;
/* Internal token numbers. */
typedef short token_number_t;
#define SUNDEF -1 /* For undefined user number. */
#define SALIAS -9991 /* for symbol generated with an alias */
#define SUNDEF -1 /* For undefined user number. */
#define SALIAS -9991 /* for symbol generated with an alias */
+#define NUMBER_UNDEFINED ((token_number_t) -1)
+
struct symbol_s
{
/* The key, name of the symbol. */
char *tag;
/* Its type. */
char *type_name;
struct symbol_s
{
/* The key, name of the symbol. */
char *tag;
/* Its type. */
char *type_name;
token_number_t number;
short prec;
associativity assoc;
token_number_t number;
short prec;
associativity assoc;