1 /* Compute lookahead criteria for Bison.
3 Copyright (C) 1984, 1986, 1989, 2000-2013 Free Software Foundation,
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
22 /* Find which rules need lookahead in each state, and which lookahead
23 tokens they accept. */
37 #include "muscle-tab.h"
43 goto_number
*goto_map
;
45 state_number
*from_state
;
46 state_number
*to_state
;
47 bitsetv goto_follows
= NULL
;
49 /* Linked list of goto numbers. */
50 typedef struct goto_list
52 struct goto_list
*next
;
57 /* LA is an NLA by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
58 LArule[l] is applicable in the appropriate state when the next
59 token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
62 static bitsetv LA
= NULL
;
66 static goto_number
**includes
;
67 static goto_list
**lookback
;
76 goto_number
*temp_map
;
78 goto_map
= xcalloc (nvars
+ 1, sizeof *goto_map
);
79 temp_map
= xnmalloc (nvars
+ 1, sizeof *temp_map
);
82 for (s
= 0; s
< nstates
; ++s
)
84 transitions
*sp
= states
[s
]->transitions
;
86 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
90 /* Abort if (ngotos + 1) would overflow. */
91 aver (ngotos
!= GOTO_NUMBER_MAXIMUM
);
93 goto_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++;
100 for (i
= ntokens
; i
< nsyms
; i
++)
102 temp_map
[i
- ntokens
] = k
;
103 k
+= goto_map
[i
- ntokens
];
106 for (i
= ntokens
; i
< nsyms
; i
++)
107 goto_map
[i
- ntokens
] = temp_map
[i
- ntokens
];
109 goto_map
[nsyms
- ntokens
] = ngotos
;
110 temp_map
[nsyms
- ntokens
] = ngotos
;
113 from_state
= xcalloc (ngotos
, sizeof *from_state
);
114 to_state
= xcalloc (ngotos
, sizeof *to_state
);
116 for (s
= 0; s
< nstates
; ++s
)
118 transitions
*sp
= states
[s
]->transitions
;
120 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
122 goto_number k
= temp_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++;
124 to_state
[k
] = sp
->states
[i
]->number
;
133 map_goto (state_number s0
, symbol_number sym
)
140 low
= goto_map
[sym
- ntokens
];
141 high
= goto_map
[sym
- ntokens
+ 1] - 1;
146 middle
= (low
+ high
) / 2;
147 s
= from_state
[middle
];
161 goto_number
**reads
= xnmalloc (ngotos
, sizeof *reads
);
162 goto_number
*edge
= xnmalloc (ngotos
+ 1, sizeof *edge
);
163 goto_number nedges
= 0;
167 goto_follows
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
169 for (i
= 0; i
< ngotos
; i
++)
171 state_number stateno
= to_state
[i
];
172 transitions
*sp
= states
[stateno
]->transitions
;
175 FOR_EACH_SHIFT (sp
, j
)
176 bitset_set (goto_follows
[i
], TRANSITION_SYMBOL (sp
, j
));
178 for (; j
< sp
->num
; j
++)
180 symbol_number sym
= TRANSITION_SYMBOL (sp
, j
);
181 if (nullable
[sym
- ntokens
])
182 edge
[nedges
++] = map_goto (stateno
, sym
);
189 reads
[i
] = xnmalloc (nedges
+ 1, sizeof reads
[i
][0]);
190 memcpy (reads
[i
], edge
, nedges
* sizeof edge
[0]);
191 reads
[i
][nedges
] = END_NODE
;
196 relation_digraph (reads
, ngotos
, &goto_follows
);
198 for (i
= 0; i
< ngotos
; i
++)
207 add_lookback_edge (state
*s
, rule
*r
, goto_number gotono
)
209 int ri
= state_reduction_find (s
, r
);
210 goto_list
*sp
= xmalloc (sizeof *sp
);
211 sp
->next
= lookback
[(s
->reductions
->lookahead_tokens
- LA
) + ri
];
213 lookback
[(s
->reductions
->lookahead_tokens
- LA
) + ri
] = sp
;
219 build_relations (void)
221 goto_number
*edge
= xnmalloc (ngotos
+ 1, sizeof *edge
);
222 state_number
*states1
= xnmalloc (ritem_longest_rhs () + 1, sizeof *states1
);
225 includes
= xnmalloc (ngotos
, sizeof *includes
);
227 for (i
= 0; i
< ngotos
; i
++)
230 symbol_number symbol1
= states
[to_state
[i
]]->accessing_symbol
;
233 for (rulep
= derives
[symbol1
- ntokens
]; *rulep
; rulep
++)
237 item_number
const *rp
;
238 state
*s
= states
[from_state
[i
]];
239 states1
[0] = s
->number
;
241 for (rp
= (*rulep
)->rhs
; ! item_number_is_rule_number (*rp
); rp
++)
243 s
= transitions_to (s
->transitions
,
244 item_number_as_symbol_number (*rp
));
245 states1
[length
++] = s
->number
;
249 add_lookback_edge (s
, *rulep
, i
);
256 /* Each rhs ends in a rule number, and there is a
257 sentinel (ritem[-1]=0) before the first rhs, so it is safe to
258 decrement RP here. */
262 /* Downcasting from item_number to symbol_number. */
263 edge
[nedges
++] = map_goto (states1
[--length
],
264 item_number_as_symbol_number (*rp
));
265 if (nullable
[*rp
- ntokens
])
276 includes
[i
] = xnmalloc (nedges
+ 1, sizeof includes
[i
][0]);
277 for (j
= 0; j
< nedges
; j
++)
278 includes
[i
][j
] = edge
[j
];
279 includes
[i
][nedges
] = END_NODE
;
286 relation_transpose (&includes
, ngotos
);
292 compute_FOLLOWS (void)
296 relation_digraph (includes
, ngotos
, &goto_follows
);
298 for (i
= 0; i
< ngotos
; i
++)
306 compute_lookahead_tokens (void)
311 for (i
= 0; i
< nLA
; i
++)
312 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
313 bitset_or (LA
[i
], LA
[i
], goto_follows
[sp
->value
]);
316 for (i
= 0; i
< nLA
; i
++)
317 LIST_FREE (goto_list
, lookback
[i
]);
323 /*----------------------------------------------------.
324 | Count the number of lookahead tokens required for S |
325 | (N_LOOKAHEAD_TOKENS member). |
326 `----------------------------------------------------*/
329 state_lookahead_tokens_count (state
*s
, bool default_reduction_only_for_accept
)
331 int n_lookahead_tokens
= 0;
332 reductions
*rp
= s
->reductions
;
333 transitions
*sp
= s
->transitions
;
335 /* Transitions are only disabled during conflict resolution, and that
336 hasn't happened yet, so there should be no need to check that
337 transition 0 hasn't been disabled before checking if it is a shift.
338 However, this check was performed at one time, so we leave it as an
340 aver (sp
->num
== 0 || !TRANSITION_IS_DISABLED (sp
, 0));
342 /* We need a lookahead either to distinguish different reductions
343 (i.e., there are two or more), or to distinguish a reduction from a
344 shift. Otherwise, it is straightforward, and the state is
345 'consistent'. However, do not treat a state with any reductions as
346 consistent unless it is the accepting state (because there is never
347 a lookahead token that makes sense there, and so no lookahead token
348 should be read) if the user has otherwise disabled default
351 || (rp
->num
== 1 && sp
->num
&& TRANSITION_IS_SHIFT (sp
, 0))
352 || (rp
->num
== 1 && rp
->rules
[0]->number
!= 0
353 && default_reduction_only_for_accept
))
354 n_lookahead_tokens
+= rp
->num
;
358 return n_lookahead_tokens
;
362 /*----------------------------------------------------.
363 | Compute LA, NLA, and the lookahead_tokens members. |
364 `----------------------------------------------------*/
371 bool default_reduction_only_for_accept
;
373 char *default_reductions
=
374 muscle_percent_define_get ("lr.default-reduction");
375 default_reduction_only_for_accept
= STREQ (default_reductions
, "accepting");
376 free (default_reductions
);
379 /* Compute the total number of reductions requiring a lookahead. */
381 for (i
= 0; i
< nstates
; i
++)
383 state_lookahead_tokens_count (states
[i
],
384 default_reduction_only_for_accept
);
385 /* Avoid having to special case 0. */
389 pLA
= LA
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
391 /* Initialize the members LOOKAHEAD_TOKENS for each state whose reductions
392 require lookahead tokens. */
393 for (i
= 0; i
< nstates
; i
++)
396 state_lookahead_tokens_count (states
[i
],
397 default_reduction_only_for_accept
);
400 states
[i
]->reductions
->lookahead_tokens
= pLA
;
407 /*---------------------------------------------.
408 | Output the lookahead tokens for each state. |
409 `---------------------------------------------*/
412 lookahead_tokens_print (FILE *out
)
416 fprintf (out
, "Lookahead tokens: BEGIN\n");
417 for (i
= 0; i
< nstates
; ++i
)
419 reductions
*reds
= states
[i
]->reductions
;
420 bitset_iterator iter
;
421 int n_lookahead_tokens
= 0;
423 if (reds
->lookahead_tokens
)
424 for (k
= 0; k
< reds
->num
; ++k
)
425 if (reds
->lookahead_tokens
[k
])
426 ++n_lookahead_tokens
;
428 fprintf (out
, "State %d: %d lookahead tokens\n",
429 i
, n_lookahead_tokens
);
431 if (reds
->lookahead_tokens
)
432 for (j
= 0; j
< reds
->num
; ++j
)
433 BITSET_FOR_EACH (iter
, reds
->lookahead_tokens
[j
], k
, 0)
435 fprintf (out
, " on %d (%s) -> rule %d\n",
437 reds
->rules
[j
]->number
);
440 fprintf (out
, "Lookahead tokens: END\n");
449 lookback
= xcalloc (nLA
, sizeof *lookback
);
452 compute_lookahead_tokens ();
454 if (trace_flag
& trace_sets
)
455 lookahead_tokens_print (stderr
);
460 lalr_update_state_numbers (state_number old_to_new
[], state_number nstates_old
)
462 goto_number ngotos_reachable
= 0;
463 symbol_number nonterminal
= 0;
464 aver (nsyms
== nvars
+ ntokens
);
467 for (i
= 0; i
< ngotos
; ++i
)
469 while (i
== goto_map
[nonterminal
])
470 goto_map
[nonterminal
++] = ngotos_reachable
;
471 /* If old_to_new[from_state[i]] = nstates_old, remove this goto
473 if (old_to_new
[from_state
[i
]] != nstates_old
)
475 /* from_state[i] is not removed, so it and thus to_state[i] are
476 reachable, so to_state[i] != nstates_old. */
477 aver (old_to_new
[to_state
[i
]] != nstates_old
);
478 from_state
[ngotos_reachable
] = old_to_new
[from_state
[i
]];
479 to_state
[ngotos_reachable
] = old_to_new
[to_state
[i
]];
484 while (nonterminal
<= nvars
)
486 aver (ngotos
== goto_map
[nonterminal
]);
487 goto_map
[nonterminal
++] = ngotos_reachable
;
489 ngotos
= ngotos_reachable
;
497 for (s
= 0; s
< nstates
; ++s
)
498 states
[s
]->reductions
->lookahead_tokens
= NULL
;