* src/state.h (SHIFT_IS_ERROR, SHIFT_IS_GOTO, SHIFT_IS_SHIFT): New.

* src/lalr.c: Use them.
This commit is contained in:
Akim Demaille
2001-12-05 09:18:57 +00:00
parent 57339f0421
commit 825b9e4ef4
3 changed files with 58 additions and 29 deletions

View File

@@ -1,3 +1,9 @@
2001-12-05 Akim Demaille <akim@epita.fr>
* src/state.h (SHIFT_IS_ERROR, SHIFT_IS_GOTO, SHIFT_IS_SHIFT): New.
* src/lalr.c: Use them.
2001-12-05 Akim Demaille <akim@epita.fr> 2001-12-05 Akim Demaille <akim@epita.fr>
* src/LR0.c (augment_automaton): Formatting changes. * src/LR0.c (augment_automaton): Formatting changes.

View File

@@ -42,15 +42,11 @@ int tokensetsize;
short *LAruleno; short *LAruleno;
unsigned *LA; unsigned *LA;
static int ngotos;
short *goto_map; short *goto_map;
short *from_state; short *from_state;
short *to_state; short *to_state;
extern void berror PARAMS ((const char *));
static int infinity;
static int ngotos;
/* And for the famous F variable, which name is so descriptive that a /* And for the famous F variable, which name is so descriptive that a
comment is hardly needed. <grin>. */ comment is hardly needed. <grin>. */
static unsigned *F = NULL; static unsigned *F = NULL;
@@ -58,11 +54,20 @@ static unsigned *F = NULL;
static short **includes; static short **includes;
static shorts **lookback; static shorts **lookback;
/*---------------------------------------------------------------.
| digraph & traverse. |
| |
| The following variables are used as common storage between the |
| two. |
`---------------------------------------------------------------*/
static short **R; static short **R;
static short *INDEX; static short *INDEX;
static short *VERTICES; static short *VERTICES;
static int top; static int top;
static int infinity;
static void static void
traverse (int i) traverse (int i)
@@ -175,16 +180,14 @@ set_state_table (void)
state_table[i].lookaheads = count; state_table[i].lookaheads = count;
if (rp if (rp
&& (rp->nreds > 1 && (rp->nreds > 1 || (sp && SHIFT_IS_SHIFT (sp, 0))))
|| (sp && !ISVAR (state_table[sp->shifts[0]].accessing_symbol))))
count += rp->nreds; count += rp->nreds;
else else
state_table[i].consistent = 1; state_table[i].consistent = 1;
if (sp) if (sp)
for (k = 0; k < sp->nshifts; k++) for (k = 0; k < sp->nshifts; k++)
if (state_table[sp->shifts[k]].accessing_symbol if (SHIFT_IS_ERROR (sp, k))
== error_token_number)
{ {
state_table[i].consistent = 0; state_table[i].consistent = 0;
break; break;
@@ -237,13 +240,10 @@ set_goto_map (void)
ngotos = 0; ngotos = 0;
for (sp = first_shift; sp; sp = sp->next) for (sp = first_shift; sp; sp = sp->next)
{ {
for (i = sp->nshifts - 1; i >= 0; i--) for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
{ {
symbol = state_table[sp->shifts[i]].accessing_symbol; symbol = state_table[sp->shifts[i]].accessing_symbol;
if (ISTOKEN (symbol))
break;
if (ngotos == MAXSHORT) if (ngotos == MAXSHORT)
fatal (_("too many gotos (max %d)"), MAXSHORT); fatal (_("too many gotos (max %d)"), MAXSHORT);
@@ -271,14 +271,11 @@ set_goto_map (void)
for (sp = first_shift; sp; sp = sp->next) for (sp = first_shift; sp; sp = sp->next)
{ {
state1 = sp->number; state1 = sp->number;
for (i = sp->nshifts - 1; i >= 0; i--) for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
{ {
state2 = sp->shifts[i]; state2 = sp->shifts[i];
symbol = state_table[state2].accessing_symbol; symbol = state_table[state2].accessing_symbol;
if (ISTOKEN (symbol))
break;
k = temp_map[symbol]++; k = temp_map[symbol]++;
from_state[k] = state1; from_state[k] = state1;
to_state[k] = state2; to_state[k] = state2;
@@ -342,11 +339,9 @@ initialize_F (void)
if (sp) if (sp)
{ {
int j; int j;
for (j = 0; j < sp->nshifts; j++) for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++)
{ {
int symbol = state_table[sp->shifts[j]].accessing_symbol; int symbol = state_table[sp->shifts[j]].accessing_symbol;
if (ISVAR (symbol))
break;
SETBIT (F + i * tokensetsize, symbol); SETBIT (F + i * tokensetsize, symbol);
} }

View File

@@ -88,6 +88,11 @@
#ifndef STATE_H_ #ifndef STATE_H_
# define STATE_H_ # define STATE_H_
/*-------.
| Core. |
`-------*/
typedef struct core typedef struct core
{ {
struct core *next; struct core *next;
@@ -96,39 +101,63 @@ typedef struct core
short accessing_symbol; short accessing_symbol;
short nitems; short nitems;
short items[1]; short items[1];
} } core;
core;
#define CORE_ALLOC(Nitems) \ #define CORE_ALLOC(Nitems) \
(core *) xcalloc ((unsigned) (sizeof (core) \ (core *) xcalloc ((unsigned) (sizeof (core) \
+ (Nitems - 1) * sizeof (short)), 1) + (Nitems - 1) * sizeof (short)), 1)
/*---------.
| Shifts. |
`---------*/
typedef struct shifts typedef struct shifts
{ {
struct shifts *next; struct shifts *next;
short number; short number;
short nshifts; short nshifts;
short shifts[1]; short shifts[1];
} } shifts;
shifts;
#define SHIFTS_ALLOC(Nshifts) \ #define SHIFTS_ALLOC(Nshifts) \
(shifts *) xcalloc ((unsigned) (sizeof (shifts) \ (shifts *) xcalloc ((unsigned) (sizeof (shifts) \
+ (Nshifts - 1) * sizeof (short)), 1) + (Nshifts - 1) * sizeof (short)), 1)
/* Is the SHIFTS->shifts[Shift] a real shift? (as opposed to gotos.) */
#define SHIFT_IS_SHIFT(Shifts, Shift) \
(ISTOKEN (state_table[Shifts->shifts[Shift]].accessing_symbol))
/* Is the SHIFTS->shifts[Shift] a goto?. */
#define SHIFT_IS_GOTO(Shifts, Shift) \
(!SHIFT_IS_SHIFT (Shifts, Shift))
/* Is the SHIFTS->shifts[Shift] then handling of the error token?. */
#define SHIFT_IS_ERROR(Shifts, Shift) \
(state_table[Shifts->shifts[Shift]].accessing_symbol == error_token_number)
/*-------.
| Errs. |
`-------*/
typedef struct errs typedef struct errs
{ {
short nerrs; short nerrs;
short errs[1]; short errs[1];
} } errs;
errs;
#define ERRS_ALLOC(Nerrs) \ #define ERRS_ALLOC(Nerrs) \
(errs *) xcalloc ((unsigned) (sizeof (errs) \ (errs *) xcalloc ((unsigned) (sizeof (errs) \
+ (Nerrs - 1) * sizeof (short)), 1) + (Nerrs - 1) * sizeof (short)), 1)
/*-------------.
| Reductions. |
`-------------*/
typedef struct reductions typedef struct reductions
{ {
@@ -136,8 +165,7 @@ typedef struct reductions
short number; short number;
short nreds; short nreds;
short rules[1]; short rules[1];
} } reductions;
reductions;
#define REDUCTIONS_ALLOC(Nreductions) \ #define REDUCTIONS_ALLOC(Nreductions) \
(reductions *) xcalloc ((unsigned) (sizeof (reductions) \ (reductions *) xcalloc ((unsigned) (sizeof (reductions) \