]> git.saurik.com Git - bison.git/blob - src/lalr.c
* src/lalr.h (LA): New macro to access to the variable LA.
[bison.git] / src / lalr.c
1 /* Compute look-ahead criteria for bison,
2 Copyright 1984, 1986, 1989, 2000 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 /* Compute how to make the finite state machine deterministic; find
23 which rules need lookahead in each state, and which lookahead
24 tokens they accept. */
25
26 #include "system.h"
27 #include "types.h"
28 #include "LR0.h"
29 #include "gram.h"
30 #include "complain.h"
31 #include "lalr.h"
32 #include "nullable.h"
33 #include "derives.h"
34
35 /* All the decorated states, indexed by the state number. Warning:
36 there is a state_TABLE in LR0.c, but it is different and static.
37 */
38 state_t *state_table = NULL;
39
40 int tokensetsize;
41 short *LAruleno;
42 unsigned *LA;
43
44 short *goto_map;
45 short *from_state;
46 short *to_state;
47
48 extern void berror PARAMS ((const char *));
49
50 static int infinity;
51 static int maxrhs;
52 static int ngotos;
53 static unsigned *F;
54 static short **includes;
55 static shorts **lookback;
56 static short **R;
57 static short *INDEX;
58 static short *VERTICES;
59 static int top;
60
61
62 static void
63 traverse (int i)
64 {
65 unsigned *fp1;
66 unsigned *fp2;
67 unsigned *fp3;
68 int j;
69 short *rp;
70
71 int height;
72 unsigned *base;
73
74 VERTICES[++top] = i;
75 INDEX[i] = height = top;
76
77 base = F + i * tokensetsize;
78 fp3 = base + tokensetsize;
79
80 rp = R[i];
81 if (rp)
82 {
83 while ((j = *rp++) >= 0)
84 {
85 if (INDEX[j] == 0)
86 traverse (j);
87
88 if (INDEX[i] > INDEX[j])
89 INDEX[i] = INDEX[j];
90
91 fp1 = base;
92 fp2 = F + j * tokensetsize;
93
94 while (fp1 < fp3)
95 *fp1++ |= *fp2++;
96 }
97 }
98
99 if (INDEX[i] == height)
100 {
101 for (;;)
102 {
103 j = VERTICES[top--];
104 INDEX[j] = infinity;
105
106 if (i == j)
107 break;
108
109 fp1 = base;
110 fp2 = F + j * tokensetsize;
111
112 while (fp1 < fp3)
113 *fp2++ = *fp1++;
114 }
115 }
116 }
117
118
119 static void
120 digraph (short **relation)
121 {
122 int i;
123
124 infinity = ngotos + 2;
125 INDEX = XCALLOC (short, ngotos + 1);
126 VERTICES = XCALLOC (short, ngotos + 1);
127 top = 0;
128
129 R = relation;
130
131 for (i = 0; i < ngotos; i++)
132 INDEX[i] = 0;
133
134 for (i = 0; i < ngotos; i++)
135 {
136 if (INDEX[i] == 0 && R[i])
137 traverse (i);
138 }
139
140 XFREE (INDEX);
141 XFREE (VERTICES);
142 }
143
144
145 /*--------------------.
146 | Build STATE_TABLE. |
147 `--------------------*/
148
149 static void
150 set_state_table (void)
151 {
152 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
153 might be used (see conflicts.c). It is too opaque for me to
154 provide a probably less hacky implementation. --akim */
155 state_table = XCALLOC (state_t, nstates + 1);
156
157 {
158 core *sp;
159 for (sp = first_state; sp; sp = sp->next)
160 {
161 state_table[sp->number].state = sp;
162 state_table[sp->number].accessing_symbol = sp->accessing_symbol;
163 }
164 }
165
166 {
167 shifts *sp;
168 for (sp = first_shift; sp; sp = sp->next)
169 state_table[sp->number].shift_table = sp;
170 }
171
172 {
173 reductions *rp;
174 for (rp = first_reduction; rp; rp = rp->next)
175 state_table[rp->number].reduction_table = rp;
176 }
177
178 /* Initializing the lookaheads members. Please note that it must be
179 performed after having set some of the other members which are
180 used below. Change with extreme caution. */
181 {
182 int i;
183 int count = 0;
184 for (i = 0; i < nstates; i++)
185 {
186 int k;
187 reductions *rp = state_table[i].reduction_table;
188 shifts *sp = state_table[i].shift_table;
189
190 state_table[i].lookaheads = count;
191
192 if (rp
193 && (rp->nreds > 1
194 || (sp && !ISVAR (state_table[sp->shifts[0]].accessing_symbol))))
195 count += rp->nreds;
196 else
197 state_table[i].consistent = 1;
198
199 if (sp)
200 for (k = 0; k < sp->nshifts; k++)
201 if (state_table[sp->shifts[k]].accessing_symbol
202 == error_token_number)
203 {
204 state_table[i].consistent = 0;
205 break;
206 }
207 }
208 state_table[nstates].lookaheads = count;
209 }
210 }
211
212
213 static void
214 set_maxrhs (void)
215 {
216 short *itemp;
217 int length;
218 int max;
219
220 length = 0;
221 max = 0;
222 for (itemp = ritem; *itemp; itemp++)
223 {
224 if (*itemp > 0)
225 {
226 length++;
227 }
228 else
229 {
230 if (length > max)
231 max = length;
232 length = 0;
233 }
234 }
235
236 maxrhs = max;
237 }
238
239
240 static void
241 initialize_LA (void)
242 {
243 int i;
244 int j;
245 short *np;
246 reductions *rp;
247
248 size_t nLA = state_table[nstates].lookaheads;
249 if (!nLA)
250 nLA = 1;
251
252 LA = XCALLOC (unsigned, nLA * tokensetsize);
253 LAruleno = XCALLOC (short, nLA);
254 lookback = XCALLOC (shorts *, nLA);
255
256 np = LAruleno;
257 for (i = 0; i < nstates; i++)
258 if (!state_table[i].consistent)
259 if ((rp = state_table[i].reduction_table))
260 for (j = 0; j < rp->nreds; j++)
261 *np++ = rp->rules[j];
262 }
263
264
265 static void
266 set_goto_map (void)
267 {
268 shifts *sp;
269 int i;
270 int symbol;
271 int k;
272 short *temp_map;
273 int state2;
274 int state1;
275
276 goto_map = XCALLOC (short, nvars + 1) - ntokens;
277 temp_map = XCALLOC (short, nvars + 1) - ntokens;
278
279 ngotos = 0;
280 for (sp = first_shift; sp; sp = sp->next)
281 {
282 for (i = sp->nshifts - 1; i >= 0; i--)
283 {
284 symbol = state_table[sp->shifts[i]].accessing_symbol;
285
286 if (ISTOKEN (symbol))
287 break;
288
289 if (ngotos == MAXSHORT)
290 fatal (_("too many gotos (max %d)"), MAXSHORT);
291
292 ngotos++;
293 goto_map[symbol]++;
294 }
295 }
296
297 k = 0;
298 for (i = ntokens; i < nsyms; i++)
299 {
300 temp_map[i] = k;
301 k += goto_map[i];
302 }
303
304 for (i = ntokens; i < nsyms; i++)
305 goto_map[i] = temp_map[i];
306
307 goto_map[nsyms] = ngotos;
308 temp_map[nsyms] = ngotos;
309
310 from_state = XCALLOC (short, ngotos);
311 to_state = XCALLOC (short, ngotos);
312
313 for (sp = first_shift; sp; sp = sp->next)
314 {
315 state1 = sp->number;
316 for (i = sp->nshifts - 1; i >= 0; i--)
317 {
318 state2 = sp->shifts[i];
319 symbol = state_table[state2].accessing_symbol;
320
321 if (ISTOKEN (symbol))
322 break;
323
324 k = temp_map[symbol]++;
325 from_state[k] = state1;
326 to_state[k] = state2;
327 }
328 }
329
330 XFREE (temp_map + ntokens);
331 }
332
333
334
335 /*----------------------------------------------------------.
336 | Map a state/symbol pair into its numeric representation. |
337 `----------------------------------------------------------*/
338
339 static int
340 map_goto (int state, int symbol)
341 {
342 int high;
343 int low;
344 int middle;
345 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 assert (0);
363 /* NOTREACHED */
364 return 0;
365 }
366
367
368 static void
369 initialize_F (void)
370 {
371 int i;
372 int j;
373 int k;
374 shifts *sp;
375 short *edge;
376 unsigned *rowp;
377 short *rp;
378 short **reads;
379 int nedges;
380 int stateno;
381 int symbol;
382 int nwords;
383
384 nwords = ngotos * tokensetsize;
385 F = XCALLOC (unsigned, nwords);
386
387 reads = XCALLOC (short *, ngotos);
388 edge = XCALLOC (short, ngotos + 1);
389 nedges = 0;
390
391 rowp = F;
392 for (i = 0; i < ngotos; i++)
393 {
394 stateno = to_state[i];
395 sp = state_table[stateno].shift_table;
396
397 if (sp)
398 {
399 k = sp->nshifts;
400
401 for (j = 0; j < k; j++)
402 {
403 symbol = state_table[sp->shifts[j]].accessing_symbol;
404 if (ISVAR (symbol))
405 break;
406 SETBIT (rowp, symbol);
407 }
408
409 for (; j < k; j++)
410 {
411 symbol = state_table[sp->shifts[j]].accessing_symbol;
412 if (nullable[symbol])
413 edge[nedges++] = map_goto (stateno, symbol);
414 }
415
416 if (nedges)
417 {
418 reads[i] = rp = XCALLOC (short, nedges + 1);
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 XFREE (reads[i]);
437 }
438
439 XFREE (reads);
440 XFREE (edge);
441 }
442
443
444 static void
445 add_lookback_edge (int stateno, int ruleno, int gotono)
446 {
447 int i;
448 int k;
449 int found;
450 shorts *sp;
451
452 i = state_table[stateno].lookaheads;
453 k = state_table[stateno + 1].lookaheads;
454 found = 0;
455 while (!found && i < k)
456 {
457 if (LAruleno[i] == ruleno)
458 found = 1;
459 else
460 i++;
461 }
462
463 assert (found);
464
465 sp = XCALLOC (shorts, 1);
466 sp->next = lookback[i];
467 sp->value = gotono;
468 lookback[i] = sp;
469 }
470
471
472 static short **
473 transpose (short **R_arg, int n)
474 {
475 short **new_R;
476 short **temp_R;
477 short *nedges;
478 short *sp;
479 int i;
480 int k;
481
482 nedges = XCALLOC (short, n);
483
484 for (i = 0; i < n; i++)
485 {
486 sp = R_arg[i];
487 if (sp)
488 {
489 while (*sp >= 0)
490 nedges[*sp++]++;
491 }
492 }
493
494 new_R = XCALLOC (short *, n);
495 temp_R = XCALLOC (short *, n);
496
497 for (i = 0; i < n; i++)
498 {
499 k = nedges[i];
500 if (k > 0)
501 {
502 sp = XCALLOC (short, k + 1);
503 new_R[i] = sp;
504 temp_R[i] = sp;
505 sp[k] = -1;
506 }
507 }
508
509 XFREE (nedges);
510
511 for (i = 0; i < n; i++)
512 {
513 sp = R_arg[i];
514 if (sp)
515 {
516 while (*sp >= 0)
517 *temp_R[*sp++]++ = i;
518 }
519 }
520
521 XFREE (temp_R);
522
523 return new_R;
524 }
525
526
527 static void
528 build_relations (void)
529 {
530 int i;
531 int j;
532 int k;
533 short *rulep;
534 short *rp;
535 shifts *sp;
536 int length;
537 int nedges;
538 int done;
539 int state1;
540 int stateno;
541 int symbol1;
542 int symbol2;
543 short *shortp;
544 short *edge;
545 short *states;
546 short **new_includes;
547
548 includes = XCALLOC (short *, ngotos);
549 edge = XCALLOC (short, ngotos + 1);
550 states = XCALLOC (short, maxrhs + 1);
551
552 for (i = 0; i < ngotos; i++)
553 {
554 nedges = 0;
555 state1 = from_state[i];
556 symbol1 = state_table[to_state[i]].accessing_symbol;
557
558 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
559 {
560 length = 1;
561 states[0] = state1;
562 stateno = state1;
563
564 for (rp = ritem + rrhs[*rulep]; *rp > 0; rp++)
565 {
566 symbol2 = *rp;
567 sp = state_table[stateno].shift_table;
568 k = sp->nshifts;
569
570 for (j = 0; j < k; j++)
571 {
572 stateno = sp->shifts[j];
573 if (state_table[stateno].accessing_symbol == symbol2)
574 break;
575 }
576
577 states[length++] = stateno;
578 }
579
580 if (!state_table[stateno].consistent)
581 add_lookback_edge (stateno, *rulep, i);
582
583 length--;
584 done = 0;
585 while (!done)
586 {
587 done = 1;
588 rp--;
589 /* JF added rp>=ritem && I hope to god its right! */
590 if (rp >= ritem && ISVAR (*rp))
591 {
592 stateno = states[--length];
593 edge[nedges++] = map_goto (stateno, *rp);
594 if (nullable[*rp])
595 done = 0;
596 }
597 }
598 }
599
600 if (nedges)
601 {
602 includes[i] = shortp = XCALLOC (short, nedges + 1);
603 for (j = 0; j < nedges; j++)
604 shortp[j] = edge[j];
605 shortp[nedges] = -1;
606 }
607 }
608
609 new_includes = transpose (includes, ngotos);
610
611 for (i = 0; i < ngotos; i++)
612 if (includes[i])
613 XFREE (includes[i]);
614
615 XFREE (includes);
616
617 includes = new_includes;
618
619 XFREE (edge);
620 XFREE (states);
621 }
622
623
624
625 static void
626 compute_FOLLOWS (void)
627 {
628 int i;
629
630 digraph (includes);
631
632 for (i = 0; i < ngotos; i++)
633 {
634 if (includes[i])
635 XFREE (includes[i]);
636 }
637
638 XFREE (includes);
639 }
640
641
642 static void
643 compute_lookaheads (void)
644 {
645 int i;
646 int n;
647 unsigned *fp1;
648 unsigned *fp2;
649 unsigned *fp3;
650 shorts *sp;
651 shorts *sptmp; /* JF */
652
653 for (i = 0; i < state_table[nstates].lookaheads; i++)
654 {
655 fp3 = LA (i + 1);
656 for (sp = lookback[i]; sp; sp = sp->next)
657 {
658 fp1 = LA (i);
659 fp2 = F + tokensetsize * sp->value;
660 while (fp1 < fp3)
661 *fp1++ |= *fp2++;
662 }
663 }
664
665 for (i = 0; i < state_table[nstates].lookaheads; i++)
666 {
667 /* JF removed ref to freed storage */
668 for (sp = lookback[i]; sp; sp = sptmp)
669 {
670 sptmp = sp->next;
671 XFREE (sp);
672 }
673 }
674
675 XFREE (lookback);
676 XFREE (F);
677 }
678
679
680 void
681 lalr (void)
682 {
683 tokensetsize = WORDSIZE (ntokens);
684
685 set_state_table ();
686 set_maxrhs ();
687 initialize_LA ();
688 set_goto_map ();
689 initialize_F ();
690 build_relations ();
691 compute_FOLLOWS ();
692 compute_lookaheads ();
693 }