32 |
#include "symtab.h" |
#include "symtab.h" |
33 |
#include "gram.h" |
#include "gram.h" |
34 |
#include "reader.h" |
#include "reader.h" |
|
#include "types.h" |
|
35 |
#include "LR0.h" |
#include "LR0.h" |
36 |
#include "complain.h" |
#include "complain.h" |
37 |
#include "lalr.h" |
#include "lalr.h" |
39 |
#include "derives.h" |
#include "derives.h" |
40 |
#include "getargs.h" |
#include "getargs.h" |
41 |
|
|
42 |
|
goto_number_t *goto_map = NULL; |
43 |
|
static goto_number_t ngotos = 0; |
44 |
|
state_number_t *from_state = NULL; |
45 |
|
state_number_t *to_state = NULL; |
46 |
|
|
47 |
|
/* Linked list of goto numbers. */ |
48 |
|
typedef struct goto_list_s |
49 |
|
{ |
50 |
|
struct goto_list_s *next; |
51 |
|
goto_number_t value; |
52 |
|
} goto_list_t; |
53 |
|
|
54 |
|
|
55 |
rule_t **LArule = NULL; |
rule_t **LArule = NULL; |
56 |
bitsetv LA = NULL; |
bitsetv LA = NULL; |
57 |
size_t nLA; |
size_t nLA; |
58 |
|
|
|
static int ngotos; |
|
|
short *goto_map = NULL; |
|
|
state_number_t *from_state = NULL; |
|
|
state_number_t *to_state = NULL; |
|
59 |
|
|
60 |
/* 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 |
61 |
comment is hardly needed. <grin>. */ |
comment is hardly needed. <grin>. */ |
62 |
static bitsetv F = NULL; |
static bitsetv F = NULL; |
63 |
|
|
64 |
static short **includes; |
static goto_number_t **includes; |
65 |
static shorts **lookback; |
static goto_list_t **lookback; |
66 |
|
|
67 |
|
|
68 |
|
|
80 |
|
|
81 |
LA = bitsetv_create (nLA, ntokens, BITSET_FIXED); |
LA = bitsetv_create (nLA, ntokens, BITSET_FIXED); |
82 |
LArule = XCALLOC (rule_t *, nLA); |
LArule = XCALLOC (rule_t *, nLA); |
83 |
lookback = XCALLOC (shorts *, nLA); |
lookback = XCALLOC (goto_list_t *, nLA); |
84 |
|
|
85 |
np = LArule; |
np = LArule; |
86 |
for (i = 0; i < nstates; i++) |
for (i = 0; i < nstates; i++) |
94 |
set_goto_map (void) |
set_goto_map (void) |
95 |
{ |
{ |
96 |
state_number_t state; |
state_number_t state; |
97 |
short *temp_map; |
goto_number_t *temp_map; |
98 |
|
|
99 |
goto_map = XCALLOC (short, nvars + 1) - ntokens; |
goto_map = XCALLOC (goto_number_t, nvars + 1) - ntokens; |
100 |
temp_map = XCALLOC (short, nvars + 1) - ntokens; |
temp_map = XCALLOC (goto_number_t, nvars + 1) - ntokens; |
101 |
|
|
102 |
ngotos = 0; |
ngotos = 0; |
103 |
for (state = 0; state < nstates; ++state) |
for (state = 0; state < nstates; ++state) |
106 |
int i; |
int i; |
107 |
for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i) |
for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i) |
108 |
{ |
{ |
109 |
if (ngotos == SHRT_MAX) |
if (ngotos == GOTO_NUMBER_MAX) |
110 |
fatal (_("too many gotos (max %d)"), SHRT_MAX); |
fatal (_("too many gotos (max %d)"), GOTO_NUMBER_MAX); |
111 |
|
|
112 |
ngotos++; |
ngotos++; |
113 |
goto_map[TRANSITION_SYMBOL (sp, i)]++; |
goto_map[TRANSITION_SYMBOL (sp, i)]++; |
186 |
static void |
static void |
187 |
initialize_F (void) |
initialize_F (void) |
188 |
{ |
{ |
189 |
short **reads = XCALLOC (short *, ngotos); |
goto_number_t **reads = XCALLOC (goto_number_t *, ngotos); |
190 |
short *edge = XCALLOC (short, ngotos + 1); |
goto_number_t *edge = XCALLOC (goto_number_t, ngotos + 1); |
191 |
int nedges = 0; |
int nedges = 0; |
192 |
|
|
193 |
int i; |
int i; |
212 |
|
|
213 |
if (nedges) |
if (nedges) |
214 |
{ |
{ |
215 |
reads[i] = XCALLOC (short, nedges + 1); |
reads[i] = XCALLOC (goto_number_t, nedges + 1); |
216 |
memcpy (reads[i], edge, nedges * sizeof (edge[0])); |
memcpy (reads[i], edge, nedges * sizeof (edge[0])); |
217 |
reads[i][nedges] = -1; |
reads[i][nedges] = -1; |
218 |
nedges = 0; |
nedges = 0; |
233 |
add_lookback_edge (state_t *state, rule_number_t ruleno, int gotono) |
add_lookback_edge (state_t *state, rule_number_t ruleno, int gotono) |
234 |
{ |
{ |
235 |
int i; |
int i; |
236 |
shorts *sp; |
goto_list_t *sp; |
237 |
|
|
238 |
for (i = 0; i < state->nlookaheads; ++i) |
for (i = 0; i < state->nlookaheads; ++i) |
239 |
if (state->lookaheads_rule[i]->number == ruleno) |
if (state->lookaheads_rule[i]->number == ruleno) |
241 |
|
|
242 |
assert (state->lookaheads_rule[i]->number == ruleno); |
assert (state->lookaheads_rule[i]->number == ruleno); |
243 |
|
|
244 |
sp = XCALLOC (shorts, 1); |
sp = XCALLOC (goto_list_t, 1); |
245 |
sp->next = lookback[(state->lookaheads - LA) + i]; |
sp->next = lookback[(state->lookaheads - LA) + i]; |
246 |
sp->value = gotono; |
sp->value = gotono; |
247 |
lookback[(state->lookaheads - LA) + i] = sp; |
lookback[(state->lookaheads - LA) + i] = sp; |
252 |
static void |
static void |
253 |
build_relations (void) |
build_relations (void) |
254 |
{ |
{ |
255 |
short *edge = XCALLOC (short, ngotos + 1); |
goto_number_t *edge = XCALLOC (goto_number_t, ngotos + 1); |
256 |
state_number_t *states1 = XCALLOC (state_number_t, ritem_longest_rhs () + 1); |
state_number_t *states1 = XCALLOC (state_number_t, ritem_longest_rhs () + 1); |
257 |
int i; |
int i; |
258 |
|
|
259 |
includes = XCALLOC (short *, ngotos); |
includes = XCALLOC (goto_number_t *, ngotos); |
260 |
|
|
261 |
for (i = 0; i < ngotos; i++) |
for (i = 0; i < ngotos; i++) |
262 |
{ |
{ |
303 |
if (nedges) |
if (nedges) |
304 |
{ |
{ |
305 |
int j; |
int j; |
306 |
includes[i] = XCALLOC (short, nedges + 1); |
includes[i] = XCALLOC (goto_number_t, nedges + 1); |
307 |
for (j = 0; j < nedges; j++) |
for (j = 0; j < nedges; j++) |
308 |
includes[i][j] = edge[j]; |
includes[i][j] = edge[j]; |
309 |
includes[i][nedges] = -1; |
includes[i][nedges] = -1; |
336 |
compute_lookaheads (void) |
compute_lookaheads (void) |
337 |
{ |
{ |
338 |
size_t i; |
size_t i; |
339 |
shorts *sp; |
goto_list_t *sp; |
340 |
|
|
341 |
for (i = 0; i < nLA; i++) |
for (i = 0; i < nLA; i++) |
342 |
for (sp = lookback[i]; sp; sp = sp->next) |
for (sp = lookback[i]; sp; sp = sp->next) |
344 |
|
|
345 |
/* Free LOOKBACK. */ |
/* Free LOOKBACK. */ |
346 |
for (i = 0; i < nLA; i++) |
for (i = 0; i < nLA; i++) |
347 |
LIST_FREE (shorts, lookback[i]); |
LIST_FREE (goto_list_t, lookback[i]); |
348 |
|
|
349 |
XFREE (lookback); |
XFREE (lookback); |
350 |
bitsetv_free (F); |
bitsetv_free (F); |