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