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