1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
|
|
|
/* under the terms of the Q Public License version 1.0. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1995-08-09 08:25:52 -07:00
|
|
|
/* Based on public-domain code from Berkeley Yacc */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
extern short *itemset;
|
|
|
|
extern short *itemsetend;
|
|
|
|
extern unsigned *ruleset;
|
|
|
|
|
|
|
|
int nstates;
|
|
|
|
core *first_state;
|
|
|
|
shifts *first_shift;
|
|
|
|
reductions *first_reduction;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
int get_state(int symbol);
|
|
|
|
core *new_state(int symbol);
|
1995-05-04 03:15:53 -07: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;
|
|
|
|
|
|
|
|
static short *redset;
|
|
|
|
static short *shiftset;
|
|
|
|
|
|
|
|
static short **kernel_base;
|
|
|
|
static short **kernel_end;
|
|
|
|
static short *kernel_items;
|
|
|
|
|
|
|
|
|
1995-08-08 09:44:29 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void initialize_states (void);
|
|
|
|
void save_reductions (void);
|
|
|
|
void new_itemsets (void);
|
|
|
|
void save_shifts (void);
|
1995-08-08 09:44:29 -07:00
|
|
|
void print_derives ();
|
1997-09-02 05:55:01 -07:00
|
|
|
void show_cores (void), show_ritems (void), show_rrhs (void), show_shifts (void);
|
1995-08-08 09:44:29 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void allocate_itemsets(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register short *itemp;
|
|
|
|
register short *item_end;
|
|
|
|
register int symbol;
|
|
|
|
register int i;
|
|
|
|
register int count;
|
|
|
|
register int max;
|
|
|
|
register short *symbol_count;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
symbol_count = NEW2(nsyms, short);
|
|
|
|
|
|
|
|
item_end = ritem + nitems;
|
|
|
|
for (itemp = ritem; itemp < item_end; itemp++)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
symbol = *itemp;
|
|
|
|
if (symbol >= 0)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
symbol_count[symbol]++;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
kernel_base = NEW2(nsyms, short *);
|
|
|
|
kernel_items = NEW2(count, short);
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
max = 0;
|
|
|
|
for (i = 0; i < nsyms; i++)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
kernel_base[i] = kernel_items + count;
|
|
|
|
count += symbol_count[i];
|
|
|
|
if (max < symbol_count[i])
|
|
|
|
max = symbol_count[i];
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
shift_symbol = symbol_count;
|
|
|
|
kernel_end = NEW2(nsyms, short *);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void allocate_storage(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
allocate_itemsets();
|
|
|
|
shiftset = NEW2(nsyms, short);
|
|
|
|
redset = NEW2(nrules + 1, short);
|
|
|
|
state_set = NEW2(nitems, core *);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void append_states(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register int j;
|
|
|
|
register int symbol;
|
|
|
|
|
2001-07-12 05:54:24 -07:00
|
|
|
#ifdef TRACE
|
1995-05-04 03:15:53 -07:00
|
|
|
fprintf(stderr, "Entering append_states()\n");
|
|
|
|
#endif
|
|
|
|
for (i = 1; i < nshifts; i++)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
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;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nshifts; i++)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
symbol = shift_symbol[i];
|
|
|
|
shiftset[i] = get_state(symbol);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void free_storage(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
FREE(shift_symbol);
|
|
|
|
FREE(redset);
|
|
|
|
FREE(shiftset);
|
|
|
|
FREE(kernel_base);
|
|
|
|
FREE(kernel_end);
|
|
|
|
FREE(kernel_items);
|
|
|
|
FREE(state_set);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void generate_states(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
allocate_storage();
|
|
|
|
itemset = NEW2(nitems, short);
|
|
|
|
ruleset = NEW2(WORDSIZE(nrules), unsigned);
|
|
|
|
set_first_derives();
|
|
|
|
initialize_states();
|
|
|
|
|
|
|
|
while (this_state)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
closure(this_state->items, this_state->nitems);
|
|
|
|
save_reductions();
|
|
|
|
new_itemsets();
|
|
|
|
append_states();
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-07-12 05:54:24 -07:00
|
|
|
if (nshifts > 0)
|
|
|
|
save_shifts();
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-07-12 05:54:24 -07:00
|
|
|
this_state = this_state->next;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
finalize_closure();
|
|
|
|
free_storage();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1997-09-02 05:55:01 -07:00
|
|
|
get_state(int symbol)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register int key;
|
|
|
|
register short *isp1;
|
|
|
|
register short *isp2;
|
|
|
|
register short *iend;
|
|
|
|
register core *sp;
|
|
|
|
register int found;
|
|
|
|
register int n;
|
|
|
|
|
2001-07-12 05:54:24 -07:00
|
|
|
#ifdef TRACE
|
1995-05-04 03:15:53 -07:00
|
|
|
fprintf(stderr, "Entering get_state(%d)\n", symbol);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
isp1 = kernel_base[symbol];
|
|
|
|
iend = kernel_end[symbol];
|
|
|
|
n = iend - isp1;
|
|
|
|
|
|
|
|
key = *isp1;
|
|
|
|
assert(0 <= key && key < nitems);
|
|
|
|
sp = state_set[key];
|
|
|
|
if (sp)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
state_set[key] = sp = new_state(symbol);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return (sp->number);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void initialize_states(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register short *start_derives;
|
|
|
|
register core *p;
|
|
|
|
|
|
|
|
start_derives = derives[start_symbol];
|
|
|
|
for (i = 0; start_derives[i] >= 0; ++i)
|
2001-07-12 05:54:24 -07:00
|
|
|
continue;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
p = (core *) MALLOC(sizeof(core) + i*sizeof(short));
|
|
|
|
if (p == 0) no_space();
|
|
|
|
|
|
|
|
p->next = 0;
|
|
|
|
p->link = 0;
|
|
|
|
p->number = 0;
|
|
|
|
p->accessing_symbol = 0;
|
|
|
|
p->nitems = i;
|
|
|
|
|
|
|
|
for (i = 0; start_derives[i] >= 0; ++i)
|
2001-07-12 05:54:24 -07:00
|
|
|
p->items[i] = rrhs[start_derives[i]];
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
first_state = last_state = this_state = p;
|
|
|
|
nstates = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void new_itemsets(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register int shiftcount;
|
|
|
|
register short *isp;
|
|
|
|
register short *ksp;
|
|
|
|
register int symbol;
|
|
|
|
|
|
|
|
for (i = 0; i < nsyms; i++)
|
2001-07-12 05:54:24 -07:00
|
|
|
kernel_end[i] = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
shiftcount = 0;
|
|
|
|
isp = itemset;
|
|
|
|
while (isp < itemsetend)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
i = *isp++;
|
|
|
|
symbol = ritem[i];
|
|
|
|
if (symbol > 0)
|
|
|
|
{
|
|
|
|
ksp = kernel_end[symbol];
|
|
|
|
if (!ksp)
|
|
|
|
{
|
|
|
|
shift_symbol[shiftcount++] = symbol;
|
|
|
|
ksp = kernel_base[symbol];
|
|
|
|
}
|
|
|
|
|
|
|
|
*ksp++ = i + 1;
|
|
|
|
kernel_end[symbol] = ksp;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nshifts = shiftcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
core *
|
1997-09-02 05:55:01 -07:00
|
|
|
new_state(int symbol)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register int n;
|
|
|
|
register core *p;
|
|
|
|
register short *isp1;
|
|
|
|
register short *isp2;
|
|
|
|
register short *iend;
|
|
|
|
|
2001-07-12 05:54:24 -07:00
|
|
|
#ifdef TRACE
|
1995-05-04 03:15:53 -07:00
|
|
|
fprintf(stderr, "Entering new_state(%d)\n", symbol);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (nstates >= MAXSHORT)
|
2001-07-12 05:54:24 -07:00
|
|
|
fatal("too many states");
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
isp1 = kernel_base[symbol];
|
|
|
|
iend = kernel_end[symbol];
|
|
|
|
n = iend - isp1;
|
|
|
|
|
|
|
|
p = (core *) allocate((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
|
|
|
|
p->accessing_symbol = symbol;
|
|
|
|
p->number = nstates;
|
|
|
|
p->nitems = n;
|
|
|
|
|
|
|
|
isp2 = p->items;
|
|
|
|
while (isp1 < iend)
|
2001-07-12 05:54:24 -07:00
|
|
|
*isp2++ = *isp1++;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
last_state->next = p;
|
|
|
|
last_state = p;
|
|
|
|
|
|
|
|
nstates++;
|
|
|
|
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* show_cores is used for debugging */
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void show_cores(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
core *p;
|
|
|
|
int i, j, k, n;
|
|
|
|
int itemno;
|
|
|
|
|
|
|
|
k = 0;
|
|
|
|
for (p = first_state; p; ++k, p = p->next)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
if (k) printf("\n");
|
|
|
|
printf("state %d, number = %d, accessing symbol = %s\n",
|
|
|
|
k, p->number, symbol_name[p->accessing_symbol]);
|
|
|
|
n = p->nitems;
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
itemno = p->items[i];
|
|
|
|
printf("%4d ", itemno);
|
|
|
|
j = itemno;
|
|
|
|
while (ritem[j] >= 0) ++j;
|
|
|
|
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);
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* show_ritems is used for debugging */
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void show_ritems(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems; ++i)
|
2001-07-12 05:54:24 -07:00
|
|
|
printf("ritem[%d] = %d\n", i, ritem[i]);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* show_rrhs is used for debugging */
|
1995-08-08 09:44:29 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void show_rrhs(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nrules; ++i)
|
2001-07-12 05:54:24 -07:00
|
|
|
printf("rrhs[%d] = %d\n", i, rrhs[i]);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* show_shifts is used for debugging */
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void show_shifts(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
shifts *p;
|
|
|
|
int i, j, k;
|
|
|
|
|
|
|
|
k = 0;
|
|
|
|
for (p = first_shift; p; ++k, p = p->next)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
if (k) printf("\n");
|
|
|
|
printf("shift %d, number = %d, nshifts = %d\n", k, p->number,
|
|
|
|
p->nshifts);
|
|
|
|
j = p->nshifts;
|
|
|
|
for (i = 0; i < j; ++i)
|
|
|
|
printf("\t%d\n", p->shift[i]);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void save_shifts(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register shifts *p;
|
|
|
|
register short *sp1;
|
|
|
|
register short *sp2;
|
|
|
|
register short *send;
|
|
|
|
|
|
|
|
p = (shifts *) allocate((unsigned) (sizeof(shifts) +
|
2001-07-12 05:54:24 -07:00
|
|
|
(nshifts - 1) * sizeof(short)));
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
p->number = this_state->number;
|
|
|
|
p->nshifts = nshifts;
|
|
|
|
|
|
|
|
sp1 = shiftset;
|
|
|
|
sp2 = p->shift;
|
|
|
|
send = shiftset + nshifts;
|
|
|
|
|
|
|
|
while (sp1 < send)
|
2001-07-12 05:54:24 -07:00
|
|
|
*sp2++ = *sp1++;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
if (last_shift)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
last_shift->next = p;
|
|
|
|
last_shift = p;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
first_shift = p;
|
|
|
|
last_shift = p;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void save_reductions(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register short *isp;
|
|
|
|
register short *rp1;
|
|
|
|
register short *rp2;
|
|
|
|
register int item;
|
|
|
|
register int count;
|
|
|
|
register reductions *p;
|
|
|
|
register short *rend;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
for (isp = itemset; isp < itemsetend; isp++)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
item = ritem[*isp];
|
|
|
|
if (item < 0)
|
|
|
|
{
|
|
|
|
redset[count++] = -item;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
p = (reductions *) allocate((unsigned) (sizeof(reductions) +
|
|
|
|
(count - 1) * sizeof(short)));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void set_derives(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register int i, k;
|
|
|
|
register int lhs;
|
|
|
|
register short *rules;
|
|
|
|
|
|
|
|
derives = NEW2(nsyms, short *);
|
|
|
|
rules = NEW2(nvars + nrules, short);
|
|
|
|
|
|
|
|
k = 0;
|
|
|
|
for (lhs = start_symbol; lhs < nsyms; lhs++)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
derives[lhs] = rules + k;
|
|
|
|
for (i = 0; i < nrules; i++)
|
|
|
|
{
|
|
|
|
if (rlhs[i] == lhs)
|
|
|
|
{
|
|
|
|
rules[k] = i;
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rules[k] = -1;
|
|
|
|
k++;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-07-12 05:54:24 -07:00
|
|
|
#ifdef DEBUG
|
1995-05-04 03:15:53 -07:00
|
|
|
print_derives();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void free_derives(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
FREE(derives[start_symbol]);
|
|
|
|
FREE(derives);
|
|
|
|
}
|
|
|
|
|
2001-07-12 05:54:24 -07:00
|
|
|
#ifdef DEBUG
|
1997-09-02 05:55:01 -07:00
|
|
|
void print_derives(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register short *sp;
|
|
|
|
|
|
|
|
printf("\nDERIVES\n\n");
|
|
|
|
|
|
|
|
for (i = start_symbol; i < nsyms; i++)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
printf("%s derives ", symbol_name[i]);
|
|
|
|
for (sp = derives[i]; *sp >= 0; sp++)
|
|
|
|
{
|
|
|
|
printf(" %d", *sp);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void set_nullable(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register int i, j;
|
|
|
|
register int empty;
|
|
|
|
int done;
|
|
|
|
|
|
|
|
nullable = MALLOC(nsyms);
|
|
|
|
if (nullable == 0) no_space();
|
|
|
|
|
|
|
|
for (i = 0; i < nsyms; ++i)
|
2001-07-12 05:54:24 -07:00
|
|
|
nullable[i] = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
done = 0;
|
|
|
|
while (!done)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
done = 1;
|
|
|
|
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;
|
|
|
|
done = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (i = 0; i < nsyms; i++)
|
|
|
|
{
|
2001-07-12 05:54:24 -07:00
|
|
|
if (nullable[i])
|
|
|
|
printf("%s is nullable\n", symbol_name[i]);
|
|
|
|
else
|
|
|
|
printf("%s is not nullable\n", symbol_name[i]);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void free_nullable(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
FREE(nullable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void lr0(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
set_derives();
|
|
|
|
set_nullable();
|
|
|
|
generate_states();
|
|
|
|
}
|