2 * Copyright (c) 1989 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/usr.bin/yacc/lalr.c,v 1.7 1999/08/28 01:07:59 peter Exp $
40 static char const sccsid[] = "@(#)lalr.c 5.3 (Berkeley) 6/1/90";
58 short *accessing_symbol;
61 reductions **reduction_table;
66 static void add_lookback_edge __P((int, int, int));
67 static void build_relations __P((void));
68 static void compute_FOLLOWS __P((void));
69 static void compute_lookaheads __P((void));
70 static void digraph __P((short **));
71 static void initialize_F __P((void));
72 static void initialize_LA __P((void));
73 static int map_goto __P((int, int));
74 static void set_accessing_symbol __P((void));
75 static void set_goto_map __P((void));
76 static void set_maxrhs __P((void));
77 static void set_reduction_table __P((void));
78 static void set_shift_table __P((void));
79 static void set_state_table __P((void));
80 static short **transpose __P((short **, int));
81 static void traverse __P((register int));
87 static short **includes;
88 static shorts **lookback;
91 static short *VERTICES;
98 tokensetsize = WORDSIZE(ntokens);
101 set_accessing_symbol();
103 set_reduction_table();
110 compute_lookaheads();
120 state_table = NEW2(nstates, core *);
121 for (sp = first_state; sp; sp = sp->next)
122 state_table[sp->number] = sp;
128 set_accessing_symbol()
132 accessing_symbol = NEW2(nstates, short);
133 for (sp = first_state; sp; sp = sp->next)
134 accessing_symbol[sp->number] = sp->accessing_symbol;
144 shift_table = NEW2(nstates, shifts *);
145 for (sp = first_shift; sp; sp = sp->next)
146 shift_table[sp->number] = sp;
152 set_reduction_table()
154 register reductions *rp;
156 reduction_table = NEW2(nstates, reductions *);
157 for (rp = first_reduction; rp; rp = rp->next)
158 reduction_table[rp->number] = rp;
166 register short *itemp;
167 register short *item_end;
173 item_end = ritem + nitems;
174 for (itemp = ritem; itemp < item_end; itemp++)
182 if (length > max) max = length;
195 register int i, j, k;
196 register reductions *rp;
198 lookaheads = NEW2(nstates + 1, short);
201 for (i = 0; i < nstates; i++)
204 rp = reduction_table[i];
208 lookaheads[nstates] = k;
210 LA = NEW2(k * tokensetsize, unsigned);
211 LAruleno = NEW2(k, short);
212 lookback = NEW2(k, shorts *);
215 for (i = 0; i < nstates; i++)
217 rp = reduction_table[i];
220 for (j = 0; j < rp->nreds; j++)
222 LAruleno[k] = rp->rules[j];
237 register short *temp_map;
241 goto_map = NEW2(nvars + 1, short) - ntokens;
242 temp_map = NEW2(nvars + 1, short) - ntokens;
245 for (sp = first_shift; sp; sp = sp->next)
247 for (i = sp->nshifts - 1; i >= 0; i--)
249 symbol = accessing_symbol[sp->shift[i]];
251 if (ISTOKEN(symbol)) break;
253 if (ngotos == MAXSHORT)
254 fatal("too many gotos");
262 for (i = ntokens; i < nsyms; i++)
268 for (i = ntokens; i < nsyms; i++)
269 goto_map[i] = temp_map[i];
271 goto_map[nsyms] = ngotos;
272 temp_map[nsyms] = ngotos;
274 from_state = NEW2(ngotos, short);
275 to_state = NEW2(ngotos, short);
277 for (sp = first_shift; sp; sp = sp->next)
280 for (i = sp->nshifts - 1; i >= 0; i--)
282 state2 = sp->shift[i];
283 symbol = accessing_symbol[state2];
285 if (ISTOKEN(symbol)) break;
287 k = temp_map[symbol]++;
288 from_state[k] = state1;
289 to_state[k] = state2;
293 FREE(temp_map + ntokens);
298 /* Map_goto maps a state/symbol pair into its numeric representation. */
301 map_goto(state, symbol)
310 low = goto_map[symbol];
311 high = goto_map[symbol + 1];
316 middle = (low + high) >> 1;
317 s = from_state[middle];
336 register short *edge;
337 register unsigned *rowp;
339 register short **reads;
341 register int stateno;
345 nwords = ngotos * tokensetsize;
346 F = NEW2(nwords, unsigned);
348 reads = NEW2(ngotos, short *);
349 edge = NEW2(ngotos + 1, short);
353 for (i = 0; i < ngotos; i++)
355 stateno = to_state[i];
356 sp = shift_table[stateno];
362 for (j = 0; j < k; j++)
364 symbol = accessing_symbol[sp->shift[j]];
367 SETBIT(rowp, symbol);
372 symbol = accessing_symbol[sp->shift[j]];
373 if (nullable[symbol])
374 edge[nedges++] = map_goto(stateno, symbol);
379 reads[i] = rp = NEW2(nedges + 1, short);
381 for (j = 0; j < nedges; j++)
389 rowp += tokensetsize;
395 for (i = 0; i < ngotos; i++)
413 register short *rulep;
420 register int stateno;
421 register int symbol1;
422 register int symbol2;
423 register short *shortp;
424 register short *edge;
425 register short *states;
426 register short **new_includes;
428 includes = NEW2(ngotos, short *);
429 edge = NEW2(ngotos + 1, short);
430 states = NEW2(maxrhs + 1, short);
432 for (i = 0; i < ngotos; i++)
435 state1 = from_state[i];
436 symbol1 = accessing_symbol[to_state[i]];
438 for (rulep = derives[symbol1]; *rulep >= 0; rulep++)
444 for (rp = ritem + rrhs[*rulep]; *rp >= 0; rp++)
447 sp = shift_table[stateno];
450 for (j = 0; j < k; j++)
452 stateno = sp->shift[j];
453 if (accessing_symbol[stateno] == symbol2) break;
456 states[length++] = stateno;
459 add_lookback_edge(stateno, *rulep, i);
469 stateno = states[--length];
470 edge[nedges++] = map_goto(stateno, *rp);
471 if (nullable[*rp] && length > 0) done = 0;
478 includes[i] = shortp = NEW2(nedges + 1, short);
479 for (j = 0; j < nedges; j++)
485 new_includes = transpose(includes, ngotos);
487 for (i = 0; i < ngotos; i++)
493 includes = new_includes;
501 add_lookback_edge(stateno, ruleno, gotono)
502 int stateno, ruleno, gotono;
508 i = lookaheads[stateno];
509 k = lookaheads[stateno + 1];
511 while (!found && i < k)
513 if (LAruleno[i] == ruleno)
521 sp->next = lookback[i];
533 register short **new_R;
534 register short **temp_R;
535 register short *nedges;
540 nedges = NEW2(n, short);
542 for (i = 0; i < n; i++)
552 new_R = NEW2(n, short *);
553 temp_R = NEW2(n, short *);
555 for (i = 0; i < n; i++)
560 sp = NEW2(k + 1, short);
569 for (i = 0; i < n; i++)
575 *temp_R[*sp++]++ = i;
597 register unsigned *fp1, *fp2, *fp3;
598 register shorts *sp, *next;
599 register unsigned *rowp;
602 n = lookaheads[nstates];
603 for (i = 0; i < n; i++)
605 fp3 = rowp + tokensetsize;
606 for (sp = lookback[i]; sp; sp = sp->next)
609 fp2 = F + tokensetsize * sp->value;
616 for (i = 0; i < n; i++)
617 for (sp = lookback[i]; sp; sp = next)
634 infinity = ngotos + 2;
635 INDEX = NEW2(ngotos + 1, short);
636 VERTICES = NEW2(ngotos + 1, short);
641 for (i = 0; i < ngotos; i++)
644 for (i = 0; i < ngotos; i++)
646 if (INDEX[i] == 0 && R[i])
660 register unsigned *fp1;
661 register unsigned *fp2;
662 register unsigned *fp3;
670 INDEX[i] = height = top;
672 base = F + i * tokensetsize;
673 fp3 = base + tokensetsize;
678 while ((j = *rp++) >= 0)
683 if (INDEX[i] > INDEX[j])
687 fp2 = F + j * tokensetsize;
694 if (INDEX[i] == height)
705 fp2 = F + j * tokensetsize;