1 /* Compute lookahead criteria for Bison.
3 Copyright (C) 1984, 1986, 1989, 2000-2015, 2018 Free Software
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
;
74 goto_number
*temp_map
= xnmalloc (nvars
+ 1, sizeof *temp_map
);
76 goto_map
= xcalloc (nvars
+ 1, sizeof *goto_map
);
78 for (s
= 0; s
< nstates
; ++s
)
80 transitions
*sp
= states
[s
]->transitions
;
82 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
86 /* Abort if (ngotos + 1) would overflow. */
87 aver (ngotos
!= GOTO_NUMBER_MAXIMUM
);
89 goto_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++;
96 for (i
= ntokens
; i
< nsyms
; i
++)
98 temp_map
[i
- ntokens
] = k
;
99 k
+= goto_map
[i
- ntokens
];
102 for (i
= ntokens
; i
< nsyms
; i
++)
103 goto_map
[i
- ntokens
] = temp_map
[i
- ntokens
];
105 goto_map
[nsyms
- ntokens
] = ngotos
;
106 temp_map
[nsyms
- ntokens
] = ngotos
;
109 from_state
= xcalloc (ngotos
, sizeof *from_state
);
110 to_state
= xcalloc (ngotos
, sizeof *to_state
);
112 for (s
= 0; s
< nstates
; ++s
)
114 transitions
*sp
= states
[s
]->transitions
;
116 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
118 goto_number k
= temp_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++;
120 to_state
[k
] = sp
->states
[i
]->number
;
129 map_goto (state_number s0
, symbol_number sym
)
131 goto_number low
= goto_map
[sym
- ntokens
];
132 goto_number high
= goto_map
[sym
- ntokens
+ 1] - 1;
139 middle
= (low
+ high
) / 2;
140 s
= from_state
[middle
];
154 goto_number
**reads
= xnmalloc (ngotos
, sizeof *reads
);
155 goto_number
*edge
= xnmalloc (ngotos
+ 1, sizeof *edge
);
156 goto_number nedges
= 0;
160 goto_follows
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
162 for (i
= 0; i
< ngotos
; i
++)
164 state_number stateno
= to_state
[i
];
165 transitions
*sp
= states
[stateno
]->transitions
;
168 FOR_EACH_SHIFT (sp
, j
)
169 bitset_set (goto_follows
[i
], TRANSITION_SYMBOL (sp
, j
));
171 for (; j
< sp
->num
; j
++)
173 symbol_number sym
= TRANSITION_SYMBOL (sp
, j
);
174 if (nullable
[sym
- ntokens
])
175 edge
[nedges
++] = map_goto (stateno
, sym
);
182 reads
[i
] = xnmalloc (nedges
+ 1, sizeof reads
[i
][0]);
183 memcpy (reads
[i
], edge
, nedges
* sizeof edge
[0]);
184 reads
[i
][nedges
] = END_NODE
;
189 relation_digraph (reads
, ngotos
, &goto_follows
);
191 for (i
= 0; i
< ngotos
; i
++)
200 add_lookback_edge (state
*s
, rule
*r
, goto_number gotono
)
202 int ri
= state_reduction_find (s
, r
);
203 goto_list
*sp
= xmalloc (sizeof *sp
);
204 sp
->next
= lookback
[(s
->reductions
->lookahead_tokens
- LA
) + ri
];
206 lookback
[(s
->reductions
->lookahead_tokens
- LA
) + ri
] = sp
;
212 build_relations (void)
214 goto_number
*edge
= xnmalloc (ngotos
+ 1, sizeof *edge
);
215 state_number
*states1
= xnmalloc (ritem_longest_rhs () + 1, sizeof *states1
);
218 includes
= xnmalloc (ngotos
, sizeof *includes
);
220 for (i
= 0; i
< ngotos
; i
++)
223 symbol_number symbol1
= states
[to_state
[i
]]->accessing_symbol
;
226 for (rulep
= derives
[symbol1
- ntokens
]; *rulep
; rulep
++)
230 item_number
const *rp
;
231 state
*s
= states
[from_state
[i
]];
232 states1
[0] = s
->number
;
234 for (rp
= (*rulep
)->rhs
; ! item_number_is_rule_number (*rp
); rp
++)
236 s
= transitions_to (s
->transitions
,
237 item_number_as_symbol_number (*rp
));
238 states1
[length
++] = s
->number
;
242 add_lookback_edge (s
, *rulep
, i
);
249 /* Each rhs ends in a rule number, and there is a
250 sentinel (ritem[-1]=0) before the first rhs, so it is safe to
251 decrement RP here. */
255 /* Downcasting from item_number to symbol_number. */
256 edge
[nedges
++] = map_goto (states1
[--length
],
257 item_number_as_symbol_number (*rp
));
258 if (nullable
[*rp
- ntokens
])
269 includes
[i
] = xnmalloc (nedges
+ 1, sizeof includes
[i
][0]);
270 for (j
= 0; j
< nedges
; j
++)
271 includes
[i
][j
] = edge
[j
];
272 includes
[i
][nedges
] = END_NODE
;
279 relation_transpose (&includes
, ngotos
);
285 compute_FOLLOWS (void)
289 relation_digraph (includes
, ngotos
, &goto_follows
);
291 for (i
= 0; i
< ngotos
; i
++)
299 compute_lookahead_tokens (void)
304 for (i
= 0; i
< nLA
; i
++)
305 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
306 bitset_or (LA
[i
], LA
[i
], goto_follows
[sp
->value
]);
309 for (i
= 0; i
< nLA
; i
++)
310 LIST_FREE (goto_list
, lookback
[i
]);
316 /*----------------------------------------------------.
317 | Count the number of lookahead tokens required for S |
318 | (N_LOOKAHEAD_TOKENS member). |
319 `----------------------------------------------------*/
322 state_lookahead_tokens_count (state
*s
, bool default_reduction_only_for_accept
)
324 int n_lookahead_tokens
= 0;
325 reductions
*rp
= s
->reductions
;
326 transitions
*sp
= s
->transitions
;
328 /* Transitions are only disabled during conflict resolution, and that
329 hasn't happened yet, so there should be no need to check that
330 transition 0 hasn't been disabled before checking if it is a shift.
331 However, this check was performed at one time, so we leave it as an
333 aver (sp
->num
== 0 || !TRANSITION_IS_DISABLED (sp
, 0));
335 /* We need a lookahead either to distinguish different reductions
336 (i.e., there are two or more), or to distinguish a reduction from a
337 shift. Otherwise, it is straightforward, and the state is
338 'consistent'. However, do not treat a state with any reductions as
339 consistent unless it is the accepting state (because there is never
340 a lookahead token that makes sense there, and so no lookahead token
341 should be read) if the user has otherwise disabled default
344 || (rp
->num
== 1 && sp
->num
&& TRANSITION_IS_SHIFT (sp
, 0))
345 || (rp
->num
== 1 && rp
->rules
[0]->number
!= 0
346 && default_reduction_only_for_accept
))
347 n_lookahead_tokens
+= rp
->num
;
351 return n_lookahead_tokens
;
355 /*----------------------------------------------------.
356 | Compute LA, NLA, and the lookahead_tokens members. |
357 `----------------------------------------------------*/
364 bool default_reduction_only_for_accept
;
366 char *default_reductions
=
367 muscle_percent_define_get ("lr.default-reduction");
368 default_reduction_only_for_accept
= STREQ (default_reductions
, "accepting");
369 free (default_reductions
);
372 /* Compute the total number of reductions requiring a lookahead. */
374 for (i
= 0; i
< nstates
; i
++)
376 state_lookahead_tokens_count (states
[i
],
377 default_reduction_only_for_accept
);
378 /* Avoid having to special case 0. */
382 pLA
= LA
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
384 /* Initialize the members LOOKAHEAD_TOKENS for each state whose reductions
385 require lookahead tokens. */
386 for (i
= 0; i
< nstates
; i
++)
389 state_lookahead_tokens_count (states
[i
],
390 default_reduction_only_for_accept
);
393 states
[i
]->reductions
->lookahead_tokens
= pLA
;
400 /*---------------------------------------------.
401 | Output the lookahead tokens for each state. |
402 `---------------------------------------------*/
405 lookahead_tokens_print (FILE *out
)
408 fprintf (out
, "Lookahead tokens: BEGIN\n");
409 for (i
= 0; i
< nstates
; ++i
)
411 reductions
*reds
= states
[i
]->reductions
;
412 bitset_iterator iter
;
413 int n_lookahead_tokens
= 0;
415 if (reds
->lookahead_tokens
)
418 for (j
= 0; j
< reds
->num
; ++j
)
419 if (reds
->lookahead_tokens
[j
])
420 ++n_lookahead_tokens
;
423 fprintf (out
, "State %d: %d lookahead tokens\n",
424 i
, n_lookahead_tokens
);
426 if (reds
->lookahead_tokens
)
429 for (j
= 0; j
< reds
->num
; ++j
)
430 BITSET_FOR_EACH (iter
, reds
->lookahead_tokens
[j
], k
, 0)
431 fprintf (out
, " on %d (%s) -> rule %d\n",
433 reds
->rules
[j
]->number
);
436 fprintf (out
, "Lookahead tokens: END\n");
445 lookback
= xcalloc (nLA
, sizeof *lookback
);
448 compute_lookahead_tokens ();
450 if (trace_flag
& trace_sets
)
451 lookahead_tokens_print (stderr
);
456 lalr_update_state_numbers (state_number old_to_new
[], state_number nstates_old
)
458 goto_number ngotos_reachable
= 0;
459 symbol_number nonterminal
= 0;
460 aver (nsyms
== nvars
+ ntokens
);
463 for (i
= 0; i
< ngotos
; ++i
)
465 while (i
== goto_map
[nonterminal
])
466 goto_map
[nonterminal
++] = ngotos_reachable
;
467 /* If old_to_new[from_state[i]] = nstates_old, remove this goto
469 if (old_to_new
[from_state
[i
]] != nstates_old
)
471 /* from_state[i] is not removed, so it and thus to_state[i] are
472 reachable, so to_state[i] != nstates_old. */
473 aver (old_to_new
[to_state
[i
]] != nstates_old
);
474 from_state
[ngotos_reachable
] = old_to_new
[from_state
[i
]];
475 to_state
[ngotos_reachable
] = old_to_new
[to_state
[i
]];
480 while (nonterminal
<= nvars
)
482 aver (ngotos
== goto_map
[nonterminal
]);
483 goto_map
[nonterminal
++] = ngotos_reachable
;
485 ngotos
= ngotos_reachable
;
493 for (s
= 0; s
< nstates
; ++s
)
494 states
[s
]->reductions
->lookahead_tokens
= NULL
;