+2002-06-30 Akim Demaille <akim@epita.fr>
+
+ * src/state.h (state_number_t, STATE_NUMBER_MAX): New.
+ * src/LR0.c, src/LR0.h, src/conflicts.c, src/lalr.c, src/lalr.h,
+ * src/output.c, src/print.c, src/print_graph.c: Propagate.
+ * src/LR0.h, src/LR0.h (final_state): Is a state_t*.
+
2002-06-30 Akim Demaille <akim@epita.fr>
Make the test suite pass with warnings checked.
#include "lalr.h"
#include "reduce.h"
-unsigned int nstates = 0;
-/* Initialize the final state to -1, otherwise, it might be set to 0
- by default, and since we don't compute the reductions of the final
- state, we end up not computing the reductions of the initial state,
- which is of course needed.
-
- FINAL_STATE is properly set by new_state when it recognizes the
+state_number_t nstates = 0;
+/* FINAL_STATE is properly set by new_state when it recognizes its
accessing symbol: EOF. */
-int final_state = -1;
+state_t *final_state = NULL;
static state_t *first_state = NULL;
static state_t *this_state = NULL;
static symbol_number_t *shift_symbol = NULL;
static short *redset = NULL;
-static short *shiftset = NULL;
+static state_number_t *shiftset = NULL;
static item_number_t **kernel_base = NULL;
static int *kernel_size = NULL;
{
allocate_itemsets ();
- shiftset = XCALLOC (short, nsyms);
+ shiftset = XCALLOC (state_number_t, nsyms);
redset = XCALLOC (short, nrules + 1);
state_hash = XCALLOC (state_t *, STATE_HASH_SIZE);
shift_symbol = XCALLOC (symbol_number_t, nsyms);
fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n",
nstates, symbol, symbol_tag_get (symbols[symbol]));
- if (nstates >= SHRT_MAX)
- fatal (_("too many states (max %d)"), SHRT_MAX);
+ if (nstates >= STATE_NUMBER_MAX)
+ fatal (_("too many states (max %d)"), STATE_NUMBER_MAX);
p = STATE_ALLOC (core_size);
p->accessing_symbol = symbol;
/* If this is the eoftoken, and this is not the initial state, then
this is the final state. */
if (symbol == 0 && first_state)
- final_state = p->number;
+ final_state = p;
if (!first_state)
first_state = p;
| equivalent one exists already. Used by append_states. |
`--------------------------------------------------------------*/
-static int
+static state_number_t
get_state (symbol_number_t symbol, size_t core_size, item_number_t *core)
{
int key;
/* If this is the final state, we want it to have no reductions at
all, although it has one for `START_SYMBOL EOF .'. */
- if (this_state->number == final_state)
+ if (final_state && this_state->number == final_state->number)
return;
/* Find and count the active items that represent ends of rules. */
void generate_states PARAMS ((void));
-extern unsigned int nstates;
-extern int final_state;
+extern state_number_t nstates;
+extern state_t *final_state;
#endif /* !LR0_H_ */
void
conflicts_solve (void)
{
- size_t i;
+ state_number_t i;
conflicts = XCALLOC (char, nstates);
shiftset = bitset_create (ntokens, BITSET_FIXED);
conflicts_output (FILE *out)
{
bool printed_sth = FALSE;
- size_t i;
+ state_number_t i;
for (i = 0; i < nstates; i++)
if (conflicts[i])
{
int
conflicts_total_count (void)
{
- unsigned i;
+ state_number_t i;
int count;
/* Conflicts by state. */
void
conflicts_print (void)
{
- size_t i;
-
/* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
not set, and then we want 0 SR, or else it is specified, in which
case we want equality. */
int rrc_total = 0;
/* Conflicts by state. */
- for (i = 0; i < nstates; i++)
- if (conflicts[i])
- {
- src_total += count_sr_conflicts (states[i]);
- rrc_total += count_rr_conflicts (states[i], TRUE);
- }
+ {
+ state_number_t i;
+
+ for (i = 0; i < nstates; i++)
+ if (conflicts[i])
+ {
+ src_total += count_sr_conflicts (states[i]);
+ rrc_total += count_rr_conflicts (states[i], TRUE);
+ }
+ }
src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
static int ngotos;
short *goto_map = NULL;
-short *from_state = NULL;
-short *to_state = NULL;
+state_number_t *from_state = NULL;
+state_number_t *to_state = NULL;
/* And for the famous F variable, which name is so descriptive that a
comment is hardly needed. <grin>. */
static void
initialize_LA (void)
{
- size_t i;
+ state_number_t i;
int j;
rule_t **np;
static void
set_goto_map (void)
{
- size_t state;
- int i;
+ state_number_t state;
short *temp_map;
goto_map = XCALLOC (short, nvars + 1) - ntokens;
for (state = 0; state < nstates; ++state)
{
shifts *sp = states[state]->shifts;
+ int i;
for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
{
if (ngotos == SHRT_MAX)
{
int k = 0;
+ int i;
for (i = ntokens; i < nsyms; i++)
{
temp_map[i] = k;
temp_map[nsyms] = ngotos;
}
- from_state = XCALLOC (short, ngotos);
- to_state = XCALLOC (short, ngotos);
+ from_state = XCALLOC (state_number_t, ngotos);
+ to_state = XCALLOC (state_number_t, ngotos);
for (state = 0; state < nstates; ++state)
{
shifts *sp = states[state]->shifts;
+ int i;
for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
{
int k = temp_map[SHIFT_SYMBOL (sp, i)]++;
`----------------------------------------------------------*/
static int
-map_goto (int state, symbol_number_t symbol)
+map_goto (state_number_t state, symbol_number_t symbol)
{
int high;
int low;
int middle;
- int s;
+ state_number_t s;
low = goto_map[symbol];
high = goto_map[symbol + 1] - 1;
for (i = 0; i < ngotos; i++)
{
- int stateno = to_state[i];
+ state_number_t stateno = to_state[i];
shifts *sp = states[stateno]->shifts;
int j;
build_relations (void)
{
short *edge = XCALLOC (short, ngotos + 1);
- short *states1 = XCALLOC (short, ritem_longest_rhs () + 1);
+ state_number_t *states1 = XCALLOC (state_number_t, ritem_longest_rhs () + 1);
int i;
includes = XCALLOC (short *, ngotos);
static void
states_lookaheads_count (void)
{
- size_t i;
+ state_number_t i;
nLA = 0;
/* Count */
static void
states_lookaheads_initialize (void)
{
- size_t i;
+ state_number_t i;
bitsetv pLA = LA;
rule_t **pLArule = LArule;
static void
lookaheads_print (FILE *out)
{
- size_t i;
+ state_number_t i;
int j, k;
fprintf (out, "Lookaheads: BEGIN\n");
for (i = 0; i < nstates; ++i)
TO_STATE of the first of them. */
extern short *goto_map;
-extern short *from_state;
-extern short *to_state;
+extern state_number_t *from_state;
+extern state_number_t *to_state;
/* LARULE is a vector which records the rules that need lookahead in
various states. The elements of LARULE that apply to state S are
GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table, short)
GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number_t)
GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number_t)
+GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number_t)
/*-----------------------------------------------------------------.
static void
prepare_states (void)
{
- size_t i;
+ state_number_t i;
symbol_number_t *values =
(symbol_number_t *) alloca (sizeof (symbol_number_t) * nstates);
for (i = 0; i < nstates; ++i)
values[i] = states[i]->accessing_symbol;
muscle_insert_symbol_number_table ("stos", values,
- 0, 1, nstates);
+ 0, 1, nstates);
}
for (i = 0; i < shiftp->nshifts; i++)
{
symbol_number_t symbol;
- int shift_state = shiftp->shifts[i];
+ state_number_t shift_state = shiftp->shifts[i];
if (!shift_state)
continue;
if (actrow[symbol] != 0)
conflicted = conflrow[symbol] = 1;
- actrow[symbol] = shift_state;
+ actrow[symbol] = state_number_as_int (shift_state);
/* Do not use any default reduction if there is a shift for
error */
static void
-save_row (int state)
+save_row (state_number_t state)
{
- int i;
+ symbol_number_t i;
int count;
short *sp = NULL;
short *sp1 = NULL;
static void
token_actions (void)
{
- size_t i;
+ state_number_t i;
int nconflict = conflicts_total_count ();
short *yydefact = XCALLOC (short, nstates);
static void
-save_column (int symbol, int default_state)
+save_column (symbol_number_t symbol, state_number_t default_state)
{
int i;
short *sp;
short *sp1;
short *sp2;
int count;
- int symno = symbol - ntokens + nstates;
+ int symno = symbol - ntokens + state_number_as_int (nstates);
- short begin = goto_map[symbol];
- short end = goto_map[symbol + 1];
+ int begin = goto_map[symbol];
+ int end = goto_map[symbol + 1];
count = 0;
for (i = begin; i < end; i++)
width[symno] = sp1[-1] - sp[0] + 1;
}
-static int
-default_goto (int symbol)
+
+static state_number_t
+default_goto (symbol_number_t symbol)
{
- size_t i;
- size_t m = goto_map[symbol];
- size_t n = goto_map[symbol + 1];
- int default_state = -1;
+ state_number_t s;
+ int i;
+ int m = goto_map[symbol];
+ int n = goto_map[symbol + 1];
+ state_number_t default_state = (state_number_t) -1;
int max = 0;
if (m == n)
- return -1;
+ return (state_number_t) -1;
- for (i = 0; i < nstates; i++)
- state_count[i] = 0;
+ for (s = 0; s < nstates; s++)
+ state_count[s] = 0;
for (i = m; i < n; i++)
state_count[to_state[i]]++;
- for (i = 0; i < nstates; i++)
- if (state_count[i] > max)
+ for (s = 0; s < nstates; s++)
+ if (state_count[s] > max)
{
- max = state_count[i];
- default_state = i;
+ max = state_count[s];
+ default_state = s;
}
return default_state;
static void
goto_actions (void)
{
- int i;
- short *yydefgoto = XMALLOC (short, nsyms - ntokens);
+ symbol_number_t i;
+ state_number_t *yydefgoto = XMALLOC (state_number_t, nsyms - ntokens);
state_count = XCALLOC (short, nstates);
for (i = ntokens; i < nsyms; ++i)
{
- int default_state = default_goto (i);
+ state_number_t default_state = default_goto (i);
save_column (i, default_state);
yydefgoto[i - ntokens] = default_state;
}
- muscle_insert_short_table ("defgoto", yydefgoto,
- yydefgoto[0], 1, nsyms - ntokens);
+ muscle_insert_state_number_table ("defgoto", yydefgoto,
+ yydefgoto[0], 1, nsyms - ntokens);
XFREE (state_count);
XFREE (yydefgoto);
}
for (k = 0; ok && k < t; k++)
{
- loc = j + from[k];
+ loc = j + state_number_as_int (from[k]);
if (loc > (int) table_size)
table_grow (loc);
{
for (k = 0; k < t; k++)
{
- loc = j + from[k];
- table[loc] = to[k];
+ loc = j + state_number_as_int (from[k]);
+ table[loc] = state_number_as_int (to[k]);
if (glr_parser && conflict_to != NULL)
conflict_table[loc] = conflict_to[k];
- check[loc] = from[k];
+ check[loc] = state_number_as_int (from[k]);
}
while (table[lowzero] != 0)
static void
output_actions (void)
{
- size_t i;
- nvectors = nstates + nvars;
+ state_number_t i;
+
+ /* That's a poor way to make sure the sizes are properly corelated,
+ in particular the signedness is not taking into account, but it's
+ not useless. */
+ assert (sizeof (nvectors) >= sizeof (nstates));
+ assert (sizeof (nvectors) >= sizeof (nvars));
+
+ nvectors = state_number_as_int (nstates) + nvars;
froms = XCALLOC (short *, nvectors);
tos = XCALLOC (short *, nvectors);
MUSCLE_INSERT_INT ("pure", pure_parser);
MUSCLE_INSERT_INT ("nsym", nsyms);
MUSCLE_INSERT_INT ("debug", debug_flag);
- MUSCLE_INSERT_INT ("final", final_state);
+ MUSCLE_INSERT_INT ("final", final_state->number);
MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number);
MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
MUSCLE_INSERT_INT ("error_verbose", error_verbose);
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
if (!SHIFT_IS_DISABLED (shiftp, i))
{
- int state1 = shiftp->shifts[i];
+ state_number_t state1 = shiftp->shifts[i];
symbol_number_t symbol = states[state1]->accessing_symbol;
fprintf (out,
_(" %-4s\tshift, and go to state %d\n"),
for (; i < shiftp->nshifts; i++)
if (!SHIFT_IS_DISABLED (shiftp, i))
{
- int state1 = shiftp->shifts[i];
+ state_number_t state1 = shiftp->shifts[i];
symbol_number_t symbol = states[state1]->accessing_symbol;
fprintf (out, _(" %-4s\tgo to state %d\n"),
symbol_tag_get (symbols[symbol]), state1);
if (shiftp->nshifts == 0 && redp->nreds == 0)
{
- if (final_state == state->number)
+ if (state->number == final_state->number)
fprintf (out, _(" $default\taccept\n"));
else
fprintf (out, _(" NO ACTIONS\n"));
void
print_results (void)
{
- size_t i;
+ state_number_t i;
/* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
that conflicts with Posix. */
for (i = 0; i < shiftp->nshifts; i++)
if (!SHIFT_IS_DISABLED (shiftp, i))
{
- int state1 = shiftp->shifts[i];
+ state_number_t state1 = shiftp->shifts[i];
symbol_number_t symbol = states[state1]->accessing_symbol;
new_edge (&edge);
void
print_graph (void)
{
- size_t i;
+ state_number_t i;
/* Output file. */
fgraph = xfopen (spec_graph_file, "w");
# include "bitsetv.h"
+
+/*-------------------.
+| Numbering states. |
+`-------------------*/
+
+typedef short state_number_t;
+# define STATE_NUMBER_MAX ((state_number_t) SHRT_MAX)
+
+/* Be ready to map a state_number_t to an int. */
+# define state_number_as_int(Tok) ((int) (Tok))
+
/*---------.
| Shifts. |
`---------*/
typedef struct shifts
{
short nshifts;
- short shifts[1];
+ state_number_t shifts[1];
} shifts;
shifts *shifts_new PARAMS ((int n));
struct state_s *next;
struct state_s *link;
- short number;
+ state_number_t number;
symbol_number_t accessing_symbol;
shifts *shifts;
reductions *reductions;