diff options
author | Ted Unangst <tedu@cvs.openbsd.org> | 2014-03-13 00:59:35 +0000 |
---|---|---|
committer | Ted Unangst <tedu@cvs.openbsd.org> | 2014-03-13 00:59:35 +0000 |
commit | e6fd37871e59a0a101e0133296ad38b5b4f2964f (patch) | |
tree | 4c3b342ba24b55d7fb4627bb85a72726a944b5ee /usr.bin/yacc | |
parent | 090c516fe4d02de24a483386db75fef232e02720 (diff) |
last bits of indentation and style
Diffstat (limited to 'usr.bin/yacc')
-rw-r--r-- | usr.bin/yacc/lr0.c | 648 | ||||
-rw-r--r-- | usr.bin/yacc/mkpar.c | 478 |
2 files changed, 527 insertions, 599 deletions
diff --git a/usr.bin/yacc/lr0.c b/usr.bin/yacc/lr0.c index 3640f55ab5b..8ac14d0a8f7 100644 --- a/usr.bin/yacc/lr0.c +++ b/usr.bin/yacc/lr0.c @@ -1,5 +1,5 @@ -/* $OpenBSD: lr0.c,v 1.16 2014/01/13 23:14:17 millert Exp $ */ -/* $NetBSD: lr0.c,v 1.4 1996/03/19 03:21:35 jtc Exp $ */ +/* $OpenBSD: lr0.c,v 1.17 2014/03/13 00:59:34 tedu Exp $ */ +/* $NetBSD: lr0.c,v 1.4 1996/03/19 03:21:35 jtc Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. @@ -79,120 +79,113 @@ static short *kernel_items; void allocate_itemsets(void) { - short *itemp; - short *item_end; - int symbol; - int i; - int count; - int max; - 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) - { - count++; - symbol_count[symbol]++; + short *itemp; + short *item_end; + int symbol; + int i; + int count; + int max; + 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) { + 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]; } - } - - 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 *); + + shift_symbol = symbol_count; + kernel_end = NEW2(nsyms, short *); } void allocate_storage(void) { - 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 *); } void append_states(void) { - int i; - int j; - int symbol; + int i; + int j; + int symbol; #ifdef TRACE - fprintf(stderr, "Entering append_states()\n"); + 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--; + 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); } - shift_symbol[j] = symbol; - } - - for (i = 0; i < nshifts; i++) - { - symbol = shift_symbol[i]; - shiftset[i] = get_state(symbol); - } } void free_storage(void) { - 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); } void generate_states(void) { - 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; - } - - finalize_closure(); - free_storage(); + 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; + } + + finalize_closure(); + free_storage(); } @@ -200,125 +193,111 @@ generate_states(void) short get_state(int symbol) { - int key; - short *isp1; - short *isp2; - short *iend; - core *sp; - int found; - int n; + int key; + short *isp1; + short *isp2; + short *iend; + core *sp; + int found; + int n; #ifdef TRACE - fprintf(stderr, "Entering get_state(%d)\n", symbol); + 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) - { - 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; + isp1 = kernel_base[symbol]; + iend = kernel_end[symbol]; + n = iend - isp1; + + 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 - { - sp = sp->link = new_state(symbol); - found = 1; - } - } + } else { + state_set[key] = sp = new_state(symbol); } - } - else - { - state_set[key] = sp = new_state(symbol); - } - return (sp->number); + return (sp->number); } void initialize_states(void) { - int i; - short *start_derives; - core *p; + int i; + short *start_derives; + core *p; - start_derives = derives[start_symbol]; - for (i = 0; start_derives[i] >= 0; ++i) - continue; + start_derives = derives[start_symbol]; + for (i = 0; start_derives[i] >= 0; ++i) + continue; - p = (core *) malloc(sizeof(core) + i*sizeof(short)); - if (p == 0) no_space(); + p = malloc(sizeof(core) + i * sizeof(short)); + if (p == NULL) + no_space(); - p->next = 0; - p->link = 0; - p->number = 0; - p->accessing_symbol = 0; - p->nitems = i; + p->next = 0; + p->link = 0; + p->number = 0; + p->accessing_symbol = 0; + p->nitems = i; - for (i = 0; start_derives[i] >= 0; ++i) - p->items[i] = rrhs[start_derives[i]]; + for (i = 0; start_derives[i] >= 0; ++i) + p->items[i] = rrhs[start_derives[i]]; - first_state = last_state = this_state = p; - nstates = 1; + first_state = last_state = this_state = p; + nstates = 1; } void new_itemsets(void) { - int i; - int shiftcount; - short *isp; - short *ksp; - int symbol; - - memset(kernel_end, 0, nsyms * sizeof(short *)); - - 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]; - } - - *ksp++ = i + 1; - kernel_end[symbol] = ksp; + int i; + int shiftcount; + short *isp; + short *ksp; + int symbol; + + memset(kernel_end, 0, nsyms * sizeof(short *)); + + 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]; + } + *ksp++ = i + 1; + kernel_end[symbol] = ksp; + } } - } - nshifts = shiftcount; + nshifts = shiftcount; } @@ -326,239 +305,220 @@ new_itemsets(void) core * new_state(int symbol) { - int n; - core *p; - short *isp1; - short *isp2; - short *iend; + int n; + core *p; + short *isp1; + short *isp2; + short *iend; #ifdef TRACE - fprintf(stderr, "Entering new_state(%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 = allocate(sizeof(core) + (n - 1) * sizeof(short)); - p->accessing_symbol = symbol; - p->number = nstates; - p->nitems = n; + p = allocate(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); } void save_shifts(void) { - shifts *p; - short *sp1; - short *sp2; - short *send; - - p = allocate(sizeof(shifts) + (nshifts - 1) * sizeof(short)); - - p->number = this_state->number; - p->nshifts = nshifts; - - 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; - } + shifts *p; + short *sp1; + short *sp2; + short *send; + + p = allocate(sizeof(shifts) + (nshifts - 1) * sizeof(short)); + + p->number = this_state->number; + p->nshifts = nshifts; + + 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; + } } void save_reductions(void) { - short *isp; - short *rp1; - short *rp2; - int item; - int count; - reductions *p; - short *rend; - - count = 0; - for (isp = itemset; isp < itemsetend; isp++) - { - item = ritem[*isp]; - if (item < 0) - { - redset[count++] = -item; + short *isp; + short *rp1; + short *rp2; + int item; + int count; + reductions *p; + short *rend; + + count = 0; + for (isp = itemset; isp < itemsetend; isp++) { + item = ritem[*isp]; + if (item < 0) { + redset[count++] = -item; + } } - } - if (count) - { - p = allocate(sizeof(reductions) + (count - 1) * sizeof(short)); + if (count) { + p = allocate(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) - { - last_reduction->next = p; - last_reduction = p; - } - else - { - first_reduction = p; - last_reduction = p; + if (last_reduction) { + last_reduction->next = p; + last_reduction = p; + } else { + first_reduction = p; + last_reduction = p; + } } - } } void set_derives(void) { - int i, k; - int lhs; - short *rules; - - 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; + int i, k; + int lhs; + short *rules; + + 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++; - } } - rules[k] = -1; - k++; - } #ifdef DEBUG - print_derives(); + print_derives(); #endif } void free_derives(void) { - free(derives[start_symbol]); - free(derives); + free(derives[start_symbol]); + free(derives); } #ifdef DEBUG void print_derives(void) { - int i; - short *sp; + int i; + short *sp; - printf("\nDERIVES\n\n"); + 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); + 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'); - } - putchar('\n'); + putchar('\n'); } #endif void set_nullable(void) { - int i, j; - int empty; - int done; - - nullable = calloc(1, nsyms); - if (nullable == 0) no_space(); - - done = 0; - while (!done) - { - 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; + int i, j; + int empty; + int done; + + nullable = calloc(1, nsyms); + if (nullable == NULL) + no_space(); + + done = 0; + while (!done) { + 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; + } + } } - } } - } #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]); - } + 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 } void free_nullable(void) { - free(nullable); + free(nullable); } void lr0(void) { - set_derives(); - set_nullable(); - generate_states(); + set_derives(); + set_nullable(); + generate_states(); } diff --git a/usr.bin/yacc/mkpar.c b/usr.bin/yacc/mkpar.c index a08f25a5d5d..9b2373d9561 100644 --- a/usr.bin/yacc/mkpar.c +++ b/usr.bin/yacc/mkpar.c @@ -1,5 +1,5 @@ -/* $OpenBSD: mkpar.c,v 1.17 2014/01/13 23:14:18 millert Exp $ */ -/* $NetBSD: mkpar.c,v 1.4 1996/03/19 03:21:39 jtc Exp $ */ +/* $OpenBSD: mkpar.c,v 1.18 2014/03/13 00:59:34 tedu Exp $ */ +/* $NetBSD: mkpar.c,v 1.4 1996/03/19 03:21:39 jtc Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. @@ -67,336 +67,304 @@ void defreds(void); void make_parser(void) { - int i; - - parser = NEW2(nstates, action *); - for (i = 0; i < nstates; i++) - parser[i] = parse_actions(i); - - find_final_state(); - remove_conflicts(); - unused_rules(); - if (SRtotal + RRtotal > 0) total_conflicts(); - defreds(); + int i; + + parser = NEW2(nstates, action *); + for (i = 0; i < nstates; i++) + parser[i] = parse_actions(i); + + find_final_state(); + remove_conflicts(); + unused_rules(); + if (SRtotal + RRtotal > 0) + total_conflicts(); + defreds(); } action * parse_actions(int stateno) { - action *actions; + action *actions; - actions = get_shifts(stateno); - actions = add_reductions(stateno, actions); - return (actions); + actions = get_shifts(stateno); + actions = add_reductions(stateno, actions); + return (actions); } action * get_shifts(int stateno) { - action *actions, *temp; - shifts *sp; - short *to_state; - int i, k; - int symbol; - - actions = 0; - sp = shift_table[stateno]; - if (sp) - { - to_state = sp->shift; - for (i = sp->nshifts - 1; i >= 0; i--) - { - k = to_state[i]; - 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; - } + action *actions, *temp; + shifts *sp; + short *to_state; + int i, k; + int symbol; + + actions = 0; + sp = shift_table[stateno]; + if (sp) { + to_state = sp->shift; + for (i = sp->nshifts - 1; i >= 0; i--) { + k = to_state[i]; + 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); + return (actions); } action * -add_reductions(int stateno, action *actions) +add_reductions(int stateno, action * actions) { - int i, j, m, n; - int ruleno, tokensetsize; - unsigned *rowp; - - 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); + int i, j, m, n; + int ruleno, tokensetsize; + unsigned *rowp; + + 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); + return (actions); } action * -add_reduce(action *actions, int ruleno, int symbol) +add_reduce(action * actions, int ruleno, int symbol) { - action *temp, *prev, *next; - - 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 && - next->action_code == REDUCE && next->number < ruleno) - { - prev = next; - next = next->next; - } - - temp = NEW(action); - temp->next = next; - temp->symbol = symbol; - temp->number = ruleno; - temp->prec = rprec[ruleno]; - temp->action_code = REDUCE; - temp->assoc = rassoc[ruleno]; - - if (prev) - prev->next = temp; - else - actions = temp; - - return (actions); + action *temp, *prev, *next; + + 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 && + next->action_code == REDUCE && next->number < ruleno) { + prev = next; + next = next->next; + } + + temp = NEW(action); + temp->next = next; + temp->symbol = symbol; + temp->number = ruleno; + temp->prec = rprec[ruleno]; + temp->action_code = REDUCE; + temp->assoc = rassoc[ruleno]; + + if (prev) + prev->next = temp; + else + actions = temp; + + return (actions); } void find_final_state(void) { - int goal, i; - short *to_state; - shifts *p; - - p = shift_table[0]; - to_state = p->shift; - goal = ritem[1]; - for (i = p->nshifts - 1; i >= 0; --i) - { - final_state = to_state[i]; - if (accessing_symbol[final_state] == goal) break; - } + int goal, i; + short *to_state; + shifts *p; + + p = shift_table[0]; + to_state = p->shift; + goal = ritem[1]; + for (i = p->nshifts - 1; i >= 0; --i) { + final_state = to_state[i]; + if (accessing_symbol[final_state] == goal) + break; + } } void unused_rules(void) { - int i; - action *p; - - rules_used = calloc(nrules, sizeof(short)); - if (rules_used == NULL) no_space(); - - 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; + int i; + action *p; + + rules_used = calloc(nrules, sizeof(short)); + if (rules_used == NULL) + no_space(); + + 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) - if (!rules_used[i]) ++nunused; + nunused = 0; + for (i = 3; i < nrules; ++i) + if (!rules_used[i]) + ++nunused; - if (nunused) { - if (nunused == 1) - fprintf(stderr, "%s: 1 rule never reduced\n", __progname); - else - fprintf(stderr, "%s: %d rules never reduced\n", __progname, - nunused); - } + if (nunused) { + if (nunused == 1) + fprintf(stderr, "%s: 1 rule never reduced\n", __progname); + else + fprintf(stderr, "%s: %d rules never reduced\n", __progname, + nunused); + } } void remove_conflicts(void) { - int i; - int symbol; - action *p, *pref = NULL; - - SRtotal = 0; - RRtotal = 0; - SRconflicts = NEW2(nstates, short); - RRconflicts = NEW2(nstates, short); - 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; - } - 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; - } + int i; + int symbol; + action *p, *pref = NULL; + + SRtotal = 0; + RRtotal = 0; + SRconflicts = NEW2(nstates, short); + RRconflicts = NEW2(nstates, short); + 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; + } 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; + } } - else - { - SRcount++; - p->suppressed = 1; - } - } - else - { - RRcount++; - p->suppressed = 1; - } + SRtotal += SRcount; + RRtotal += RRcount; + SRconflicts[i] = SRcount; + RRconflicts[i] = RRcount; } - SRtotal += SRcount; - RRtotal += RRcount; - SRconflicts[i] = SRcount; - RRconflicts[i] = RRcount; - } } void total_conflicts(void) { - /* Warn if s/r != expect or if any r/r */ - if ((SRtotal != SRexpect) || RRtotal) - { - if (SRtotal == 1) - fprintf(stderr, "%s: %s finds 1 shift/reduce conflict\n", - input_file_name, __progname); - else if (SRtotal > 1) - fprintf(stderr, "%s: %s finds %d shift/reduce conflicts\n", - input_file_name, __progname, SRtotal); - } - - if (RRtotal == 1) - fprintf(stderr, "%s: %s finds 1 reduce/reduce conflict\n", - input_file_name, __progname); - else if (RRtotal > 1) - fprintf(stderr, "%s: %s finds %d reduce/reduce conflicts\n", - input_file_name, __progname, RRtotal); + /* Warn if s/r != expect or if any r/r */ + if ((SRtotal != SRexpect) || RRtotal) { + if (SRtotal == 1) + fprintf(stderr, "%s: %s finds 1 shift/reduce conflict\n", + input_file_name, __progname); + else if (SRtotal > 1) + fprintf(stderr, "%s: %s finds %d shift/reduce conflicts\n", + input_file_name, __progname, SRtotal); + } + if (RRtotal == 1) + fprintf(stderr, "%s: %s finds 1 reduce/reduce conflict\n", + input_file_name, __progname); + else if (RRtotal > 1) + fprintf(stderr, "%s: %s finds %d reduce/reduce conflicts\n", + input_file_name, __progname, RRtotal); } short sole_reduction(int stateno) { - int count; - short ruleno; - action *p; - - count = 0; - ruleno = 0; - 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; + int count; + short ruleno; + action *p; + + count = 0; + ruleno = 0; + 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); + if (count == 0) + return (0); + return (ruleno); } void defreds(void) { - int i; + int i; - defred = NEW2(nstates, short); - for (i = 0; i < nstates; i++) - defred[i] = sole_reduction(i); + defred = NEW2(nstates, short); + for (i = 0; i < nstates; i++) + defred[i] = sole_reduction(i); } void -free_action_row(action *p) +free_action_row(action * p) { - action *q; - - while (p) - { - q = p->next; - free(p); - p = q; - } + action *q; + + while (p) { + q = p->next; + free(p); + p = q; + } } void free_parser(void) { - int i; + int i; - for (i = 0; i < nstates; i++) - free_action_row(parser[i]); + for (i = 0; i < nstates; i++) + free_action_row(parser[i]); - free(parser); + free(parser); } |