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