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