+
#include "defs.h"
extern short *itemset;
allocate_itemsets()
{
- 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++)
+ 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++)
{
- symbol = *itemp;
- if (symbol >= 0)
+ symbol = *itemp;
+ if (symbol >= 0)
{
- count++;
- symbol_count[symbol]++;
+ count++;
+ symbol_count[symbol]++;
}
}
- kernel_base = NEW2(nsyms, short *);
- kernel_items = NEW2(count, short);
+ kernel_base = NEW2(nsyms, short *);
+ kernel_items = NEW2(count, short);
- count = 0;
- max = 0;
- for (i = 0; i < nsyms; i++)
+ 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];
+ 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 *);
+ shift_symbol = symbol_count;
+ kernel_end = NEW2(nsyms, short *);
}
-
allocate_storage()
{
- allocate_itemsets();
-
- shiftset = NEW2(nsyms, short);
- redset = NEW2(nrules + 1, short);
- state_set = NEW2(nitems, core *);
+ allocate_itemsets();
+ shiftset = NEW2(nsyms, short);
+ redset = NEW2(nrules + 1, short);
+ state_set = NEW2(nitems, core *);
}
-
append_states()
{
- register int i;
- register int j;
- register int symbol;
+ register int i;
+ register int j;
+ register int symbol;
#ifdef TRACE
- fprintf(stderr, "Entering append_states\n");
+ fprintf(stderr, "Entering append_states()\n");
#endif
-
- for (i = 1; i < nshifts; i++)
+ for (i = 1; i < nshifts; i++)
{
- symbol = shift_symbol[i];
- j = i;
- while (j > 0 && shift_symbol[j - 1] > symbol)
+ 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] = shift_symbol[j - 1];
+ j--;
}
- shift_symbol[j] = symbol;
+ shift_symbol[j] = symbol;
}
- for (i = 0; i < nshifts; i++)
+ for (i = 0; i < nshifts; i++)
{
- symbol = shift_symbol[i];
- shiftset[i] = get_state(symbol);
+ symbol = shift_symbol[i];
+ shiftset[i] = get_state(symbol);
}
}
free_storage()
{
- FREE(shift_symbol);
- FREE(redset);
- FREE(shiftset);
- FREE(kernel_base);
- FREE(kernel_end);
- FREE(kernel_items);
- FREE(state_set);
+ FREE(shift_symbol);
+ FREE(redset);
+ FREE(shiftset);
+ FREE(kernel_base);
+ FREE(kernel_end);
+ FREE(kernel_items);
+ FREE(state_set);
}
generate_states()
{
- allocate_storage();
- itemset = NEW2(nitems, short);
- ruleset = NEW2(WORDSIZE(nrules), unsigned);
- set_first_derives();
- initialize_states();
+ allocate_storage();
+ itemset = NEW2(nitems, short);
+ ruleset = NEW2(WORDSIZE(nrules), unsigned);
+ set_first_derives();
+ initialize_states();
- while (this_state)
+ while (this_state)
{
- closure(this_state->items, this_state->nitems);
- save_reductions();
- new_itemsets();
- append_states();
+ closure(this_state->items, this_state->nitems);
+ save_reductions();
+ new_itemsets();
+ append_states();
- if (nshifts > 0)
- save_shifts();
+ if (nshifts > 0)
+ save_shifts();
- this_state = this_state->next;
+ this_state = this_state->next;
}
- finalize_closure();
- free_storage();
+ finalize_closure();
+ free_storage();
}
get_state(symbol)
int symbol;
{
- register int key;
- register short *isp1;
- register short *isp2;
- register short *iend;
- register core *sp;
- register int found;
-
- int n;
+ register int key;
+ register short *isp1;
+ register short *isp2;
+ register short *iend;
+ register core *sp;
+ register int found;
+ register int n;
#ifdef TRACE
- fprintf(stderr, "Entering get_state, symbol = %d\n", symbol);
+ fprintf(stderr, "Entering get_state(%d)\n", symbol);
#endif
- isp1 = kernel_base[symbol];
- iend = kernel_end[symbol];
- n = iend - isp1;
+ isp1 = kernel_base[symbol];
+ iend = kernel_end[symbol];
+ n = iend - isp1;
- key = *isp1;
- assert(0 <= key && key < nitems);
- sp = state_set[key];
- if (sp)
+ key = *isp1;
+ assert(0 <= key && key < nitems);
+ sp = state_set[key];
+ if (sp)
{
- found = 0;
- while (!found)
+ found = 0;
+ while (!found)
{
- if (sp->nitems == n)
+ if (sp->nitems == n)
{
- found = 1;
- isp1 = kernel_base[symbol];
- isp2 = sp->items;
+ found = 1;
+ isp1 = kernel_base[symbol];
+ isp2 = sp->items;
- while (found && isp1 < iend)
+ while (found && isp1 < iend)
{
- if (*isp1++ != *isp2++)
- found = 0;
+ if (*isp1++ != *isp2++)
+ found = 0;
}
}
- if (!found)
+ if (!found)
{
- if (sp->link)
+ if (sp->link)
{
- sp = sp->link;
+ sp = sp->link;
}
- else
+ else
{
- sp = sp->link = new_state(symbol);
- found = 1;
+ sp = sp->link = new_state(symbol);
+ found = 1;
}
}
}
}
- else
+ else
{
- state_set[key] = sp = new_state(symbol);
+ state_set[key] = sp = new_state(symbol);
}
- return (sp->number);
+ return (sp->number);
}
new_itemsets()
{
- register int i;
- register int shiftcount;
- register short *isp;
- register short *ksp;
- register int symbol;
-
- for (i = 0; i < nsyms; i++)
- kernel_end[i] = 0;
-
- shiftcount = 0;
- isp = itemset;
- while (isp < itemsetend)
+ register int i;
+ register int shiftcount;
+ register short *isp;
+ register short *ksp;
+ register int symbol;
+
+ 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)
+ i = *isp++;
+ symbol = ritem[i];
+ if (symbol > 0)
{
- ksp = kernel_end[symbol];
-
- if (!ksp)
+ ksp = kernel_end[symbol];
+ if (!ksp)
{
- shift_symbol[shiftcount++] = symbol;
- ksp = kernel_base[symbol];
+ shift_symbol[shiftcount++] = symbol;
+ ksp = kernel_base[symbol];
}
- *ksp++ = i + 1;
- kernel_end[symbol] = ksp;
+ *ksp++ = i + 1;
+ kernel_end[symbol] = ksp;
}
}
- nshifts = shiftcount;
+ nshifts = shiftcount;
}
new_state(symbol)
int symbol;
{
- register int n;
- register core *p;
- register short *isp1;
- register short *isp2;
- register short *iend;
+ register int n;
+ register core *p;
+ register short *isp1;
+ register short *isp2;
+ register short *iend;
#ifdef TRACE
- fprintf(stderr, "Entering new_state, symbol = %d\n", symbol);
+ fprintf(stderr, "Entering new_state(%d)\n", symbol);
#endif
- if (nstates >= MAXSHORT)
- fatal("too many states");
+ if (nstates >= MAXSHORT)
+ fatal("too many states");
- isp1 = kernel_base[symbol];
- iend = kernel_end[symbol];
- n = iend - isp1;
+ 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;
+ 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)
- *isp2++ = *isp1++;
+ isp2 = p->items;
+ while (isp1 < iend)
+ *isp2++ = *isp1++;
- last_state->next = p;
- last_state = p;
+ last_state->next = p;
+ last_state = p;
- nstates++;
+ nstates++;
- return (p);
+ return (p);
}
save_shifts()
{
- register shifts *p;
- register short *sp1;
- register short *sp2;
- register short *send;
+ register shifts *p;
+ register short *sp1;
+ register short *sp2;
+ register short *send;
- p = (shifts *) allocate((unsigned) (sizeof(shifts) +
+ p = (shifts *) allocate((unsigned) (sizeof(shifts) +
(nshifts - 1) * sizeof(short)));
- p->number = this_state->number;
- p->nshifts = nshifts;
+ p->number = this_state->number;
+ p->nshifts = nshifts;
- sp1 = shiftset;
- sp2 = p->shift;
- send = shiftset + nshifts;
+ sp1 = shiftset;
+ sp2 = p->shift;
+ send = shiftset + nshifts;
- while (sp1 < send)
- *sp2++ = *sp1++;
+ while (sp1 < send)
+ *sp2++ = *sp1++;
- if (last_shift)
+ if (last_shift)
{
- last_shift->next = p;
- last_shift = p;
+ last_shift->next = p;
+ last_shift = p;
}
- else
+ else
{
- first_shift = p;
- last_shift = p;
+ first_shift = p;
+ last_shift = p;
}
}
save_reductions()
{
- register short *isp;
- register short *rp1;
- register short *rp2;
- register int item;
- register int count;
- register reductions *p;
-
- short *rend;
-
- count = 0;
- for (isp = itemset; isp < itemsetend; isp++)
+ 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++)
{
- item = ritem[*isp];
- if (item < 0)
+ item = ritem[*isp];
+ if (item < 0)
{
- redset[count++] = -item;
+ redset[count++] = -item;
}
}
- if (count)
+ if (count)
{
- p = (reductions *) allocate((unsigned) (sizeof(reductions) +
+ p = (reductions *) allocate((unsigned) (sizeof(reductions) +
(count - 1) * sizeof(short)));
- p->number = this_state->number;
- p->nreds = count;
+ p->number = this_state->number;
+ p->nreds = count;
- rp1 = redset;
- rp2 = p->rules;
- rend = rp1 + count;
+ rp1 = redset;
+ rp2 = p->rules;
+ rend = rp1 + count;
- while (rp1 < rend)
- *rp2++ = *rp1++;
+ while (rp1 < rend)
+ *rp2++ = *rp1++;
- if (last_reduction)
+ if (last_reduction)
{
- last_reduction->next = p;
- last_reduction = p;
+ last_reduction->next = p;
+ last_reduction = p;
}
- else
+ else
{
- first_reduction = p;
- last_reduction = p;
+ first_reduction = p;
+ last_reduction = p;
}
}
}
set_derives()
{
- register int i, k;
- register int lhs;
- register short *rules;
+ register int i, k;
+ register int lhs;
+ register short *rules;
- derives = NEW2(nsyms, short *);
- rules = NEW2(nvars + nrules, short);
+ derives = NEW2(nsyms, short *);
+ rules = NEW2(nvars + nrules, short);
- k = 0;
- for (lhs = start_symbol; lhs < nsyms; lhs++)
+ k = 0;
+ for (lhs = start_symbol; lhs < nsyms; lhs++)
{
- derives[lhs] = rules + k;
- for (i = 0; i < nrules; i++)
+ derives[lhs] = rules + k;
+ for (i = 0; i < nrules; i++)
{
- if (rlhs[i] == lhs)
+ if (rlhs[i] == lhs)
{
- rules[k] = i;
- k++;
+ rules[k] = i;
+ k++;
}
}
- rules[k] = -1;
- k++;
+ rules[k] = -1;
+ k++;
}
#ifdef DEBUG
- print_derives();
+ print_derives();
#endif
}
free_derives()
{
- FREE(derives[start_symbol]);
- FREE(derives);
+ FREE(derives[start_symbol]);
+ FREE(derives);
}
#ifdef DEBUG
print_derives()
{
- register int i;
- register short *sp;
+ register int i;
+ register short *sp;
- printf("\nDERIVES\n\n");
+ printf("\nDERIVES\n\n");
- for (i = start_symbol; i < nsyms; i++)
+ for (i = start_symbol; i < nsyms; i++)
{
- printf("%s derives ", symbol_name[i]);
- for (sp = derives[i]; *sp >= 0; sp++)
+ printf("%s derives ", symbol_name[i]);
+ for (sp = derives[i]; *sp >= 0; sp++)
{
- printf(" %d", *sp);
+ printf(" %d", *sp);
}
- putchar('\n');
+ putchar('\n');
}
- putchar('\n');
+ putchar('\n');
}
#endif
free_nullable()
{
- FREE(nullable);
+ FREE(nullable);
}
+
#include "defs.h"
action **parser;
{
register int i;
register int symbol;
- register action *p, *q;
+ register action *p, *pref;
SRtotal = 0;
RRtotal = 0;
{
SRcount = 0;
RRcount = 0;
- for (p = parser[i]; p; p = q->next)
+ symbol = -1;
+ for (p = parser[i]; p; p = p->next)
{
- symbol = p->symbol;
- q = p;
- while (q->next && q->next->symbol == symbol)
- q = q->next;
- if (i == final_state && symbol == 0)
- end_conflicts(p, q);
- else if (p != q)
- resolve_conflicts(p, q);
+ if (p->symbol != symbol)
+ {
+ pref = p;
+ symbol = p->symbol;
+ }
+ else if (i == final_state && symbol == 0)
+ {
+ SRcount++;
+ p->suppressed = 1;
+ }
+ else if (pref->action_code == SHIFT)
+ {
+ if (pref->prec > 0 && p->prec > 0)
+ {
+ 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)
+ {
+ p->suppressed = 2;
+ }
+ else
+ {
+ pref->suppressed = 2;
+ p->suppressed = 2;
+ }
+ }
+ else
+ {
+ SRcount++;
+ p->suppressed = 1;
+ }
+ }
+ else
+ {
+ RRcount++;
+ p->suppressed = 1;
+ }
}
SRtotal += SRcount;
RRtotal += RRcount;
}
-end_conflicts(p, q)
-register action *p, *q;
-{
- for (;;)
- {
- SRcount++;
- p->suppressed = 1;
- if (p == q) break;
- p = p->next;
- }
-}
-
-
-resolve_conflicts(first, last)
-register action *first, *last;
-{
- register action *p;
- register int count;
-
- count = 1;
- for (p = first; p != last; p = p->next)
- ++count;
- assert(count > 1);
-
- if (first->action_code == SHIFT && count == 2 &&
- first->prec > 0 && last->prec > 0)
- {
- if (first->prec == last->prec)
- {
- if (first->assoc == LEFT)
- first->suppressed = 2;
- else if (first->assoc == RIGHT)
- last->suppressed = 2;
- else
- {
- first->suppressed = 2;
- last->suppressed = 2;
- first->action_code = ERROR;
- last->action_code = ERROR;
- }
- }
- else if (first->prec < last->prec)
- first->suppressed = 2;
- else
- last->suppressed = 2;
- }
- else
- {
- if (first->action_code == SHIFT)
- SRcount += (count - 1);
- else
- RRcount += (count - 1);
- for (p = first; p != last; p = p->next, p->suppressed = 1)
- continue;
- }
-}
-
-
total_conflicts()
{
fprintf(stderr, "%s: ", myname);
FREE(parser);
}
+
+
#include "defs.h"
print_conflicts(state)
int state;
{
- register int symbol;
- register action *p, *q, *r;
+ register int symbol, act, number;
+ register action *p;
- for (p = parser[state]; p; p = q->next)
+ symbol = -1;
+ for (p = parser[state]; p; p = p->next)
{
- q = p;
- if (p->action_code == ERROR || p->suppressed == 2)
+ if (p->suppressed == 2)
continue;
- symbol = p->symbol;
- while (q->next && q->next->symbol == symbol)
- q = q->next;
- if (state == final_state && symbol == 0)
+ if (p->symbol != symbol)
{
- r = p;
- for (;;)
- {
- fprintf(verbose_file, "%d: shift/reduce conflict \
-(accept, reduce %d) on $end\n", state, r->number - 2);
- if (r == q) break;
- r = r->next;
- }
+ symbol = p->symbol;
+ number = p->number;
+ if (p->action_code == SHIFT)
+ act = SHIFT;
+ else
+ act = REDUCE;
}
- else if (p != q)
+ else if (p->suppressed == 1)
{
- r = p->next;
- if (p->action_code == SHIFT)
+ if (state == final_state && symbol == 0)
{
- for (;;)
- {
- if (r->action_code == REDUCE && p->suppressed != 2)
- fprintf(verbose_file, "%d: shift/reduce conflict \
-(shift %d, reduce %d) on %s\n", state, p->number, r->number - 2,
- symbol_name[symbol]);
- if (r == q) break;
- r = r->next;
- }
+ fprintf(verbose_file, "%d: shift/reduce conflict \
+(accept, reduce %d) on $end\n", state, p->number - 2);
}
else
{
- for (;;)
+ if (act == SHIFT)
{
- if (r->action_code == REDUCE && p->suppressed != 2)
- fprintf(verbose_file, "%d: reduce/reduce conflict \
-(reduce %d, reduce %d) on %s\n", state, p->number - 2, r->number - 2,
- symbol_name[symbol]);
- if (r == q) break;
- r = r->next;
+ 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]);
}
}
}
fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);
}
}
+