e1e2719debeff974ef49b7f0d86314f7074c23cd
[bison.git] / src / lalr.c
1 /* Compute look-ahead criteria for bison,
2 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 /* Compute how to make the finite state machine deterministic; find
24 which rules need lookahead in each state, and which lookahead
25 tokens they accept. */
26
27 #include "system.h"
28 #include "bitset.h"
29 #include "bitsetv.h"
30 #include "reader.h"
31 #include "types.h"
32 #include "LR0.h"
33 #include "symtab.h"
34 #include "gram.h"
35 #include "complain.h"
36 #include "lalr.h"
37 #include "nullable.h"
38 #include "derives.h"
39 #include "getargs.h"
40
41 /* All the decorated states, indexed by the state number. */
42 state_t **states = NULL;
43
44 rule_t **LArule = NULL;
45 bitsetv LA = NULL;
46 size_t nLA;
47
48 static int ngotos;
49 short *goto_map = NULL;
50 short *from_state = NULL;
51 short *to_state = NULL;
52
53 /* And for the famous F variable, which name is so descriptive that a
54 comment is hardly needed. <grin>. */
55 static bitsetv F = NULL;
56
57 static short **includes;
58 static shorts **lookback;
59
60
61 /*---------------------------------------------------------------.
62 | digraph & traverse. |
63 | |
64 | The following variables are used as common storage between the |
65 | two. |
66 `---------------------------------------------------------------*/
67
68 static short **R;
69 static short *INDEX;
70 static short *VERTICES;
71 static int top;
72 static int infinity;
73
74 static void
75 traverse (int i)
76 {
77 int j;
78 int height;
79
80 VERTICES[++top] = i;
81 INDEX[i] = height = top;
82
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]);
88
89 if (INDEX[i] > INDEX[R[i][j]])
90 INDEX[i] = INDEX[R[i][j]];
91
92 bitset_or (F[i], F[i], F[R[i][j]]);
93 }
94
95 if (INDEX[i] == height)
96 for (;;)
97 {
98 j = VERTICES[top--];
99 INDEX[j] = infinity;
100
101 if (i == j)
102 break;
103
104 bitset_copy (F[j], F[i]);
105 }
106 }
107
108
109 static void
110 digraph (short **relation)
111 {
112 int i;
113
114 infinity = ngotos + 2;
115 INDEX = XCALLOC (short, ngotos + 1);
116 VERTICES = XCALLOC (short, ngotos + 1);
117 top = 0;
118
119 R = relation;
120
121 for (i = 0; i < ngotos; i++)
122 INDEX[i] = 0;
123
124 for (i = 0; i < ngotos; i++)
125 if (INDEX[i] == 0 && R[i])
126 traverse (i);
127
128 XFREE (INDEX);
129 XFREE (VERTICES);
130 }
131
132
133 static void
134 initialize_LA (void)
135 {
136 size_t i;
137 int j;
138 rule_t **np;
139
140 /* Avoid having to special case 0. */
141 if (!nLA)
142 nLA = 1;
143
144 LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
145 LArule = XCALLOC (rule_t *, nLA);
146 lookback = XCALLOC (shorts *, nLA);
147
148 np = LArule;
149 for (i = 0; i < nstates; i++)
150 if (!states[i]->consistent)
151 for (j = 0; j < states[i]->reductions->nreds; j++)
152 *np++ = &rules[states[i]->reductions->rules[j]];
153 }
154
155
156 static void
157 set_goto_map (void)
158 {
159 size_t state;
160 int i;
161 short *temp_map;
162
163 goto_map = XCALLOC (short, nvars + 1) - ntokens;
164 temp_map = XCALLOC (short, nvars + 1) - ntokens;
165
166 ngotos = 0;
167 for (state = 0; state < nstates; ++state)
168 {
169 shifts *sp = states[state]->shifts;
170 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
171 {
172 if (ngotos == MAXSHORT)
173 fatal (_("too many gotos (max %d)"), MAXSHORT);
174
175 ngotos++;
176 goto_map[SHIFT_SYMBOL (sp, i)]++;
177 }
178 }
179
180 {
181 int k = 0;
182 for (i = ntokens; i < nsyms; i++)
183 {
184 temp_map[i] = k;
185 k += goto_map[i];
186 }
187
188 for (i = ntokens; i < nsyms; i++)
189 goto_map[i] = temp_map[i];
190
191 goto_map[nsyms] = ngotos;
192 temp_map[nsyms] = ngotos;
193 }
194
195 from_state = XCALLOC (short, ngotos);
196 to_state = XCALLOC (short, ngotos);
197
198 for (state = 0; state < nstates; ++state)
199 {
200 shifts *sp = states[state]->shifts;
201 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
202 {
203 int k = temp_map[SHIFT_SYMBOL (sp, i)]++;
204 from_state[k] = state;
205 to_state[k] = sp->shifts[i];
206 }
207 }
208
209 XFREE (temp_map + ntokens);
210 }
211
212
213
214 /*----------------------------------------------------------.
215 | Map a state/symbol pair into its numeric representation. |
216 `----------------------------------------------------------*/
217
218 static int
219 map_goto (int state, int symbol)
220 {
221 int high;
222 int low;
223 int middle;
224 int s;
225
226 low = goto_map[symbol];
227 high = goto_map[symbol + 1] - 1;
228
229 while (low <= high)
230 {
231 middle = (low + high) / 2;
232 s = from_state[middle];
233 if (s == state)
234 return middle;
235 else if (s < state)
236 low = middle + 1;
237 else
238 high = middle - 1;
239 }
240
241 assert (0);
242 /* NOTREACHED */
243 return 0;
244 }
245
246
247 static void
248 initialize_F (void)
249 {
250 short **reads = XCALLOC (short *, ngotos);
251 short *edge = XCALLOC (short, ngotos + 1);
252 int nedges = 0;
253
254 int i;
255
256 F = bitsetv_create (ngotos, ntokens, BITSET_FIXED);
257
258 for (i = 0; i < ngotos; i++)
259 {
260 int stateno = to_state[i];
261 shifts *sp = states[stateno]->shifts;
262
263 int j;
264 for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++)
265 bitset_set (F[i], SHIFT_SYMBOL (sp, j));
266
267 for (; j < sp->nshifts; j++)
268 {
269 int symbol = SHIFT_SYMBOL (sp, j);
270 if (nullable[symbol])
271 edge[nedges++] = map_goto (stateno, symbol);
272 }
273
274 if (nedges)
275 {
276 reads[i] = XCALLOC (short, nedges + 1);
277 shortcpy (reads[i], edge, nedges);
278 reads[i][nedges] = -1;
279 nedges = 0;
280 }
281 }
282
283 digraph (reads);
284
285 for (i = 0; i < ngotos; i++)
286 XFREE (reads[i]);
287
288 XFREE (reads);
289 XFREE (edge);
290 }
291
292
293 static void
294 add_lookback_edge (state_t *state, int ruleno, int gotono)
295 {
296 int i;
297 shorts *sp;
298
299 for (i = 0; i < state->nlookaheads; ++i)
300 if (LArule[state->lookaheadsp + i]->number == ruleno)
301 break;
302
303 assert (LArule[state->lookaheadsp + i]->number == ruleno);
304
305 sp = XCALLOC (shorts, 1);
306 sp->next = lookback[state->lookaheadsp + i];
307 sp->value = gotono;
308 lookback[state->lookaheadsp + i] = sp;
309 }
310
311
312 static void
313 matrix_print (FILE *out, short **matrix, int n)
314 {
315 int i, j;
316
317 for (i = 0; i < n; ++i)
318 {
319 fprintf (out, "%3d: ", i);
320 if (matrix[i])
321 for (j = 0; matrix[i][j] != -1; ++j)
322 fprintf (out, "%3d ", matrix[i][j]);
323 fputc ('\n', out);
324 }
325 fputc ('\n', out);
326 }
327
328 /*-------------------------------------------------------------------.
329 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
330 | replaced with the result. |
331 | |
332 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
333 | |
334 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
335 | is in R_ARG[I]. |
336 `-------------------------------------------------------------------*/
337
338 static short **
339 transpose (short **R_arg, int n)
340 {
341 /* The result. */
342 short **new_R = XCALLOC (short *, n);
343 /* END_R[I] -- next entry of NEW_R[I]. */
344 short **end_R = XCALLOC (short *, n);
345 /* NEDGES[I] -- total size of NEW_R[I]. */
346 short *nedges = XCALLOC (short, n);
347 int i, j;
348
349 if (trace_flag)
350 {
351 fputs ("transpose: input\n", stderr);
352 matrix_print (stderr, R_arg, n);
353 }
354
355 /* Count. */
356 for (i = 0; i < n; i++)
357 if (R_arg[i])
358 for (j = 0; R_arg[i][j] >= 0; ++j)
359 ++nedges[R_arg[i][j]];
360
361 /* Allocate. */
362 for (i = 0; i < n; i++)
363 if (nedges[i] > 0)
364 {
365 short *sp = XCALLOC (short, nedges[i] + 1);
366 sp[nedges[i]] = -1;
367 new_R[i] = sp;
368 end_R[i] = sp;
369 }
370
371 /* Store. */
372 for (i = 0; i < n; i++)
373 if (R_arg[i])
374 for (j = 0; R_arg[i][j] >= 0; ++j)
375 {
376 *end_R[R_arg[i][j]] = i;
377 ++end_R[R_arg[i][j]];
378 }
379
380 free (nedges);
381 free (end_R);
382
383 /* Free the input: it is replaced with the result. */
384 for (i = 0; i < n; i++)
385 XFREE (R_arg[i]);
386 free (R_arg);
387
388 if (trace_flag)
389 {
390 fputs ("transpose: output\n", stderr);
391 matrix_print (stderr, new_R, n);
392 }
393
394 return new_R;
395 }
396
397
398 static void
399 build_relations (void)
400 {
401 short *edge = XCALLOC (short, ngotos + 1);
402 short *states1 = XCALLOC (short, ritem_longest_rhs () + 1);
403 int i;
404
405 includes = XCALLOC (short *, ngotos);
406
407 for (i = 0; i < ngotos; i++)
408 {
409 int nedges = 0;
410 int symbol1 = states[to_state[i]]->accessing_symbol;
411 short *rulep;
412
413 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
414 {
415 int done;
416 int length = 1;
417 short *rp;
418 state_t *state = states[from_state[i]];
419 states1[0] = state->number;
420
421 for (rp = rules[*rulep].rhs; *rp >= 0; rp++)
422 {
423 shifts *sp = state->shifts;
424 int j;
425 for (j = 0; j < sp->nshifts; j++)
426 {
427 state = states[sp->shifts[j]];
428 if (state->accessing_symbol == *rp)
429 break;
430 }
431
432 states1[length++] = state->number;
433 }
434
435 if (!state->consistent)
436 add_lookback_edge (state, *rulep, i);
437
438 length--;
439 done = 0;
440 while (!done)
441 {
442 done = 1;
443 rp--;
444 /* JF added rp>=ritem && I hope to god its right! */
445 if (rp >= ritem && ISVAR (*rp))
446 {
447 edge[nedges++] = map_goto (states1[--length], *rp);
448 if (nullable[*rp])
449 done = 0;
450 }
451 }
452 }
453
454 if (nedges)
455 {
456 int j;
457 includes[i] = XCALLOC (short, nedges + 1);
458 for (j = 0; j < nedges; j++)
459 includes[i][j] = edge[j];
460 includes[i][nedges] = -1;
461 }
462 }
463
464 XFREE (edge);
465 XFREE (states1);
466
467 includes = transpose (includes, ngotos);
468 }
469
470
471
472 static void
473 compute_FOLLOWS (void)
474 {
475 int i;
476
477 digraph (includes);
478
479 for (i = 0; i < ngotos; i++)
480 XFREE (includes[i]);
481
482 XFREE (includes);
483 }
484
485
486 static void
487 compute_lookaheads (void)
488 {
489 size_t i;
490 shorts *sp;
491
492 for (i = 0; i < nLA; i++)
493 for (sp = lookback[i]; sp; sp = sp->next)
494 bitset_or (LA[i], LA[i], F[sp->value]);
495
496 /* Free LOOKBACK. */
497 for (i = 0; i < nLA; i++)
498 LIST_FREE (shorts, lookback[i]);
499
500 XFREE (lookback);
501 bitsetv_free (F);
502 }
503
504
505 /*--------------------------------------.
506 | Initializing the lookaheads members. |
507 `--------------------------------------*/
508
509 static void
510 initialize_lookaheads (void)
511 {
512 size_t i;
513 nLA = 0;
514 for (i = 0; i < nstates; i++)
515 {
516 int k;
517 int nlookaheads = 0;
518 reductions *rp = states[i]->reductions;
519 shifts *sp = states[i]->shifts;
520
521 /* We need a lookahead either to distinguish different
522 reductions (i.e., there are two or more), or to distinguish a
523 reduction from a shift. Otherwise, it is straightforward,
524 and the state is `consistent'. */
525 if (rp->nreds > 1
526 || (rp->nreds == 1 && sp->nshifts && SHIFT_IS_SHIFT (sp, 0)))
527 nlookaheads += rp->nreds;
528 else
529 states[i]->consistent = 1;
530
531 for (k = 0; k < sp->nshifts; k++)
532 if (SHIFT_IS_ERROR (sp, k))
533 {
534 states[i]->consistent = 0;
535 break;
536 }
537
538 states[i]->nlookaheads = nlookaheads;
539 states[i]->lookaheadsp = nLA;
540 nLA += nlookaheads;
541 }
542 }
543
544
545 /*---------------------------------------.
546 | Output the lookaheads for each state. |
547 `---------------------------------------*/
548
549 static void
550 lookaheads_print (FILE *out)
551 {
552 size_t i;
553 int j, k;
554 fprintf (out, "Lookaheads: BEGIN\n");
555 for (i = 0; i < nstates; ++i)
556 {
557 fprintf (out, "State %d: %d lookaheads\n",
558 i, states[i]->nlookaheads);
559
560 for (j = 0; j < states[i]->nlookaheads; ++j)
561 for (k = 0; k < ntokens; ++k)
562 if (bitset_test (LA[states[i]->lookaheadsp + j], j))
563 fprintf (out, " on %d (%s) -> rule %d\n",
564 k, symbols[k]->tag,
565 LArule[states[i]->lookaheadsp + j]->number - 1);
566 }
567 fprintf (out, "Lookaheads: END\n");
568 }
569
570 void
571 lalr (void)
572 {
573 initialize_lookaheads ();
574 initialize_LA ();
575 set_goto_map ();
576 initialize_F ();
577 build_relations ();
578 compute_FOLLOWS ();
579 compute_lookaheads ();
580
581 if (trace_flag)
582 lookaheads_print (stderr);
583 }