]> git.saurik.com Git - bison.git/blob - src/lalr.c
* src/state.h, src/state.c (shifts_to): New.
[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, rule_number_t 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 rule_number_t *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 state = shifts_to (state->shifts,
424 item_number_as_symbol_number (*rp));
425 states1[length++] = state->number;
426 }
427
428 if (!state->consistent)
429 add_lookback_edge (state, *rulep, i);
430
431 length--;
432 done = 0;
433 while (!done)
434 {
435 done = 1;
436 rp--;
437 /* JF added rp>=ritem && I hope to god its right! */
438 if (rp >= ritem && ISVAR (*rp))
439 {
440 /* Downcasting from item_number_t to symbol_number_t. */
441 edge[nedges++] = map_goto (states1[--length],
442 item_number_as_symbol_number (*rp));
443 if (nullable[*rp])
444 done = 0;
445 }
446 }
447 }
448
449 if (nedges)
450 {
451 int j;
452 includes[i] = XCALLOC (short, nedges + 1);
453 for (j = 0; j < nedges; j++)
454 includes[i][j] = edge[j];
455 includes[i][nedges] = -1;
456 }
457 }
458
459 XFREE (edge);
460 XFREE (states1);
461
462 includes = transpose (includes, ngotos);
463 }
464
465
466
467 static void
468 compute_FOLLOWS (void)
469 {
470 int i;
471
472 digraph (includes);
473
474 for (i = 0; i < ngotos; i++)
475 XFREE (includes[i]);
476
477 XFREE (includes);
478 }
479
480
481 static void
482 compute_lookaheads (void)
483 {
484 size_t i;
485 shorts *sp;
486
487 for (i = 0; i < nLA; i++)
488 for (sp = lookback[i]; sp; sp = sp->next)
489 bitset_or (LA[i], LA[i], F[sp->value]);
490
491 /* Free LOOKBACK. */
492 for (i = 0; i < nLA; i++)
493 LIST_FREE (shorts, lookback[i]);
494
495 XFREE (lookback);
496 bitsetv_free (F);
497 }
498
499
500 /*-------------------------------------------------------------.
501 | Count the number of lookaheads required for each state |
502 | (NLOOKAHEADS member). Compute the total number of LA, NLA. |
503 `-------------------------------------------------------------*/
504
505 static void
506 states_lookaheads_count (void)
507 {
508 state_number_t i;
509 nLA = 0;
510
511 /* Count */
512 for (i = 0; i < nstates; i++)
513 {
514 int k;
515 int nlookaheads = 0;
516 reductions_t *rp = states[i]->reductions;
517 shifts_t *sp = states[i]->shifts;
518
519 /* We need a lookahead either to distinguish different
520 reductions (i.e., there are two or more), or to distinguish a
521 reduction from a shift. Otherwise, it is straightforward,
522 and the state is `consistent'. */
523 if (rp->nreds > 1
524 || (rp->nreds == 1 && sp->nshifts && SHIFT_IS_SHIFT (sp, 0)))
525 nlookaheads += rp->nreds;
526 else
527 states[i]->consistent = 1;
528
529 for (k = 0; k < sp->nshifts; k++)
530 if (SHIFT_IS_ERROR (sp, k))
531 {
532 states[i]->consistent = 0;
533 break;
534 }
535
536 states[i]->nlookaheads = nlookaheads;
537 nLA += nlookaheads;
538 }
539 }
540
541
542 /*--------------------------------------.
543 | Initializing the lookaheads members. |
544 `--------------------------------------*/
545
546 static void
547 states_lookaheads_initialize (void)
548 {
549 state_number_t i;
550 bitsetv pLA = LA;
551 rule_t **pLArule = LArule;
552
553 /* Initialize the members LOOKAHEADS and LOOKAHEADS_RULE for each
554 state. */
555 for (i = 0; i < nstates; i++)
556 {
557 states[i]->lookaheads = pLA;
558 states[i]->lookaheads_rule = pLArule;
559 pLA += states[i]->nlookaheads;
560 pLArule += states[i]->nlookaheads;
561 }
562 }
563
564
565 /*---------------------------------------.
566 | Output the lookaheads for each state. |
567 `---------------------------------------*/
568
569 static void
570 lookaheads_print (FILE *out)
571 {
572 state_number_t i;
573 int j, k;
574 fprintf (out, "Lookaheads: BEGIN\n");
575 for (i = 0; i < nstates; ++i)
576 {
577 fprintf (out, "State %d: %d lookaheads\n",
578 i, states[i]->nlookaheads);
579
580 for (j = 0; j < states[i]->nlookaheads; ++j)
581 for (k = 0; k < ntokens; ++k)
582 if (bitset_test (states[i]->lookaheads[j], k))
583 fprintf (out, " on %d (%s) -> rule %d\n",
584 k, symbol_tag_get (symbols[k]),
585 states[i]->lookaheads_rule[j]->number - 1);
586 }
587 fprintf (out, "Lookaheads: END\n");
588 }
589
590 void
591 lalr (void)
592 {
593 states_lookaheads_count ();
594 initialize_LA ();
595 states_lookaheads_initialize ();
596 set_goto_map ();
597 initialize_F ();
598 build_relations ();
599 compute_FOLLOWS ();
600 compute_lookaheads ();
601
602 if (trace_flag)
603 lookaheads_print (stderr);
604 }