38 |
typedef short *rule; |
typedef short *rule; |
39 |
|
|
40 |
|
|
41 |
/* N is set of all nonterminals which are not useless. P is set of |
/* Set of all nonterminals which are not useless. */ |
42 |
all rules which have no useless nonterminals in their RHS. V is |
static BSet N; |
|
the set of all accessible symbols. */ |
|
43 |
|
|
44 |
static BSet N, P, V, V1; |
/* Set of all rules which have no useless nonterminals in their RHS. */ |
45 |
|
static BSet P; |
46 |
|
|
47 |
|
/* Set of all accessible symbols. */ |
48 |
|
static BSet V; |
49 |
|
|
50 |
|
/* Set of symbols used to define rule precedence (so they are |
51 |
|
`useless', but no warning should be issued). */ |
52 |
|
static BSet V1; |
53 |
|
|
54 |
static int nuseful_productions; |
static int nuseful_productions; |
55 |
static int nuseless_productions; |
static int nuseless_productions; |
325 |
} |
} |
326 |
} |
} |
327 |
} |
} |
328 |
|
} |
329 |
|
|
|
/* remove useless symbols */ |
|
|
if (nuseless_nonterminals > 0) |
|
|
{ |
|
330 |
|
|
331 |
int i, n; |
/*------------------------------. |
332 |
/* short j; JF unused */ |
| Remove useless nonterminals. | |
333 |
short *nontermmap; |
`------------------------------*/ |
|
rule r; |
|
|
|
|
|
/* Create a map of nonterminal number to new nonterminal |
|
|
number. -1 in the map means it was useless and is being |
|
|
eliminated. */ |
|
334 |
|
|
335 |
nontermmap = XCALLOC (short, nvars) - ntokens; |
static void |
336 |
for (i = ntokens; i < nsyms; i++) |
nonterminals_reduce (void) |
337 |
nontermmap[i] = -1; |
{ |
338 |
|
int i, n; |
339 |
|
rule r; |
340 |
|
|
341 |
n = ntokens; |
/* Create a map of nonterminal number to new nonterminal number. -1 |
342 |
for (i = ntokens; i < nsyms; i++) |
in the map means it was useless and is being eliminated. */ |
|
if (BITISSET (V, i)) |
|
|
nontermmap[i] = n++; |
|
343 |
|
|
344 |
/* Shuffle elements of tables indexed by symbol number. */ |
short *nontermmap = XCALLOC (short, nvars) - ntokens; |
345 |
|
for (i = ntokens; i < nsyms; i++) |
346 |
|
nontermmap[i] = -1; |
347 |
|
|
348 |
for (i = ntokens; i < nsyms; i++) |
n = ntokens; |
349 |
{ |
for (i = ntokens; i < nsyms; i++) |
350 |
n = nontermmap[i]; |
if (BITISSET (V, i)) |
351 |
if (n >= 0) |
nontermmap[i] = n++; |
|
{ |
|
|
sassoc[n] = sassoc[i]; |
|
|
sprec[n] = sprec[i]; |
|
|
tags[n] = tags[i]; |
|
|
} |
|
|
} |
|
352 |
|
|
353 |
/* Replace all symbol numbers in valid data structures. */ |
/* Shuffle elements of tables indexed by symbol number. */ |
354 |
|
|
355 |
for (i = 1; i <= nrules; i++) |
for (i = ntokens; i < nsyms; i++) |
356 |
|
{ |
357 |
|
n = nontermmap[i]; |
358 |
|
if (n >= 0) |
359 |
{ |
{ |
360 |
/* Ignore the rules disabled above. */ |
sassoc[n] = sassoc[i]; |
361 |
if (rule_table[i].lhs >= 0) |
sprec[n] = sprec[i]; |
362 |
rule_table[i].lhs = nontermmap[rule_table[i].lhs]; |
tags[n] = tags[i]; |
|
if (ISVAR (rule_table[i].precsym)) |
|
|
/* Can this happen? */ |
|
|
rule_table[i].precsym = nontermmap[rule_table[i].precsym]; |
|
363 |
} |
} |
364 |
|
} |
365 |
|
|
366 |
for (r = ritem; *r; r++) |
/* Replace all symbol numbers in valid data structures. */ |
|
if (ISVAR (*r)) |
|
|
*r = nontermmap[*r]; |
|
367 |
|
|
368 |
start_symbol = nontermmap[start_symbol]; |
for (i = 1; i <= nrules; i++) |
369 |
|
{ |
370 |
|
/* Ignore the rules disabled above. */ |
371 |
|
if (rule_table[i].lhs >= 0) |
372 |
|
rule_table[i].lhs = nontermmap[rule_table[i].lhs]; |
373 |
|
if (ISVAR (rule_table[i].precsym)) |
374 |
|
/* Can this happen? */ |
375 |
|
rule_table[i].precsym = nontermmap[rule_table[i].precsym]; |
376 |
|
} |
377 |
|
|
378 |
nsyms -= nuseless_nonterminals; |
for (r = ritem; *r; r++) |
379 |
nvars -= nuseless_nonterminals; |
if (ISVAR (*r)) |
380 |
|
*r = nontermmap[*r]; |
381 |
|
|
382 |
free (&nontermmap[ntokens]); |
start_symbol = nontermmap[start_symbol]; |
383 |
} |
|
384 |
|
nsyms -= nuseless_nonterminals; |
385 |
|
nvars -= nuseless_nonterminals; |
386 |
|
|
387 |
|
free (&nontermmap[ntokens]); |
388 |
} |
} |
389 |
|
|
390 |
|
|
391 |
/*-----------------------------------------------------------------. |
/*------------------------------------------------------------------. |
392 |
| Ouput the detailed results of the reductions. For FILE.output. | |
| Output the detailed results of the reductions. For FILE.output. | |
393 |
`-----------------------------------------------------------------*/ |
`------------------------------------------------------------------*/ |
394 |
|
|
395 |
void |
void |
396 |
reduce_output (FILE *out) |
reduce_output (FILE *out) |
545 |
tags[start_symbol]); |
tags[start_symbol]); |
546 |
|
|
547 |
reduce_grammar_tables (); |
reduce_grammar_tables (); |
548 |
|
if (nuseless_nonterminals > 0) |
549 |
|
nonterminals_reduce (); |
550 |
|
|
551 |
if (trace_flag) |
if (trace_flag) |
552 |
{ |
{ |