]> git.saurik.com Git - bison.git/blame - src/lalr.c
tests: enhance AT_SYNCLINES_COMPILE.
[bison.git] / src / lalr.c
CommitLineData
742e4900 1/* Compute lookahead criteria for Bison.
5362cd5f 2
e141f4d4
JD
3 Copyright (C) 1984, 1986, 1989, 2000-2010 Free Software Foundation,
4 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
fca9c5ef 22/* Find which rules need lookahead in each state, and which lookahead
340ef489 23 tokens they accept. */
d0fb370f 24
2cec9080 25#include <config.h>
d0fb370f 26#include "system.h"
5dde5425
PE
27
28#include <bitset.h>
29#include <bitsetv.h>
30#include <quotearg.h>
31
b2ca4022 32#include "LR0.h"
a0f6b076 33#include "complain.h"
340ef489 34#include "derives.h"
f67d13aa 35#include "getargs.h"
5dde5425
PE
36#include "gram.h"
37#include "lalr.h"
7254f6a8 38#include "muscle-tab.h"
5dde5425
PE
39#include "nullable.h"
40#include "reader.h"
41#include "relation.h"
42#include "symtab.h"
d0fb370f 43
4cf44c00 44goto_number *goto_map;
db34f798 45goto_number ngotos;
4cf44c00
PE
46state_number *from_state;
47state_number *to_state;
db34f798 48bitsetv goto_follows = 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
b09f4f48 58/* LA is an NLA by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
3325ddc4
AD
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
5dde5425
PE
67static goto_number **includes;
68static goto_list **lookback;
aa2aab3c
AD
69
70
720d742f 71
bb527fc2 72
db34f798 73void
d2729d44 74set_goto_map (void)
d0fb370f 75{
5dde5425
PE
76 state_number s;
77 goto_number *temp_map;
d0fb370f 78
da2a7671
PE
79 goto_map = xcalloc (nvars + 1, sizeof *goto_map);
80 temp_map = xnmalloc (nvars + 1, sizeof *temp_map);
d0fb370f
RS
81
82 ngotos = 0;
5dde5425 83 for (s = 0; s < nstates; ++s)
7215de24 84 {
5dde5425 85 transitions *sp = states[s]->transitions;
d57650a5 86 int i;
ccaf65bc 87 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
7215de24 88 {
7215de24 89 ngotos++;
60e3ecc7
PE
90
91 /* Abort if (ngotos + 1) would overflow. */
4f82b42a 92 aver (ngotos != GOTO_NUMBER_MAXIMUM);
60e3ecc7 93
5362cd5f 94 goto_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
7215de24
AD
95 }
96 }
d0fb370f 97
d0b0fefa 98 {
60e3ecc7 99 goto_number k = 0;
d57650a5 100 int i;
d0b0fefa
AD
101 for (i = ntokens; i < nsyms; i++)
102 {
5362cd5f
PE
103 temp_map[i - ntokens] = k;
104 k += goto_map[i - ntokens];
d0b0fefa 105 }
d0fb370f 106
d0b0fefa 107 for (i = ntokens; i < nsyms; i++)
5362cd5f 108 goto_map[i - ntokens] = temp_map[i - ntokens];
d0fb370f 109
5362cd5f
PE
110 goto_map[nsyms - ntokens] = ngotos;
111 temp_map[nsyms - ntokens] = ngotos;
d0b0fefa 112 }
d0fb370f 113
da2a7671
PE
114 from_state = xcalloc (ngotos, sizeof *from_state);
115 to_state = xcalloc (ngotos, sizeof *to_state);
d0fb370f 116
5dde5425 117 for (s = 0; s < nstates; ++s)
d0fb370f 118 {
5dde5425 119 transitions *sp = states[s]->transitions;
d57650a5 120 int i;
ccaf65bc 121 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
d0fb370f 122 {
60e3ecc7 123 goto_number k = temp_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
5dde5425 124 from_state[k] = s;
640748ee 125 to_state[k] = sp->states[i]->number;
d0fb370f
RS
126 }
127 }
128
afbb696d 129 free (temp_map);
d0fb370f
RS
130}
131
132
db34f798 133goto_number
5dde5425 134map_goto (state_number s0, symbol_number sym)
d0fb370f 135{
60e3ecc7
PE
136 goto_number high;
137 goto_number low;
138 goto_number middle;
5dde5425 139 state_number s;
d0fb370f 140
5362cd5f
PE
141 low = goto_map[sym - ntokens];
142 high = goto_map[sym - ntokens + 1] - 1;
d0fb370f 143
58a84254 144 for (;;)
d0fb370f 145 {
4f82b42a 146 aver (low <= high);
d0fb370f
RS
147 middle = (low + high) / 2;
148 s = from_state[middle];
5dde5425 149 if (s == s0)
36281465 150 return middle;
5dde5425 151 else if (s < s0)
d0fb370f
RS
152 low = middle + 1;
153 else
154 high = middle - 1;
155 }
d0fb370f
RS
156}
157
158
4a120d45 159static void
d2729d44 160initialize_F (void)
d0fb370f 161{
da2a7671
PE
162 goto_number **reads = xnmalloc (ngotos, sizeof *reads);
163 goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
60e3ecc7 164 goto_number nedges = 0;
d0fb370f 165
60e3ecc7 166 goto_number i;
d0fb370f 167
db34f798 168 goto_follows = bitsetv_create (ngotos, ntokens, BITSET_FIXED);
d0fb370f 169
d0fb370f
RS
170 for (i = 0; i < ngotos; i++)
171 {
5dde5425
PE
172 state_number stateno = to_state[i];
173 transitions *sp = states[stateno]->transitions;
d0fb370f 174
d954473d 175 int j;
640748ee 176 FOR_EACH_SHIFT (sp, j)
db34f798 177 bitset_set (goto_follows[i], TRANSITION_SYMBOL (sp, j));
d0fb370f 178
ccaf65bc 179 for (; j < sp->num; j++)
d954473d 180 {
5dde5425 181 symbol_number sym = TRANSITION_SYMBOL (sp, j);
5362cd5f 182 if (nullable[sym - ntokens])
5dde5425 183 edge[nedges++] = map_goto (stateno, sym);
d954473d 184 }
a0f6b076 185
da2a7671
PE
186 if (nedges == 0)
187 reads[i] = NULL;
188 else
d954473d 189 {
da2a7671
PE
190 reads[i] = xnmalloc (nedges + 1, sizeof reads[i][0]);
191 memcpy (reads[i], edge, nedges * sizeof edge[0]);
60e3ecc7 192 reads[i][nedges] = END_NODE;
d954473d 193 nedges = 0;
d0fb370f 194 }
d0fb370f
RS
195 }
196
db34f798 197 relation_digraph (reads, ngotos, &goto_follows);
d0fb370f
RS
198
199 for (i = 0; i < ngotos; i++)
afbb696d 200 free (reads[i]);
d0fb370f 201
afbb696d
PE
202 free (reads);
203 free (edge);
d0fb370f
RS
204}
205
206
4a120d45 207static void
60e3ecc7 208add_lookback_edge (state *s, rule *r, goto_number gotono)
d0fb370f 209{
5dde5425 210 int ri = state_reduction_find (s, r);
da2a7671 211 goto_list *sp = xmalloc (sizeof *sp);
742e4900 212 sp->next = lookback[(s->reductions->lookahead_tokens - LA) + ri];
d0fb370f 213 sp->value = gotono;
742e4900 214 lookback[(s->reductions->lookahead_tokens - LA) + ri] = sp;
d0fb370f
RS
215}
216
217
d0fb370f 218
4a120d45 219static void
720d742f 220build_relations (void)
d0fb370f 221{
da2a7671
PE
222 goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
223 state_number *states1 = xnmalloc (ritem_longest_rhs () + 1, sizeof *states1);
60e3ecc7 224 goto_number i;
720d742f 225
da2a7671 226 includes = xnmalloc (ngotos, sizeof *includes);
d0fb370f
RS
227
228 for (i = 0; i < ngotos; i++)
229 {
9887c18a 230 int nedges = 0;
5dde5425
PE
231 symbol_number symbol1 = states[to_state[i]]->accessing_symbol;
232 rule **rulep;
d0fb370f 233
5362cd5f 234 for (rulep = derives[symbol1 - ntokens]; *rulep; rulep++)
720d742f 235 {
d0829076 236 bool done;
80a69750 237 int length = 1;
e9ad4aec 238 item_number const *rp;
5dde5425
PE
239 state *s = states[from_state[i]];
240 states1[0] = s->number;
d0fb370f 241
e9ad4aec 242 for (rp = (*rulep)->rhs; ! item_number_is_rule_number (*rp); rp++)
720d742f 243 {
5dde5425
PE
244 s = transitions_to (s->transitions,
245 item_number_as_symbol_number (*rp));
246 states1[length++] = s->number;
720d742f
AD
247 }
248
5dde5425
PE
249 if (!s->consistent)
250 add_lookback_edge (s, *rulep, i);
720d742f
AD
251
252 length--;
d0829076 253 done = false;
720d742f
AD
254 while (!done)
255 {
d0829076 256 done = true;
fa6aa7b3 257 /* Each rhs ends in a rule number, and there is a
db34f798 258 sentinel (ritem[-1]=0) before the first rhs, so it is safe to
e9ad4aec 259 decrement RP here. */
720d742f 260 rp--;
e9ad4aec 261 if (ISVAR (*rp))
720d742f 262 {
5dde5425 263 /* Downcasting from item_number to symbol_number. */
5fbb0954 264 edge[nedges++] = map_goto (states1[--length],
a49aecd5 265 item_number_as_symbol_number (*rp));
5362cd5f 266 if (nullable[*rp - ntokens])
d0829076 267 done = false;
720d742f
AD
268 }
269 }
d0fb370f
RS
270 }
271
da2a7671
PE
272 if (nedges == 0)
273 includes[i] = NULL;
274 else
720d742f 275 {
9887c18a 276 int j;
da2a7671 277 includes[i] = xnmalloc (nedges + 1, sizeof includes[i][0]);
720d742f 278 for (j = 0; j < nedges; j++)
80a69750 279 includes[i][j] = edge[j];
60e3ecc7 280 includes[i][nedges] = END_NODE;
720d742f 281 }
d0fb370f
RS
282 }
283
afbb696d
PE
284 free (edge);
285 free (states1);
9887c18a 286
0e4d5753 287 relation_transpose (&includes, ngotos);
d0fb370f
RS
288}
289
290
720d742f 291
4a120d45 292static void
720d742f 293compute_FOLLOWS (void)
d0fb370f 294{
60e3ecc7 295 goto_number i;
d0fb370f 296
db34f798 297 relation_digraph (includes, ngotos, &goto_follows);
d0fb370f
RS
298
299 for (i = 0; i < ngotos; i++)
afbb696d 300 free (includes[i]);
d0fb370f 301
afbb696d 302 free (includes);
d0fb370f
RS
303}
304
305
4a120d45 306static void
742e4900 307compute_lookahead_tokens (void)
d0fb370f 308{
d200e455 309 size_t i;
5dde5425 310 goto_list *sp;
d0fb370f 311
d200e455 312 for (i = 0; i < nLA; i++)
ddcd5fdf 313 for (sp = lookback[i]; sp; sp = sp->next)
db34f798 314 bitset_or (LA[i], LA[i], goto_follows[sp->value]);
d0fb370f 315
ddcd5fdf 316 /* Free LOOKBACK. */
d200e455 317 for (i = 0; i < nLA; i++)
5dde5425 318 LIST_FREE (goto_list, lookback[i]);
d0fb370f 319
afbb696d 320 free (lookback);
720d742f 321}
d0fb370f 322
d0fb370f 323
742e4900
JD
324/*----------------------------------------------------.
325| Count the number of lookahead tokens required for S |
326| (N_LOOKAHEAD_TOKENS member). |
327`----------------------------------------------------*/
5edafffd 328
cd08e51e 329static int
110ef36a 330state_lookahead_tokens_count (state *s, bool default_reduction_only_for_accept)
5edafffd 331{
742e4900 332 int n_lookahead_tokens = 0;
5dde5425
PE
333 reductions *rp = s->reductions;
334 transitions *sp = s->transitions;
cd08e51e 335
7254f6a8
JD
336 /* Transitions are only disabled during conflict resolution, and that
337 hasn't happened yet, so there should be no need to check that
338 transition 0 hasn't been disabled before checking if it is a shift.
339 However, this check was performed at one time, so we leave it as an
340 aver. */
2a6b783d 341 aver (sp->num == 0 || !TRANSITION_IS_DISABLED (sp, 0));
7254f6a8
JD
342
343 /* We need a lookahead either to distinguish different reductions
344 (i.e., there are two or more), or to distinguish a reduction from a
345 shift. Otherwise, it is straightforward, and the state is
e459fb0e
JD
346 `consistent'. However, do not treat a state with any reductions as
347 consistent unless it is the accepting state (because there is never
348 a lookahead token that makes sense there, and so no lookahead token
349 should be read) if the user has otherwise disabled default
350 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}