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