2012-06-06 15:25:48 +02:00
|
|
|
/* $NetBSD: verbose.c,v 1.6 2011/09/10 21:29:04 christos Exp $ */
|
|
|
|
/* Id: verbose.c,v 1.9 2010/06/09 08:58:29 tom Exp */
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__RCSID("$NetBSD: verbose.c,v 1.6 2011/09/10 21:29:04 christos Exp $");
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void log_conflicts(void);
|
2012-06-06 15:25:48 +02:00
|
|
|
static void log_unused(void);
|
|
|
|
static void print_actions(int stateno);
|
|
|
|
static void print_conflicts(int state);
|
|
|
|
static void print_core(int state);
|
|
|
|
static void print_gotos(int stateno);
|
|
|
|
static void print_nulls(int state);
|
|
|
|
static void print_shifts(action *p);
|
|
|
|
static void print_state(int state);
|
|
|
|
static void print_reductions(action *p, int defred2);
|
|
|
|
|
|
|
|
static short *null_rules;
|
2010-05-12 18:28:54 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
verbose(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
if (!vflag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
null_rules = (short *)MALLOC((unsigned)nrules * sizeof(short));
|
|
|
|
NO_SPACE(null_rules);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
fprintf(verbose_file, "\f\n");
|
|
|
|
for (i = 0; i < nstates; i++)
|
|
|
|
print_state(i);
|
|
|
|
FREE(null_rules);
|
|
|
|
|
|
|
|
if (nunused)
|
|
|
|
log_unused();
|
|
|
|
if (SRtotal || RRtotal)
|
|
|
|
log_conflicts();
|
|
|
|
|
|
|
|
fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
|
|
|
|
nvars);
|
|
|
|
fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
log_unused(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i;
|
|
|
|
short *p;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
fprintf(verbose_file, "\n\nRules never reduced:\n");
|
|
|
|
for (i = 3; i < nrules; ++i)
|
|
|
|
{
|
|
|
|
if (!rules_used[i])
|
|
|
|
{
|
|
|
|
fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
|
|
|
|
for (p = ritem + rrhs[i]; *p >= 0; ++p)
|
|
|
|
fprintf(verbose_file, " %s", symbol_name[*p]);
|
|
|
|
fprintf(verbose_file, " (%d)\n", i - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
log_conflicts(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
fprintf(verbose_file, "\n\n");
|
|
|
|
for (i = 0; i < nstates; i++)
|
|
|
|
{
|
|
|
|
if (SRconflicts[i] || RRconflicts[i])
|
|
|
|
{
|
|
|
|
fprintf(verbose_file, "State %d contains ", i);
|
2012-06-06 15:25:48 +02:00
|
|
|
if (SRconflicts[i] > 0)
|
|
|
|
fprintf(verbose_file, "%d shift/reduce conflict%s",
|
|
|
|
SRconflicts[i],
|
|
|
|
PLURAL(SRconflicts[i]));
|
2005-04-21 16:53:53 +02:00
|
|
|
if (SRconflicts[i] && RRconflicts[i])
|
|
|
|
fprintf(verbose_file, ", ");
|
2012-06-06 15:25:48 +02:00
|
|
|
if (RRconflicts[i] > 0)
|
|
|
|
fprintf(verbose_file, "%d reduce/reduce conflict%s",
|
|
|
|
RRconflicts[i],
|
|
|
|
PLURAL(RRconflicts[i]));
|
2005-04-21 16:53:53 +02:00
|
|
|
fprintf(verbose_file, ".\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
print_state(int state)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
if (state)
|
|
|
|
fprintf(verbose_file, "\n\n");
|
|
|
|
if (SRconflicts[state] || RRconflicts[state])
|
|
|
|
print_conflicts(state);
|
|
|
|
fprintf(verbose_file, "state %d\n", state);
|
|
|
|
print_core(state);
|
|
|
|
print_nulls(state);
|
|
|
|
print_actions(state);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
print_conflicts(int state)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int symbol, act, number;
|
|
|
|
action *p;
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
act = 0; /* not shift/reduce... */
|
|
|
|
number = -1;
|
2005-04-21 16:53:53 +02:00
|
|
|
symbol = -1;
|
|
|
|
for (p = parser[state]; p; p = p->next)
|
|
|
|
{
|
|
|
|
if (p->suppressed == 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (p->symbol != symbol)
|
|
|
|
{
|
|
|
|
symbol = p->symbol;
|
|
|
|
number = p->number;
|
|
|
|
if (p->action_code == SHIFT)
|
|
|
|
act = SHIFT;
|
|
|
|
else
|
|
|
|
act = REDUCE;
|
|
|
|
}
|
|
|
|
else if (p->suppressed == 1)
|
|
|
|
{
|
|
|
|
if (state == final_state && symbol == 0)
|
|
|
|
{
|
|
|
|
fprintf(verbose_file, "%d: shift/reduce conflict \
|
|
|
|
(accept, reduce %d) on $end\n", state, p->number - 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (act == SHIFT)
|
|
|
|
{
|
|
|
|
fprintf(verbose_file, "%d: shift/reduce conflict \
|
|
|
|
(shift %d, reduce %d) on %s\n", state, number, p->number - 2,
|
|
|
|
symbol_name[symbol]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(verbose_file, "%d: reduce/reduce conflict \
|
|
|
|
(reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
|
|
|
|
symbol_name[symbol]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
print_core(int state)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i;
|
|
|
|
int k;
|
|
|
|
int rule;
|
|
|
|
core *statep;
|
|
|
|
short *sp;
|
|
|
|
short *sp1;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
statep = state_table[state];
|
|
|
|
k = statep->nitems;
|
|
|
|
|
|
|
|
for (i = 0; i < k; i++)
|
|
|
|
{
|
|
|
|
sp1 = sp = ritem + statep->items[i];
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
while (*sp >= 0)
|
|
|
|
++sp;
|
2005-04-21 16:53:53 +02:00
|
|
|
rule = -(*sp);
|
|
|
|
fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
|
2005-04-21 16:53:53 +02:00
|
|
|
fprintf(verbose_file, "%s ", symbol_name[*sp]);
|
|
|
|
|
|
|
|
putc('.', verbose_file);
|
|
|
|
|
|
|
|
while (*sp >= 0)
|
|
|
|
{
|
|
|
|
fprintf(verbose_file, " %s", symbol_name[*sp]);
|
|
|
|
sp++;
|
|
|
|
}
|
|
|
|
fprintf(verbose_file, " (%d)\n", -2 - *sp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
print_nulls(int state)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
action *p;
|
2012-06-06 15:25:48 +02:00
|
|
|
Value_t i, j, k, nnulls;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
nnulls = 0;
|
|
|
|
for (p = parser[state]; p; p = p->next)
|
|
|
|
{
|
|
|
|
if (p->action_code == REDUCE &&
|
2012-06-06 15:25:48 +02:00
|
|
|
(p->suppressed == 0 || p->suppressed == 1))
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
i = p->number;
|
2012-06-06 15:25:48 +02:00
|
|
|
if (rrhs[i] + 1 == rrhs[i + 1])
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
for (j = 0; j < nnulls && i > null_rules[j]; ++j)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (j == nnulls)
|
|
|
|
{
|
|
|
|
++nnulls;
|
|
|
|
null_rules[j] = i;
|
|
|
|
}
|
|
|
|
else if (i != null_rules[j])
|
|
|
|
{
|
|
|
|
++nnulls;
|
2012-06-06 15:25:48 +02:00
|
|
|
for (k = (Value_t) (nnulls - 1); k > j; --k)
|
|
|
|
null_rules[k] = null_rules[k - 1];
|
2005-04-21 16:53:53 +02:00
|
|
|
null_rules[j] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nnulls; ++i)
|
|
|
|
{
|
|
|
|
j = null_rules[i];
|
|
|
|
fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
|
|
|
|
j - 2);
|
|
|
|
}
|
|
|
|
fprintf(verbose_file, "\n");
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
print_actions(int stateno)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
action *p;
|
|
|
|
shifts *sp;
|
|
|
|
int as;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
if (stateno == final_state)
|
|
|
|
fprintf(verbose_file, "\t$end accept\n");
|
|
|
|
|
|
|
|
p = parser[stateno];
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
print_shifts(p);
|
|
|
|
print_reductions(p, defred[stateno]);
|
|
|
|
}
|
|
|
|
|
|
|
|
sp = shift_table[stateno];
|
|
|
|
if (sp && sp->nshifts > 0)
|
|
|
|
{
|
|
|
|
as = accessing_symbol[sp->shift[sp->nshifts - 1]];
|
|
|
|
if (ISVAR(as))
|
|
|
|
print_gotos(stateno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
print_shifts(action *p)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int count;
|
|
|
|
action *q;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
for (q = p; q; q = q->next)
|
|
|
|
{
|
|
|
|
if (q->suppressed < 2 && q->action_code == SHIFT)
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
for (; p; p = p->next)
|
|
|
|
{
|
|
|
|
if (p->action_code == SHIFT && p->suppressed == 0)
|
|
|
|
fprintf(verbose_file, "\t%s shift %d\n",
|
2012-06-06 15:25:48 +02:00
|
|
|
symbol_name[p->symbol], p->number);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
2012-06-06 15:25:48 +02:00
|
|
|
print_reductions(action *p, int defred2)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int k, anyreds;
|
|
|
|
action *q;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
anyreds = 0;
|
2012-06-06 15:25:48 +02:00
|
|
|
for (q = p; q; q = q->next)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
if (q->action_code == REDUCE && q->suppressed < 2)
|
|
|
|
{
|
|
|
|
anyreds = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anyreds == 0)
|
|
|
|
fprintf(verbose_file, "\t. error\n");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (; p; p = p->next)
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
if (p->action_code == REDUCE && p->number != defred2)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
k = p->number - 2;
|
|
|
|
if (p->suppressed == 0)
|
|
|
|
fprintf(verbose_file, "\t%s reduce %d\n",
|
|
|
|
symbol_name[p->symbol], k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
if (defred2 > 0)
|
|
|
|
fprintf(verbose_file, "\t. reduce %d\n", defred2 - 2);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
print_gotos(int stateno)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i, k;
|
|
|
|
int as;
|
2012-06-06 15:25:48 +02:00
|
|
|
short *to_state2;
|
2010-05-12 18:28:54 +02:00
|
|
|
shifts *sp;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
putc('\n', verbose_file);
|
|
|
|
sp = shift_table[stateno];
|
2012-06-06 15:25:48 +02:00
|
|
|
to_state2 = sp->shift;
|
2005-04-21 16:53:53 +02:00
|
|
|
for (i = 0; i < sp->nshifts; ++i)
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
k = to_state2[i];
|
2005-04-21 16:53:53 +02:00
|
|
|
as = accessing_symbol[k];
|
|
|
|
if (ISVAR(as))
|
|
|
|
fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);
|
|
|
|
}
|
|
|
|
}
|