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