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