]> git.saurik.com Git - bison.git/blob - src/lalr.c
c2554dcf5c5215fdfa3e4519468d57c80bc8514d
[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
35 /* All the decorated states, indexed by the state number. Warning:
36 there is a state_TABLE in LR0.c, but it is different and static.
37 */
38 state_t *state_table = NULL;
39
40 int tokensetsize;
41 short *LAruleno;
42 unsigned *LA;
43
44 short *goto_map;
45 short *from_state;
46 short *to_state;
47
48 extern void berror PARAMS ((const char *));
49
50 static int infinity;
51 static int ngotos;
52
53 /* And for the famous F variable, which name is so descriptive that a
54 comment is hardly needed. <grin>. */
55 static unsigned *F = NULL;
56 #define F(Rule) (F + (Rule) * tokensetsize)
57
58 static short **includes;
59 static shorts **lookback;
60 static short **R;
61 static short *INDEX;
62 static short *VERTICES;
63 static int top;
64
65
66 static void
67 traverse (int i)
68 {
69 int j;
70 size_t k;
71 int height;
72 size_t size = F (i + 1) - F(i);
73
74 VERTICES[++top] = i;
75 INDEX[i] = height = top;
76
77 if (R[i])
78 for (j = 0; R[i][j] >= 0; ++j)
79 {
80 if (INDEX[R[i][j]] == 0)
81 traverse (R[i][j]);
82
83 if (INDEX[i] > INDEX[R[i][j]])
84 INDEX[i] = INDEX[R[i][j]];
85
86 for (k = 0; k < size; ++k)
87 F (i)[k] |= F (R[i][j])[k];
88 }
89
90 if (INDEX[i] == height)
91 for (;;)
92 {
93 j = VERTICES[top--];
94 INDEX[j] = infinity;
95
96 if (i == j)
97 break;
98
99 for (k = 0; k < size; ++k)
100 F (i)[k] = F (j)[k];
101 }
102 }
103
104
105 static void
106 digraph (short **relation)
107 {
108 int i;
109
110 infinity = ngotos + 2;
111 INDEX = XCALLOC (short, ngotos + 1);
112 VERTICES = XCALLOC (short, ngotos + 1);
113 top = 0;
114
115 R = relation;
116
117 for (i = 0; i < ngotos; i++)
118 INDEX[i] = 0;
119
120 for (i = 0; i < ngotos; i++)
121 if (INDEX[i] == 0 && R[i])
122 traverse (i);
123
124 XFREE (INDEX);
125 XFREE (VERTICES);
126 }
127
128
129 /*--------------------.
130 | Build STATE_TABLE. |
131 `--------------------*/
132
133 static void
134 set_state_table (void)
135 {
136 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
137 might be used (see conflicts.c). It is too opaque for me to
138 provide a probably less hacky implementation. --akim */
139 state_table = XCALLOC (state_t, nstates + 1);
140
141 {
142 core *sp;
143 for (sp = first_state; sp; sp = sp->next)
144 {
145 state_table[sp->number].state = sp;
146 state_table[sp->number].accessing_symbol = sp->accessing_symbol;
147 }
148 }
149
150 {
151 shifts *sp;
152 for (sp = first_shift; sp; sp = sp->next)
153 state_table[sp->number].shift_table = sp;
154 }
155
156 {
157 reductions *rp;
158 for (rp = first_reduction; rp; rp = rp->next)
159 state_table[rp->number].reduction_table = rp;
160 }
161
162 /* Initializing the lookaheads members. Please note that it must be
163 performed after having set some of the other members which are
164 used below. Change with extreme caution. */
165 {
166 int i;
167 int count = 0;
168 for (i = 0; i < nstates; i++)
169 {
170 int k;
171 reductions *rp = state_table[i].reduction_table;
172 shifts *sp = state_table[i].shift_table;
173
174 state_table[i].lookaheads = count;
175
176 if (rp
177 && (rp->nreds > 1
178 || (sp && !ISVAR (state_table[sp->shifts[0]].accessing_symbol))))
179 count += rp->nreds;
180 else
181 state_table[i].consistent = 1;
182
183 if (sp)
184 for (k = 0; k < sp->nshifts; k++)
185 if (state_table[sp->shifts[k]].accessing_symbol
186 == error_token_number)
187 {
188 state_table[i].consistent = 0;
189 break;
190 }
191 }
192 state_table[nstates].lookaheads = count;
193 }
194 }
195
196
197 /*------------------------------------------.
198 | Return the size of the longest rule RHS. |
199 `------------------------------------------*/
200
201 static size_t
202 maxrhs (void)
203 {
204 short *itemp;
205 int length;
206 int max;
207
208 length = 0;
209 max = 0;
210 for (itemp = ritem; *itemp; itemp++)
211 {
212 if (*itemp > 0)
213 {
214 length++;
215 }
216 else
217 {
218 if (length > max)
219 max = length;
220 length = 0;
221 }
222 }
223
224 return max;
225 }
226
227
228 static void
229 initialize_LA (void)
230 {
231 int i;
232 int j;
233 short *np;
234 reductions *rp;
235
236 size_t nLA = state_table[nstates].lookaheads;
237 if (!nLA)
238 nLA = 1;
239
240 LA = XCALLOC (unsigned, nLA * tokensetsize);
241 LAruleno = XCALLOC (short, nLA);
242 lookback = XCALLOC (shorts *, nLA);
243
244 np = LAruleno;
245 for (i = 0; i < nstates; i++)
246 if (!state_table[i].consistent)
247 if ((rp = state_table[i].reduction_table))
248 for (j = 0; j < rp->nreds; j++)
249 *np++ = rp->rules[j];
250 }
251
252
253 static void
254 set_goto_map (void)
255 {
256 shifts *sp;
257 int i;
258 int symbol;
259 int k;
260 short *temp_map;
261 int state2;
262 int state1;
263
264 goto_map = XCALLOC (short, nvars + 1) - ntokens;
265 temp_map = XCALLOC (short, nvars + 1) - ntokens;
266
267 ngotos = 0;
268 for (sp = first_shift; sp; sp = sp->next)
269 {
270 for (i = sp->nshifts - 1; i >= 0; i--)
271 {
272 symbol = state_table[sp->shifts[i]].accessing_symbol;
273
274 if (ISTOKEN (symbol))
275 break;
276
277 if (ngotos == MAXSHORT)
278 fatal (_("too many gotos (max %d)"), MAXSHORT);
279
280 ngotos++;
281 goto_map[symbol]++;
282 }
283 }
284
285 k = 0;
286 for (i = ntokens; i < nsyms; i++)
287 {
288 temp_map[i] = k;
289 k += goto_map[i];
290 }
291
292 for (i = ntokens; i < nsyms; i++)
293 goto_map[i] = temp_map[i];
294
295 goto_map[nsyms] = ngotos;
296 temp_map[nsyms] = ngotos;
297
298 from_state = XCALLOC (short, ngotos);
299 to_state = XCALLOC (short, ngotos);
300
301 for (sp = first_shift; sp; sp = sp->next)
302 {
303 state1 = sp->number;
304 for (i = sp->nshifts - 1; i >= 0; i--)
305 {
306 state2 = sp->shifts[i];
307 symbol = state_table[state2].accessing_symbol;
308
309 if (ISTOKEN (symbol))
310 break;
311
312 k = temp_map[symbol]++;
313 from_state[k] = state1;
314 to_state[k] = state2;
315 }
316 }
317
318 XFREE (temp_map + ntokens);
319 }
320
321
322
323 /*----------------------------------------------------------.
324 | Map a state/symbol pair into its numeric representation. |
325 `----------------------------------------------------------*/
326
327 static int
328 map_goto (int state, int symbol)
329 {
330 int high;
331 int low;
332 int middle;
333 int s;
334
335 low = goto_map[symbol];
336 high = goto_map[symbol + 1] - 1;
337
338 while (low <= high)
339 {
340 middle = (low + high) / 2;
341 s = from_state[middle];
342 if (s == state)
343 return middle;
344 else if (s < state)
345 low = middle + 1;
346 else
347 high = middle - 1;
348 }
349
350 assert (0);
351 /* NOTREACHED */
352 return 0;
353 }
354
355
356 static void
357 initialize_F (void)
358 {
359 short **reads = XCALLOC (short *, ngotos);
360 short *edge = XCALLOC (short, ngotos + 1);
361 int nedges = 0;
362
363 int i;
364
365 F = XCALLOC (unsigned, ngotos * tokensetsize);
366
367 for (i = 0; i < ngotos; i++)
368 {
369 int stateno = to_state[i];
370 shifts *sp = state_table[stateno].shift_table;
371
372 if (sp)
373 {
374 int j;
375 for (j = 0; j < sp->nshifts; j++)
376 {
377 int symbol = state_table[sp->shifts[j]].accessing_symbol;
378 if (ISVAR (symbol))
379 break;
380 SETBIT (F + i * tokensetsize, symbol);
381 }
382
383 for (; j < sp->nshifts; j++)
384 {
385 int symbol = state_table[sp->shifts[j]].accessing_symbol;
386 if (nullable[symbol])
387 edge[nedges++] = map_goto (stateno, symbol);
388 }
389
390 if (nedges)
391 {
392 reads[i] = XCALLOC (short, nedges + 1);
393 shortcpy (reads[i], edge, nedges);
394 reads[i][nedges] = -1;
395 nedges = 0;
396 }
397 }
398 }
399
400 digraph (reads);
401
402 for (i = 0; i < ngotos; i++)
403 XFREE (reads[i]);
404
405 XFREE (reads);
406 XFREE (edge);
407 }
408
409
410 static void
411 add_lookback_edge (int stateno, int ruleno, int gotono)
412 {
413 int i;
414 int k;
415 int found;
416 shorts *sp;
417
418 i = state_table[stateno].lookaheads;
419 k = state_table[stateno + 1].lookaheads;
420 found = 0;
421 while (!found && i < k)
422 {
423 if (LAruleno[i] == ruleno)
424 found = 1;
425 else
426 i++;
427 }
428
429 assert (found);
430
431 sp = XCALLOC (shorts, 1);
432 sp->next = lookback[i];
433 sp->value = gotono;
434 lookback[i] = sp;
435 }
436
437
438 static short **
439 transpose (short **R_arg, int n)
440 {
441 short **new_R;
442 short **temp_R;
443 short *nedges;
444 int i;
445
446 nedges = XCALLOC (short, n);
447
448 for (i = 0; i < n; i++)
449 {
450 short *sp = R_arg[i];
451 if (sp)
452 {
453 while (*sp >= 0)
454 nedges[*sp++]++;
455 }
456 }
457
458 new_R = XCALLOC (short *, n);
459 temp_R = XCALLOC (short *, n);
460
461 for (i = 0; i < n; i++)
462 if (nedges[i] > 0)
463 {
464 short *sp = XCALLOC (short, nedges[i] + 1);
465 new_R[i] = sp;
466 temp_R[i] = sp;
467 sp[nedges[i]] = -1;
468 }
469
470 XFREE (nedges);
471
472 for (i = 0; i < n; i++)
473 {
474 short *sp = R_arg[i];
475 if (sp)
476 while (*sp >= 0)
477 *temp_R[*sp++]++ = i;
478 }
479
480 XFREE (temp_R);
481
482 return new_R;
483 }
484
485
486 static void
487 build_relations (void)
488 {
489 int i;
490 int j;
491 short *rulep;
492 short *rp;
493 int nedges;
494 int done;
495 int state1;
496 int stateno;
497 int symbol1;
498 short *edge;
499 short *states;
500 short **new_includes;
501
502 includes = XCALLOC (short *, ngotos);
503 edge = XCALLOC (short, ngotos + 1);
504 states = XCALLOC (short, maxrhs () + 1);
505
506 for (i = 0; i < ngotos; i++)
507 {
508 nedges = 0;
509 state1 = from_state[i];
510 symbol1 = state_table[to_state[i]].accessing_symbol;
511
512 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
513 {
514 int length = 1;
515 states[0] = state1;
516 stateno = state1;
517
518 for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++)
519 {
520 int symbol2 = *rp;
521 shifts *sp = state_table[stateno].shift_table;
522
523 for (j = 0; j < sp->nshifts; j++)
524 {
525 stateno = sp->shifts[j];
526 if (state_table[stateno].accessing_symbol == symbol2)
527 break;
528 }
529
530 states[length++] = stateno;
531 }
532
533 if (!state_table[stateno].consistent)
534 add_lookback_edge (stateno, *rulep, i);
535
536 length--;
537 done = 0;
538 while (!done)
539 {
540 done = 1;
541 rp--;
542 /* JF added rp>=ritem && I hope to god its right! */
543 if (rp >= ritem && ISVAR (*rp))
544 {
545 stateno = states[--length];
546 edge[nedges++] = map_goto (stateno, *rp);
547 if (nullable[*rp])
548 done = 0;
549 }
550 }
551 }
552
553 if (nedges)
554 {
555 includes[i] = XCALLOC (short, nedges + 1);
556 for (j = 0; j < nedges; j++)
557 includes[i][j] = edge[j];
558 includes[i][nedges] = -1;
559 }
560 }
561
562 new_includes = transpose (includes, ngotos);
563
564 for (i = 0; i < ngotos; i++)
565 XFREE (includes[i]);
566 XFREE (includes);
567
568 includes = new_includes;
569
570 XFREE (edge);
571 XFREE (states);
572 }
573
574
575
576 static void
577 compute_FOLLOWS (void)
578 {
579 int i;
580
581 digraph (includes);
582
583 for (i = 0; i < ngotos; i++)
584 XFREE (includes[i]);
585
586 XFREE (includes);
587 }
588
589
590 static void
591 compute_lookaheads (void)
592 {
593 int i;
594 shorts *sp;
595
596 for (i = 0; i < state_table[nstates].lookaheads; i++)
597 for (sp = lookback[i]; sp; sp = sp->next)
598 {
599 unsigned *fp1 = LA (i);
600 unsigned *fp2 = F (sp->value);
601 while (fp1 < LA (i + 1))
602 *fp1++ |= *fp2++;
603 }
604
605 /* Free LOOKBACK. */
606 for (i = 0; i < state_table[nstates].lookaheads; i++)
607 LIST_FREE (shorts, lookback[i]);
608
609 XFREE (lookback);
610 XFREE (F);
611 }
612
613
614 void
615 lalr (void)
616 {
617 tokensetsize = WORDSIZE (ntokens);
618
619 set_state_table ();
620 initialize_LA ();
621 set_goto_map ();
622 initialize_F ();
623 build_relations ();
624 compute_FOLLOWS ();
625 compute_lookaheads ();
626 }