]> git.saurik.com Git - bison.git/blame - src/lalr.c
(resolve_sr_conflict): Remove use of alloca.
[bison.git] / src / lalr.c
CommitLineData
d0fb370f
RS
1/* Compute look-ahead criteria for bison,
2 Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
3
4This file is part of Bison, the GNU Compiler Compiler.
5
6Bison is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11Bison is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Bison; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21/* Compute how to make the finite state machine deterministic;
22 find which rules need lookahead in each state, and which lookahead tokens they accept.
23
24lalr(), the entry point, builds these data structures:
25
26goto_map, from_state and to_state
27 record each shift transition which accepts a variable (a nonterminal).
28ngotos is the number of such transitions.
29from_state[t] is the state number which a transition leads from
30and to_state[t] is the state number it leads to.
31All the transitions that accept a particular variable are grouped together and
32goto_map[i - ntokens] is the index in from_state and to_state of the first of them.
33
34consistent[s] is nonzero if no lookahead is needed to decide what to do in state s.
35
36LAruleno is a vector which records the rules that need lookahead in various states.
37The elements of LAruleno that apply to state s are those from
38 lookaheads[s] through lookaheads[s+1]-1.
39Each element of LAruleno is a rule number.
40
41If lr is the length of LAruleno, then a number from 0 to lr-1
42can specify both a rule and a state where the rule might be applied.
43
44LA is a lr by ntokens matrix of bits.
45LA[l, i] is 1 if the rule LAruleno[l] is applicable in the appropriate state
46 when the next token is symbol i.
47If LA[l, i] and LA[l, j] are both 1 for i != j, it is a conflict.
48*/
49
50#include <stdio.h>
51#include "system.h"
52#include "machine.h"
53#include "types.h"
54#include "state.h"
55#include "new.h"
56#include "gram.h"
57
58
59extern short **derives;
60extern char *nullable;
61
62
63int tokensetsize;
64short *lookaheads;
65short *LAruleno;
66unsigned *LA;
67short *accessing_symbol;
68char *consistent;
69core **state_table;
70shifts **shift_table;
71reductions **reduction_table;
72short *goto_map;
73short *from_state;
74short *to_state;
75
76short **transpose();
77void set_state_table();
78void set_accessing_symbol();
79void set_shift_table();
80void set_reduction_table();
81void set_maxrhs();
82void initialize_LA();
83void set_goto_map();
84void initialize_F();
85void build_relations();
86void add_lookback_edge();
87void compute_FOLLOWS();
88void compute_lookaheads();
89void digraph();
90void traverse();
91
92extern void toomany();
93extern void berror();
94
95static int infinity;
96static int maxrhs;
97static int ngotos;
98static unsigned *F;
99static short **includes;
100static shorts **lookback;
101static short **R;
102static short *INDEX;
103static short *VERTICES;
104static int top;
105
106
107void
108lalr()
109{
110 tokensetsize = WORDSIZE(ntokens);
111
112 set_state_table();
113 set_accessing_symbol();
114 set_shift_table();
115 set_reduction_table();
116 set_maxrhs();
117 initialize_LA();
118 set_goto_map();
119 initialize_F();
120 build_relations();
121 compute_FOLLOWS();
122 compute_lookaheads();
123}
124
125
126void
127set_state_table()
128{
129 register core *sp;
130
131 state_table = NEW2(nstates, core *);
132
133 for (sp = first_state; sp; sp = sp->next)
134 state_table[sp->number] = sp;
135}
136
137
138void
139set_accessing_symbol()
140{
141 register core *sp;
142
143 accessing_symbol = NEW2(nstates, short);
144
145 for (sp = first_state; sp; sp = sp->next)
146 accessing_symbol[sp->number] = sp->accessing_symbol;
147}
148
149
150void
151set_shift_table()
152{
153 register shifts *sp;
154
155 shift_table = NEW2(nstates, shifts *);
156
157 for (sp = first_shift; sp; sp = sp->next)
158 shift_table[sp->number] = sp;
159}
160
161
162void
163set_reduction_table()
164{
165 register reductions *rp;
166
167 reduction_table = NEW2(nstates, reductions *);
168
169 for (rp = first_reduction; rp; rp = rp->next)
170 reduction_table[rp->number] = rp;
171}
172
173
174void
175set_maxrhs()
176{
177 register short *itemp;
178 register int length;
179 register int max;
180
181 length = 0;
182 max = 0;
183 for (itemp = ritem; *itemp; itemp++)
184 {
185 if (*itemp > 0)
186 {
187 length++;
188 }
189 else
190 {
191 if (length > max) max = length;
192 length = 0;
193 }
194 }
195
196 maxrhs = max;
197}
198
199
200void
201initialize_LA()
202{
203 register int i;
204 register int j;
205 register int count;
206 register reductions *rp;
207 register shifts *sp;
208 register short *np;
209
210 consistent = NEW2(nstates, char);
211 lookaheads = NEW2(nstates + 1, short);
212
213 count = 0;
214 for (i = 0; i < nstates; i++)
215 {
216 register int k;
217
218 lookaheads[i] = count;
219
220 rp = reduction_table[i];
221 sp = shift_table[i];
222 if (rp && (rp->nreds > 1
223 || (sp && ! ISVAR(accessing_symbol[sp->shifts[0]]))))
224 count += rp->nreds;
225 else
226 consistent[i] = 1;
227
228 if (sp)
229 for (k = 0; k < sp->nshifts; k++)
230 {
231 if (accessing_symbol[sp->shifts[k]] == error_token_number)
232 {
233 consistent[i] = 0;
234 break;
235 }
236 }
237 }
238
239 lookaheads[nstates] = count;
240
241 if (count == 0)
242 {
243 LA = NEW2(1 * tokensetsize, unsigned);
244 LAruleno = NEW2(1, short);
245 lookback = NEW2(1, shorts *);
246 }
247 else
248 {
249 LA = NEW2(count * tokensetsize, unsigned);
250 LAruleno = NEW2(count, short);
251 lookback = NEW2(count, shorts *);
252 }
253
254 np = LAruleno;
255 for (i = 0; i < nstates; i++)
256 {
257 if (!consistent[i])
258 {
259 if (rp = reduction_table[i])
260 for (j = 0; j < rp->nreds; j++)
261 *np++ = rp->rules[j];
262 }
263 }
264}
265
266
267void
268set_goto_map()
269{
270 register shifts *sp;
271 register int i;
272 register int symbol;
273 register int k;
274 register short *temp_map;
275 register int state2;
276 register int state1;
277
278 goto_map = NEW2(nvars + 1, short) - ntokens;
279 temp_map = NEW2(nvars + 1, short) - ntokens;
280
281 ngotos = 0;
282 for (sp = first_shift; sp; sp = sp->next)
283 {
284 for (i = sp->nshifts - 1; i >= 0; i--)
285 {
286 symbol = accessing_symbol[sp->shifts[i]];
287
288 if (ISTOKEN(symbol)) break;
289
290 if (ngotos == MAXSHORT)
291 toomany("gotos");
292
293 ngotos++;
294 goto_map[symbol]++;
295 }
296 }
297
298 k = 0;
299 for (i = ntokens; i < nsyms; i++)
300 {
301 temp_map[i] = k;
302 k += goto_map[i];
303 }
304
305 for (i = ntokens; i < nsyms; i++)
306 goto_map[i] = temp_map[i];
307
308 goto_map[nsyms] = ngotos;
309 temp_map[nsyms] = ngotos;
310
311 from_state = NEW2(ngotos, short);
312 to_state = NEW2(ngotos, short);
313
314 for (sp = first_shift; sp; sp = sp->next)
315 {
316 state1 = sp->number;
317 for (i = sp->nshifts - 1; i >= 0; i--)
318 {
319 state2 = sp->shifts[i];
320 symbol = accessing_symbol[state2];
321
322 if (ISTOKEN(symbol)) break;
323
324 k = temp_map[symbol]++;
325 from_state[k] = state1;
326 to_state[k] = state2;
327 }
328 }
329
330 FREE(temp_map + ntokens);
331}
332
333
334
335/* Map_goto maps a state/symbol pair into its numeric representation. */
336
337int
338map_goto(state, symbol)
339int state;
340int symbol;
341{
342 register int high;
343 register int low;
344 register int middle;
345 register int s;
346
347 low = goto_map[symbol];
348 high = goto_map[symbol + 1] - 1;
349
350 while (low <= high)
351 {
352 middle = (low + high) / 2;
353 s = from_state[middle];
354 if (s == state)
355 return (middle);
356 else if (s < state)
357 low = middle + 1;
358 else
359 high = middle - 1;
360 }
361
362 berror("map_goto");
363/* NOTREACHED */
364 return 0;
365}
366
367
368void
369initialize_F()
370{
371 register int i;
372 register int j;
373 register int k;
374 register shifts *sp;
375 register short *edge;
376 register unsigned *rowp;
377 register short *rp;
378 register short **reads;
379 register int nedges;
380 register int stateno;
381 register int symbol;
382 register int nwords;
383
384 nwords = ngotos * tokensetsize;
385 F = NEW2(nwords, unsigned);
386
387 reads = NEW2(ngotos, short *);
388 edge = NEW2(ngotos + 1, short);
389 nedges = 0;
390
391 rowp = F;
392 for (i = 0; i < ngotos; i++)
393 {
394 stateno = to_state[i];
395 sp = shift_table[stateno];
396
397 if (sp)
398 {
399 k = sp->nshifts;
400
401 for (j = 0; j < k; j++)
402 {
403 symbol = accessing_symbol[sp->shifts[j]];
404 if (ISVAR(symbol))
405 break;
406 SETBIT(rowp, symbol);
407 }
408
409 for (; j < k; j++)
410 {
411 symbol = accessing_symbol[sp->shifts[j]];
412 if (nullable[symbol])
413 edge[nedges++] = map_goto(stateno, symbol);
414 }
415
416 if (nedges)
417 {
418 reads[i] = rp = NEW2(nedges + 1, short);
419
420 for (j = 0; j < nedges; j++)
421 rp[j] = edge[j];
422
423 rp[nedges] = -1;
424 nedges = 0;
425 }
426 }
427
428 rowp += tokensetsize;
429 }
430
431 digraph(reads);
432
433 for (i = 0; i < ngotos; i++)
434 {
435 if (reads[i])
436 FREE(reads[i]);
437 }
438
439 FREE(reads);
440 FREE(edge);
441}
442
443
444void
445build_relations()
446{
447 register int i;
448 register int j;
449 register int k;
450 register short *rulep;
451 register short *rp;
452 register shifts *sp;
453 register int length;
454 register int nedges;
455 register int done;
456 register int state1;
457 register int stateno;
458 register int symbol1;
459 register int symbol2;
460 register short *shortp;
461 register short *edge;
462 register short *states;
463 register short **new_includes;
464
465 includes = NEW2(ngotos, short *);
466 edge = NEW2(ngotos + 1, short);
467 states = NEW2(maxrhs + 1, short);
468
469 for (i = 0; i < ngotos; i++)
470 {
471 nedges = 0;
472 state1 = from_state[i];
473 symbol1 = accessing_symbol[to_state[i]];
474
475 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
476 {
477 length = 1;
478 states[0] = state1;
479 stateno = state1;
480
481 for (rp = ritem + rrhs[*rulep]; *rp > 0; rp++)
482 {
483 symbol2 = *rp;
484 sp = shift_table[stateno];
485 k = sp->nshifts;
486
487 for (j = 0; j < k; j++)
488 {
489 stateno = sp->shifts[j];
490 if (accessing_symbol[stateno] == symbol2) break;
491 }
492
493 states[length++] = stateno;
494 }
495
496 if (!consistent[stateno])
497 add_lookback_edge(stateno, *rulep, i);
498
499 length--;
500 done = 0;
501 while (!done)
502 {
503 done = 1;
504 rp--;
505 /* JF added rp>=ritem && I hope to god its right! */
506 if (rp>=ritem && ISVAR(*rp))
507 {
508 stateno = states[--length];
509 edge[nedges++] = map_goto(stateno, *rp);
510 if (nullable[*rp]) done = 0;
511 }
512 }
513 }
514
515 if (nedges)
516 {
517 includes[i] = shortp = NEW2(nedges + 1, short);
518 for (j = 0; j < nedges; j++)
519 shortp[j] = edge[j];
520 shortp[nedges] = -1;
521 }
522 }
523
524 new_includes = transpose(includes, ngotos);
525
526 for (i = 0; i < ngotos; i++)
527 if (includes[i])
528 FREE(includes[i]);
529
530 FREE(includes);
531
532 includes = new_includes;
533
534 FREE(edge);
535 FREE(states);
536}
537
538
539void
540add_lookback_edge(stateno, ruleno, gotono)
541int stateno;
542int ruleno;
543int gotono;
544{
545 register int i;
546 register int k;
547 register int found;
548 register shorts *sp;
549
550 i = lookaheads[stateno];
551 k = lookaheads[stateno + 1];
552 found = 0;
553 while (!found && i < k)
554 {
555 if (LAruleno[i] == ruleno)
556 found = 1;
557 else
558 i++;
559 }
560
561 if (found == 0)
562 berror("add_lookback_edge");
563
564 sp = NEW(shorts);
565 sp->next = lookback[i];
566 sp->value = gotono;
567 lookback[i] = sp;
568}
569
570
571
572short **
573transpose(R_arg, n)
574short **R_arg;
575int n;
576{
577 register short **new_R;
578 register short **temp_R;
579 register short *nedges;
580 register short *sp;
581 register int i;
582 register int k;
583
584 nedges = NEW2(n, short);
585
586 for (i = 0; i < n; i++)
587 {
588 sp = R_arg[i];
589 if (sp)
590 {
591 while (*sp >= 0)
592 nedges[*sp++]++;
593 }
594 }
595
596 new_R = NEW2(n, short *);
597 temp_R = NEW2(n, short *);
598
599 for (i = 0; i < n; i++)
600 {
601 k = nedges[i];
602 if (k > 0)
603 {
604 sp = NEW2(k + 1, short);
605 new_R[i] = sp;
606 temp_R[i] = sp;
607 sp[k] = -1;
608 }
609 }
610
611 FREE(nedges);
612
613 for (i = 0; i < n; i++)
614 {
615 sp = R_arg[i];
616 if (sp)
617 {
618 while (*sp >= 0)
619 *temp_R[*sp++]++ = i;
620 }
621 }
622
623 FREE(temp_R);
624
625 return (new_R);
626}
627
628
629void
630compute_FOLLOWS()
631{
632 register int i;
633
634 digraph(includes);
635
636 for (i = 0; i < ngotos; i++)
637 {
638 if (includes[i]) FREE(includes[i]);
639 }
640
641 FREE(includes);
642}
643
644
645void
646compute_lookaheads()
647{
648 register int i;
649 register int n;
650 register unsigned *fp1;
651 register unsigned *fp2;
652 register unsigned *fp3;
653 register shorts *sp;
654 register unsigned *rowp;
655/* register short *rulep; JF unused */
656/* register int count; JF unused */
657 register shorts *sptmp;/* JF */
658
659 rowp = LA;
660 n = lookaheads[nstates];
661 for (i = 0; i < n; i++)
662 {
663 fp3 = rowp + tokensetsize;
664 for (sp = lookback[i]; sp; sp = sp->next)
665 {
666 fp1 = rowp;
667 fp2 = F + tokensetsize * sp->value;
668 while (fp1 < fp3)
669 *fp1++ |= *fp2++;
670 }
671
672 rowp = fp3;
673 }
674
675 for (i = 0; i < n; i++)
676 {/* JF removed ref to freed storage */
677 for (sp = lookback[i]; sp; sp = sptmp) {
678 sptmp=sp->next;
679 FREE(sp);
680 }
681 }
682
683 FREE(lookback);
684 FREE(F);
685}
686
687
688void
689digraph(relation)
690short **relation;
691{
692 register int i;
693
694 infinity = ngotos + 2;
695 INDEX = NEW2(ngotos + 1, short);
696 VERTICES = NEW2(ngotos + 1, short);
697 top = 0;
698
699 R = relation;
700
701 for (i = 0; i < ngotos; i++)
702 INDEX[i] = 0;
703
704 for (i = 0; i < ngotos; i++)
705 {
706 if (INDEX[i] == 0 && R[i])
707 traverse(i);
708 }
709
710 FREE(INDEX);
711 FREE(VERTICES);
712}
713
714
715void
716traverse(i)
717register int i;
718{
719 register unsigned *fp1;
720 register unsigned *fp2;
721 register unsigned *fp3;
722 register int j;
723 register short *rp;
724
725 int height;
726 unsigned *base;
727
728 VERTICES[++top] = i;
729 INDEX[i] = height = top;
730
731 base = F + i * tokensetsize;
732 fp3 = base + tokensetsize;
733
734 rp = R[i];
735 if (rp)
736 {
737 while ((j = *rp++) >= 0)
738 {
739 if (INDEX[j] == 0)
740 traverse(j);
741
742 if (INDEX[i] > INDEX[j])
743 INDEX[i] = INDEX[j];
744
745 fp1 = base;
746 fp2 = F + j * tokensetsize;
747
748 while (fp1 < fp3)
749 *fp1++ |= *fp2++;
750 }
751 }
752
753 if (INDEX[i] == height)
754 {
755 for (;;)
756 {
757 j = VERTICES[top--];
758 INDEX[j] = infinity;
759
760 if (i == j)
761 break;
762
763 fp1 = base;
764 fp2 = F + j * tokensetsize;
765
766 while (fp1 < fp3)
767 *fp2++ = *fp1++;
768 }
769 }
770}