]> git.saurik.com Git - bison.git/blob - src/lalr.c
* src/L0.c, src/LR0.h (nstates): Be size_t.
[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 unsigned *F = NULL;
55 #define F(Rule) (F + (Rule) * tokensetsize)
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 size_t k;
79 int height;
80 size_t size = F (i + 1) - F(i);
81
82 VERTICES[++top] = i;
83 INDEX[i] = height = top;
84
85 if (R[i])
86 for (j = 0; R[i][j] >= 0; ++j)
87 {
88 if (INDEX[R[i][j]] == 0)
89 traverse (R[i][j]);
90
91 if (INDEX[i] > INDEX[R[i][j]])
92 INDEX[i] = INDEX[R[i][j]];
93
94 for (k = 0; k < size; ++k)
95 F (i)[k] |= F (R[i][j])[k];
96 }
97
98 if (INDEX[i] == height)
99 for (;;)
100 {
101 j = VERTICES[top--];
102 INDEX[j] = infinity;
103
104 if (i == j)
105 break;
106
107 for (k = 0; k < size; ++k)
108 F (j)[k] = F (i)[k];
109 }
110 }
111
112
113 static void
114 digraph (short **relation)
115 {
116 int i;
117
118 infinity = ngotos + 2;
119 INDEX = XCALLOC (short, ngotos + 1);
120 VERTICES = XCALLOC (short, ngotos + 1);
121 top = 0;
122
123 R = relation;
124
125 for (i = 0; i < ngotos; i++)
126 INDEX[i] = 0;
127
128 for (i = 0; i < ngotos; i++)
129 if (INDEX[i] == 0 && R[i])
130 traverse (i);
131
132 XFREE (INDEX);
133 XFREE (VERTICES);
134 }
135
136
137 static void
138 initialize_LA (void)
139 {
140 size_t i;
141 int j;
142 short *np;
143
144 /* Avoid having to special case 0. */
145 if (!nLA)
146 nLA = 1;
147
148 LA = XCALLOC (bitset, nLA);
149 for (i = 0; i < nLA; ++i)
150 {
151 LA[i] = bitset_create (ntokens, BITSET_FIXED);
152 bitset_zero (LA[i]);
153 }
154 LAruleno = XCALLOC (short, nLA);
155 lookback = XCALLOC (shorts *, nLA);
156
157 np = LAruleno;
158 for (i = 0; i < nstates; i++)
159 if (!states[i]->consistent)
160 for (j = 0; j < states[i]->reductions->nreds; j++)
161 *np++ = states[i]->reductions->rules[j];
162 }
163
164
165 static void
166 set_goto_map (void)
167 {
168 size_t state;
169 int i;
170 short *temp_map;
171
172 goto_map = XCALLOC (short, nvars + 1) - ntokens;
173 temp_map = XCALLOC (short, nvars + 1) - ntokens;
174
175 ngotos = 0;
176 for (state = 0; state < nstates; ++state)
177 {
178 shifts *sp = states[state]->shifts;
179 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
180 {
181 if (ngotos == MAXSHORT)
182 fatal (_("too many gotos (max %d)"), MAXSHORT);
183
184 ngotos++;
185 goto_map[SHIFT_SYMBOL (sp, i)]++;
186 }
187 }
188
189 {
190 int k = 0;
191 for (i = ntokens; i < nsyms; i++)
192 {
193 temp_map[i] = k;
194 k += goto_map[i];
195 }
196
197 for (i = ntokens; i < nsyms; i++)
198 goto_map[i] = temp_map[i];
199
200 goto_map[nsyms] = ngotos;
201 temp_map[nsyms] = ngotos;
202 }
203
204 from_state = XCALLOC (short, ngotos);
205 to_state = XCALLOC (short, ngotos);
206
207 for (state = 0; state < nstates; ++state)
208 {
209 shifts *sp = states[state]->shifts;
210 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
211 {
212 int k = temp_map[SHIFT_SYMBOL (sp, i)]++;
213 from_state[k] = state;
214 to_state[k] = sp->shifts[i];
215 }
216 }
217
218 XFREE (temp_map + ntokens);
219 }
220
221
222
223 /*----------------------------------------------------------.
224 | Map a state/symbol pair into its numeric representation. |
225 `----------------------------------------------------------*/
226
227 static int
228 map_goto (int state, int symbol)
229 {
230 int high;
231 int low;
232 int middle;
233 int s;
234
235 low = goto_map[symbol];
236 high = goto_map[symbol + 1] - 1;
237
238 while (low <= high)
239 {
240 middle = (low + high) / 2;
241 s = from_state[middle];
242 if (s == state)
243 return middle;
244 else if (s < state)
245 low = middle + 1;
246 else
247 high = middle - 1;
248 }
249
250 assert (0);
251 /* NOTREACHED */
252 return 0;
253 }
254
255
256 static void
257 initialize_F (void)
258 {
259 short **reads = XCALLOC (short *, ngotos);
260 short *edge = XCALLOC (short, ngotos + 1);
261 int nedges = 0;
262
263 int i;
264
265 F = XCALLOC (unsigned, ngotos * tokensetsize);
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 SETBIT (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 {
504 int j;
505 for (j = 0; j < ntokens; ++j)
506 if (BITISSET (F (sp->value), j))
507 bitset_set (LA[i], j);
508 }
509
510 /* Free LOOKBACK. */
511 for (i = 0; i < nLA; i++)
512 LIST_FREE (shorts, lookback[i]);
513
514 XFREE (lookback);
515 XFREE (F);
516 }
517
518
519 /*--------------------------------------.
520 | Initializing the lookaheads members. |
521 `--------------------------------------*/
522
523 static void
524 initialize_lookaheads (void)
525 {
526 size_t i;
527 nLA = 0;
528 for (i = 0; i < nstates; i++)
529 {
530 int k;
531 int nlookaheads = 0;
532 reductions *rp = states[i]->reductions;
533 shifts *sp = states[i]->shifts;
534
535 /* We need a lookahead either to distinguish different
536 reductions (i.e., there are two or more), or to distinguish a
537 reduction from a shift. Otherwise, it is straightforward,
538 and the state is `consistent'. */
539 if (rp->nreds > 1
540 || (rp->nreds == 1 && sp->nshifts && SHIFT_IS_SHIFT (sp, 0)))
541 nlookaheads += rp->nreds;
542 else
543 states[i]->consistent = 1;
544
545 for (k = 0; k < sp->nshifts; k++)
546 if (SHIFT_IS_ERROR (sp, k))
547 {
548 states[i]->consistent = 0;
549 break;
550 }
551
552 states[i]->nlookaheads = nlookaheads;
553 states[i]->lookaheadsp = nLA;
554 nLA += nlookaheads;
555 }
556 }
557
558
559 /*---------------------------------------.
560 | Output the lookaheads for each state. |
561 `---------------------------------------*/
562
563 static void
564 lookaheads_print (FILE *out)
565 {
566 size_t i;
567 int j, k;
568 fprintf (out, "Lookaheads: BEGIN\n");
569 for (i = 0; i < nstates; ++i)
570 {
571 fprintf (out, "State %d: %d lookaheads\n",
572 i, states[i]->nlookaheads);
573
574 for (j = 0; j < states[i]->nlookaheads; ++j)
575 for (k = 0; k < ntokens; ++k)
576 if (bitset_test (LA[states[i]->lookaheadsp + j], j))
577 fprintf (out, " on %d (%s) -> rule %d\n",
578 k, symbols[k]->tag,
579 -LAruleno[states[i]->lookaheadsp + j] - 1);
580 }
581 fprintf (out, "Lookaheads: END\n");
582 }
583
584 void
585 lalr (void)
586 {
587 tokensetsize = WORDSIZE (ntokens);
588
589 initialize_lookaheads ();
590 initialize_LA ();
591 set_goto_map ();
592 initialize_F ();
593 build_relations ();
594 compute_FOLLOWS ();
595 compute_lookaheads ();
596
597 if (trace_flag)
598 lookaheads_print (stderr);
599 }