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