2012-06-06 15:25:48 +02:00
|
|
|
/* $NetBSD: mkpar.c,v 1.6 2011/09/10 21:29:04 christos Exp $ */
|
|
|
|
/* Id: mkpar.c,v 1.11 2010/06/09 08:53:17 tom Exp */
|
|
|
|
|
|
|
|
#include "defs.h"
|
2010-05-12 18:28:54 +02:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2012-06-06 15:25:48 +02:00
|
|
|
__RCSID("$NetBSD: mkpar.c,v 1.6 2011/09/10 21:29:04 christos Exp $");
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
static action *add_reduce(action *actions, int ruleno, int symbol);
|
|
|
|
static action *add_reductions(int stateno, action *actions);
|
|
|
|
static action *get_shifts(int stateno);
|
|
|
|
static action *parse_actions(int stateno);
|
|
|
|
static int sole_reduction(int stateno);
|
|
|
|
static void defreds(void);
|
|
|
|
static void find_final_state(void);
|
|
|
|
static void free_action_row(action *p);
|
|
|
|
static void remove_conflicts(void);
|
|
|
|
static void total_conflicts(void);
|
|
|
|
static void unused_rules(void);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
action **parser;
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
int SRexpect;
|
|
|
|
int RRexpect;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
int SRtotal;
|
|
|
|
int RRtotal;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
Value_t *SRconflicts;
|
|
|
|
Value_t *RRconflicts;
|
|
|
|
Value_t *defred;
|
|
|
|
Value_t *rules_used;
|
|
|
|
Value_t nunused;
|
|
|
|
Value_t final_state;
|
2010-05-12 18:28:54 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
static Value_t SRcount;
|
|
|
|
static Value_t RRcount;
|
2010-05-12 18:28:54 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
make_parser(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
|
|
|
|
|
|
|
parser = NEW2(nstates, action *);
|
|
|
|
for (i = 0; i < nstates; i++)
|
|
|
|
parser[i] = parse_actions(i);
|
|
|
|
|
|
|
|
find_final_state();
|
|
|
|
remove_conflicts();
|
|
|
|
unused_rules();
|
2012-06-06 15:25:48 +02:00
|
|
|
if (SRtotal + RRtotal > 0)
|
|
|
|
total_conflicts();
|
2005-04-21 16:53:53 +02:00
|
|
|
defreds();
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static action *
|
|
|
|
parse_actions(int stateno)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
action *actions;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
actions = get_shifts(stateno);
|
|
|
|
actions = add_reductions(stateno, actions);
|
|
|
|
return (actions);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static action *
|
|
|
|
get_shifts(int stateno)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
action *actions, *temp;
|
|
|
|
shifts *sp;
|
2012-06-06 15:25:48 +02:00
|
|
|
Value_t *to_state2;
|
|
|
|
Value_t i, k;
|
|
|
|
Value_t symbol;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
actions = 0;
|
|
|
|
sp = shift_table[stateno];
|
|
|
|
if (sp)
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
to_state2 = sp->shift;
|
|
|
|
for (i = (Value_t) (sp->nshifts - 1); i >= 0; i--)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
k = to_state2[i];
|
2005-04-21 16:53:53 +02:00
|
|
|
symbol = accessing_symbol[k];
|
|
|
|
if (ISTOKEN(symbol))
|
|
|
|
{
|
|
|
|
temp = NEW(action);
|
|
|
|
temp->next = actions;
|
|
|
|
temp->symbol = symbol;
|
|
|
|
temp->number = k;
|
|
|
|
temp->prec = symbol_prec[symbol];
|
|
|
|
temp->action_code = SHIFT;
|
|
|
|
temp->assoc = symbol_assoc[symbol];
|
|
|
|
actions = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (actions);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static action *
|
|
|
|
add_reductions(int stateno, action *actions)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i, j, m, n;
|
|
|
|
int ruleno, tokensetsize;
|
|
|
|
unsigned *rowp;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
tokensetsize = WORDSIZE(ntokens);
|
|
|
|
m = lookaheads[stateno];
|
|
|
|
n = lookaheads[stateno + 1];
|
|
|
|
for (i = m; i < n; i++)
|
|
|
|
{
|
|
|
|
ruleno = LAruleno[i];
|
|
|
|
rowp = LA + i * tokensetsize;
|
|
|
|
for (j = ntokens - 1; j >= 0; j--)
|
|
|
|
{
|
|
|
|
if (BIT(rowp, j))
|
|
|
|
actions = add_reduce(actions, ruleno, j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (actions);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static action *
|
2012-06-06 15:25:48 +02:00
|
|
|
add_reduce(action *actions,
|
|
|
|
int ruleno,
|
|
|
|
int symbol)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
action *temp, *prev, *next;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
prev = 0;
|
|
|
|
for (next = actions; next && next->symbol < symbol; next = next->next)
|
|
|
|
prev = next;
|
|
|
|
|
|
|
|
while (next && next->symbol == symbol && next->action_code == SHIFT)
|
|
|
|
{
|
|
|
|
prev = next;
|
|
|
|
next = next->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (next && next->symbol == symbol &&
|
2012-06-06 15:25:48 +02:00
|
|
|
next->action_code == REDUCE && next->number < ruleno)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
prev = next;
|
|
|
|
next = next->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = NEW(action);
|
|
|
|
temp->next = next;
|
2012-06-06 15:25:48 +02:00
|
|
|
temp->symbol = (Value_t) symbol;
|
|
|
|
temp->number = (Value_t) ruleno;
|
2005-04-21 16:53:53 +02:00
|
|
|
temp->prec = rprec[ruleno];
|
|
|
|
temp->action_code = REDUCE;
|
|
|
|
temp->assoc = rassoc[ruleno];
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
prev->next = temp;
|
|
|
|
else
|
|
|
|
actions = temp;
|
|
|
|
|
|
|
|
return (actions);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
find_final_state(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int goal, i;
|
2012-06-06 15:25:48 +02:00
|
|
|
Value_t *to_state2;
|
2010-05-12 18:28:54 +02:00
|
|
|
shifts *p;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
p = shift_table[0];
|
2012-06-06 15:25:48 +02:00
|
|
|
to_state2 = p->shift;
|
2005-04-21 16:53:53 +02:00
|
|
|
goal = ritem[1];
|
|
|
|
for (i = p->nshifts - 1; i >= 0; --i)
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
final_state = to_state2[i];
|
|
|
|
if (accessing_symbol[final_state] == goal)
|
|
|
|
break;
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
unused_rules(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i;
|
|
|
|
action *p;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
rules_used = (Value_t *) MALLOC((unsigned)nrules * sizeof(Value_t));
|
|
|
|
NO_SPACE(rules_used);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
for (i = 0; i < nrules; ++i)
|
|
|
|
rules_used[i] = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nstates; ++i)
|
|
|
|
{
|
|
|
|
for (p = parser[i]; p; p = p->next)
|
|
|
|
{
|
|
|
|
if (p->action_code == REDUCE && p->suppressed == 0)
|
|
|
|
rules_used[p->number] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nunused = 0;
|
|
|
|
for (i = 3; i < nrules; ++i)
|
2012-06-06 15:25:48 +02:00
|
|
|
if (!rules_used[i])
|
|
|
|
++nunused;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
if (nunused)
|
|
|
|
{
|
2005-04-21 16:53:53 +02:00
|
|
|
if (nunused == 1)
|
|
|
|
fprintf(stderr, "%s: 1 rule never reduced\n", myname);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s: %d rules never reduced\n", myname, nunused);
|
2010-05-12 18:28:54 +02:00
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
remove_conflicts(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i;
|
|
|
|
int symbol;
|
2012-06-06 15:25:48 +02:00
|
|
|
action *p, *pref = 0;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
SRtotal = 0;
|
|
|
|
RRtotal = 0;
|
2012-06-06 15:25:48 +02:00
|
|
|
SRconflicts = NEW2(nstates, Value_t);
|
|
|
|
RRconflicts = NEW2(nstates, Value_t);
|
2005-04-21 16:53:53 +02:00
|
|
|
for (i = 0; i < nstates; i++)
|
|
|
|
{
|
|
|
|
SRcount = 0;
|
|
|
|
RRcount = 0;
|
|
|
|
symbol = -1;
|
|
|
|
for (p = parser[i]; p; p = p->next)
|
|
|
|
{
|
|
|
|
if (p->symbol != symbol)
|
|
|
|
{
|
|
|
|
pref = p;
|
|
|
|
symbol = p->symbol;
|
|
|
|
}
|
|
|
|
else if (i == final_state && symbol == 0)
|
|
|
|
{
|
|
|
|
SRcount++;
|
|
|
|
p->suppressed = 1;
|
|
|
|
}
|
2012-06-06 15:25:48 +02:00
|
|
|
else if (pref != 0 && pref->action_code == SHIFT)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
if (pref->prec > 0 && p->prec > 0)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
if (pref->prec < p->prec)
|
|
|
|
{
|
|
|
|
pref->suppressed = 2;
|
|
|
|
pref = p;
|
|
|
|
}
|
|
|
|
else if (pref->prec > p->prec)
|
|
|
|
{
|
|
|
|
p->suppressed = 2;
|
|
|
|
}
|
|
|
|
else if (pref->assoc == LEFT)
|
|
|
|
{
|
|
|
|
pref->suppressed = 2;
|
|
|
|
pref = p;
|
|
|
|
}
|
|
|
|
else if (pref->assoc == RIGHT)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
p->suppressed = 2;
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
pref->suppressed = 2;
|
|
|
|
p->suppressed = 2;
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
SRcount++;
|
2005-04-21 16:53:53 +02:00
|
|
|
p->suppressed = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-06-06 15:25:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
RRcount++;
|
|
|
|
p->suppressed = 1;
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
SRtotal += SRcount;
|
|
|
|
RRtotal += RRcount;
|
|
|
|
SRconflicts[i] = SRcount;
|
|
|
|
RRconflicts[i] = RRcount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
total_conflicts(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: ", myname);
|
|
|
|
if (SRtotal == 1)
|
|
|
|
fprintf(stderr, "1 shift/reduce conflict");
|
|
|
|
else if (SRtotal > 1)
|
|
|
|
fprintf(stderr, "%d shift/reduce conflicts", SRtotal);
|
|
|
|
|
|
|
|
if (SRtotal && RRtotal)
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
|
|
|
|
if (RRtotal == 1)
|
|
|
|
fprintf(stderr, "1 reduce/reduce conflict");
|
|
|
|
else if (RRtotal > 1)
|
|
|
|
fprintf(stderr, "%d reduce/reduce conflicts", RRtotal);
|
|
|
|
|
|
|
|
fprintf(stderr, ".\n");
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
if (SRexpect >= 0 && SRtotal != SRexpect)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: ", myname);
|
|
|
|
fprintf(stderr, "expected %d shift/reduce conflict%s.\n",
|
|
|
|
SRexpect, PLURAL(SRexpect));
|
|
|
|
exit_code = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (RRexpect >= 0 && RRtotal != RRexpect)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: ", myname);
|
|
|
|
fprintf(stderr, "expected %d reduce/reduce conflict%s.\n",
|
|
|
|
RRexpect, PLURAL(RRexpect));
|
|
|
|
exit_code = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static int
|
|
|
|
sole_reduction(int stateno)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int count, ruleno;
|
|
|
|
action *p;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
count = 0;
|
2012-06-06 15:25:48 +02:00
|
|
|
ruleno = 0;
|
2005-04-21 16:53:53 +02:00
|
|
|
for (p = parser[stateno]; p; p = p->next)
|
|
|
|
{
|
|
|
|
if (p->action_code == SHIFT && p->suppressed == 0)
|
|
|
|
return (0);
|
|
|
|
else if (p->action_code == REDUCE && p->suppressed == 0)
|
|
|
|
{
|
|
|
|
if (ruleno > 0 && p->number != ruleno)
|
|
|
|
return (0);
|
|
|
|
if (p->symbol != 1)
|
|
|
|
++count;
|
|
|
|
ruleno = p->number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
return (0);
|
|
|
|
return (ruleno);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
defreds(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
|
|
|
defred = NEW2(nstates, Value_t);
|
2005-04-21 16:53:53 +02:00
|
|
|
for (i = 0; i < nstates; i++)
|
2012-06-06 15:25:48 +02:00
|
|
|
defred[i] = (Value_t) sole_reduction(i);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2010-05-12 18:28:54 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
static void
|
2010-05-12 18:28:54 +02:00
|
|
|
free_action_row(action *p)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
action *q;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
while (p)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
q = p->next;
|
|
|
|
FREE(p);
|
|
|
|
p = q;
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
void
|
|
|
|
free_parser(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
int i;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
for (i = 0; i < nstates; i++)
|
|
|
|
free_action_row(parser[i]);
|
|
|
|
|
|
|
|
FREE(parser);
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
#ifdef NO_LEAKS
|
|
|
|
void
|
|
|
|
mkpar_leaks(void)
|
|
|
|
{
|
|
|
|
DO_FREE(defred);
|
|
|
|
DO_FREE(rules_used);
|
|
|
|
DO_FREE(SRconflicts);
|
|
|
|
DO_FREE(RRconflicts);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2012-06-06 15:25:48 +02:00
|
|
|
#endif
|