]> git.saurik.com Git - bison.git/blame - src/lalr.c
* data/yacc.c: (b4_lex_param): Corrected for the case where
[bison.git] / src / lalr.c
CommitLineData
5362cd5f
PE
1/* Compute look-ahead criteria for Bison.
2
b0299a2e
AD
3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002
4 Free Software Foundation, Inc.
d0fb370f 5
340ef489 6 This file is part of Bison, the GNU Compiler Compiler.
d0fb370f 7
340ef489
AD
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
d0fb370f 12
340ef489
AD
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
d0fb370f 17
340ef489
AD
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
d0fb370f
RS
22
23
720d742f
AD
24/* Compute how to make the finite state machine deterministic; find
25 which rules need lookahead in each state, and which lookahead
340ef489 26 tokens they accept. */
d0fb370f 27
d0fb370f 28#include "system.h"
5dde5425
PE
29
30#include <bitset.h>
31#include <bitsetv.h>
32#include <quotearg.h>
33
b2ca4022 34#include "LR0.h"
a0f6b076 35#include "complain.h"
340ef489 36#include "derives.h"
f67d13aa 37#include "getargs.h"
5dde5425
PE
38#include "gram.h"
39#include "lalr.h"
40#include "nullable.h"
41#include "reader.h"
42#include "relation.h"
43#include "symtab.h"
d0fb370f 44
5dde5425
PE
45goto_number *goto_map = NULL;
46static goto_number ngotos = 0;
47state_number *from_state = NULL;
48state_number *to_state = NULL;
e68e0410
AD
49
50/* Linked list of goto numbers. */
5dde5425 51typedef struct goto_list
e68e0410 52{
5dde5425
PE
53 struct goto_list *next;
54 goto_number value;
55} goto_list;
e68e0410
AD
56
57
3325ddc4
AD
58/* LA is a LR by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
59 LArule[l] is applicable in the appropriate state when the next
60 token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
61 it is a conflict. */
62
63static bitsetv LA = NULL;
d200e455 64size_t nLA;
9703cc49 65
d0fb370f 66
fe961097 67/* And for the famous F variable, which name is so descriptive that a
ddcd5fdf 68 comment is hardly needed. <grin>. */
914feea9 69static bitsetv F = NULL;
ddcd5fdf 70
5dde5425
PE
71static goto_number **includes;
72static goto_list **lookback;
aa2aab3c
AD
73
74
720d742f 75
bb527fc2 76
4a120d45 77static void
d2729d44 78set_goto_map (void)
d0fb370f 79{
5dde5425
PE
80 state_number s;
81 goto_number *temp_map;
d0fb370f 82
5362cd5f
PE
83 CALLOC (goto_map, nvars + 1);
84 CALLOC (temp_map, nvars + 1);
d0fb370f
RS
85
86 ngotos = 0;
5dde5425 87 for (s = 0; s < nstates; ++s)
7215de24 88 {
5dde5425 89 transitions *sp = states[s]->transitions;
d57650a5 90 int i;
ccaf65bc 91 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
7215de24 92 {
5dde5425 93 if (ngotos >= GOTO_NUMBER_MAXIMUM)
58a84254 94 abort ();
7215de24 95 ngotos++;
5362cd5f 96 goto_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
7215de24
AD
97 }
98 }
d0fb370f 99
d0b0fefa
AD
100 {
101 int k = 0;
d57650a5 102 int i;
d0b0fefa
AD
103 for (i = ntokens; i < nsyms; i++)
104 {
5362cd5f
PE
105 temp_map[i - ntokens] = k;
106 k += goto_map[i - ntokens];
d0b0fefa 107 }
d0fb370f 108
d0b0fefa 109 for (i = ntokens; i < nsyms; i++)
5362cd5f 110 goto_map[i - ntokens] = temp_map[i - ntokens];
d0fb370f 111
5362cd5f
PE
112 goto_map[nsyms - ntokens] = ngotos;
113 temp_map[nsyms - ntokens] = ngotos;
d0b0fefa 114 }
d0fb370f 115
5362cd5f
PE
116 CALLOC (from_state, ngotos);
117 CALLOC (to_state, ngotos);
d0fb370f 118
5dde5425 119 for (s = 0; s < nstates; ++s)
d0fb370f 120 {
5dde5425 121 transitions *sp = states[s]->transitions;
d57650a5 122 int i;
ccaf65bc 123 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
d0fb370f 124 {
5362cd5f 125 int k = temp_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
5dde5425 126 from_state[k] = s;
640748ee 127 to_state[k] = sp->states[i]->number;
d0fb370f
RS
128 }
129 }
130
5362cd5f 131 XFREE (temp_map);
d0fb370f
RS
132}
133
134
135
43591cec
AD
136/*----------------------------------------------------------.
137| Map a state/symbol pair into its numeric representation. |
138`----------------------------------------------------------*/
d0fb370f 139
4a120d45 140static int
5dde5425 141map_goto (state_number s0, symbol_number sym)
d0fb370f 142{
720d742f
AD
143 int high;
144 int low;
145 int middle;
5dde5425 146 state_number s;
d0fb370f 147
5362cd5f
PE
148 low = goto_map[sym - ntokens];
149 high = goto_map[sym - ntokens + 1] - 1;
d0fb370f 150
58a84254 151 for (;;)
d0fb370f 152 {
58a84254
PE
153 if (high < low)
154 abort ();
d0fb370f
RS
155 middle = (low + high) / 2;
156 s = from_state[middle];
5dde5425 157 if (s == s0)
36281465 158 return middle;
5dde5425 159 else if (s < s0)
d0fb370f
RS
160 low = middle + 1;
161 else
162 high = middle - 1;
163 }
d0fb370f
RS
164}
165
166
4a120d45 167static void
d2729d44 168initialize_F (void)
d0fb370f 169{
5362cd5f
PE
170 goto_number **reads = CALLOC (reads, ngotos);
171 goto_number *edge = CALLOC (edge, ngotos + 1);
4d4f699c 172 int nedges = 0;
d0fb370f 173
4d4f699c 174 int i;
d0fb370f 175
914feea9 176 F = bitsetv_create (ngotos, ntokens, BITSET_FIXED);
d0fb370f 177
d0fb370f
RS
178 for (i = 0; i < ngotos; i++)
179 {
5dde5425
PE
180 state_number stateno = to_state[i];
181 transitions *sp = states[stateno]->transitions;
d0fb370f 182
d954473d 183 int j;
640748ee 184 FOR_EACH_SHIFT (sp, j)
ccaf65bc 185 bitset_set (F[i], TRANSITION_SYMBOL (sp, j));
d0fb370f 186
ccaf65bc 187 for (; j < sp->num; j++)
d954473d 188 {
5dde5425 189 symbol_number sym = TRANSITION_SYMBOL (sp, j);
5362cd5f 190 if (nullable[sym - ntokens])
5dde5425 191 edge[nedges++] = map_goto (stateno, sym);
d954473d 192 }
a0f6b076 193
d954473d
AD
194 if (nedges)
195 {
5362cd5f 196 CALLOC (reads[i], nedges + 1);
62a3e4f0 197 memcpy (reads[i], edge, nedges * sizeof (edge[0]));
d954473d
AD
198 reads[i][nedges] = -1;
199 nedges = 0;
d0fb370f 200 }
d0fb370f
RS
201 }
202
0e4d5753 203 relation_digraph (reads, ngotos, &F);
d0fb370f
RS
204
205 for (i = 0; i < ngotos; i++)
ddcd5fdf 206 XFREE (reads[i]);
d0fb370f 207
d7913476
AD
208 XFREE (reads);
209 XFREE (edge);
d0fb370f
RS
210}
211
212
4a120d45 213static void
5dde5425 214add_lookback_edge (state *s, rule *r, int gotono)
d0fb370f 215{
5dde5425 216 int ri = state_reduction_find (s, r);
5362cd5f 217 goto_list *sp = MALLOC (sp, 1);
5dde5425 218 sp->next = lookback[(s->reductions->lookaheads - LA) + ri];
d0fb370f 219 sp->value = gotono;
5dde5425 220 lookback[(s->reductions->lookaheads - LA) + ri] = sp;
d0fb370f
RS
221}
222
223
d0fb370f 224
4a120d45 225static void
720d742f 226build_relations (void)
d0fb370f 227{
5362cd5f
PE
228 goto_number *edge = CALLOC (edge, ngotos + 1);
229 state_number *states1 = CALLOC (states1, ritem_longest_rhs () + 1);
720d742f 230 int i;
720d742f 231
5362cd5f 232 CALLOC (includes, ngotos);
d0fb370f
RS
233
234 for (i = 0; i < ngotos; i++)
235 {
9887c18a 236 int nedges = 0;
5dde5425
PE
237 symbol_number symbol1 = states[to_state[i]]->accessing_symbol;
238 rule **rulep;
d0fb370f 239
5362cd5f 240 for (rulep = derives[symbol1 - ntokens]; *rulep; rulep++)
720d742f 241 {
9887c18a 242 int done;
80a69750 243 int length = 1;
5dde5425
PE
244 item_number *rp;
245 state *s = states[from_state[i]];
246 states1[0] = s->number;
d0fb370f 247
bb0027a9 248 for (rp = (*rulep)->rhs; *rp >= 0; rp++)
720d742f 249 {
5dde5425
PE
250 s = transitions_to (s->transitions,
251 item_number_as_symbol_number (*rp));
252 states1[length++] = s->number;
720d742f
AD
253 }
254
5dde5425
PE
255 if (!s->consistent)
256 add_lookback_edge (s, *rulep, i);
720d742f
AD
257
258 length--;
259 done = 0;
260 while (!done)
261 {
262 done = 1;
263 rp--;
264 /* JF added rp>=ritem && I hope to god its right! */
265 if (rp >= ritem && ISVAR (*rp))
266 {
5dde5425 267 /* Downcasting from item_number to symbol_number. */
5fbb0954 268 edge[nedges++] = map_goto (states1[--length],
a49aecd5 269 item_number_as_symbol_number (*rp));
5362cd5f 270 if (nullable[*rp - ntokens])
720d742f
AD
271 done = 0;
272 }
273 }
d0fb370f
RS
274 }
275
720d742f
AD
276 if (nedges)
277 {
9887c18a 278 int j;
5362cd5f 279 CALLOC (includes[i], nedges + 1);
720d742f 280 for (j = 0; j < nedges; j++)
80a69750
AD
281 includes[i][j] = edge[j];
282 includes[i][nedges] = -1;
720d742f 283 }
d0fb370f
RS
284 }
285
d7913476 286 XFREE (edge);
b9f71f19 287 XFREE (states1);
9887c18a 288
0e4d5753 289 relation_transpose (&includes, ngotos);
d0fb370f
RS
290}
291
292
720d742f 293
4a120d45 294static void
720d742f 295compute_FOLLOWS (void)
d0fb370f 296{
720d742f 297 int i;
d0fb370f 298
0e4d5753 299 relation_digraph (includes, ngotos, &F);
d0fb370f
RS
300
301 for (i = 0; i < ngotos; i++)
ddcd5fdf 302 XFREE (includes[i]);
d0fb370f 303
d7913476 304 XFREE (includes);
d0fb370f
RS
305}
306
307
4a120d45 308static void
720d742f 309compute_lookaheads (void)
d0fb370f 310{
d200e455 311 size_t i;
5dde5425 312 goto_list *sp;
d0fb370f 313
d200e455 314 for (i = 0; i < nLA; i++)
ddcd5fdf 315 for (sp = lookback[i]; sp; sp = sp->next)
0fb1ffb1 316 bitset_or (LA[i], LA[i], F[sp->value]);
d0fb370f 317
ddcd5fdf 318 /* Free LOOKBACK. */
d200e455 319 for (i = 0; i < nLA; i++)
5dde5425 320 LIST_FREE (goto_list, lookback[i]);
d0fb370f 321
d7913476 322 XFREE (lookback);
914feea9 323 bitsetv_free (F);
720d742f 324}
d0fb370f 325
d0fb370f 326
5dde5425
PE
327/*-----------------------------------------------------------.
328| Count the number of lookaheads required for S (NLOOKAHEADS |
329| member). |
330`-----------------------------------------------------------*/
5edafffd 331
cd08e51e 332static int
5dde5425 333state_lookaheads_count (state *s)
5edafffd 334{
cd08e51e
AD
335 int k;
336 int nlookaheads = 0;
5dde5425
PE
337 reductions *rp = s->reductions;
338 transitions *sp = s->transitions;
cd08e51e
AD
339
340 /* We need a lookahead either to distinguish different
341 reductions (i.e., there are two or more), or to distinguish a
342 reduction from a shift. Otherwise, it is straightforward,
343 and the state is `consistent'. */
344 if (rp->num > 1
345 || (rp->num == 1 && sp->num &&
346 !TRANSITION_IS_DISABLED (sp, 0) && TRANSITION_IS_SHIFT (sp, 0)))
347 nlookaheads += rp->num;
348 else
5dde5425 349 s->consistent = 1;
cd08e51e
AD
350
351 for (k = 0; k < sp->num; k++)
352 if (!TRANSITION_IS_DISABLED (sp, k) && TRANSITION_IS_ERROR (sp, k))
353 {
5dde5425 354 s->consistent = 0;
cd08e51e
AD
355 break;
356 }
3877f72b 357
cd08e51e 358 return nlookaheads;
5edafffd
AD
359}
360
7c6b64d0 361
cd08e51e
AD
362/*----------------------------------------------.
363| Compute LA, NLA, and the lookaheads members. |
364`----------------------------------------------*/
c0263492
AD
365
366static void
cd08e51e 367initialize_LA (void)
c0263492 368{
5dde5425 369 state_number i;
cd08e51e 370 bitsetv pLA;
c0263492 371
cd08e51e
AD
372 /* Compute the total number of reductions requiring a lookahead. */
373 nLA = 0;
374 for (i = 0; i < nstates; i++)
375 nLA += state_lookaheads_count (states[i]);
376 /* Avoid having to special case 0. */
377 if (!nLA)
378 nLA = 1;
379
380 pLA = LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
5362cd5f 381 CALLOC (lookback, nLA);
cd08e51e
AD
382
383 /* Initialize the members LOOKAHEADS for each state which reductions
384 require lookaheads. */
c0263492
AD
385 for (i = 0; i < nstates; i++)
386 {
cd08e51e
AD
387 int count = state_lookaheads_count (states[i]);
388 if (count)
389 {
390 states[i]->reductions->lookaheads = pLA;
391 pLA += count;
392 }
c0263492
AD
393 }
394}
395
396
7c6b64d0
AD
397/*---------------------------------------.
398| Output the lookaheads for each state. |
399`---------------------------------------*/
400
401static void
402lookaheads_print (FILE *out)
403{
5dde5425 404 state_number i;
602bbf31 405 int j, k;
427c0dda 406 fprintf (out, "Lookaheads: BEGIN\n");
7c6b64d0
AD
407 for (i = 0; i < nstates; ++i)
408 {
5dde5425 409 reductions *reds = states[i]->reductions;
613f5e1a 410 bitset_iterator iter;
cd08e51e
AD
411 int nlookaheads = 0;
412
413 if (reds->lookaheads)
414 for (k = 0; k < reds->num; ++k)
415 if (reds->lookaheads[k])
416 ++nlookaheads;
613f5e1a 417
427c0dda 418 fprintf (out, "State %d: %d lookaheads\n",
cd08e51e 419 i, nlookaheads);
7c6b64d0 420
cd08e51e
AD
421 if (reds->lookaheads)
422 for (j = 0; j < reds->num; ++j)
423 BITSET_FOR_EACH (iter, reds->lookaheads[j], k, 0)
424 {
427c0dda 425 fprintf (out, " on %d (%s) -> rule %d\n",
cd08e51e
AD
426 k, symbols[k]->tag,
427 reds->rules[j]->number);
428 };
7c6b64d0 429 }
427c0dda 430 fprintf (out, "Lookaheads: END\n");
7c6b64d0
AD
431}
432
720d742f
AD
433void
434lalr (void)
435{
720d742f
AD
436 initialize_LA ();
437 set_goto_map ();
438 initialize_F ();
439 build_relations ();
440 compute_FOLLOWS ();
441 compute_lookaheads ();
7c6b64d0 442
273a74fa 443 if (trace_flag & trace_sets)
7c6b64d0 444 lookaheads_print (stderr);
d0fb370f 445}
3325ddc4
AD
446
447
448void
449lalr_free (void)
450{
5dde5425 451 state_number s;
3325ddc4 452 for (s = 0; s < nstates; ++s)
cd08e51e 453 states[s]->reductions->lookaheads = NULL;
3325ddc4 454 bitsetv_free (LA);
3325ddc4 455}