36 |
/* All the decorated states, indexed by the state number. Warning: |
/* All the decorated states, indexed by the state number. Warning: |
37 |
there is a state_TABLE in LR0.c, but it is different and static. |
there is a state_TABLE in LR0.c, but it is different and static. |
38 |
*/ |
*/ |
39 |
state_t *state_table = NULL; |
state_t **state_table = NULL; |
40 |
|
|
41 |
int tokensetsize; |
int tokensetsize; |
42 |
short *LAruleno; |
short *LAruleno; |
142 |
/* NSTATES + 1 because lookahead for the pseudo state number NSTATES |
/* NSTATES + 1 because lookahead for the pseudo state number NSTATES |
143 |
might be used (see conflicts.c). It is too opaque for me to |
might be used (see conflicts.c). It is too opaque for me to |
144 |
provide a probably less hacky implementation. --akim */ |
provide a probably less hacky implementation. --akim */ |
145 |
state_table = XCALLOC (state_t, nstates + 1); |
state_table = XCALLOC (state_t *, nstates + 1); |
146 |
|
|
147 |
{ |
{ |
148 |
core *sp; |
state_t *sp; |
149 |
for (sp = first_state; sp; sp = sp->next) |
for (sp = first_state; sp; sp = sp->next) |
150 |
{ |
state_table[sp->number] = sp; |
|
state_table[sp->number].state = sp; |
|
|
state_table[sp->number].accessing_symbol = sp->accessing_symbol; |
|
|
} |
|
151 |
} |
} |
152 |
|
|
153 |
{ |
{ |
154 |
shifts *sp; |
shifts *sp; |
155 |
for (sp = first_shift; sp; sp = sp->next) |
for (sp = first_shift; sp; sp = sp->next) |
156 |
state_table[sp->number].shifts = sp; |
state_table[sp->number]->shifts = sp; |
157 |
} |
} |
158 |
|
|
159 |
{ |
{ |
160 |
reductions *rp; |
reductions *rp; |
161 |
for (rp = first_reduction; rp; rp = rp->next) |
for (rp = first_reduction; rp; rp = rp->next) |
162 |
state_table[rp->number].reductions = rp; |
state_table[rp->number]->reductions = rp; |
163 |
} |
} |
164 |
|
|
165 |
/* Pessimization, but simplification of the code: make sense all the |
/* Pessimization, but simplification of the code: make sure all the |
166 |
states have a shifts, even if reduced to 0 shifts. */ |
states have a shifts, even if reduced to 0 shifts. */ |
167 |
{ |
{ |
168 |
int i; |
int i; |
169 |
for (i = 0; i < nstates; i++) |
for (i = 0; i < nstates; i++) |
170 |
if (!state_table[i].shifts) |
if (!state_table[i]->shifts) |
171 |
state_table[i].shifts = shifts_new (0); |
state_table[i]->shifts = shifts_new (0); |
172 |
} |
} |
173 |
|
|
174 |
/* Initializing the lookaheads members. Please note that it must be |
/* Initializing the lookaheads members. Please note that it must be |
180 |
for (i = 0; i < nstates; i++) |
for (i = 0; i < nstates; i++) |
181 |
{ |
{ |
182 |
int k; |
int k; |
183 |
reductions *rp = state_table[i].reductions; |
reductions *rp = state_table[i]->reductions; |
184 |
shifts *sp = state_table[i].shifts; |
shifts *sp = state_table[i]->shifts; |
185 |
|
|
186 |
state_table[i].lookaheads = count; |
state_table[i]->lookaheads = count; |
187 |
|
|
188 |
if (rp |
if (rp |
189 |
&& (rp->nreds > 1 || (sp->nshifts && SHIFT_IS_SHIFT (sp, 0)))) |
&& (rp->nreds > 1 || (sp->nshifts && SHIFT_IS_SHIFT (sp, 0)))) |
190 |
count += rp->nreds; |
count += rp->nreds; |
191 |
else |
else |
192 |
state_table[i].consistent = 1; |
state_table[i]->consistent = 1; |
193 |
|
|
194 |
for (k = 0; k < sp->nshifts; k++) |
for (k = 0; k < sp->nshifts; k++) |
195 |
if (SHIFT_IS_ERROR (sp, k)) |
if (SHIFT_IS_ERROR (sp, k)) |
196 |
{ |
{ |
197 |
state_table[i].consistent = 0; |
state_table[i]->consistent = 0; |
198 |
break; |
break; |
199 |
} |
} |
200 |
} |
} |
201 |
state_table[nstates].lookaheads = count; |
|
202 |
|
/* Seems to be needed by conflicts.c. */ |
203 |
|
state_table[nstates] = STATE_ALLOC (0); |
204 |
|
state_table[nstates]->lookaheads = count; |
205 |
} |
} |
206 |
} |
} |
207 |
|
|
214 |
short *np; |
short *np; |
215 |
reductions *rp; |
reductions *rp; |
216 |
|
|
217 |
size_t nLA = state_table[nstates].lookaheads; |
size_t nLA = state_table[nstates]->lookaheads; |
218 |
if (!nLA) |
if (!nLA) |
219 |
nLA = 1; |
nLA = 1; |
220 |
|
|
224 |
|
|
225 |
np = LAruleno; |
np = LAruleno; |
226 |
for (i = 0; i < nstates; i++) |
for (i = 0; i < nstates; i++) |
227 |
if (!state_table[i].consistent) |
if (!state_table[i]->consistent) |
228 |
if ((rp = state_table[i].reductions)) |
if ((rp = state_table[i]->reductions)) |
229 |
for (j = 0; j < rp->nreds; j++) |
for (j = 0; j < rp->nreds; j++) |
230 |
*np++ = rp->rules[j]; |
*np++ = rp->rules[j]; |
231 |
} |
} |
249 |
for (sp = first_shift; sp; sp = sp->next) |
for (sp = first_shift; sp; sp = sp->next) |
250 |
for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
251 |
{ |
{ |
252 |
symbol = state_table[sp->shifts[i]].accessing_symbol; |
symbol = state_table[sp->shifts[i]]->accessing_symbol; |
253 |
|
|
254 |
if (ngotos == MAXSHORT) |
if (ngotos == MAXSHORT) |
255 |
fatal (_("too many gotos (max %d)"), MAXSHORT); |
fatal (_("too many gotos (max %d)"), MAXSHORT); |
280 |
for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
281 |
{ |
{ |
282 |
state2 = sp->shifts[i]; |
state2 = sp->shifts[i]; |
283 |
symbol = state_table[state2].accessing_symbol; |
symbol = state_table[state2]->accessing_symbol; |
284 |
|
|
285 |
k = temp_map[symbol]++; |
k = temp_map[symbol]++; |
286 |
from_state[k] = state1; |
from_state[k] = state1; |
340 |
for (i = 0; i < ngotos; i++) |
for (i = 0; i < ngotos; i++) |
341 |
{ |
{ |
342 |
int stateno = to_state[i]; |
int stateno = to_state[i]; |
343 |
shifts *sp = state_table[stateno].shifts; |
shifts *sp = state_table[stateno]->shifts; |
344 |
|
|
345 |
int j; |
int j; |
346 |
for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++) |
for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++) |
347 |
{ |
{ |
348 |
int symbol = state_table[sp->shifts[j]].accessing_symbol; |
int symbol = state_table[sp->shifts[j]]->accessing_symbol; |
349 |
SETBIT (F (i), symbol); |
SETBIT (F (i), symbol); |
350 |
} |
} |
351 |
|
|
352 |
for (; j < sp->nshifts; j++) |
for (; j < sp->nshifts; j++) |
353 |
{ |
{ |
354 |
int symbol = state_table[sp->shifts[j]].accessing_symbol; |
int symbol = state_table[sp->shifts[j]]->accessing_symbol; |
355 |
if (nullable[symbol]) |
if (nullable[symbol]) |
356 |
edge[nedges++] = map_goto (stateno, symbol); |
edge[nedges++] = map_goto (stateno, symbol); |
357 |
} |
} |
383 |
int found; |
int found; |
384 |
shorts *sp; |
shorts *sp; |
385 |
|
|
386 |
i = state_table[stateno].lookaheads; |
i = state_table[stateno]->lookaheads; |
387 |
k = state_table[stateno + 1].lookaheads; |
k = state_table[stateno + 1]->lookaheads; |
388 |
found = 0; |
found = 0; |
389 |
while (!found && i < k) |
while (!found && i < k) |
390 |
{ |
{ |
502 |
{ |
{ |
503 |
int nedges = 0; |
int nedges = 0; |
504 |
int state1 = from_state[i]; |
int state1 = from_state[i]; |
505 |
int symbol1 = state_table[to_state[i]].accessing_symbol; |
int symbol1 = state_table[to_state[i]]->accessing_symbol; |
506 |
short *rulep; |
short *rulep; |
507 |
|
|
508 |
for (rulep = derives[symbol1]; *rulep > 0; rulep++) |
for (rulep = derives[symbol1]; *rulep > 0; rulep++) |
515 |
|
|
516 |
for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++) |
for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++) |
517 |
{ |
{ |
518 |
shifts *sp = state_table[stateno].shifts; |
shifts *sp = state_table[stateno]->shifts; |
519 |
int j; |
int j; |
520 |
for (j = 0; j < sp->nshifts; j++) |
for (j = 0; j < sp->nshifts; j++) |
521 |
{ |
{ |
522 |
stateno = sp->shifts[j]; |
stateno = sp->shifts[j]; |
523 |
if (state_table[stateno].accessing_symbol == *rp) |
if (state_table[stateno]->accessing_symbol == *rp) |
524 |
break; |
break; |
525 |
} |
} |
526 |
|
|
527 |
states[length++] = stateno; |
states[length++] = stateno; |
528 |
} |
} |
529 |
|
|
530 |
if (!state_table[stateno].consistent) |
if (!state_table[stateno]->consistent) |
531 |
add_lookback_edge (stateno, *rulep, i); |
add_lookback_edge (stateno, *rulep, i); |
532 |
|
|
533 |
length--; |
length--; |
585 |
int i; |
int i; |
586 |
shorts *sp; |
shorts *sp; |
587 |
|
|
588 |
for (i = 0; i < state_table[nstates].lookaheads; i++) |
for (i = 0; i < state_table[nstates]->lookaheads; i++) |
589 |
for (sp = lookback[i]; sp; sp = sp->next) |
for (sp = lookback[i]; sp; sp = sp->next) |
590 |
{ |
{ |
591 |
int size = LA (i + 1) - LA (i); |
int size = LA (i + 1) - LA (i); |
595 |
} |
} |
596 |
|
|
597 |
/* Free LOOKBACK. */ |
/* Free LOOKBACK. */ |
598 |
for (i = 0; i < state_table[nstates].lookaheads; i++) |
for (i = 0; i < state_table[nstates]->lookaheads; i++) |
599 |
LIST_FREE (shorts, lookback[i]); |
LIST_FREE (shorts, lookback[i]); |
600 |
|
|
601 |
XFREE (lookback); |
XFREE (lookback); |