2012-06-06 15:25:48 +02:00
|
|
|
/* $NetBSD: lr0.c,v 1.6 2011/09/10 21:29:04 christos Exp $ */
|
|
|
|
/* Id: lr0.c,v 1.12 2010/06/09 08:53:17 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: lr0.c,v 1.6 2011/09/10 21:29:04 christos Exp $");
|
2010-05-12 18:28:54 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
static core *new_state(int symbol);
|
|
|
|
static Value_t get_state(int symbol);
|
2010-05-12 18:28:54 +02:00
|
|
|
static void allocate_itemsets(void);
|
|
|
|
static void allocate_storage(void);
|
|
|
|
static void append_states(void);
|
|
|
|
static void free_storage(void);
|
|
|
|
static void generate_states(void);
|
|
|
|
static void initialize_states(void);
|
|
|
|
static void new_itemsets(void);
|
|
|
|
static void save_reductions(void);
|
2012-06-06 15:25:48 +02:00
|
|
|
static void save_shifts(void);
|
2010-05-12 18:28:54 +02:00
|
|
|
static void set_derives(void);
|
|
|
|
static void set_nullable(void);
|
2012-06-06 15:25:48 +02:00
|
|
|
|
|
|
|
int nstates;
|
|
|
|
core *first_state;
|
|
|
|
shifts *first_shift;
|
|
|
|
reductions *first_reduction;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
static core **state_set;
|
|
|
|
static core *this_state;
|
|
|
|
static core *last_state;
|
|
|
|
static shifts *last_shift;
|
|
|
|
static reductions *last_reduction;
|
|
|
|
|
|
|
|
static int nshifts;
|
|
|
|
static short *shift_symbol;
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
static Value_t *redset;
|
|
|
|
static Value_t *shiftset;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
static Value_t **kernel_base;
|
|
|
|
static Value_t **kernel_end;
|
|
|
|
static Value_t *kernel_items;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
allocate_itemsets(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
short *itemp;
|
|
|
|
short *item_end;
|
|
|
|
int symbol;
|
|
|
|
int i;
|
|
|
|
int count;
|
|
|
|
int max;
|
|
|
|
short *symbol_count;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
symbol_count = NEW2(nsyms, short);
|
|
|
|
|
|
|
|
item_end = ritem + nitems;
|
|
|
|
for (itemp = ritem; itemp < item_end; itemp++)
|
|
|
|
{
|
|
|
|
symbol = *itemp;
|
|
|
|
if (symbol >= 0)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
symbol_count[symbol]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kernel_base = NEW2(nsyms, short *);
|
|
|
|
kernel_items = NEW2(count, short);
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
max = 0;
|
|
|
|
for (i = 0; i < nsyms; i++)
|
|
|
|
{
|
|
|
|
kernel_base[i] = kernel_items + count;
|
|
|
|
count += symbol_count[i];
|
|
|
|
if (max < symbol_count[i])
|
|
|
|
max = symbol_count[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
shift_symbol = symbol_count;
|
|
|
|
kernel_end = NEW2(nsyms, short *);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
allocate_storage(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
allocate_itemsets();
|
|
|
|
shiftset = NEW2(nsyms, short);
|
|
|
|
redset = NEW2(nrules + 1, short);
|
|
|
|
state_set = NEW2(nitems, core *);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
append_states(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i;
|
|
|
|
int j;
|
2012-06-06 15:25:48 +02:00
|
|
|
Value_t symbol;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
#ifdef TRACE
|
|
|
|
fprintf(stderr, "Entering append_states()\n");
|
|
|
|
#endif
|
|
|
|
for (i = 1; i < nshifts; i++)
|
|
|
|
{
|
|
|
|
symbol = shift_symbol[i];
|
|
|
|
j = i;
|
|
|
|
while (j > 0 && shift_symbol[j - 1] > symbol)
|
|
|
|
{
|
|
|
|
shift_symbol[j] = shift_symbol[j - 1];
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
shift_symbol[j] = symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nshifts; i++)
|
|
|
|
{
|
|
|
|
symbol = shift_symbol[i];
|
|
|
|
shiftset[i] = get_state(symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
free_storage(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
FREE(shift_symbol);
|
|
|
|
FREE(redset);
|
|
|
|
FREE(shiftset);
|
|
|
|
FREE(kernel_base);
|
|
|
|
FREE(kernel_end);
|
|
|
|
FREE(kernel_items);
|
|
|
|
FREE(state_set);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
generate_states(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
allocate_storage();
|
|
|
|
itemset = NEW2(nitems, short);
|
|
|
|
ruleset = NEW2(WORDSIZE(nrules), unsigned);
|
|
|
|
set_first_derives();
|
|
|
|
initialize_states();
|
|
|
|
|
|
|
|
while (this_state)
|
|
|
|
{
|
|
|
|
closure(this_state->items, this_state->nitems);
|
|
|
|
save_reductions();
|
|
|
|
new_itemsets();
|
|
|
|
append_states();
|
|
|
|
|
|
|
|
if (nshifts > 0)
|
|
|
|
save_shifts();
|
|
|
|
|
|
|
|
this_state = this_state->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_storage();
|
|
|
|
}
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
static Value_t
|
2010-05-12 18:28:54 +02:00
|
|
|
get_state(int symbol)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int key;
|
|
|
|
short *isp1;
|
|
|
|
short *isp2;
|
|
|
|
short *iend;
|
|
|
|
core *sp;
|
|
|
|
int found;
|
|
|
|
int n;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
#ifdef TRACE
|
|
|
|
fprintf(stderr, "Entering get_state(%d)\n", symbol);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
isp1 = kernel_base[symbol];
|
|
|
|
iend = kernel_end[symbol];
|
2012-06-06 15:25:48 +02:00
|
|
|
n = (int)(iend - isp1);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
key = *isp1;
|
|
|
|
assert(0 <= key && key < nitems);
|
|
|
|
sp = state_set[key];
|
|
|
|
if (sp)
|
|
|
|
{
|
|
|
|
found = 0;
|
|
|
|
while (!found)
|
|
|
|
{
|
|
|
|
if (sp->nitems == n)
|
|
|
|
{
|
|
|
|
found = 1;
|
|
|
|
isp1 = kernel_base[symbol];
|
|
|
|
isp2 = sp->items;
|
|
|
|
|
|
|
|
while (found && isp1 < iend)
|
|
|
|
{
|
|
|
|
if (*isp1++ != *isp2++)
|
|
|
|
found = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
if (sp->link)
|
|
|
|
{
|
|
|
|
sp = sp->link;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sp = sp->link = new_state(symbol);
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state_set[key] = sp = new_state(symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (sp->number);
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
initialize_states(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
unsigned i;
|
2010-05-12 18:28:54 +02:00
|
|
|
short *start_derives;
|
|
|
|
core *p;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
start_derives = derives[start_symbol];
|
|
|
|
for (i = 0; start_derives[i] >= 0; ++i)
|
|
|
|
continue;
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
p = (core *)MALLOC(sizeof(core) + i * sizeof(short));
|
|
|
|
NO_SPACE(p);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
p->next = 0;
|
|
|
|
p->link = 0;
|
|
|
|
p->number = 0;
|
|
|
|
p->accessing_symbol = 0;
|
2012-06-06 15:25:48 +02:00
|
|
|
p->nitems = (Value_t) i;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
for (i = 0; start_derives[i] >= 0; ++i)
|
2005-04-21 16:53:53 +02:00
|
|
|
p->items[i] = rrhs[start_derives[i]];
|
|
|
|
|
|
|
|
first_state = last_state = this_state = p;
|
|
|
|
nstates = 1;
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
new_itemsets(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
Value_t i;
|
2010-05-12 18:28:54 +02:00
|
|
|
int shiftcount;
|
|
|
|
short *isp;
|
|
|
|
short *ksp;
|
2012-06-06 15:25:48 +02:00
|
|
|
Value_t symbol;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
for (i = 0; i < nsyms; i++)
|
|
|
|
kernel_end[i] = 0;
|
|
|
|
|
|
|
|
shiftcount = 0;
|
|
|
|
isp = itemset;
|
|
|
|
while (isp < itemsetend)
|
|
|
|
{
|
|
|
|
i = *isp++;
|
|
|
|
symbol = ritem[i];
|
|
|
|
if (symbol > 0)
|
|
|
|
{
|
|
|
|
ksp = kernel_end[symbol];
|
|
|
|
if (!ksp)
|
|
|
|
{
|
|
|
|
shift_symbol[shiftcount++] = symbol;
|
|
|
|
ksp = kernel_base[symbol];
|
|
|
|
}
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
*ksp++ = (Value_t) (i + 1);
|
2005-04-21 16:53:53 +02:00
|
|
|
kernel_end[symbol] = ksp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nshifts = shiftcount;
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static core *
|
|
|
|
new_state(int symbol)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
unsigned n;
|
2010-05-12 18:28:54 +02:00
|
|
|
core *p;
|
|
|
|
short *isp1;
|
|
|
|
short *isp2;
|
|
|
|
short *iend;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
#ifdef TRACE
|
|
|
|
fprintf(stderr, "Entering new_state(%d)\n", symbol);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (nstates >= MAXSHORT)
|
|
|
|
fatal("too many states");
|
|
|
|
|
|
|
|
isp1 = kernel_base[symbol];
|
|
|
|
iend = kernel_end[symbol];
|
2012-06-06 15:25:48 +02:00
|
|
|
n = (unsigned)(iend - isp1);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
p = (core *)allocate((sizeof(core) + (n - 1) * sizeof(short)));
|
|
|
|
p->accessing_symbol = (Value_t) symbol;
|
|
|
|
p->number = (Value_t) nstates;
|
|
|
|
p->nitems = (Value_t) n;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
isp2 = p->items;
|
|
|
|
while (isp1 < iend)
|
|
|
|
*isp2++ = *isp1++;
|
|
|
|
|
|
|
|
last_state->next = p;
|
|
|
|
last_state = p;
|
|
|
|
|
|
|
|
nstates++;
|
|
|
|
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* show_cores is used for debugging */
|
2012-06-06 15:25:48 +02:00
|
|
|
|
|
|
|
void
|
2010-05-12 18:28:54 +02:00
|
|
|
show_cores(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
core *p;
|
|
|
|
int i, j, k, n;
|
|
|
|
int itemno;
|
|
|
|
|
|
|
|
k = 0;
|
|
|
|
for (p = first_state; p; ++k, p = p->next)
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
if (k)
|
|
|
|
printf("\n");
|
2005-04-21 16:53:53 +02:00
|
|
|
printf("state %d, number = %d, accessing symbol = %s\n",
|
2012-06-06 15:25:48 +02:00
|
|
|
k, p->number, symbol_name[p->accessing_symbol]);
|
2005-04-21 16:53:53 +02:00
|
|
|
n = p->nitems;
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
itemno = p->items[i];
|
|
|
|
printf("%4d ", itemno);
|
|
|
|
j = itemno;
|
2012-06-06 15:25:48 +02:00
|
|
|
while (ritem[j] >= 0)
|
|
|
|
++j;
|
2005-04-21 16:53:53 +02:00
|
|
|
printf("%s :", symbol_name[rlhs[-ritem[j]]]);
|
|
|
|
j = rrhs[-ritem[j]];
|
|
|
|
while (j < itemno)
|
|
|
|
printf(" %s", symbol_name[ritem[j++]]);
|
|
|
|
printf(" .");
|
|
|
|
while (ritem[j] >= 0)
|
|
|
|
printf(" %s", symbol_name[ritem[j++]]);
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* show_ritems is used for debugging */
|
2012-06-06 15:25:48 +02:00
|
|
|
|
|
|
|
void
|
2010-05-12 18:28:54 +02:00
|
|
|
show_ritems(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems; ++i)
|
|
|
|
printf("ritem[%d] = %d\n", i, ritem[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* show_rrhs is used for debugging */
|
2012-06-06 15:25:48 +02:00
|
|
|
void
|
2010-05-12 18:28:54 +02:00
|
|
|
show_rrhs(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nrules; ++i)
|
|
|
|
printf("rrhs[%d] = %d\n", i, rrhs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* show_shifts is used for debugging */
|
2012-06-06 15:25:48 +02:00
|
|
|
|
|
|
|
void
|
2010-05-12 18:28:54 +02:00
|
|
|
show_shifts(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
shifts *p;
|
|
|
|
int i, j, k;
|
|
|
|
|
|
|
|
k = 0;
|
|
|
|
for (p = first_shift; p; ++k, p = p->next)
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
if (k)
|
|
|
|
printf("\n");
|
2005-04-21 16:53:53 +02:00
|
|
|
printf("shift %d, number = %d, nshifts = %d\n", k, p->number,
|
2012-06-06 15:25:48 +02:00
|
|
|
p->nshifts);
|
2005-04-21 16:53:53 +02:00
|
|
|
j = p->nshifts;
|
|
|
|
for (i = 0; i < j; ++i)
|
|
|
|
printf("\t%d\n", p->shift[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
save_shifts(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
shifts *p;
|
|
|
|
short *sp1;
|
|
|
|
short *sp2;
|
|
|
|
short *send;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
p = (shifts *)allocate((sizeof(shifts) +
|
|
|
|
(unsigned)(nshifts - 1) * sizeof(short)));
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
p->number = this_state->number;
|
2012-06-06 15:25:48 +02:00
|
|
|
p->nshifts = (Value_t) nshifts;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
sp1 = shiftset;
|
|
|
|
sp2 = p->shift;
|
|
|
|
send = shiftset + nshifts;
|
|
|
|
|
|
|
|
while (sp1 < send)
|
|
|
|
*sp2++ = *sp1++;
|
|
|
|
|
|
|
|
if (last_shift)
|
|
|
|
{
|
|
|
|
last_shift->next = p;
|
|
|
|
last_shift = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
first_shift = p;
|
|
|
|
last_shift = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
save_reductions(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
short *isp;
|
|
|
|
short *rp1;
|
|
|
|
short *rp2;
|
|
|
|
int item;
|
2012-06-06 15:25:48 +02:00
|
|
|
Value_t count;
|
2010-05-12 18:28:54 +02:00
|
|
|
reductions *p;
|
|
|
|
short *rend;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
for (isp = itemset; isp < itemsetend; isp++)
|
|
|
|
{
|
|
|
|
item = ritem[*isp];
|
|
|
|
if (item < 0)
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
redset[count++] = (Value_t) - item;
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
p = (reductions *)allocate((sizeof(reductions) +
|
|
|
|
(unsigned)(count - 1) *
|
|
|
|
sizeof(short)));
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
p->number = this_state->number;
|
|
|
|
p->nreds = count;
|
|
|
|
|
|
|
|
rp1 = redset;
|
|
|
|
rp2 = p->rules;
|
|
|
|
rend = rp1 + count;
|
|
|
|
|
|
|
|
while (rp1 < rend)
|
|
|
|
*rp2++ = *rp1++;
|
|
|
|
|
|
|
|
if (last_reduction)
|
|
|
|
{
|
|
|
|
last_reduction->next = p;
|
|
|
|
last_reduction = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
first_reduction = p;
|
|
|
|
last_reduction = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
set_derives(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
Value_t i, k;
|
2010-05-12 18:28:54 +02:00
|
|
|
int lhs;
|
|
|
|
short *rules;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
derives = NEW2(nsyms, short *);
|
|
|
|
rules = NEW2(nvars + nrules, short);
|
|
|
|
|
|
|
|
k = 0;
|
|
|
|
for (lhs = start_symbol; lhs < nsyms; lhs++)
|
|
|
|
{
|
|
|
|
derives[lhs] = rules + k;
|
|
|
|
for (i = 0; i < nrules; i++)
|
|
|
|
{
|
|
|
|
if (rlhs[i] == lhs)
|
|
|
|
{
|
|
|
|
rules[k] = i;
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rules[k] = -1;
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
print_derives();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-06-06 15:25:48 +02:00
|
|
|
void
|
2010-05-12 18:28:54 +02:00
|
|
|
print_derives(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i;
|
|
|
|
short *sp;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
printf("\nDERIVES\n\n");
|
|
|
|
|
|
|
|
for (i = start_symbol; i < nsyms; i++)
|
|
|
|
{
|
|
|
|
printf("%s derives ", symbol_name[i]);
|
|
|
|
for (sp = derives[i]; *sp >= 0; sp++)
|
|
|
|
{
|
|
|
|
printf(" %d", *sp);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
static void
|
|
|
|
set_nullable(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-05-12 18:28:54 +02:00
|
|
|
int i, j;
|
|
|
|
int empty;
|
2012-06-06 15:25:48 +02:00
|
|
|
int done_flag;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
nullable = MALLOC(nsyms);
|
2012-06-06 15:25:48 +02:00
|
|
|
NO_SPACE(nullable);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
for (i = 0; i < nsyms; ++i)
|
|
|
|
nullable[i] = 0;
|
|
|
|
|
2012-06-06 15:25:48 +02:00
|
|
|
done_flag = 0;
|
|
|
|
while (!done_flag)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2012-06-06 15:25:48 +02:00
|
|
|
done_flag = 1;
|
2005-04-21 16:53:53 +02:00
|
|
|
for (i = 1; i < nitems; i++)
|
|
|
|
{
|
|
|
|
empty = 1;
|
|
|
|
while ((j = ritem[i]) >= 0)
|
|
|
|
{
|
|
|
|
if (!nullable[j])
|
|
|
|
empty = 0;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (empty)
|
|
|
|
{
|
|
|
|
j = rlhs[-j];
|
|
|
|
if (!nullable[j])
|
|
|
|
{
|
|
|
|
nullable[j] = 1;
|
2012-06-06 15:25:48 +02:00
|
|
|
done_flag = 0;
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (i = 0; i < nsyms; i++)
|
|
|
|
{
|
|
|
|
if (nullable[i])
|
|
|
|
printf("%s is nullable\n", symbol_name[i]);
|
|
|
|
else
|
|
|
|
printf("%s is not nullable\n", symbol_name[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-05-12 18:28:54 +02:00
|
|
|
void
|
|
|
|
lr0(void)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
set_derives();
|
|
|
|
set_nullable();
|
|
|
|
generate_states();
|
|
|
|
}
|
2012-06-06 15:25:48 +02:00
|
|
|
|
|
|
|
#ifdef NO_LEAKS
|
|
|
|
void
|
|
|
|
lr0_leaks(void)
|
|
|
|
{
|
|
|
|
DO_FREE(derives[start_symbol]);
|
|
|
|
DO_FREE(derives);
|
|
|
|
DO_FREE(nullable);
|
|
|
|
}
|
|
|
|
#endif
|