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