1 |
/* Grammar reduction for Bison. |
/* Grammar reduction for Bison. |
2 |
|
|
3 |
Copyright (C) 1988, 1989, 2000, 2001, 2002 Free Software Foundation, Inc. |
Copyright (C) 1988, 1989, 2000, 2001, 2002 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of Bison, the GNU Compiler Compiler. |
This file is part of Bison, the GNU Compiler Compiler. |
246 |
{ |
{ |
247 |
int useful = 0; |
int useful = 0; |
248 |
int useless = nrules - nuseless_productions; |
int useless = nrules - nuseless_productions; |
249 |
rule *rules_sorted = XMALLOC (rule, nrules); |
rule *rules_sorted = MALLOC (rules_sorted, nrules); |
250 |
rule_number r; |
rule_number r; |
251 |
for (r = 0; r < nrules; ++r) |
for (r = 0; r < nrules; ++r) |
252 |
rules_sorted[rules[r].useful ? useful++ : useless++] = rules[r]; |
rules_sorted[rules[r].useful ? useful++ : useless++] = rules[r]; |
290 |
/* Map the nonterminals to their new index: useful first, useless |
/* Map the nonterminals to their new index: useful first, useless |
291 |
afterwards. Kept for later report. */ |
afterwards. Kept for later report. */ |
292 |
|
|
293 |
symbol_number *nontermmap = XCALLOC (symbol_number, nvars) - ntokens; |
symbol_number *nontermmap = CALLOC (nontermmap, nvars); |
294 |
n = ntokens; |
n = ntokens; |
295 |
for (i = ntokens; i < nsyms; i++) |
for (i = ntokens; i < nsyms; i++) |
296 |
if (bitset_test (V, i)) |
if (bitset_test (V, i)) |
297 |
nontermmap[i] = n++; |
nontermmap[i - ntokens] = n++; |
298 |
for (i = ntokens; i < nsyms; i++) |
for (i = ntokens; i < nsyms; i++) |
299 |
if (!bitset_test (V, i)) |
if (!bitset_test (V, i)) |
300 |
{ |
{ |
301 |
nontermmap[i] = n++; |
nontermmap[i - ntokens] = n++; |
302 |
warn_at (symbols[i]->location, _("useless nonterminal: %s"), |
warn_at (symbols[i]->location, _("useless nonterminal: %s"), |
303 |
symbols[i]->tag); |
symbols[i]->tag); |
304 |
} |
} |
306 |
|
|
307 |
/* Shuffle elements of tables indexed by symbol number. */ |
/* Shuffle elements of tables indexed by symbol number. */ |
308 |
{ |
{ |
309 |
symbol **symbols_sorted = XMALLOC (symbol *, nvars) - ntokens; |
symbol **symbols_sorted = MALLOC (symbols_sorted, nvars); |
310 |
|
|
311 |
for (i = ntokens; i < nsyms; i++) |
for (i = ntokens; i < nsyms; i++) |
312 |
symbols[i]->number = nontermmap[i]; |
symbols[i]->number = nontermmap[i - ntokens]; |
313 |
for (i = ntokens; i < nsyms; i++) |
for (i = ntokens; i < nsyms; i++) |
314 |
symbols_sorted[nontermmap[i]] = symbols[i]; |
symbols_sorted[nontermmap[i - ntokens] - ntokens] = symbols[i]; |
315 |
for (i = ntokens; i < nsyms; i++) |
for (i = ntokens; i < nsyms; i++) |
316 |
symbols[i] = symbols_sorted[i]; |
symbols[i] = symbols_sorted[i - ntokens]; |
317 |
free (symbols_sorted + ntokens); |
free (symbols_sorted); |
318 |
} |
} |
319 |
|
|
320 |
{ |
{ |
324 |
item_number *rhsp; |
item_number *rhsp; |
325 |
for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) |
for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) |
326 |
if (ISVAR (*rhsp)) |
if (ISVAR (*rhsp)) |
327 |
*rhsp = symbol_number_as_item_number (nontermmap[*rhsp]); |
*rhsp = symbol_number_as_item_number (nontermmap[*rhsp |
328 |
|
- ntokens]); |
329 |
} |
} |
330 |
accept->number = nontermmap[accept->number]; |
accept->number = nontermmap[accept->number - ntokens]; |
331 |
} |
} |
332 |
|
|
333 |
nsyms -= nuseless_nonterminals; |
nsyms -= nuseless_nonterminals; |
334 |
nvars -= nuseless_nonterminals; |
nvars -= nuseless_nonterminals; |
335 |
|
|
336 |
free (nontermmap + ntokens); |
free (nontermmap); |
337 |
} |
} |
338 |
|
|
339 |
|
|