]> git.saurik.com Git - bison.git/blob - src/lalr.c
0eb32411a92750dc33f209db6181b228d2f3da83
[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 (j)[k] = F (i)[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]->lookaheadsp;
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 shorts *sp;
314
315 for (i = 0; i < state_table[stateno]->nlookaheads; ++i)
316 if (LAruleno[state_table[stateno]->lookaheadsp + i] == ruleno)
317 break;
318
319 assert (LAruleno[state_table[stateno]->lookaheadsp + i] == ruleno);
320
321 sp = XCALLOC (shorts, 1);
322 sp->next = lookback[state_table[stateno]->lookaheadsp + i];
323 sp->value = gotono;
324 lookback[state_table[stateno]->lookaheadsp + i] = sp;
325 }
326
327
328 static void
329 matrix_print (FILE *out, short **matrix, int n)
330 {
331 int i, j;
332
333 for (i = 0; i < n; ++i)
334 {
335 fprintf (out, "%3d: ", i);
336 if (matrix[i])
337 for (j = 0; matrix[i][j] != -1; ++j)
338 fprintf (out, "%3d ", matrix[i][j]);
339 fputc ('\n', out);
340 }
341 fputc ('\n', out);
342 }
343
344 /*-------------------------------------------------------------------.
345 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
346 | replaced with the result. |
347 | |
348 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
349 | |
350 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
351 | is in R_ARG[I]. |
352 `-------------------------------------------------------------------*/
353
354 static short **
355 transpose (short **R_arg, int n)
356 {
357 /* The result. */
358 short **new_R = XCALLOC (short *, n);
359 /* END_R[I] -- next entry of NEW_R[I]. */
360 short **end_R = XCALLOC (short *, n);
361 /* NEDGES[I] -- total size of NEW_R[I]. */
362 short *nedges = XCALLOC (short, n);
363 int i, j;
364
365 if (trace_flag)
366 {
367 fputs ("transpose: input\n", stderr);
368 matrix_print (stderr, R_arg, n);
369 }
370
371 /* Count. */
372 for (i = 0; i < n; i++)
373 if (R_arg[i])
374 for (j = 0; R_arg[i][j] >= 0; ++j)
375 ++nedges[R_arg[i][j]];
376
377 /* Allocate. */
378 for (i = 0; i < n; i++)
379 if (nedges[i] > 0)
380 {
381 short *sp = XCALLOC (short, nedges[i] + 1);
382 sp[nedges[i]] = -1;
383 new_R[i] = sp;
384 end_R[i] = sp;
385 }
386
387 /* Store. */
388 for (i = 0; i < n; i++)
389 if (R_arg[i])
390 for (j = 0; R_arg[i][j] >= 0; ++j)
391 {
392 *end_R[R_arg[i][j]] = i;
393 ++end_R[R_arg[i][j]];
394 }
395
396 free (nedges);
397 free (end_R);
398
399 /* Free the input: it is replaced with the result. */
400 for (i = 0; i < n; i++)
401 XFREE (R_arg[i]);
402 free (R_arg);
403
404 if (trace_flag)
405 {
406 fputs ("transpose: output\n", stderr);
407 matrix_print (stderr, new_R, n);
408 }
409
410 return new_R;
411 }
412
413
414 static void
415 build_relations (void)
416 {
417 short *edge = XCALLOC (short, ngotos + 1);
418 short *states = XCALLOC (short, ritem_longest_rhs () + 1);
419 int i;
420
421 includes = XCALLOC (short *, ngotos);
422
423 for (i = 0; i < ngotos; i++)
424 {
425 int nedges = 0;
426 int state1 = from_state[i];
427 int symbol1 = state_table[to_state[i]]->accessing_symbol;
428 short *rulep;
429
430 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
431 {
432 int done;
433 int length = 1;
434 int stateno = state1;
435 short *rp;
436 states[0] = state1;
437
438 for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++)
439 {
440 shifts *sp = state_table[stateno]->shifts;
441 int j;
442 for (j = 0; j < sp->nshifts; j++)
443 {
444 stateno = sp->shifts[j];
445 if (state_table[stateno]->accessing_symbol == *rp)
446 break;
447 }
448
449 states[length++] = stateno;
450 }
451
452 if (!state_table[stateno]->consistent)
453 add_lookback_edge (stateno, *rulep, i);
454
455 length--;
456 done = 0;
457 while (!done)
458 {
459 done = 1;
460 rp--;
461 /* JF added rp>=ritem && I hope to god its right! */
462 if (rp >= ritem && ISVAR (*rp))
463 {
464 stateno = states[--length];
465 edge[nedges++] = map_goto (stateno, *rp);
466 if (nullable[*rp])
467 done = 0;
468 }
469 }
470 }
471
472 if (nedges)
473 {
474 int j;
475 includes[i] = XCALLOC (short, nedges + 1);
476 for (j = 0; j < nedges; j++)
477 includes[i][j] = edge[j];
478 includes[i][nedges] = -1;
479 }
480 }
481
482 XFREE (edge);
483 XFREE (states);
484
485 includes = transpose (includes, ngotos);
486 }
487
488
489
490 static void
491 compute_FOLLOWS (void)
492 {
493 int i;
494
495 digraph (includes);
496
497 for (i = 0; i < ngotos; i++)
498 XFREE (includes[i]);
499
500 XFREE (includes);
501 }
502
503
504 static void
505 compute_lookaheads (void)
506 {
507 int i;
508 shorts *sp;
509
510 for (i = 0; i < state_table[nstates]->lookaheadsp; i++)
511 for (sp = lookback[i]; sp; sp = sp->next)
512 {
513 int size = LA (i + 1) - LA (i);
514 int j;
515 for (j = 0; j < size; ++j)
516 LA (i)[j] |= F (sp->value)[j];
517 }
518
519 /* Free LOOKBACK. */
520 for (i = 0; i < state_table[nstates]->lookaheadsp; i++)
521 LIST_FREE (shorts, lookback[i]);
522
523 XFREE (lookback);
524 XFREE (F);
525 }
526
527
528 /*--------------------------------------.
529 | Initializing the lookaheads members. |
530 `--------------------------------------*/
531
532 static void
533 initialize_lookaheads (void)
534 {
535 int i;
536 int count = 0;
537 for (i = 0; i < nstates; i++)
538 {
539 int k;
540 int nlookaheads = 0;
541 reductions *rp = state_table[i]->reductions;
542 shifts *sp = state_table[i]->shifts;
543
544 if (rp
545 && (rp->nreds > 1 || (sp->nshifts && SHIFT_IS_SHIFT (sp, 0))))
546 nlookaheads += rp->nreds;
547 else
548 state_table[i]->consistent = 1;
549
550 for (k = 0; k < sp->nshifts; k++)
551 if (SHIFT_IS_ERROR (sp, k))
552 {
553 state_table[i]->consistent = 0;
554 break;
555 }
556
557 state_table[i]->nlookaheads = nlookaheads;
558 state_table[i]->lookaheadsp = count;
559 count += nlookaheads;
560 }
561
562 /* Seems to be needed by conflicts.c. */
563 state_table[nstates] = STATE_ALLOC (0);
564 state_table[nstates]->lookaheadsp = count;
565 }
566
567 void
568 lalr (void)
569 {
570 tokensetsize = WORDSIZE (ntokens);
571
572 initialize_lookaheads ();
573 initialize_LA ();
574 set_goto_map ();
575 initialize_F ();
576 build_relations ();
577 compute_FOLLOWS ();
578 compute_lookaheads ();
579 }