]> git.saurik.com Git - bison.git/blame - src/lalr.c
Rename lr.default_reductions to lr.default-reductions.
[bison.git] / src / lalr.c
CommitLineData
742e4900 1/* Compute lookahead criteria for Bison.
5362cd5f 2
e9ad4aec 3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2003, 2004, 2005,
7254f6a8 4 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
d0fb370f 5
340ef489 6 This file is part of Bison, the GNU Compiler Compiler.
d0fb370f 7
f16b0819 8 This program is free software: you can redistribute it and/or modify
340ef489 9 it under the terms of the GNU General Public License as published by
f16b0819
PE
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
d0fb370f 12
f16b0819 13 This program is distributed in the hope that it will be useful,
340ef489
AD
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 18 You should have received a copy of the GNU General Public License
f16b0819 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d0fb370f
RS
20
21
720d742f 22/* Compute how to make the finite state machine deterministic; find
742e4900 23 which rules need lookahead in each state, and which lookahead
340ef489 24 tokens they accept. */
d0fb370f 25
2cec9080 26#include <config.h>
d0fb370f 27#include "system.h"
5dde5425
PE
28
29#include <bitset.h>
30#include <bitsetv.h>
31#include <quotearg.h>
32
b2ca4022 33#include "LR0.h"
a0f6b076 34#include "complain.h"
340ef489 35#include "derives.h"
f67d13aa 36#include "getargs.h"
5dde5425
PE
37#include "gram.h"
38#include "lalr.h"
7254f6a8 39#include "muscle-tab.h"
5dde5425
PE
40#include "nullable.h"
41#include "reader.h"
42#include "relation.h"
43#include "symtab.h"
d0fb370f 44
4cf44c00 45goto_number *goto_map;
db34f798 46goto_number ngotos;
4cf44c00
PE
47state_number *from_state;
48state_number *to_state;
db34f798 49bitsetv goto_follows = NULL;
e68e0410
AD
50
51/* Linked list of goto numbers. */
5dde5425 52typedef struct goto_list
e68e0410 53{
5dde5425
PE
54 struct goto_list *next;
55 goto_number value;
56} goto_list;
e68e0410
AD
57
58
b09f4f48 59/* LA is an NLA by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
3325ddc4
AD
60 LArule[l] is applicable in the appropriate state when the next
61 token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
62 it is a conflict. */
63
64static bitsetv LA = NULL;
d200e455 65size_t nLA;
9703cc49 66
d0fb370f 67
5dde5425
PE
68static goto_number **includes;
69static goto_list **lookback;
aa2aab3c
AD
70
71
720d742f 72
bb527fc2 73
db34f798 74void
d2729d44 75set_goto_map (void)
d0fb370f 76{
5dde5425
PE
77 state_number s;
78 goto_number *temp_map;
d0fb370f 79
da2a7671
PE
80 goto_map = xcalloc (nvars + 1, sizeof *goto_map);
81 temp_map = xnmalloc (nvars + 1, sizeof *temp_map);
d0fb370f
RS
82
83 ngotos = 0;
5dde5425 84 for (s = 0; s < nstates; ++s)
7215de24 85 {
5dde5425 86 transitions *sp = states[s]->transitions;
d57650a5 87 int i;
ccaf65bc 88 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
7215de24 89 {
7215de24 90 ngotos++;
60e3ecc7
PE
91
92 /* Abort if (ngotos + 1) would overflow. */
4f82b42a 93 aver (ngotos != GOTO_NUMBER_MAXIMUM);
60e3ecc7 94
5362cd5f 95 goto_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
7215de24
AD
96 }
97 }
d0fb370f 98
d0b0fefa 99 {
60e3ecc7 100 goto_number k = 0;
d57650a5 101 int i;
d0b0fefa
AD
102 for (i = ntokens; i < nsyms; i++)
103 {
5362cd5f
PE
104 temp_map[i - ntokens] = k;
105 k += goto_map[i - ntokens];
d0b0fefa 106 }
d0fb370f 107
d0b0fefa 108 for (i = ntokens; i < nsyms; i++)
5362cd5f 109 goto_map[i - ntokens] = temp_map[i - ntokens];
d0fb370f 110
5362cd5f
PE
111 goto_map[nsyms - ntokens] = ngotos;
112 temp_map[nsyms - ntokens] = ngotos;
d0b0fefa 113 }
d0fb370f 114
da2a7671
PE
115 from_state = xcalloc (ngotos, sizeof *from_state);
116 to_state = xcalloc (ngotos, sizeof *to_state);
d0fb370f 117
5dde5425 118 for (s = 0; s < nstates; ++s)
d0fb370f 119 {
5dde5425 120 transitions *sp = states[s]->transitions;
d57650a5 121 int i;
ccaf65bc 122 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
d0fb370f 123 {
60e3ecc7 124 goto_number k = temp_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
5dde5425 125 from_state[k] = s;
640748ee 126 to_state[k] = sp->states[i]->number;
d0fb370f
RS
127 }
128 }
129
afbb696d 130 free (temp_map);
d0fb370f
RS
131}
132
133
db34f798 134goto_number
5dde5425 135map_goto (state_number s0, symbol_number sym)
d0fb370f 136{
60e3ecc7
PE
137 goto_number high;
138 goto_number low;
139 goto_number middle;
5dde5425 140 state_number s;
d0fb370f 141
5362cd5f
PE
142 low = goto_map[sym - ntokens];
143 high = goto_map[sym - ntokens + 1] - 1;
d0fb370f 144
58a84254 145 for (;;)
d0fb370f 146 {
4f82b42a 147 aver (low <= high);
d0fb370f
RS
148 middle = (low + high) / 2;
149 s = from_state[middle];
5dde5425 150 if (s == s0)
36281465 151 return middle;
5dde5425 152 else if (s < s0)
d0fb370f
RS
153 low = middle + 1;
154 else
155 high = middle - 1;
156 }
d0fb370f
RS
157}
158
159
4a120d45 160static void
d2729d44 161initialize_F (void)
d0fb370f 162{
da2a7671
PE
163 goto_number **reads = xnmalloc (ngotos, sizeof *reads);
164 goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
60e3ecc7 165 goto_number nedges = 0;
d0fb370f 166
60e3ecc7 167 goto_number i;
d0fb370f 168
db34f798 169 goto_follows = bitsetv_create (ngotos, ntokens, BITSET_FIXED);
d0fb370f 170
d0fb370f
RS
171 for (i = 0; i < ngotos; i++)
172 {
5dde5425
PE
173 state_number stateno = to_state[i];
174 transitions *sp = states[stateno]->transitions;
d0fb370f 175
d954473d 176 int j;
640748ee 177 FOR_EACH_SHIFT (sp, j)
db34f798 178 bitset_set (goto_follows[i], TRANSITION_SYMBOL (sp, j));
d0fb370f 179
ccaf65bc 180 for (; j < sp->num; j++)
d954473d 181 {
5dde5425 182 symbol_number sym = TRANSITION_SYMBOL (sp, j);
5362cd5f 183 if (nullable[sym - ntokens])
5dde5425 184 edge[nedges++] = map_goto (stateno, sym);
d954473d 185 }
a0f6b076 186
da2a7671
PE
187 if (nedges == 0)
188 reads[i] = NULL;
189 else
d954473d 190 {
da2a7671
PE
191 reads[i] = xnmalloc (nedges + 1, sizeof reads[i][0]);
192 memcpy (reads[i], edge, nedges * sizeof edge[0]);
60e3ecc7 193 reads[i][nedges] = END_NODE;
d954473d 194 nedges = 0;
d0fb370f 195 }
d0fb370f
RS
196 }
197
db34f798 198 relation_digraph (reads, ngotos, &goto_follows);
d0fb370f
RS
199
200 for (i = 0; i < ngotos; i++)
afbb696d 201 free (reads[i]);
d0fb370f 202
afbb696d
PE
203 free (reads);
204 free (edge);
d0fb370f
RS
205}
206
207
4a120d45 208static void
60e3ecc7 209add_lookback_edge (state *s, rule *r, goto_number gotono)
d0fb370f 210{
5dde5425 211 int ri = state_reduction_find (s, r);
da2a7671 212 goto_list *sp = xmalloc (sizeof *sp);
742e4900 213 sp->next = lookback[(s->reductions->lookahead_tokens - LA) + ri];
d0fb370f 214 sp->value = gotono;
742e4900 215 lookback[(s->reductions->lookahead_tokens - LA) + ri] = sp;
d0fb370f
RS
216}
217
218
d0fb370f 219
4a120d45 220static void
720d742f 221build_relations (void)
d0fb370f 222{
da2a7671
PE
223 goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
224 state_number *states1 = xnmalloc (ritem_longest_rhs () + 1, sizeof *states1);
60e3ecc7 225 goto_number i;
720d742f 226
da2a7671 227 includes = xnmalloc (ngotos, sizeof *includes);
d0fb370f
RS
228
229 for (i = 0; i < ngotos; i++)
230 {
9887c18a 231 int nedges = 0;
5dde5425
PE
232 symbol_number symbol1 = states[to_state[i]]->accessing_symbol;
233 rule **rulep;
d0fb370f 234
5362cd5f 235 for (rulep = derives[symbol1 - ntokens]; *rulep; rulep++)
720d742f 236 {
d0829076 237 bool done;
80a69750 238 int length = 1;
e9ad4aec 239 item_number const *rp;
5dde5425
PE
240 state *s = states[from_state[i]];
241 states1[0] = s->number;
d0fb370f 242
e9ad4aec 243 for (rp = (*rulep)->rhs; ! item_number_is_rule_number (*rp); rp++)
720d742f 244 {
5dde5425
PE
245 s = transitions_to (s->transitions,
246 item_number_as_symbol_number (*rp));
247 states1[length++] = s->number;
720d742f
AD
248 }
249
5dde5425
PE
250 if (!s->consistent)
251 add_lookback_edge (s, *rulep, i);
720d742f
AD
252
253 length--;
d0829076 254 done = false;
720d742f
AD
255 while (!done)
256 {
d0829076 257 done = true;
fa6aa7b3 258 /* Each rhs ends in a rule number, and there is a
db34f798 259 sentinel (ritem[-1]=0) before the first rhs, so it is safe to
e9ad4aec 260 decrement RP here. */
720d742f 261 rp--;
e9ad4aec 262 if (ISVAR (*rp))
720d742f 263 {
5dde5425 264 /* Downcasting from item_number to symbol_number. */
5fbb0954 265 edge[nedges++] = map_goto (states1[--length],
a49aecd5 266 item_number_as_symbol_number (*rp));
5362cd5f 267 if (nullable[*rp - ntokens])
d0829076 268 done = false;
720d742f
AD
269 }
270 }
d0fb370f
RS
271 }
272
da2a7671
PE
273 if (nedges == 0)
274 includes[i] = NULL;
275 else
720d742f 276 {
9887c18a 277 int j;
da2a7671 278 includes[i] = xnmalloc (nedges + 1, sizeof includes[i][0]);
720d742f 279 for (j = 0; j < nedges; j++)
80a69750 280 includes[i][j] = edge[j];
60e3ecc7 281 includes[i][nedges] = END_NODE;
720d742f 282 }
d0fb370f
RS
283 }
284
afbb696d
PE
285 free (edge);
286 free (states1);
9887c18a 287
0e4d5753 288 relation_transpose (&includes, ngotos);
d0fb370f
RS
289}
290
291
720d742f 292
4a120d45 293static void
720d742f 294compute_FOLLOWS (void)
d0fb370f 295{
60e3ecc7 296 goto_number i;
d0fb370f 297
db34f798 298 relation_digraph (includes, ngotos, &goto_follows);
d0fb370f
RS
299
300 for (i = 0; i < ngotos; i++)
afbb696d 301 free (includes[i]);
d0fb370f 302
afbb696d 303 free (includes);
d0fb370f
RS
304}
305
306
4a120d45 307static void
742e4900 308compute_lookahead_tokens (void)
d0fb370f 309{
d200e455 310 size_t i;
5dde5425 311 goto_list *sp;
d0fb370f 312
d200e455 313 for (i = 0; i < nLA; i++)
ddcd5fdf 314 for (sp = lookback[i]; sp; sp = sp->next)
db34f798 315 bitset_or (LA[i], LA[i], goto_follows[sp->value]);
d0fb370f 316
ddcd5fdf 317 /* Free LOOKBACK. */
d200e455 318 for (i = 0; i < nLA; i++)
5dde5425 319 LIST_FREE (goto_list, lookback[i]);
d0fb370f 320
afbb696d 321 free (lookback);
720d742f 322}
d0fb370f 323
d0fb370f 324
742e4900
JD
325/*----------------------------------------------------.
326| Count the number of lookahead tokens required for S |
327| (N_LOOKAHEAD_TOKENS member). |
328`----------------------------------------------------*/
5edafffd 329
cd08e51e 330static int
110ef36a 331state_lookahead_tokens_count (state *s, bool default_reduction_only_for_accept)
5edafffd 332{
742e4900 333 int n_lookahead_tokens = 0;
5dde5425
PE
334 reductions *rp = s->reductions;
335 transitions *sp = s->transitions;
cd08e51e 336
7254f6a8
JD
337 /* Transitions are only disabled during conflict resolution, and that
338 hasn't happened yet, so there should be no need to check that
339 transition 0 hasn't been disabled before checking if it is a shift.
340 However, this check was performed at one time, so we leave it as an
341 aver. */
2a6b783d 342 aver (sp->num == 0 || !TRANSITION_IS_DISABLED (sp, 0));
7254f6a8
JD
343
344 /* We need a lookahead either to distinguish different reductions
345 (i.e., there are two or more), or to distinguish a reduction from a
346 shift. Otherwise, it is straightforward, and the state is
110ef36a
JD
347 `consistent'. However, treat only the accepting state as
348 consistent (because there is never a lookahead token that makes
349 sense there, and so no lookahead token should be read) if the user
350 has otherwise disabled default reductions. */
cd08e51e 351 if (rp->num > 1
7254f6a8
JD
352 || (rp->num == 1 && sp->num && TRANSITION_IS_SHIFT (sp, 0))
353 || (rp->num == 1 && rp->rules[0]->number != 0
110ef36a 354 && default_reduction_only_for_accept))
742e4900 355 n_lookahead_tokens += rp->num;
cd08e51e 356 else
5dde5425 357 s->consistent = 1;
cd08e51e 358
742e4900 359 return n_lookahead_tokens;
5edafffd
AD
360}
361
7c6b64d0 362
742e4900
JD
363/*----------------------------------------------------.
364| Compute LA, NLA, and the lookahead_tokens members. |
365`----------------------------------------------------*/
c0263492 366
db34f798 367void
cd08e51e 368initialize_LA (void)
c0263492 369{
5dde5425 370 state_number i;
cd08e51e 371 bitsetv pLA;
110ef36a 372 bool default_reduction_only_for_accept;
7254f6a8 373 {
110ef36a 374 char *default_reductions =
5bab9d08 375 muscle_percent_define_get ("lr.default-reductions");
110ef36a
JD
376 default_reduction_only_for_accept =
377 0 == strcmp (default_reductions, "accepting");
378 free (default_reductions);
7254f6a8 379 }
c0263492 380
742e4900 381 /* Compute the total number of reductions requiring a lookahead. */
cd08e51e
AD
382 nLA = 0;
383 for (i = 0; i < nstates; i++)
7254f6a8 384 nLA +=
110ef36a
JD
385 state_lookahead_tokens_count (states[i],
386 default_reduction_only_for_accept);
cd08e51e
AD
387 /* Avoid having to special case 0. */
388 if (!nLA)
389 nLA = 1;
390
391 pLA = LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
cd08e51e 392
742e4900
JD
393 /* Initialize the members LOOKAHEAD_TOKENS for each state whose reductions
394 require lookahead tokens. */
c0263492
AD
395 for (i = 0; i < nstates; i++)
396 {
7254f6a8 397 int count =
110ef36a
JD
398 state_lookahead_tokens_count (states[i],
399 default_reduction_only_for_accept);
cd08e51e
AD
400 if (count)
401 {
742e4900 402 states[i]->reductions->lookahead_tokens = pLA;
cd08e51e
AD
403 pLA += count;
404 }
c0263492
AD
405 }
406}
407
408
742e4900
JD
409/*---------------------------------------------.
410| Output the lookahead tokens for each state. |
411`---------------------------------------------*/
7c6b64d0
AD
412
413static void
742e4900 414lookahead_tokens_print (FILE *out)
7c6b64d0 415{
5dde5425 416 state_number i;
602bbf31 417 int j, k;
742e4900 418 fprintf (out, "Lookahead tokens: BEGIN\n");
7c6b64d0
AD
419 for (i = 0; i < nstates; ++i)
420 {
5dde5425 421 reductions *reds = states[i]->reductions;
613f5e1a 422 bitset_iterator iter;
742e4900 423 int n_lookahead_tokens = 0;
cd08e51e 424
742e4900 425 if (reds->lookahead_tokens)
cd08e51e 426 for (k = 0; k < reds->num; ++k)
742e4900
JD
427 if (reds->lookahead_tokens[k])
428 ++n_lookahead_tokens;
613f5e1a 429
742e4900
JD
430 fprintf (out, "State %d: %d lookahead tokens\n",
431 i, n_lookahead_tokens);
7c6b64d0 432
742e4900 433 if (reds->lookahead_tokens)
cd08e51e 434 for (j = 0; j < reds->num; ++j)
742e4900 435 BITSET_FOR_EACH (iter, reds->lookahead_tokens[j], k, 0)
cd08e51e 436 {
427c0dda 437 fprintf (out, " on %d (%s) -> rule %d\n",
cd08e51e
AD
438 k, symbols[k]->tag,
439 reds->rules[j]->number);
440 };
7c6b64d0 441 }
742e4900 442 fprintf (out, "Lookahead tokens: END\n");
7c6b64d0
AD
443}
444
720d742f
AD
445void
446lalr (void)
447{
720d742f
AD
448 initialize_LA ();
449 set_goto_map ();
450 initialize_F ();
db34f798 451 lookback = xcalloc (nLA, sizeof *lookback);
720d742f
AD
452 build_relations ();
453 compute_FOLLOWS ();
742e4900 454 compute_lookahead_tokens ();
7c6b64d0 455
273a74fa 456 if (trace_flag & trace_sets)
742e4900 457 lookahead_tokens_print (stderr);
d0fb370f 458}
3325ddc4
AD
459
460
5967f0cf
JD
461void
462lalr_update_state_numbers (state_number old_to_new[], state_number nstates_old)
463{
464 goto_number ngotos_reachable = 0;
465 symbol_number nonterminal = 0;
466 aver (nsyms == nvars + ntokens);
14462c2b
JD
467 {
468 goto_number i;
469 for (i = 0; i < ngotos; ++i)
470 {
471 while (i == goto_map[nonterminal])
472 goto_map[nonterminal++] = ngotos_reachable;
473 /* If old_to_new[from_state[i]] = nstates_old, remove this goto
474 entry. */
475 if (old_to_new[from_state[i]] != nstates_old)
476 {
477 /* from_state[i] is not removed, so it and thus to_state[i] are
478 reachable, so to_state[i] != nstates_old. */
479 aver (old_to_new[to_state[i]] != nstates_old);
480 from_state[ngotos_reachable] = old_to_new[from_state[i]];
481 to_state[ngotos_reachable] = old_to_new[to_state[i]];
482 ++ngotos_reachable;
483 }
484 }
485 }
5967f0cf
JD
486 while (nonterminal <= nvars)
487 {
488 aver (ngotos == goto_map[nonterminal]);
489 goto_map[nonterminal++] = ngotos_reachable;
490 }
491 ngotos = ngotos_reachable;
492}
493
494
3325ddc4
AD
495void
496lalr_free (void)
497{
5dde5425 498 state_number s;
3325ddc4 499 for (s = 0; s < nstates; ++s)
742e4900 500 states[s]->reductions->lookahead_tokens = NULL;
3325ddc4 501 bitsetv_free (LA);
3325ddc4 502}