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