]> git.saurik.com Git - bison.git/blame - src/lalr.c
* src/lalr.c (initialize_F): Improve variable locality.
[bison.git] / src / lalr.c
CommitLineData
d0fb370f 1/* Compute look-ahead criteria for bison,
3feec034 2 Copyright 1984, 1986, 1989, 2000, 2001 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"
d0fb370f 29#include "gram.h"
a0f6b076 30#include "complain.h"
720d742f 31#include "lalr.h"
3519ec76 32#include "nullable.h"
340ef489 33#include "derives.h"
d0fb370f 34
9703cc49
AD
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 */
38state_t *state_table = NULL;
39
d0fb370f 40int tokensetsize;
d0fb370f
RS
41short *LAruleno;
42unsigned *LA;
9703cc49 43
d0fb370f
RS
44short *goto_map;
45short *from_state;
46short *to_state;
47
720d742f 48extern void berror PARAMS ((const char *));
d0fb370f
RS
49
50static int infinity;
d0fb370f 51static int ngotos;
ddcd5fdf 52
fe961097 53/* And for the famous F variable, which name is so descriptive that a
ddcd5fdf
AD
54 comment is hardly needed. <grin>. */
55static unsigned *F = NULL;
56#define F(Rule) (F + (Rule) * tokensetsize)
57
d0fb370f
RS
58static short **includes;
59static shorts **lookback;
60static short **R;
61static short *INDEX;
62static short *VERTICES;
63static int top;
64
65
720d742f
AD
66static void
67traverse (int i)
d0fb370f 68{
720d742f 69 int j;
fe961097 70 size_t k;
720d742f 71 int height;
fe961097 72 size_t size = F (i + 1) - F(i);
720d742f
AD
73
74 VERTICES[++top] = i;
75 INDEX[i] = height = top;
76
fe961097
AD
77 if (R[i])
78 for (j = 0; R[i][j] >= 0; ++j)
79 {
80 if (INDEX[R[i][j]] == 0)
81 traverse (R[i][j]);
720d742f 82
fe961097
AD
83 if (INDEX[i] > INDEX[R[i][j]])
84 INDEX[i] = INDEX[R[i][j]];
720d742f 85
fe961097
AD
86 for (k = 0; k < size; ++k)
87 F (i)[k] |= F (R[i][j])[k];
88 }
720d742f
AD
89
90 if (INDEX[i] == height)
fe961097
AD
91 for (;;)
92 {
93 j = VERTICES[top--];
94 INDEX[j] = infinity;
720d742f 95
fe961097
AD
96 if (i == j)
97 break;
720d742f 98
fe961097
AD
99 for (k = 0; k < size; ++k)
100 F (i)[k] = F (j)[k];
101 }
d0fb370f
RS
102}
103
104
720d742f
AD
105static void
106digraph (short **relation)
107{
108 int i;
109
110 infinity = ngotos + 2;
d7913476
AD
111 INDEX = XCALLOC (short, ngotos + 1);
112 VERTICES = XCALLOC (short, ngotos + 1);
720d742f
AD
113 top = 0;
114
115 R = relation;
116
117 for (i = 0; i < ngotos; i++)
118 INDEX[i] = 0;
119
120 for (i = 0; i < ngotos; i++)
ddcd5fdf
AD
121 if (INDEX[i] == 0 && R[i])
122 traverse (i);
720d742f 123
d7913476
AD
124 XFREE (INDEX);
125 XFREE (VERTICES);
720d742f
AD
126}
127
bb527fc2
AD
128
129/*--------------------.
130| Build STATE_TABLE. |
131`--------------------*/
132
4a120d45 133static void
d2729d44 134set_state_table (void)
d0fb370f 135{
f004bf6a
AD
136 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
137 might be used (see conflicts.c). It is too opaque for me to
138 provide a probably less hacky implementation. --akim */
139 state_table = XCALLOC (state_t, nstates + 1);
d0fb370f 140
90b4416b
AD
141 {
142 core *sp;
143 for (sp = first_state; sp; sp = sp->next)
144 {
145 state_table[sp->number].state = sp;
146 state_table[sp->number].accessing_symbol = sp->accessing_symbol;
147 }
148 }
149
150 {
151 shifts *sp;
152 for (sp = first_shift; sp; sp = sp->next)
153 state_table[sp->number].shift_table = sp;
154 }
155
156 {
157 reductions *rp;
158 for (rp = first_reduction; rp; rp = rp->next)
159 state_table[rp->number].reduction_table = rp;
160 }
a845a697
AD
161
162 /* Initializing the lookaheads members. Please note that it must be
163 performed after having set some of the other members which are
164 used below. Change with extreme caution. */
165 {
166 int i;
167 int count = 0;
168 for (i = 0; i < nstates; i++)
169 {
170 int k;
171 reductions *rp = state_table[i].reduction_table;
172 shifts *sp = state_table[i].shift_table;
173
174 state_table[i].lookaheads = count;
175
176 if (rp
177 && (rp->nreds > 1
178 || (sp && !ISVAR (state_table[sp->shifts[0]].accessing_symbol))))
179 count += rp->nreds;
180 else
181 state_table[i].consistent = 1;
182
183 if (sp)
184 for (k = 0; k < sp->nshifts; k++)
185 if (state_table[sp->shifts[k]].accessing_symbol
186 == error_token_number)
187 {
188 state_table[i].consistent = 0;
189 break;
190 }
191 }
192 state_table[nstates].lookaheads = count;
193 }
d0fb370f
RS
194}
195
196
fe961097
AD
197/*------------------------------------------.
198| Return the size of the longest rule RHS. |
199`------------------------------------------*/
200
3feec034
AD
201static size_t
202maxrhs (void)
d0fb370f 203{
720d742f
AD
204 short *itemp;
205 int length;
206 int max;
d0fb370f
RS
207
208 length = 0;
209 max = 0;
210 for (itemp = ritem; *itemp; itemp++)
211 {
212 if (*itemp > 0)
213 {
214 length++;
215 }
216 else
217 {
720d742f
AD
218 if (length > max)
219 max = length;
d0fb370f
RS
220 length = 0;
221 }
222 }
223
3feec034 224 return max;
d0fb370f
RS
225}
226
227
4a120d45 228static void
d2729d44 229initialize_LA (void)
d0fb370f 230{
720d742f
AD
231 int i;
232 int j;
720d742f 233 short *np;
a845a697 234 reductions *rp;
d0fb370f 235
a845a697
AD
236 size_t nLA = state_table[nstates].lookaheads;
237 if (!nLA)
238 nLA = 1;
d0fb370f 239
a845a697
AD
240 LA = XCALLOC (unsigned, nLA * tokensetsize);
241 LAruleno = XCALLOC (short, nLA);
242 lookback = XCALLOC (shorts *, nLA);
d0fb370f
RS
243
244 np = LAruleno;
245 for (i = 0; i < nstates; i++)
a845a697
AD
246 if (!state_table[i].consistent)
247 if ((rp = state_table[i].reduction_table))
248 for (j = 0; j < rp->nreds; j++)
249 *np++ = rp->rules[j];
d0fb370f
RS
250}
251
252
4a120d45 253static void
d2729d44 254set_goto_map (void)
d0fb370f 255{
720d742f
AD
256 shifts *sp;
257 int i;
258 int symbol;
259 int k;
260 short *temp_map;
261 int state2;
262 int state1;
d0fb370f 263
d7913476
AD
264 goto_map = XCALLOC (short, nvars + 1) - ntokens;
265 temp_map = XCALLOC (short, nvars + 1) - ntokens;
d0fb370f
RS
266
267 ngotos = 0;
268 for (sp = first_shift; sp; sp = sp->next)
269 {
270 for (i = sp->nshifts - 1; i >= 0; i--)
271 {
9703cc49 272 symbol = state_table[sp->shifts[i]].accessing_symbol;
d0fb370f 273
720d742f
AD
274 if (ISTOKEN (symbol))
275 break;
d0fb370f
RS
276
277 if (ngotos == MAXSHORT)
a0f6b076 278 fatal (_("too many gotos (max %d)"), MAXSHORT);
d0fb370f
RS
279
280 ngotos++;
281 goto_map[symbol]++;
720d742f 282 }
d0fb370f
RS
283 }
284
285 k = 0;
286 for (i = ntokens; i < nsyms; i++)
287 {
288 temp_map[i] = k;
289 k += goto_map[i];
290 }
291
292 for (i = ntokens; i < nsyms; i++)
293 goto_map[i] = temp_map[i];
294
295 goto_map[nsyms] = ngotos;
296 temp_map[nsyms] = ngotos;
297
d7913476
AD
298 from_state = XCALLOC (short, ngotos);
299 to_state = XCALLOC (short, ngotos);
d0fb370f
RS
300
301 for (sp = first_shift; sp; sp = sp->next)
302 {
303 state1 = sp->number;
304 for (i = sp->nshifts - 1; i >= 0; i--)
305 {
306 state2 = sp->shifts[i];
9703cc49 307 symbol = state_table[state2].accessing_symbol;
d0fb370f 308
720d742f
AD
309 if (ISTOKEN (symbol))
310 break;
d0fb370f
RS
311
312 k = temp_map[symbol]++;
313 from_state[k] = state1;
314 to_state[k] = state2;
315 }
316 }
317
d7913476 318 XFREE (temp_map + ntokens);
d0fb370f
RS
319}
320
321
322
43591cec
AD
323/*----------------------------------------------------------.
324| Map a state/symbol pair into its numeric representation. |
325`----------------------------------------------------------*/
d0fb370f 326
4a120d45 327static int
d2729d44 328map_goto (int state, int symbol)
d0fb370f 329{
720d742f
AD
330 int high;
331 int low;
332 int middle;
333 int s;
d0fb370f
RS
334
335 low = goto_map[symbol];
336 high = goto_map[symbol + 1] - 1;
337
338 while (low <= high)
339 {
340 middle = (low + high) / 2;
341 s = from_state[middle];
342 if (s == state)
36281465 343 return middle;
d0fb370f
RS
344 else if (s < state)
345 low = middle + 1;
346 else
347 high = middle - 1;
348 }
349
43591cec
AD
350 assert (0);
351 /* NOTREACHED */
d0fb370f
RS
352 return 0;
353}
354
355
4a120d45 356static void
d2729d44 357initialize_F (void)
d0fb370f 358{
4d4f699c
AD
359 short **reads = XCALLOC (short *, ngotos);
360 short *edge = XCALLOC (short, ngotos + 1);
361 int nedges = 0;
d0fb370f 362
4d4f699c 363 int i;
d0fb370f 364
4d4f699c 365 F = XCALLOC (unsigned, ngotos * tokensetsize);
d0fb370f 366
d0fb370f
RS
367 for (i = 0; i < ngotos; i++)
368 {
80a69750
AD
369 int stateno = to_state[i];
370 shifts *sp = state_table[stateno].shift_table;
d0fb370f
RS
371
372 if (sp)
373 {
4d4f699c 374 int j;
80a69750 375 for (j = 0; j < sp->nshifts; j++)
d0fb370f 376 {
4d4f699c 377 int symbol = state_table[sp->shifts[j]].accessing_symbol;
720d742f 378 if (ISVAR (symbol))
d0fb370f 379 break;
4d4f699c 380 SETBIT (F + i * tokensetsize, symbol);
d0fb370f
RS
381 }
382
80a69750 383 for (; j < sp->nshifts; j++)
d0fb370f 384 {
4d4f699c 385 int symbol = state_table[sp->shifts[j]].accessing_symbol;
d0fb370f 386 if (nullable[symbol])
720d742f 387 edge[nedges++] = map_goto (stateno, symbol);
d0fb370f 388 }
a0f6b076 389
d0fb370f
RS
390 if (nedges)
391 {
4d4f699c
AD
392 reads[i] = XCALLOC (short, nedges + 1);
393 shortcpy (reads[i], edge, nedges);
394 reads[i][nedges] = -1;
d0fb370f
RS
395 nedges = 0;
396 }
397 }
d0fb370f
RS
398 }
399
720d742f 400 digraph (reads);
d0fb370f
RS
401
402 for (i = 0; i < ngotos; i++)
ddcd5fdf 403 XFREE (reads[i]);
d0fb370f 404
d7913476
AD
405 XFREE (reads);
406 XFREE (edge);
d0fb370f
RS
407}
408
409
4a120d45 410static void
d2729d44 411add_lookback_edge (int stateno, int ruleno, int gotono)
d0fb370f 412{
720d742f
AD
413 int i;
414 int k;
415 int found;
416 shorts *sp;
d0fb370f 417
f004bf6a
AD
418 i = state_table[stateno].lookaheads;
419 k = state_table[stateno + 1].lookaheads;
d0fb370f
RS
420 found = 0;
421 while (!found && i < k)
422 {
423 if (LAruleno[i] == ruleno)
424 found = 1;
425 else
426 i++;
427 }
428
340ef489 429 assert (found);
d0fb370f 430
d7913476 431 sp = XCALLOC (shorts, 1);
d0fb370f
RS
432 sp->next = lookback[i];
433 sp->value = gotono;
434 lookback[i] = sp;
435}
436
437
4a120d45 438static short **
d2729d44 439transpose (short **R_arg, int n)
d0fb370f 440{
720d742f
AD
441 short **new_R;
442 short **temp_R;
443 short *nedges;
720d742f 444 int i;
d0fb370f 445
d7913476 446 nedges = XCALLOC (short, n);
d0fb370f
RS
447
448 for (i = 0; i < n; i++)
449 {
80a69750 450 short *sp = R_arg[i];
d0fb370f
RS
451 if (sp)
452 {
453 while (*sp >= 0)
454 nedges[*sp++]++;
455 }
456 }
457
d7913476
AD
458 new_R = XCALLOC (short *, n);
459 temp_R = XCALLOC (short *, n);
d0fb370f
RS
460
461 for (i = 0; i < n; i++)
80a69750
AD
462 if (nedges[i] > 0)
463 {
464 short *sp = XCALLOC (short, nedges[i] + 1);
465 new_R[i] = sp;
466 temp_R[i] = sp;
467 sp[nedges[i]] = -1;
468 }
d0fb370f 469
d7913476 470 XFREE (nedges);
d0fb370f
RS
471
472 for (i = 0; i < n; i++)
473 {
80a69750 474 short *sp = R_arg[i];
d0fb370f 475 if (sp)
80a69750
AD
476 while (*sp >= 0)
477 *temp_R[*sp++]++ = i;
d0fb370f
RS
478 }
479
d7913476 480 XFREE (temp_R);
d0fb370f 481
36281465 482 return new_R;
d0fb370f
RS
483}
484
485
4a120d45 486static void
720d742f 487build_relations (void)
d0fb370f 488{
720d742f
AD
489 int i;
490 int j;
720d742f
AD
491 short *rulep;
492 short *rp;
720d742f
AD
493 int nedges;
494 int done;
495 int state1;
496 int stateno;
497 int symbol1;
720d742f
AD
498 short *edge;
499 short *states;
500 short **new_includes;
501
d7913476
AD
502 includes = XCALLOC (short *, ngotos);
503 edge = XCALLOC (short, ngotos + 1);
3feec034 504 states = XCALLOC (short, maxrhs () + 1);
d0fb370f
RS
505
506 for (i = 0; i < ngotos; i++)
507 {
720d742f
AD
508 nedges = 0;
509 state1 = from_state[i];
9703cc49 510 symbol1 = state_table[to_state[i]].accessing_symbol;
d0fb370f 511
720d742f
AD
512 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
513 {
80a69750 514 int length = 1;
720d742f
AD
515 states[0] = state1;
516 stateno = state1;
d0fb370f 517
b2ed6e58 518 for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++)
720d742f 519 {
80a69750
AD
520 int symbol2 = *rp;
521 shifts *sp = state_table[stateno].shift_table;
d0fb370f 522
80a69750 523 for (j = 0; j < sp->nshifts; j++)
720d742f
AD
524 {
525 stateno = sp->shifts[j];
9703cc49 526 if (state_table[stateno].accessing_symbol == symbol2)
720d742f
AD
527 break;
528 }
d0fb370f 529
720d742f
AD
530 states[length++] = stateno;
531 }
532
de326cc0 533 if (!state_table[stateno].consistent)
720d742f
AD
534 add_lookback_edge (stateno, *rulep, i);
535
536 length--;
537 done = 0;
538 while (!done)
539 {
540 done = 1;
541 rp--;
542 /* JF added rp>=ritem && I hope to god its right! */
543 if (rp >= ritem && ISVAR (*rp))
544 {
545 stateno = states[--length];
546 edge[nedges++] = map_goto (stateno, *rp);
547 if (nullable[*rp])
548 done = 0;
549 }
550 }
d0fb370f
RS
551 }
552
720d742f
AD
553 if (nedges)
554 {
80a69750 555 includes[i] = XCALLOC (short, nedges + 1);
720d742f 556 for (j = 0; j < nedges; j++)
80a69750
AD
557 includes[i][j] = edge[j];
558 includes[i][nedges] = -1;
720d742f 559 }
d0fb370f
RS
560 }
561
720d742f 562 new_includes = transpose (includes, ngotos);
d0fb370f 563
720d742f 564 for (i = 0; i < ngotos; i++)
80a69750 565 XFREE (includes[i]);
d7913476 566 XFREE (includes);
720d742f
AD
567
568 includes = new_includes;
569
d7913476
AD
570 XFREE (edge);
571 XFREE (states);
d0fb370f
RS
572}
573
574
720d742f 575
4a120d45 576static void
720d742f 577compute_FOLLOWS (void)
d0fb370f 578{
720d742f 579 int i;
d0fb370f 580
720d742f 581 digraph (includes);
d0fb370f
RS
582
583 for (i = 0; i < ngotos; i++)
ddcd5fdf 584 XFREE (includes[i]);
d0fb370f 585
d7913476 586 XFREE (includes);
d0fb370f
RS
587}
588
589
4a120d45 590static void
720d742f 591compute_lookaheads (void)
d0fb370f 592{
720d742f 593 int i;
720d742f 594 shorts *sp;
d0fb370f 595
f004bf6a 596 for (i = 0; i < state_table[nstates].lookaheads; i++)
ddcd5fdf
AD
597 for (sp = lookback[i]; sp; sp = sp->next)
598 {
599 unsigned *fp1 = LA (i);
600 unsigned *fp2 = F (sp->value);
601 while (fp1 < LA (i + 1))
602 *fp1++ |= *fp2++;
603 }
d0fb370f 604
ddcd5fdf 605 /* Free LOOKBACK. */
f004bf6a 606 for (i = 0; i < state_table[nstates].lookaheads; i++)
300f275f 607 LIST_FREE (shorts, lookback[i]);
d0fb370f 608
d7913476
AD
609 XFREE (lookback);
610 XFREE (F);
720d742f 611}
d0fb370f 612
d0fb370f 613
720d742f
AD
614void
615lalr (void)
616{
617 tokensetsize = WORDSIZE (ntokens);
618
619 set_state_table ();
720d742f
AD
620 initialize_LA ();
621 set_goto_map ();
622 initialize_F ();
623 build_relations ();
624 compute_FOLLOWS ();
625 compute_lookaheads ();
d0fb370f 626}