]> git.saurik.com Git - bison.git/blob - src/lalr.c
1dfb293d7f0168f2854caf86d92e3e592c22fd60
[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. */
37 state_t **state_table = NULL;
38
39 int tokensetsize;
40 short *LAruleno;
41 unsigned *LA;
42 size_t nLA;
43
44 static int ngotos;
45 short *goto_map;
46 short *from_state;
47 short *to_state;
48
49 /* And for the famous F variable, which name is so descriptive that a
50 comment is hardly needed. <grin>. */
51 static unsigned *F = NULL;
52 #define F(Rule) (F + (Rule) * tokensetsize)
53
54 static short **includes;
55 static shorts **lookback;
56
57
58 /*---------------------------------------------------------------.
59 | digraph & traverse. |
60 | |
61 | The following variables are used as common storage between the |
62 | two. |
63 `---------------------------------------------------------------*/
64
65 static short **R;
66 static short *INDEX;
67 static short *VERTICES;
68 static int top;
69 static int infinity;
70
71 static void
72 traverse (int i)
73 {
74 int j;
75 size_t k;
76 int height;
77 size_t size = F (i + 1) - F(i);
78
79 VERTICES[++top] = i;
80 INDEX[i] = height = top;
81
82 if (R[i])
83 for (j = 0; R[i][j] >= 0; ++j)
84 {
85 if (INDEX[R[i][j]] == 0)
86 traverse (R[i][j]);
87
88 if (INDEX[i] > INDEX[R[i][j]])
89 INDEX[i] = INDEX[R[i][j]];
90
91 for (k = 0; k < size; ++k)
92 F (i)[k] |= F (R[i][j])[k];
93 }
94
95 if (INDEX[i] == height)
96 for (;;)
97 {
98 j = VERTICES[top--];
99 INDEX[j] = infinity;
100
101 if (i == j)
102 break;
103
104 for (k = 0; k < size; ++k)
105 F (j)[k] = F (i)[k];
106 }
107 }
108
109
110 static void
111 digraph (short **relation)
112 {
113 int i;
114
115 infinity = ngotos + 2;
116 INDEX = XCALLOC (short, ngotos + 1);
117 VERTICES = XCALLOC (short, ngotos + 1);
118 top = 0;
119
120 R = relation;
121
122 for (i = 0; i < ngotos; i++)
123 INDEX[i] = 0;
124
125 for (i = 0; i < ngotos; i++)
126 if (INDEX[i] == 0 && R[i])
127 traverse (i);
128
129 XFREE (INDEX);
130 XFREE (VERTICES);
131 }
132
133
134 static void
135 initialize_LA (void)
136 {
137 int i;
138 int j;
139 short *np;
140 reductions *rp;
141
142 /* Avoid having to special case 0. */
143 if (!nLA)
144 nLA = 1;
145
146 LA = XCALLOC (unsigned, nLA * tokensetsize);
147 LAruleno = XCALLOC (short, nLA);
148 lookback = XCALLOC (shorts *, nLA);
149
150 np = LAruleno;
151 for (i = 0; i < nstates; i++)
152 if (!state_table[i]->consistent)
153 if ((rp = state_table[i]->reductions))
154 for (j = 0; j < rp->nreds; j++)
155 *np++ = rp->rules[j];
156 }
157
158
159 static void
160 set_goto_map (void)
161 {
162 int state, i;
163 short *temp_map;
164
165 goto_map = XCALLOC (short, nvars + 1) - ntokens;
166 temp_map = XCALLOC (short, nvars + 1) - ntokens;
167
168 ngotos = 0;
169 for (state = 0; state < nstates; ++state)
170 {
171 shifts *sp = state_table[state]->shifts;
172 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
173 {
174 int symbol = state_table[sp->shifts[i]]->accessing_symbol;
175
176 if (ngotos == MAXSHORT)
177 fatal (_("too many gotos (max %d)"), MAXSHORT);
178
179 ngotos++;
180 goto_map[symbol]++;
181 }
182 }
183
184 {
185 int k = 0;
186 for (i = ntokens; i < nsyms; i++)
187 {
188 temp_map[i] = k;
189 k += goto_map[i];
190 }
191
192 for (i = ntokens; i < nsyms; i++)
193 goto_map[i] = temp_map[i];
194
195 goto_map[nsyms] = ngotos;
196 temp_map[nsyms] = ngotos;
197 }
198
199 from_state = XCALLOC (short, ngotos);
200 to_state = XCALLOC (short, ngotos);
201
202 for (state = 0; state < nstates; ++state)
203 {
204 shifts *sp = state_table[state]->shifts;
205 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
206 {
207 int state2 = sp->shifts[i];
208 int symbol = state_table[state2]->accessing_symbol;
209
210 int k = temp_map[symbol]++;
211 from_state[k] = state;
212 to_state[k] = state2;
213 }
214 }
215
216 XFREE (temp_map + ntokens);
217 }
218
219
220
221 /*----------------------------------------------------------.
222 | Map a state/symbol pair into its numeric representation. |
223 `----------------------------------------------------------*/
224
225 static int
226 map_goto (int state, int symbol)
227 {
228 int high;
229 int low;
230 int middle;
231 int s;
232
233 low = goto_map[symbol];
234 high = goto_map[symbol + 1] - 1;
235
236 while (low <= high)
237 {
238 middle = (low + high) / 2;
239 s = from_state[middle];
240 if (s == state)
241 return middle;
242 else if (s < state)
243 low = middle + 1;
244 else
245 high = middle - 1;
246 }
247
248 assert (0);
249 /* NOTREACHED */
250 return 0;
251 }
252
253
254 static void
255 initialize_F (void)
256 {
257 short **reads = XCALLOC (short *, ngotos);
258 short *edge = XCALLOC (short, ngotos + 1);
259 int nedges = 0;
260
261 int i;
262
263 F = XCALLOC (unsigned, ngotos * tokensetsize);
264
265 for (i = 0; i < ngotos; i++)
266 {
267 int stateno = to_state[i];
268 shifts *sp = state_table[stateno]->shifts;
269
270 int j;
271 for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++)
272 {
273 int symbol = state_table[sp->shifts[j]]->accessing_symbol;
274 SETBIT (F (i), symbol);
275 }
276
277 for (; j < sp->nshifts; j++)
278 {
279 int symbol = state_table[sp->shifts[j]]->accessing_symbol;
280 if (nullable[symbol])
281 edge[nedges++] = map_goto (stateno, symbol);
282 }
283
284 if (nedges)
285 {
286 reads[i] = XCALLOC (short, nedges + 1);
287 shortcpy (reads[i], edge, nedges);
288 reads[i][nedges] = -1;
289 nedges = 0;
290 }
291 }
292
293 digraph (reads);
294
295 for (i = 0; i < ngotos; i++)
296 XFREE (reads[i]);
297
298 XFREE (reads);
299 XFREE (edge);
300 }
301
302
303 static void
304 add_lookback_edge (int stateno, int ruleno, int gotono)
305 {
306 int i;
307 shorts *sp;
308
309 for (i = 0; i < state_table[stateno]->nlookaheads; ++i)
310 if (LAruleno[state_table[stateno]->lookaheadsp + i] == ruleno)
311 break;
312
313 assert (LAruleno[state_table[stateno]->lookaheadsp + i] == ruleno);
314
315 sp = XCALLOC (shorts, 1);
316 sp->next = lookback[state_table[stateno]->lookaheadsp + i];
317 sp->value = gotono;
318 lookback[state_table[stateno]->lookaheadsp + i] = sp;
319 }
320
321
322 static void
323 matrix_print (FILE *out, short **matrix, int n)
324 {
325 int i, j;
326
327 for (i = 0; i < n; ++i)
328 {
329 fprintf (out, "%3d: ", i);
330 if (matrix[i])
331 for (j = 0; matrix[i][j] != -1; ++j)
332 fprintf (out, "%3d ", matrix[i][j]);
333 fputc ('\n', out);
334 }
335 fputc ('\n', out);
336 }
337
338 /*-------------------------------------------------------------------.
339 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
340 | replaced with the result. |
341 | |
342 | R_ARG[I] is NULL or a -1 terminated list of numbers. |
343 | |
344 | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
345 | is in R_ARG[I]. |
346 `-------------------------------------------------------------------*/
347
348 static short **
349 transpose (short **R_arg, int n)
350 {
351 /* The result. */
352 short **new_R = XCALLOC (short *, n);
353 /* END_R[I] -- next entry of NEW_R[I]. */
354 short **end_R = XCALLOC (short *, n);
355 /* NEDGES[I] -- total size of NEW_R[I]. */
356 short *nedges = XCALLOC (short, n);
357 int i, j;
358
359 if (trace_flag)
360 {
361 fputs ("transpose: input\n", stderr);
362 matrix_print (stderr, R_arg, n);
363 }
364
365 /* Count. */
366 for (i = 0; i < n; i++)
367 if (R_arg[i])
368 for (j = 0; R_arg[i][j] >= 0; ++j)
369 ++nedges[R_arg[i][j]];
370
371 /* Allocate. */
372 for (i = 0; i < n; i++)
373 if (nedges[i] > 0)
374 {
375 short *sp = XCALLOC (short, nedges[i] + 1);
376 sp[nedges[i]] = -1;
377 new_R[i] = sp;
378 end_R[i] = sp;
379 }
380
381 /* Store. */
382 for (i = 0; i < n; i++)
383 if (R_arg[i])
384 for (j = 0; R_arg[i][j] >= 0; ++j)
385 {
386 *end_R[R_arg[i][j]] = i;
387 ++end_R[R_arg[i][j]];
388 }
389
390 free (nedges);
391 free (end_R);
392
393 /* Free the input: it is replaced with the result. */
394 for (i = 0; i < n; i++)
395 XFREE (R_arg[i]);
396 free (R_arg);
397
398 if (trace_flag)
399 {
400 fputs ("transpose: output\n", stderr);
401 matrix_print (stderr, new_R, n);
402 }
403
404 return new_R;
405 }
406
407
408 static void
409 build_relations (void)
410 {
411 short *edge = XCALLOC (short, ngotos + 1);
412 short *states = XCALLOC (short, ritem_longest_rhs () + 1);
413 int i;
414
415 includes = XCALLOC (short *, ngotos);
416
417 for (i = 0; i < ngotos; i++)
418 {
419 int nedges = 0;
420 int state1 = from_state[i];
421 int symbol1 = state_table[to_state[i]]->accessing_symbol;
422 short *rulep;
423
424 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
425 {
426 int done;
427 int length = 1;
428 int stateno = state1;
429 short *rp;
430 states[0] = state1;
431
432 for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++)
433 {
434 shifts *sp = state_table[stateno]->shifts;
435 int j;
436 for (j = 0; j < sp->nshifts; j++)
437 {
438 stateno = sp->shifts[j];
439 if (state_table[stateno]->accessing_symbol == *rp)
440 break;
441 }
442
443 states[length++] = stateno;
444 }
445
446 if (!state_table[stateno]->consistent)
447 add_lookback_edge (stateno, *rulep, i);
448
449 length--;
450 done = 0;
451 while (!done)
452 {
453 done = 1;
454 rp--;
455 /* JF added rp>=ritem && I hope to god its right! */
456 if (rp >= ritem && ISVAR (*rp))
457 {
458 stateno = states[--length];
459 edge[nedges++] = map_goto (stateno, *rp);
460 if (nullable[*rp])
461 done = 0;
462 }
463 }
464 }
465
466 if (nedges)
467 {
468 int j;
469 includes[i] = XCALLOC (short, nedges + 1);
470 for (j = 0; j < nedges; j++)
471 includes[i][j] = edge[j];
472 includes[i][nedges] = -1;
473 }
474 }
475
476 XFREE (edge);
477 XFREE (states);
478
479 includes = transpose (includes, ngotos);
480 }
481
482
483
484 static void
485 compute_FOLLOWS (void)
486 {
487 int i;
488
489 digraph (includes);
490
491 for (i = 0; i < ngotos; i++)
492 XFREE (includes[i]);
493
494 XFREE (includes);
495 }
496
497
498 static void
499 compute_lookaheads (void)
500 {
501 size_t i;
502 shorts *sp;
503
504 for (i = 0; i < nLA; i++)
505 for (sp = lookback[i]; sp; sp = sp->next)
506 {
507 int size = LA (i + 1) - LA (i);
508 int j;
509 for (j = 0; j < size; ++j)
510 LA (i)[j] |= F (sp->value)[j];
511 }
512
513 /* Free LOOKBACK. */
514 for (i = 0; i < nLA; i++)
515 LIST_FREE (shorts, lookback[i]);
516
517 XFREE (lookback);
518 XFREE (F);
519 }
520
521
522 /*--------------------------------------.
523 | Initializing the lookaheads members. |
524 `--------------------------------------*/
525
526 static void
527 initialize_lookaheads (void)
528 {
529 int i;
530 nLA = 0;
531 for (i = 0; i < nstates; i++)
532 {
533 int k;
534 int nlookaheads = 0;
535 reductions *rp = state_table[i]->reductions;
536 shifts *sp = state_table[i]->shifts;
537
538 if (rp
539 && (rp->nreds > 1 || (sp->nshifts && SHIFT_IS_SHIFT (sp, 0))))
540 nlookaheads += rp->nreds;
541 else
542 state_table[i]->consistent = 1;
543
544 for (k = 0; k < sp->nshifts; k++)
545 if (SHIFT_IS_ERROR (sp, k))
546 {
547 state_table[i]->consistent = 0;
548 break;
549 }
550
551 state_table[i]->nlookaheads = nlookaheads;
552 state_table[i]->lookaheadsp = nLA;
553 nLA += nlookaheads;
554 }
555 }
556
557 void
558 lalr (void)
559 {
560 tokensetsize = WORDSIZE (ntokens);
561
562 initialize_lookaheads ();
563 initialize_LA ();
564 set_goto_map ();
565 initialize_F ();
566 build_relations ();
567 compute_FOLLOWS ();
568 compute_lookaheads ();
569 }