]> git.saurik.com Git - bison.git/blob - src/lalr.c
84f32894578b89dd547e1de05779a5eb6ab4ca52
[bison.git] / src / lalr.c
1 /* Compute look-ahead criteria for bison,
2 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 /* Compute how to make the finite state machine deterministic; find
24 which rules need lookahead in each state, and which lookahead
25 tokens they accept. */
26
27 #include "system.h"
28 #include "bitset.h"
29 #include "bitsetv.h"
30 #include "quotearg.h"
31 #include "symtab.h"
32 #include "gram.h"
33 #include "reader.h"
34 #include "types.h"
35 #include "LR0.h"
36 #include "complain.h"
37 #include "lalr.h"
38 #include "nullable.h"
39 #include "derives.h"
40 #include "getargs.h"
41
42 rule_t **LArule = NULL;
43 bitsetv LA = NULL;
44 size_t nLA;
45
46 static int ngotos;
47 short *goto_map = NULL;
48 state_number_t *from_state = NULL;
49 state_number_t *to_state = NULL;
50
51 /* And for the famous F variable, which name is so descriptive that a
52 comment is hardly needed. <grin>. */
53 static bitsetv F = NULL;
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 int height;
77
78 VERTICES[++top] = i;
79 INDEX[i] = height = top;
80
81 if (R[i])
82 for (j = 0; R[i][j] >= 0; ++j)
83 {
84 if (INDEX[R[i][j]] == 0)
85 traverse (R[i][j]);
86
87 if (INDEX[i] > INDEX[R[i][j]])
88 INDEX[i] = INDEX[R[i][j]];
89
90 bitset_or (F[i], F[i], F[R[i][j]]);
91 }
92
93 if (INDEX[i] == height)
94 for (;;)
95 {
96 j = VERTICES[top--];
97 INDEX[j] = infinity;
98
99 if (i == j)
100 break;
101
102 bitset_copy (F[j], F[i]);
103 }
104 }
105
106
107 static void
108 digraph (short **relation)
109 {
110 int i;
111
112 infinity = ngotos + 2;
113 INDEX = XCALLOC (short, ngotos + 1);
114 VERTICES = XCALLOC (short, ngotos + 1);
115 top = 0;
116
117 R = relation;
118
119 for (i = 0; i < ngotos; i++)
120 INDEX[i] = 0;
121
122 for (i = 0; i < ngotos; i++)
123 if (INDEX[i] == 0 && R[i])
124 traverse (i);
125
126 XFREE (INDEX);
127 XFREE (VERTICES);
128 }
129
130
131 static void
132 initialize_LA (void)
133 {
134 state_number_t i;
135 int j;
136 rule_t **np;
137
138 /* Avoid having to special case 0. */
139 if (!nLA)
140 nLA = 1;
141
142 LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
143 LArule = XCALLOC (rule_t *, nLA);
144 lookback = XCALLOC (shorts *, nLA);
145
146 np = LArule;
147 for (i = 0; i < nstates; i++)
148 if (!states[i]->consistent)
149 for (j = 0; j < states[i]->reductions->nreds; j++)
150 *np++ = &rules[states[i]->reductions->rules[j]];
151 }
152
153
154 static void
155 set_goto_map (void)
156 {
157 state_number_t state;
158 short *temp_map;
159
160 goto_map = XCALLOC (short, nvars + 1) - ntokens;
161 temp_map = XCALLOC (short, nvars + 1) - ntokens;
162
163 ngotos = 0;
164 for (state = 0; state < nstates; ++state)
165 {
166 shifts_t *sp = states[state]->shifts;
167 int i;
168 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
169 {
170 if (ngotos == SHRT_MAX)
171 fatal (_("too many gotos (max %d)"), SHRT_MAX);
172
173 ngotos++;
174 goto_map[SHIFT_SYMBOL (sp, i)]++;
175 }
176 }
177
178 {
179 int k = 0;
180 int i;
181 for (i = ntokens; i < nsyms; i++)
182 {
183 temp_map[i] = k;
184 k += goto_map[i];
185 }
186
187 for (i = ntokens; i < nsyms; i++)
188 goto_map[i] = temp_map[i];
189
190 goto_map[nsyms] = ngotos;
191 temp_map[nsyms] = ngotos;
192 }
193
194 from_state = XCALLOC (state_number_t, ngotos);
195 to_state = XCALLOC (state_number_t, ngotos);
196
197 for (state = 0; state < nstates; ++state)
198 {
199 shifts_t *sp = states[state]->shifts;
200 int i;
201 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
202 {
203 int k = temp_map[SHIFT_SYMBOL (sp, i)]++;
204 from_state[k] = state;
205 to_state[k] = sp->shifts[i];
206 }
207 }
208
209 XFREE (temp_map + ntokens);
210 }
211
212
213
214 /*----------------------------------------------------------.
215 | Map a state/symbol pair into its numeric representation. |
216 `----------------------------------------------------------*/
217
218 static int
219 map_goto (state_number_t state, symbol_number_t symbol)
220 {
221 int high;
222 int low;
223 int middle;
224 state_number_t s;
225
226 low = goto_map[symbol];
227 high = goto_map[symbol + 1] - 1;
228
229 while (low <= high)
230 {
231 middle = (low + high) / 2;
232 s = from_state[middle];
233 if (s == state)
234 return middle;
235 else if (s < state)
236 low = middle + 1;
237 else
238 high = middle - 1;
239 }
240
241 assert (0);
242 /* NOTREACHED */
243 return 0;
244 }
245
246
247 static void
248 initialize_F (void)
249 {
250 short **reads = XCALLOC (short *, ngotos);
251 short *edge = XCALLOC (short, ngotos + 1);
252 int nedges = 0;
253
254 int i;
255
256 F = bitsetv_create (ngotos, ntokens, BITSET_FIXED);
257
258 for (i = 0; i < ngotos; i++)
259 {
260 state_number_t stateno = to_state[i];
261 shifts_t *sp = states[stateno]->shifts;
262
263 int j;
264 for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++)
265 bitset_set (F[i], SHIFT_SYMBOL (sp, j));
266
267 for (; j < sp->nshifts; j++)
268 {
269 symbol_number_t symbol = SHIFT_SYMBOL (sp, j);
270 if (nullable[symbol])
271 edge[nedges++] = map_goto (stateno, symbol);
272 }
273
274 if (nedges)
275 {
276 reads[i] = XCALLOC (short, nedges + 1);
277 memcpy (reads[i], edge, nedges * sizeof (edge[0]));
278 reads[i][nedges] = -1;
279 nedges = 0;
280 }
281 }
282
283 digraph (reads);
284
285 for (i = 0; i < ngotos; i++)
286 XFREE (reads[i]);
287
288 XFREE (reads);
289 XFREE (edge);
290 }
291
292
293 static void
294 add_lookback_edge (state_t *state, int ruleno, int gotono)
295 {
296 int i;
297 shorts *sp;
298
299 for (i = 0; i < state->nlookaheads; ++i)
300 if (state->lookaheads_rule[i]->number == ruleno)
301 break;
302
303 assert (state->lookaheads_rule[i]->number == ruleno);
304
305 sp = XCALLOC (shorts, 1);
306 sp->next = lookback[(state->lookaheads - LA) + i];
307 sp->value = gotono;
308 lookback[(state->lookaheads - LA) + i] = sp;
309 }
310
311
312 static void
313 matrix_print (FILE *out, short **matrix, int n)
314 {
315 int i, j;
316
317 for (i = 0; i < n; ++i)
318 {
319 fprintf (out, "%3d: ", i);
320 if (matrix[i])
321 for (j = 0; matrix[i][j] != -1; ++j)
322 fprintf (out, "%3d ", matrix[i][j]);
323 fputc ('\n', out);
324 }
325 fputc ('\n', out);
326 }
327
328 /*-------------------------------------------------------------------.
329 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
330 | replaced with the result. |
331 | |
332 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
333 | |
334 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
335 | is in R_ARG[I]. |
336 `-------------------------------------------------------------------*/
337
338 static short **
339 transpose (short **R_arg, int n)
340 {
341 /* The result. */
342 short **new_R = XCALLOC (short *, n);
343 /* END_R[I] -- next entry of NEW_R[I]. */
344 short **end_R = XCALLOC (short *, n);
345 /* NEDGES[I] -- total size of NEW_R[I]. */
346 short *nedges = XCALLOC (short, n);
347 int i, j;
348
349 if (trace_flag)
350 {
351 fputs ("transpose: input\n", stderr);
352 matrix_print (stderr, R_arg, n);
353 }
354
355 /* Count. */
356 for (i = 0; i < n; i++)
357 if (R_arg[i])
358 for (j = 0; R_arg[i][j] >= 0; ++j)
359 ++nedges[R_arg[i][j]];
360
361 /* Allocate. */
362 for (i = 0; i < n; i++)
363 if (nedges[i] > 0)
364 {
365 short *sp = XCALLOC (short, nedges[i] + 1);
366 sp[nedges[i]] = -1;
367 new_R[i] = sp;
368 end_R[i] = sp;
369 }
370
371 /* Store. */
372 for (i = 0; i < n; i++)
373 if (R_arg[i])
374 for (j = 0; R_arg[i][j] >= 0; ++j)
375 {
376 *end_R[R_arg[i][j]] = i;
377 ++end_R[R_arg[i][j]];
378 }
379
380 free (nedges);
381 free (end_R);
382
383 /* Free the input: it is replaced with the result. */
384 for (i = 0; i < n; i++)
385 XFREE (R_arg[i]);
386 free (R_arg);
387
388 if (trace_flag)
389 {
390 fputs ("transpose: output\n", stderr);
391 matrix_print (stderr, new_R, n);
392 }
393
394 return new_R;
395 }
396
397
398 static void
399 build_relations (void)
400 {
401 short *edge = XCALLOC (short, ngotos + 1);
402 state_number_t *states1 = XCALLOC (state_number_t, ritem_longest_rhs () + 1);
403 int i;
404
405 includes = XCALLOC (short *, ngotos);
406
407 for (i = 0; i < ngotos; i++)
408 {
409 int nedges = 0;
410 symbol_number_t symbol1 = states[to_state[i]]->accessing_symbol;
411 short *rulep;
412
413 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
414 {
415 int done;
416 int length = 1;
417 item_number_t *rp;
418 state_t *state = states[from_state[i]];
419 states1[0] = state->number;
420
421 for (rp = rules[*rulep].rhs; *rp >= 0; rp++)
422 {
423 shifts_t *sp = state->shifts;
424 int j;
425 for (j = 0; j < sp->nshifts; j++)
426 {
427 state = states[sp->shifts[j]];
428 if (state->accessing_symbol
429 == item_number_as_symbol_number (*rp))
430 break;
431 }
432
433 states1[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 /* Downcasting from item_number_t to symbol_number_t. */
449 edge[nedges++] = map_goto (states1[--length],
450 item_number_as_symbol_number (*rp));
451 if (nullable[*rp])
452 done = 0;
453 }
454 }
455 }
456
457 if (nedges)
458 {
459 int j;
460 includes[i] = XCALLOC (short, nedges + 1);
461 for (j = 0; j < nedges; j++)
462 includes[i][j] = edge[j];
463 includes[i][nedges] = -1;
464 }
465 }
466
467 XFREE (edge);
468 XFREE (states1);
469
470 includes = transpose (includes, ngotos);
471 }
472
473
474
475 static void
476 compute_FOLLOWS (void)
477 {
478 int i;
479
480 digraph (includes);
481
482 for (i = 0; i < ngotos; i++)
483 XFREE (includes[i]);
484
485 XFREE (includes);
486 }
487
488
489 static void
490 compute_lookaheads (void)
491 {
492 size_t i;
493 shorts *sp;
494
495 for (i = 0; i < nLA; i++)
496 for (sp = lookback[i]; sp; sp = sp->next)
497 bitset_or (LA[i], LA[i], F[sp->value]);
498
499 /* Free LOOKBACK. */
500 for (i = 0; i < nLA; i++)
501 LIST_FREE (shorts, lookback[i]);
502
503 XFREE (lookback);
504 bitsetv_free (F);
505 }
506
507
508 /*-------------------------------------------------------------.
509 | Count the number of lookaheads required for each state |
510 | (NLOOKAHEADS member). Compute the total number of LA, NLA. |
511 `-------------------------------------------------------------*/
512
513 static void
514 states_lookaheads_count (void)
515 {
516 state_number_t i;
517 nLA = 0;
518
519 /* Count */
520 for (i = 0; i < nstates; i++)
521 {
522 int k;
523 int nlookaheads = 0;
524 reductions *rp = states[i]->reductions;
525 shifts_t *sp = states[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 states[i]->consistent = 1;
536
537 for (k = 0; k < sp->nshifts; k++)
538 if (SHIFT_IS_ERROR (sp, k))
539 {
540 states[i]->consistent = 0;
541 break;
542 }
543
544 states[i]->nlookaheads = nlookaheads;
545 nLA += nlookaheads;
546 }
547 }
548
549
550 /*--------------------------------------.
551 | Initializing the lookaheads members. |
552 `--------------------------------------*/
553
554 static void
555 states_lookaheads_initialize (void)
556 {
557 state_number_t i;
558 bitsetv pLA = LA;
559 rule_t **pLArule = LArule;
560
561 /* Initialize the members LOOKAHEADS and LOOKAHEADS_RULE for each
562 state. */
563 for (i = 0; i < nstates; i++)
564 {
565 states[i]->lookaheads = pLA;
566 states[i]->lookaheads_rule = pLArule;
567 pLA += states[i]->nlookaheads;
568 pLArule += states[i]->nlookaheads;
569 }
570 }
571
572
573 /*---------------------------------------.
574 | Output the lookaheads for each state. |
575 `---------------------------------------*/
576
577 static void
578 lookaheads_print (FILE *out)
579 {
580 state_number_t i;
581 int j, k;
582 fprintf (out, "Lookaheads: BEGIN\n");
583 for (i = 0; i < nstates; ++i)
584 {
585 fprintf (out, "State %d: %d lookaheads\n",
586 i, states[i]->nlookaheads);
587
588 for (j = 0; j < states[i]->nlookaheads; ++j)
589 for (k = 0; k < ntokens; ++k)
590 if (bitset_test (states[i]->lookaheads[j], k))
591 fprintf (out, " on %d (%s) -> rule %d\n",
592 k, symbol_tag_get (symbols[k]),
593 states[i]->lookaheads_rule[j]->number - 1);
594 }
595 fprintf (out, "Lookaheads: END\n");
596 }
597
598 void
599 lalr (void)
600 {
601 states_lookaheads_count ();
602 initialize_LA ();
603 states_lookaheads_initialize ();
604 set_goto_map ();
605 initialize_F ();
606 build_relations ();
607 compute_FOLLOWS ();
608 compute_lookaheads ();
609
610 if (trace_flag)
611 lookaheads_print (stderr);
612 }