]> git.saurik.com Git - bison.git/blob - src/lalr.c
Fix some comments concerning LR(0) versus LALR(1).
[bison.git] / src / lalr.c
1 /* Compute lookahead criteria for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 2000-2010 Free Software Foundation,
4 Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program 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 3 of the License, or
11 (at your option) any later version.
12
13 This program 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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 /* Find which rules need lookahead in each state, and which lookahead
23 tokens they accept. */
24
25 #include <config.h>
26 #include "system.h"
27
28 #include <bitset.h>
29 #include <bitsetv.h>
30 #include <quotearg.h>
31
32 #include "LR0.h"
33 #include "complain.h"
34 #include "derives.h"
35 #include "getargs.h"
36 #include "gram.h"
37 #include "lalr.h"
38 #include "muscle-tab.h"
39 #include "nullable.h"
40 #include "reader.h"
41 #include "relation.h"
42 #include "symtab.h"
43
44 goto_number *goto_map;
45 goto_number ngotos;
46 state_number *from_state;
47 state_number *to_state;
48 bitsetv goto_follows = NULL;
49
50 /* Linked list of goto numbers. */
51 typedef struct goto_list
52 {
53 struct goto_list *next;
54 goto_number value;
55 } goto_list;
56
57
58 /* LA is an NLA 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
63 static bitsetv LA = NULL;
64 size_t nLA;
65
66
67 static goto_number **includes;
68 static goto_list **lookback;
69
70
71
72
73 void
74 set_goto_map (void)
75 {
76 state_number s;
77 goto_number *temp_map;
78
79 goto_map = xcalloc (nvars + 1, sizeof *goto_map);
80 temp_map = xnmalloc (nvars + 1, sizeof *temp_map);
81
82 ngotos = 0;
83 for (s = 0; s < nstates; ++s)
84 {
85 transitions *sp = states[s]->transitions;
86 int i;
87 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
88 {
89 ngotos++;
90
91 /* Abort if (ngotos + 1) would overflow. */
92 aver (ngotos != GOTO_NUMBER_MAXIMUM);
93
94 goto_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
95 }
96 }
97
98 {
99 goto_number k = 0;
100 int i;
101 for (i = ntokens; i < nsyms; i++)
102 {
103 temp_map[i - ntokens] = k;
104 k += goto_map[i - ntokens];
105 }
106
107 for (i = ntokens; i < nsyms; i++)
108 goto_map[i - ntokens] = temp_map[i - ntokens];
109
110 goto_map[nsyms - ntokens] = ngotos;
111 temp_map[nsyms - ntokens] = ngotos;
112 }
113
114 from_state = xcalloc (ngotos, sizeof *from_state);
115 to_state = xcalloc (ngotos, sizeof *to_state);
116
117 for (s = 0; s < nstates; ++s)
118 {
119 transitions *sp = states[s]->transitions;
120 int i;
121 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
122 {
123 goto_number k = temp_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
124 from_state[k] = s;
125 to_state[k] = sp->states[i]->number;
126 }
127 }
128
129 free (temp_map);
130 }
131
132
133 goto_number
134 map_goto (state_number s0, symbol_number sym)
135 {
136 goto_number high;
137 goto_number low;
138 goto_number middle;
139 state_number s;
140
141 low = goto_map[sym - ntokens];
142 high = goto_map[sym - ntokens + 1] - 1;
143
144 for (;;)
145 {
146 aver (low <= high);
147 middle = (low + high) / 2;
148 s = from_state[middle];
149 if (s == s0)
150 return middle;
151 else if (s < s0)
152 low = middle + 1;
153 else
154 high = middle - 1;
155 }
156 }
157
158
159 static void
160 initialize_F (void)
161 {
162 goto_number **reads = xnmalloc (ngotos, sizeof *reads);
163 goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
164 goto_number nedges = 0;
165
166 goto_number i;
167
168 goto_follows = bitsetv_create (ngotos, ntokens, BITSET_FIXED);
169
170 for (i = 0; i < ngotos; i++)
171 {
172 state_number stateno = to_state[i];
173 transitions *sp = states[stateno]->transitions;
174
175 int j;
176 FOR_EACH_SHIFT (sp, j)
177 bitset_set (goto_follows[i], TRANSITION_SYMBOL (sp, j));
178
179 for (; j < sp->num; j++)
180 {
181 symbol_number sym = TRANSITION_SYMBOL (sp, j);
182 if (nullable[sym - ntokens])
183 edge[nedges++] = map_goto (stateno, sym);
184 }
185
186 if (nedges == 0)
187 reads[i] = NULL;
188 else
189 {
190 reads[i] = xnmalloc (nedges + 1, sizeof reads[i][0]);
191 memcpy (reads[i], edge, nedges * sizeof edge[0]);
192 reads[i][nedges] = END_NODE;
193 nedges = 0;
194 }
195 }
196
197 relation_digraph (reads, ngotos, &goto_follows);
198
199 for (i = 0; i < ngotos; i++)
200 free (reads[i]);
201
202 free (reads);
203 free (edge);
204 }
205
206
207 static void
208 add_lookback_edge (state *s, rule *r, goto_number gotono)
209 {
210 int ri = state_reduction_find (s, r);
211 goto_list *sp = xmalloc (sizeof *sp);
212 sp->next = lookback[(s->reductions->lookahead_tokens - LA) + ri];
213 sp->value = gotono;
214 lookback[(s->reductions->lookahead_tokens - LA) + ri] = sp;
215 }
216
217
218
219 static void
220 build_relations (void)
221 {
222 goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
223 state_number *states1 = xnmalloc (ritem_longest_rhs () + 1, sizeof *states1);
224 goto_number i;
225
226 includes = xnmalloc (ngotos, sizeof *includes);
227
228 for (i = 0; i < ngotos; i++)
229 {
230 int nedges = 0;
231 symbol_number symbol1 = states[to_state[i]]->accessing_symbol;
232 rule **rulep;
233
234 for (rulep = derives[symbol1 - ntokens]; *rulep; rulep++)
235 {
236 bool done;
237 int length = 1;
238 item_number const *rp;
239 state *s = states[from_state[i]];
240 states1[0] = s->number;
241
242 for (rp = (*rulep)->rhs; ! item_number_is_rule_number (*rp); rp++)
243 {
244 s = transitions_to (s->transitions,
245 item_number_as_symbol_number (*rp));
246 states1[length++] = s->number;
247 }
248
249 if (!s->consistent)
250 add_lookback_edge (s, *rulep, i);
251
252 length--;
253 done = false;
254 while (!done)
255 {
256 done = true;
257 /* Each rhs ends in a rule number, and there is a
258 sentinel (ritem[-1]=0) before the first rhs, so it is safe to
259 decrement RP here. */
260 rp--;
261 if (ISVAR (*rp))
262 {
263 /* Downcasting from item_number to symbol_number. */
264 edge[nedges++] = map_goto (states1[--length],
265 item_number_as_symbol_number (*rp));
266 if (nullable[*rp - ntokens])
267 done = false;
268 }
269 }
270 }
271
272 if (nedges == 0)
273 includes[i] = NULL;
274 else
275 {
276 int j;
277 includes[i] = xnmalloc (nedges + 1, sizeof includes[i][0]);
278 for (j = 0; j < nedges; j++)
279 includes[i][j] = edge[j];
280 includes[i][nedges] = END_NODE;
281 }
282 }
283
284 free (edge);
285 free (states1);
286
287 relation_transpose (&includes, ngotos);
288 }
289
290
291
292 static void
293 compute_FOLLOWS (void)
294 {
295 goto_number i;
296
297 relation_digraph (includes, ngotos, &goto_follows);
298
299 for (i = 0; i < ngotos; i++)
300 free (includes[i]);
301
302 free (includes);
303 }
304
305
306 static void
307 compute_lookahead_tokens (void)
308 {
309 size_t i;
310 goto_list *sp;
311
312 for (i = 0; i < nLA; i++)
313 for (sp = lookback[i]; sp; sp = sp->next)
314 bitset_or (LA[i], LA[i], goto_follows[sp->value]);
315
316 /* Free LOOKBACK. */
317 for (i = 0; i < nLA; i++)
318 LIST_FREE (goto_list, lookback[i]);
319
320 free (lookback);
321 }
322
323
324 /*----------------------------------------------------.
325 | Count the number of lookahead tokens required for S |
326 | (N_LOOKAHEAD_TOKENS member). |
327 `----------------------------------------------------*/
328
329 static int
330 state_lookahead_tokens_count (state *s, bool default_reduction_only_for_accept)
331 {
332 int n_lookahead_tokens = 0;
333 reductions *rp = s->reductions;
334 transitions *sp = s->transitions;
335
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. */
341 aver (sp->num == 0 || !TRANSITION_IS_DISABLED (sp, 0));
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
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. */
351 if (rp->num > 1
352 || (rp->num == 1 && sp->num && TRANSITION_IS_SHIFT (sp, 0))
353 || (rp->num == 1 && rp->rules[0]->number != 0
354 && default_reduction_only_for_accept))
355 n_lookahead_tokens += rp->num;
356 else
357 s->consistent = 1;
358
359 return n_lookahead_tokens;
360 }
361
362
363 /*----------------------------------------------------.
364 | Compute LA, NLA, and the lookahead_tokens members. |
365 `----------------------------------------------------*/
366
367 void
368 initialize_LA (void)
369 {
370 state_number i;
371 bitsetv pLA;
372 bool default_reduction_only_for_accept;
373 {
374 char *default_reductions =
375 muscle_percent_define_get ("lr.default-reductions");
376 default_reduction_only_for_accept =
377 0 == strcmp (default_reductions, "accepting");
378 free (default_reductions);
379 }
380
381 /* Compute the total number of reductions requiring a lookahead. */
382 nLA = 0;
383 for (i = 0; i < nstates; i++)
384 nLA +=
385 state_lookahead_tokens_count (states[i],
386 default_reduction_only_for_accept);
387 /* Avoid having to special case 0. */
388 if (!nLA)
389 nLA = 1;
390
391 pLA = LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
392
393 /* Initialize the members LOOKAHEAD_TOKENS for each state whose reductions
394 require lookahead tokens. */
395 for (i = 0; i < nstates; i++)
396 {
397 int count =
398 state_lookahead_tokens_count (states[i],
399 default_reduction_only_for_accept);
400 if (count)
401 {
402 states[i]->reductions->lookahead_tokens = pLA;
403 pLA += count;
404 }
405 }
406 }
407
408
409 /*---------------------------------------------.
410 | Output the lookahead tokens for each state. |
411 `---------------------------------------------*/
412
413 static void
414 lookahead_tokens_print (FILE *out)
415 {
416 state_number i;
417 int j, k;
418 fprintf (out, "Lookahead tokens: BEGIN\n");
419 for (i = 0; i < nstates; ++i)
420 {
421 reductions *reds = states[i]->reductions;
422 bitset_iterator iter;
423 int n_lookahead_tokens = 0;
424
425 if (reds->lookahead_tokens)
426 for (k = 0; k < reds->num; ++k)
427 if (reds->lookahead_tokens[k])
428 ++n_lookahead_tokens;
429
430 fprintf (out, "State %d: %d lookahead tokens\n",
431 i, n_lookahead_tokens);
432
433 if (reds->lookahead_tokens)
434 for (j = 0; j < reds->num; ++j)
435 BITSET_FOR_EACH (iter, reds->lookahead_tokens[j], k, 0)
436 {
437 fprintf (out, " on %d (%s) -> rule %d\n",
438 k, symbols[k]->tag,
439 reds->rules[j]->number);
440 };
441 }
442 fprintf (out, "Lookahead tokens: END\n");
443 }
444
445 void
446 lalr (void)
447 {
448 initialize_LA ();
449 set_goto_map ();
450 initialize_F ();
451 lookback = xcalloc (nLA, sizeof *lookback);
452 build_relations ();
453 compute_FOLLOWS ();
454 compute_lookahead_tokens ();
455
456 if (trace_flag & trace_sets)
457 lookahead_tokens_print (stderr);
458 }
459
460
461 void
462 lalr_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);
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 }
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
495 void
496 lalr_free (void)
497 {
498 state_number s;
499 for (s = 0; s < nstates; ++s)
500 states[s]->reductions->lookahead_tokens = NULL;
501 bitsetv_free (LA);
502 }