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