]> git.saurik.com Git - bison.git/blame - src/lalr.c
* src/lalr.c (transpose): Free the memory allocated to the
[bison.git] / src / lalr.c
CommitLineData
d0fb370f 1/* Compute look-ahead criteria for bison,
3feec034 2 Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc.
d0fb370f 3
340ef489 4 This file is part of Bison, the GNU Compiler Compiler.
d0fb370f 5
340ef489
AD
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.
d0fb370f 10
340ef489
AD
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.
d0fb370f 15
340ef489
AD
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. */
d0fb370f
RS
20
21
720d742f
AD
22/* Compute how to make the finite state machine deterministic; find
23 which rules need lookahead in each state, and which lookahead
340ef489 24 tokens they accept. */
d0fb370f 25
d0fb370f 26#include "system.h"
d0fb370f 27#include "types.h"
b2ca4022 28#include "LR0.h"
d0fb370f 29#include "gram.h"
a0f6b076 30#include "complain.h"
720d742f 31#include "lalr.h"
3519ec76 32#include "nullable.h"
340ef489 33#include "derives.h"
d0fb370f 34
9703cc49
AD
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 */
38state_t *state_table = NULL;
39
d0fb370f 40int tokensetsize;
d0fb370f
RS
41short *LAruleno;
42unsigned *LA;
9703cc49 43
d0fb370f
RS
44short *goto_map;
45short *from_state;
46short *to_state;
47
720d742f 48extern void berror PARAMS ((const char *));
d0fb370f
RS
49
50static int infinity;
d0fb370f 51static int ngotos;
ddcd5fdf 52
fe961097 53/* And for the famous F variable, which name is so descriptive that a
ddcd5fdf
AD
54 comment is hardly needed. <grin>. */
55static unsigned *F = NULL;
56#define F(Rule) (F + (Rule) * tokensetsize)
57
d0fb370f
RS
58static short **includes;
59static shorts **lookback;
60static short **R;
61static short *INDEX;
62static short *VERTICES;
63static int top;
64
65
720d742f
AD
66static void
67traverse (int i)
d0fb370f 68{
720d742f 69 int j;
fe961097 70 size_t k;
720d742f 71 int height;
fe961097 72 size_t size = F (i + 1) - F(i);
720d742f
AD
73
74 VERTICES[++top] = i;
75 INDEX[i] = height = top;
76
fe961097
AD
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]);
720d742f 82
fe961097
AD
83 if (INDEX[i] > INDEX[R[i][j]])
84 INDEX[i] = INDEX[R[i][j]];
720d742f 85
fe961097
AD
86 for (k = 0; k < size; ++k)
87 F (i)[k] |= F (R[i][j])[k];
88 }
720d742f
AD
89
90 if (INDEX[i] == height)
fe961097
AD
91 for (;;)
92 {
93 j = VERTICES[top--];
94 INDEX[j] = infinity;
720d742f 95
fe961097
AD
96 if (i == j)
97 break;
720d742f 98
fe961097
AD
99 for (k = 0; k < size; ++k)
100 F (i)[k] = F (j)[k];
101 }
d0fb370f
RS
102}
103
104
720d742f
AD
105static void
106digraph (short **relation)
107{
108 int i;
109
110 infinity = ngotos + 2;
d7913476
AD
111 INDEX = XCALLOC (short, ngotos + 1);
112 VERTICES = XCALLOC (short, ngotos + 1);
720d742f
AD
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++)
ddcd5fdf
AD
121 if (INDEX[i] == 0 && R[i])
122 traverse (i);
720d742f 123
d7913476
AD
124 XFREE (INDEX);
125 XFREE (VERTICES);
720d742f
AD
126}
127
bb527fc2
AD
128
129/*--------------------.
130| Build STATE_TABLE. |
131`--------------------*/
132
4a120d45 133static void
d2729d44 134set_state_table (void)
d0fb370f 135{
f004bf6a
AD
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);
d0fb370f 140
90b4416b
AD
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 }
a845a697
AD
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 }
d0fb370f
RS
194}
195
196
fe961097
AD
197/*------------------------------------------.
198| Return the size of the longest rule RHS. |
199`------------------------------------------*/
200
3feec034
AD
201static size_t
202maxrhs (void)
d0fb370f 203{
720d742f
AD
204 short *itemp;
205 int length;
206 int max;
d0fb370f
RS
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 {
720d742f
AD
218 if (length > max)
219 max = length;
d0fb370f
RS
220 length = 0;
221 }
222 }
223
3feec034 224 return max;
d0fb370f
RS
225}
226
227
4a120d45 228static void
d2729d44 229initialize_LA (void)
d0fb370f 230{
720d742f
AD
231 int i;
232 int j;
720d742f 233 short *np;
a845a697 234 reductions *rp;
d0fb370f 235
a845a697
AD
236 size_t nLA = state_table[nstates].lookaheads;
237 if (!nLA)
238 nLA = 1;
d0fb370f 239
a845a697
AD
240 LA = XCALLOC (unsigned, nLA * tokensetsize);
241 LAruleno = XCALLOC (short, nLA);
242 lookback = XCALLOC (shorts *, nLA);
d0fb370f
RS
243
244 np = LAruleno;
245 for (i = 0; i < nstates; i++)
a845a697
AD
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];
d0fb370f
RS
250}
251
252
4a120d45 253static void
d2729d44 254set_goto_map (void)
d0fb370f 255{
720d742f
AD
256 shifts *sp;
257 int i;
258 int symbol;
259 int k;
260 short *temp_map;
261 int state2;
262 int state1;
d0fb370f 263
d7913476
AD
264 goto_map = XCALLOC (short, nvars + 1) - ntokens;
265 temp_map = XCALLOC (short, nvars + 1) - ntokens;
d0fb370f
RS
266
267 ngotos = 0;
268 for (sp = first_shift; sp; sp = sp->next)
269 {
270 for (i = sp->nshifts - 1; i >= 0; i--)
271 {
9703cc49 272 symbol = state_table[sp->shifts[i]].accessing_symbol;
d0fb370f 273
720d742f
AD
274 if (ISTOKEN (symbol))
275 break;
d0fb370f
RS
276
277 if (ngotos == MAXSHORT)
a0f6b076 278 fatal (_("too many gotos (max %d)"), MAXSHORT);
d0fb370f
RS
279
280 ngotos++;
281 goto_map[symbol]++;
720d742f 282 }
d0fb370f
RS
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
d7913476
AD
298 from_state = XCALLOC (short, ngotos);
299 to_state = XCALLOC (short, ngotos);
d0fb370f
RS
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];
9703cc49 307 symbol = state_table[state2].accessing_symbol;
d0fb370f 308
720d742f
AD
309 if (ISTOKEN (symbol))
310 break;
d0fb370f
RS
311
312 k = temp_map[symbol]++;
313 from_state[k] = state1;
314 to_state[k] = state2;
315 }
316 }
317
d7913476 318 XFREE (temp_map + ntokens);
d0fb370f
RS
319}
320
321
322
43591cec
AD
323/*----------------------------------------------------------.
324| Map a state/symbol pair into its numeric representation. |
325`----------------------------------------------------------*/
d0fb370f 326
4a120d45 327static int
d2729d44 328map_goto (int state, int symbol)
d0fb370f 329{
720d742f
AD
330 int high;
331 int low;
332 int middle;
333 int s;
d0fb370f
RS
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)
36281465 343 return middle;
d0fb370f
RS
344 else if (s < state)
345 low = middle + 1;
346 else
347 high = middle - 1;
348 }
349
43591cec
AD
350 assert (0);
351 /* NOTREACHED */
d0fb370f
RS
352 return 0;
353}
354
355
4a120d45 356static void
d2729d44 357initialize_F (void)
d0fb370f 358{
4d4f699c
AD
359 short **reads = XCALLOC (short *, ngotos);
360 short *edge = XCALLOC (short, ngotos + 1);
361 int nedges = 0;
d0fb370f 362
4d4f699c 363 int i;
d0fb370f 364
4d4f699c 365 F = XCALLOC (unsigned, ngotos * tokensetsize);
d0fb370f 366
d0fb370f
RS
367 for (i = 0; i < ngotos; i++)
368 {
80a69750
AD
369 int stateno = to_state[i];
370 shifts *sp = state_table[stateno].shift_table;
d0fb370f
RS
371
372 if (sp)
373 {
4d4f699c 374 int j;
80a69750 375 for (j = 0; j < sp->nshifts; j++)
d0fb370f 376 {
4d4f699c 377 int symbol = state_table[sp->shifts[j]].accessing_symbol;
720d742f 378 if (ISVAR (symbol))
d0fb370f 379 break;
4d4f699c 380 SETBIT (F + i * tokensetsize, symbol);
d0fb370f
RS
381 }
382
80a69750 383 for (; j < sp->nshifts; j++)
d0fb370f 384 {
4d4f699c 385 int symbol = state_table[sp->shifts[j]].accessing_symbol;
d0fb370f 386 if (nullable[symbol])
720d742f 387 edge[nedges++] = map_goto (stateno, symbol);
d0fb370f 388 }
a0f6b076 389
d0fb370f
RS
390 if (nedges)
391 {
4d4f699c
AD
392 reads[i] = XCALLOC (short, nedges + 1);
393 shortcpy (reads[i], edge, nedges);
394 reads[i][nedges] = -1;
d0fb370f
RS
395 nedges = 0;
396 }
397 }
d0fb370f
RS
398 }
399
720d742f 400 digraph (reads);
d0fb370f
RS
401
402 for (i = 0; i < ngotos; i++)
ddcd5fdf 403 XFREE (reads[i]);
d0fb370f 404
d7913476
AD
405 XFREE (reads);
406 XFREE (edge);
d0fb370f
RS
407}
408
409
4a120d45 410static void
d2729d44 411add_lookback_edge (int stateno, int ruleno, int gotono)
d0fb370f 412{
720d742f
AD
413 int i;
414 int k;
415 int found;
416 shorts *sp;
d0fb370f 417
f004bf6a
AD
418 i = state_table[stateno].lookaheads;
419 k = state_table[stateno + 1].lookaheads;
d0fb370f
RS
420 found = 0;
421 while (!found && i < k)
422 {
423 if (LAruleno[i] == ruleno)
424 found = 1;
425 else
426 i++;
427 }
428
340ef489 429 assert (found);
d0fb370f 430
d7913476 431 sp = XCALLOC (shorts, 1);
d0fb370f
RS
432 sp->next = lookback[i];
433 sp->value = gotono;
434 lookback[i] = sp;
435}
436
437
9887c18a
AD
438/*-------------------------------------------------------------------.
439| Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
440| replaced with the result. |
441`-------------------------------------------------------------------*/
442
4a120d45 443static short **
d2729d44 444transpose (short **R_arg, int n)
d0fb370f 445{
720d742f
AD
446 short **new_R;
447 short **temp_R;
448 short *nedges;
720d742f 449 int i;
d0fb370f 450
d7913476 451 nedges = XCALLOC (short, n);
d0fb370f
RS
452
453 for (i = 0; i < n; i++)
454 {
80a69750 455 short *sp = R_arg[i];
d0fb370f
RS
456 if (sp)
457 {
458 while (*sp >= 0)
459 nedges[*sp++]++;
460 }
461 }
462
d7913476
AD
463 new_R = XCALLOC (short *, n);
464 temp_R = XCALLOC (short *, n);
d0fb370f
RS
465
466 for (i = 0; i < n; i++)
80a69750
AD
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 }
d0fb370f 474
d7913476 475 XFREE (nedges);
d0fb370f
RS
476
477 for (i = 0; i < n; i++)
478 {
80a69750 479 short *sp = R_arg[i];
d0fb370f 480 if (sp)
80a69750
AD
481 while (*sp >= 0)
482 *temp_R[*sp++]++ = i;
d0fb370f
RS
483 }
484
d7913476 485 XFREE (temp_R);
d0fb370f 486
9887c18a
AD
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
36281465 492 return new_R;
d0fb370f
RS
493}
494
495
4a120d45 496static void
720d742f 497build_relations (void)
d0fb370f 498{
9887c18a
AD
499 short *edge = XCALLOC (short, ngotos + 1);
500 short *states = XCALLOC (short, maxrhs () + 1);
720d742f 501 int i;
720d742f 502
d7913476 503 includes = XCALLOC (short *, ngotos);
d0fb370f
RS
504
505 for (i = 0; i < ngotos; i++)
506 {
9887c18a
AD
507 int nedges = 0;
508 int state1 = from_state[i];
509 int symbol1 = state_table[to_state[i]].accessing_symbol;
510 short *rulep;
d0fb370f 511
720d742f
AD
512 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
513 {
9887c18a 514 int done;
80a69750 515 int length = 1;
9887c18a
AD
516 int stateno = state1;
517 short *rp;
720d742f 518 states[0] = state1;
d0fb370f 519
b2ed6e58 520 for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++)
720d742f 521 {
80a69750 522 shifts *sp = state_table[stateno].shift_table;
9887c18a 523 int j;
80a69750 524 for (j = 0; j < sp->nshifts; j++)
720d742f
AD
525 {
526 stateno = sp->shifts[j];
9887c18a 527 if (state_table[stateno].accessing_symbol == *rp)
720d742f
AD
528 break;
529 }
d0fb370f 530
720d742f
AD
531 states[length++] = stateno;
532 }
533
de326cc0 534 if (!state_table[stateno].consistent)
720d742f
AD
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 }
d0fb370f
RS
552 }
553
720d742f
AD
554 if (nedges)
555 {
9887c18a 556 int j;
80a69750 557 includes[i] = XCALLOC (short, nedges + 1);
720d742f 558 for (j = 0; j < nedges; j++)
80a69750
AD
559 includes[i][j] = edge[j];
560 includes[i][nedges] = -1;
720d742f 561 }
d0fb370f
RS
562 }
563
d7913476
AD
564 XFREE (edge);
565 XFREE (states);
9887c18a
AD
566
567 includes = transpose (includes, ngotos);
d0fb370f
RS
568}
569
570
720d742f 571
4a120d45 572static void
720d742f 573compute_FOLLOWS (void)
d0fb370f 574{
720d742f 575 int i;
d0fb370f 576
720d742f 577 digraph (includes);
d0fb370f
RS
578
579 for (i = 0; i < ngotos; i++)
ddcd5fdf 580 XFREE (includes[i]);
d0fb370f 581
d7913476 582 XFREE (includes);
d0fb370f
RS
583}
584
585
4a120d45 586static void
720d742f 587compute_lookaheads (void)
d0fb370f 588{
720d742f 589 int i;
720d742f 590 shorts *sp;
d0fb370f 591
f004bf6a 592 for (i = 0; i < state_table[nstates].lookaheads; i++)
ddcd5fdf
AD
593 for (sp = lookback[i]; sp; sp = sp->next)
594 {
9887c18a
AD
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];
ddcd5fdf 599 }
d0fb370f 600
ddcd5fdf 601 /* Free LOOKBACK. */
f004bf6a 602 for (i = 0; i < state_table[nstates].lookaheads; i++)
300f275f 603 LIST_FREE (shorts, lookback[i]);
d0fb370f 604
d7913476
AD
605 XFREE (lookback);
606 XFREE (F);
720d742f 607}
d0fb370f 608
d0fb370f 609
720d742f
AD
610void
611lalr (void)
612{
613 tokensetsize = WORDSIZE (ntokens);
614
615 set_state_table ();
720d742f
AD
616 initialize_LA ();
617 set_goto_map ();
618 initialize_F ();
619 build_relations ();
620 compute_FOLLOWS ();
621 compute_lookaheads ();
d0fb370f 622}