15 short *accessing_symbol;
18 reductions **reduction_table;
29 static short **includes;
30 static shorts **lookback;
33 static short *VERTICES;
39 tokensetsize = WORDSIZE(ntokens);
42 set_accessing_symbol();
44 set_reduction_table();
60 state_table = NEW2(nstates, core *);
61 for (sp = first_state; sp; sp = sp->next)
62 state_table[sp->number] = sp;
67 set_accessing_symbol()
71 accessing_symbol = NEW2(nstates, short);
72 for (sp = first_state; sp; sp = sp->next)
73 accessing_symbol[sp->number] = sp->accessing_symbol;
82 shift_table = NEW2(nstates, shifts *);
83 for (sp = first_shift; sp; sp = sp->next)
84 shift_table[sp->number] = sp;
91 register reductions *rp;
93 reduction_table = NEW2(nstates, reductions *);
94 for (rp = first_reduction; rp; rp = rp->next)
95 reduction_table[rp->number] = rp;
102 register short *itemp;
103 register short *item_end;
109 item_end = ritem + nitems;
110 for (itemp = ritem; itemp < item_end; itemp++)
118 if (length > max) max = length;
130 register int i, j, k;
131 register reductions *rp;
133 lookaheads = NEW2(nstates + 1, short);
136 for (i = 0; i < nstates; i++)
139 rp = reduction_table[i];
143 lookaheads[nstates] = k;
145 LA = NEW2(k * tokensetsize, unsigned);
146 LAruleno = NEW2(k, short);
147 lookback = NEW2(k, shorts *);
150 for (i = 0; i < nstates; i++)
152 rp = reduction_table[i];
155 for (j = 0; j < rp->nreds; j++)
157 LAruleno[k] = rp->rules[j];
171 register short *temp_map;
175 goto_map = NEW2(nvars + 1, short) - ntokens;
176 temp_map = NEW2(nvars + 1, short) - ntokens;
179 for (sp = first_shift; sp; sp = sp->next)
181 for (i = sp->nshifts - 1; i >= 0; i--)
183 symbol = accessing_symbol[sp->shift[i]];
185 if (ISTOKEN(symbol)) break;
187 if (ngotos == MAXSHORT)
188 fatal("too many gotos");
196 for (i = ntokens; i < nsyms; i++)
202 for (i = ntokens; i < nsyms; i++)
203 goto_map[i] = temp_map[i];
205 goto_map[nsyms] = ngotos;
206 temp_map[nsyms] = ngotos;
208 from_state = NEW2(ngotos, short);
209 to_state = NEW2(ngotos, short);
211 for (sp = first_shift; sp; sp = sp->next)
214 for (i = sp->nshifts - 1; i >= 0; i--)
216 state2 = sp->shift[i];
217 symbol = accessing_symbol[state2];
219 if (ISTOKEN(symbol)) break;
221 k = temp_map[symbol]++;
222 from_state[k] = state1;
223 to_state[k] = state2;
227 FREE(temp_map + ntokens);
232 /* Map_goto maps a state/symbol pair into its numeric representation. */
235 map_goto(state, symbol)
244 low = goto_map[symbol];
245 high = goto_map[symbol + 1];
250 middle = (low + high) >> 1;
251 s = from_state[middle];
269 register short *edge;
270 register unsigned *rowp;
272 register short **reads;
274 register int stateno;
278 nwords = ngotos * tokensetsize;
279 F = NEW2(nwords, unsigned);
281 reads = NEW2(ngotos, short *);
282 edge = NEW2(ngotos + 1, short);
286 for (i = 0; i < ngotos; i++)
288 stateno = to_state[i];
289 sp = shift_table[stateno];
295 for (j = 0; j < k; j++)
297 symbol = accessing_symbol[sp->shift[j]];
300 SETBIT(rowp, symbol);
305 symbol = accessing_symbol[sp->shift[j]];
306 if (nullable[symbol])
307 edge[nedges++] = map_goto(stateno, symbol);
312 reads[i] = rp = NEW2(nedges + 1, short);
314 for (j = 0; j < nedges; j++)
322 rowp += tokensetsize;
328 for (i = 0; i < ngotos; i++)
345 register short *rulep;
352 register int stateno;
353 register int symbol1;
354 register int symbol2;
355 register short *shortp;
356 register short *edge;
357 register short *states;
358 register short **new_includes;
360 includes = NEW2(ngotos, short *);
361 edge = NEW2(ngotos + 1, short);
362 states = NEW2(maxrhs + 1, short);
364 for (i = 0; i < ngotos; i++)
367 state1 = from_state[i];
368 symbol1 = accessing_symbol[to_state[i]];
370 for (rulep = derives[symbol1]; *rulep >= 0; rulep++)
376 for (rp = ritem + rrhs[*rulep]; *rp >= 0; rp++)
379 sp = shift_table[stateno];
382 for (j = 0; j < k; j++)
384 stateno = sp->shift[j];
385 if (accessing_symbol[stateno] == symbol2) break;
388 states[length++] = stateno;
391 add_lookback_edge(stateno, *rulep, i);
401 stateno = states[--length];
402 edge[nedges++] = map_goto(stateno, *rp);
403 if (nullable[*rp] && length > 0) done = 0;
410 includes[i] = shortp = NEW2(nedges + 1, short);
411 for (j = 0; j < nedges; j++)
417 new_includes = transpose(includes, ngotos);
419 for (i = 0; i < ngotos; i++)
425 includes = new_includes;
432 add_lookback_edge(stateno, ruleno, gotono)
433 int stateno, ruleno, gotono;
439 i = lookaheads[stateno];
440 k = lookaheads[stateno + 1];
442 while (!found && i < k)
444 if (LAruleno[i] == ruleno)
452 sp->next = lookback[i];
464 register short **new_R;
465 register short **temp_R;
466 register short *nedges;
471 nedges = NEW2(n, short);
473 for (i = 0; i < n; i++)
483 new_R = NEW2(n, short *);
484 temp_R = NEW2(n, short *);
486 for (i = 0; i < n; i++)
491 sp = NEW2(k + 1, short);
500 for (i = 0; i < n; i++)
506 *temp_R[*sp++]++ = i;
526 register unsigned *fp1, *fp2, *fp3;
527 register shorts *sp, *next;
528 register unsigned *rowp;
531 n = lookaheads[nstates];
532 for (i = 0; i < n; i++)
534 fp3 = rowp + tokensetsize;
535 for (sp = lookback[i]; sp; sp = sp->next)
538 fp2 = F + tokensetsize * sp->value;
545 for (i = 0; i < n; i++)
546 for (sp = lookback[i]; sp; sp = next)
562 infinity = ngotos + 2;
563 INDEX = NEW2(ngotos + 1, short);
564 VERTICES = NEW2(ngotos + 1, short);
569 for (i = 0; i < ngotos; i++)
572 for (i = 0; i < ngotos; i++)
574 if (INDEX[i] == 0 && R[i])
587 register unsigned *fp1;
588 register unsigned *fp2;
589 register unsigned *fp3;
597 INDEX[i] = height = top;
599 base = F + i * tokensetsize;
600 fp3 = base + tokensetsize;
605 while ((j = *rp++) >= 0)
610 if (INDEX[i] > INDEX[j])
614 fp2 = F + j * tokensetsize;
621 if (INDEX[i] == height)
632 fp2 = F + j * tokensetsize;