]> git.saurik.com Git - bison.git/blob - src/lalr.c
* src/lalr.c (transpose): Free the memory allocated to the
[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 /*-------------------------------------------------------------------.
439 | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
440 | replaced with the result. |
441 `-------------------------------------------------------------------*/
442
443 static short **
444 transpose (short **R_arg, int n)
445 {
446 short **new_R;
447 short **temp_R;
448 short *nedges;
449 int i;
450
451 nedges = XCALLOC (short, n);
452
453 for (i = 0; i < n; i++)
454 {
455 short *sp = R_arg[i];
456 if (sp)
457 {
458 while (*sp >= 0)
459 nedges[*sp++]++;
460 }
461 }
462
463 new_R = XCALLOC (short *, n);
464 temp_R = XCALLOC (short *, n);
465
466 for (i = 0; i < n; i++)
467 if (nedges[i] > 0)
468 {
469 short *sp = XCALLOC (short, nedges[i] + 1);
470 new_R[i] = sp;
471 temp_R[i] = sp;
472 sp[nedges[i]] = -1;
473 }
474
475 XFREE (nedges);
476
477 for (i = 0; i < n; i++)
478 {
479 short *sp = R_arg[i];
480 if (sp)
481 while (*sp >= 0)
482 *temp_R[*sp++]++ = i;
483 }
484
485 XFREE (temp_R);
486
487 /* Free the input: it is replaced with the result. */
488 for (i = 0; i < n; i++)
489 XFREE (R_arg[i]);
490 XFREE (R_arg);
491
492 return new_R;
493 }
494
495
496 static void
497 build_relations (void)
498 {
499 short *edge = XCALLOC (short, ngotos + 1);
500 short *states = XCALLOC (short, maxrhs () + 1);
501 int i;
502
503 includes = XCALLOC (short *, ngotos);
504
505 for (i = 0; i < ngotos; i++)
506 {
507 int nedges = 0;
508 int state1 = from_state[i];
509 int symbol1 = state_table[to_state[i]].accessing_symbol;
510 short *rulep;
511
512 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
513 {
514 int done;
515 int length = 1;
516 int stateno = state1;
517 short *rp;
518 states[0] = state1;
519
520 for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++)
521 {
522 shifts *sp = state_table[stateno].shift_table;
523 int j;
524 for (j = 0; j < sp->nshifts; j++)
525 {
526 stateno = sp->shifts[j];
527 if (state_table[stateno].accessing_symbol == *rp)
528 break;
529 }
530
531 states[length++] = stateno;
532 }
533
534 if (!state_table[stateno].consistent)
535 add_lookback_edge (stateno, *rulep, i);
536
537 length--;
538 done = 0;
539 while (!done)
540 {
541 done = 1;
542 rp--;
543 /* JF added rp>=ritem && I hope to god its right! */
544 if (rp >= ritem && ISVAR (*rp))
545 {
546 stateno = states[--length];
547 edge[nedges++] = map_goto (stateno, *rp);
548 if (nullable[*rp])
549 done = 0;
550 }
551 }
552 }
553
554 if (nedges)
555 {
556 int j;
557 includes[i] = XCALLOC (short, nedges + 1);
558 for (j = 0; j < nedges; j++)
559 includes[i][j] = edge[j];
560 includes[i][nedges] = -1;
561 }
562 }
563
564 XFREE (edge);
565 XFREE (states);
566
567 includes = transpose (includes, ngotos);
568 }
569
570
571
572 static void
573 compute_FOLLOWS (void)
574 {
575 int i;
576
577 digraph (includes);
578
579 for (i = 0; i < ngotos; i++)
580 XFREE (includes[i]);
581
582 XFREE (includes);
583 }
584
585
586 static void
587 compute_lookaheads (void)
588 {
589 int i;
590 shorts *sp;
591
592 for (i = 0; i < state_table[nstates].lookaheads; i++)
593 for (sp = lookback[i]; sp; sp = sp->next)
594 {
595 int size = LA (i + 1) - LA (i);
596 int j;
597 for (j = 0; j < size; ++j)
598 LA (i)[j] |= F (sp->value)[j];
599 }
600
601 /* Free LOOKBACK. */
602 for (i = 0; i < state_table[nstates].lookaheads; i++)
603 LIST_FREE (shorts, lookback[i]);
604
605 XFREE (lookback);
606 XFREE (F);
607 }
608
609
610 void
611 lalr (void)
612 {
613 tokensetsize = WORDSIZE (ntokens);
614
615 set_state_table ();
616 initialize_LA ();
617 set_goto_map ();
618 initialize_F ();
619 build_relations ();
620 compute_FOLLOWS ();
621 compute_lookaheads ();
622 }