Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!husc6!think!ames!pasteur!ucbvax!hplabs!felix!dhw68k!macintosh From: wetter@tybalt.caltech.edu (Pierce T. Wetter) Newsgroups: comp.sources.mac Subject: Bison Macintosh Sources (part 2 of 6) Message-ID: <8002@dhw68k.cts.com> Date: 15 May 88 08:05:20 GMT References: <7986@dhw68k.cts.com> Sender: macintosh@dhw68k.cts.com Organization: California Institute of Technology Lines: 1722 Approved: bytebug@dhw68k.cts.com (Roger L. Long) [Bison Macintosh Sources - part 2 of 6] --- #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files: # Bison/allocate.c # Bison/closure.c # Bison/conflicts.c # Bison/derives.c # Bison/files.c # Bison/getargs.c # This archive created: Sat May 14 02:41:46 1988 # By: Roger L. Long (bytebug@dhw68k.cts.com) export PATH; PATH=/bin:$PATH echo shar: extracting "'allocate.c'" '(1332 characters)' if test -f 'allocate.c' then echo shar: will not over-write existing file "'allocate.c'" else sed 's/^X//' << \SHAR_EOF > 'allocate.c' X/* Allocate and clear storage for bison, X Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc. X XBISON is distributed in the hope that it will be useful, but WITHOUT ANY XWARRANTY. No author or distributor accepts responsibility to anyone Xfor the consequences of using it or for whether it serves any Xparticular purpose or works at all, unless he says so in writing. XRefer to the BISON General Public License for full details. X XEveryone is granted permission to copy, modify and redistribute BISON, Xbut only under the conditions described in the BISON General Public XLicense. A copy of this license is supposed to have been given to you Xalong with BISON so you can know your rights and responsibilities. It Xshould be in a file named COPYING. Among other things, the copyright Xnotice and this notice must be preserved on all copies. X X In other words, you are welcome to use, share and improve this program. X You are forbidden to forbid anyone else to use, share and improve X what you give them. Help stamp out software-hoarding! */ X X#include X X#define __SEG__ files X Xchar * Xallocate(n) Xregister unsigned n; X{ X register char *block; X X extern char *calloc(); X X block = calloc(n,1); X if (block == NULL) X { X fprintf(stderr, "bison: memory exhausted\n"); X done(1); X } X X return (block); X} SHAR_EOF if test 1332 -ne "`wc -c < 'allocate.c'`" then echo shar: error transmitting "'allocate.c'" '(should have been 1332 characters)' fi fi # end of overwriting check echo shar: extracting "'closure.c'" '(7374 characters)' if test -f 'closure.c' then echo shar: will not over-write existing file "'closure.c'" else sed 's/^X//' << \SHAR_EOF > 'closure.c' X/* Subroutines for bison X Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc. X XBISON is distributed in the hope that it will be useful, but WITHOUT ANY XWARRANTY. No author or distributor accepts responsibility to anyone Xfor the consequences of using it or for whether it serves any Xparticular purpose or works at all, unless he says so in writing. XRefer to the BISON General Public License for full details. X XEveryone is granted permission to copy, modify and redistribute BISON, Xbut only under the conditions described in the BISON General Public XLicense. A copy of this license is supposed to have been given to you Xalong with BISON so you can know your rights and responsibilities. It Xshould be in a file named COPYING. Among other things, the copyright Xnotice and this notice must be preserved on all copies. X X In other words, you are welcome to use, share and improve this program. X You are forbidden to forbid anyone else to use, share and improve X what you give them. Help stamp out software-hoarding! */ X X/* subroutines of file LR0.c. X XEntry points: X X closure (items, n) X XGiven a vector of item numbers items, of length n, Xset up ruleset and itemset to indicate what rules could be run Xand which items could be accepted when those items are the active ones. X Xruleset contains a bit for each rule. closure sets the bits Xfor all rules which could potentially describe the next input to be read. X Xitemset is a vector of item numbers; itemsetend points to just beyond the end X of the part of it that is significant. Xclosure places there the indices of all items which represent units of Xinput that could arrive next. X X initialize_closure (n) X XAllocates the itemset and ruleset vectors, Xand precomputes useful data so that closure can be called. Xn is the number of elements to allocate for itemset. X X finalize_closure () X XFrees itemset, ruleset and internal data. X X*/ X X#include X#include "machine.h" X#include "new.h" X#include "gram.h" X X Xextern short **derives; X X Xshort *itemset; Xshort *itemsetend; Xstatic unsigned *ruleset; X X/* internal data. See comments before set_fderives and set_firsts. */ Xstatic unsigned *fderives; Xstatic unsigned *firsts; X X/* number of words required to hold a bit for each rule */ Xstatic int rulesetsize; X X/* number of words required to hold a bit for each variable */ Xstatic int varsetsize; X X X Xinitialize_closure(n) Xint n; X{ X itemset = NEW2(n, short); X X rulesetsize = WORDSIZE(nrules + 1); X ruleset = NEW2(rulesetsize, unsigned); X X set_fderives(); X} X X X X/* set fderives to an nvars by nrules matrix of bits X indicating which rules can help derive the beginning of the data X for each nonterminal. For example, if symbol 5 can be derived as X the sequence of symbols 8 3 20, and one of the rules for deriving X symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */ X Xset_fderives() X{ X register unsigned *rrow; X register unsigned *vrow; X register int j; X register unsigned mask; X register unsigned cword; X register short *rp; X X int ruleno; X int i; X X fderives = NEW2(nvars * rulesetsize, unsigned) - ntokens * rulesetsize; X X set_firsts(); X X rrow = fderives + ntokens * rulesetsize; X X for (i = ntokens; i < nsyms; i++) X { X vrow = firsts + ((i - ntokens) * varsetsize); X cword = *vrow++; X mask = 1; X for (j = ntokens; j < nsyms; j++) X { X if (cword & mask) X { X rp = derives[j]; X while ((ruleno = *rp++) > 0) X { X SETBIT(rrow, ruleno); X } X } X X mask <<= 1; X if (mask == 0 && j + 1 < nsyms) X { X cword = *vrow++; X mask = 1; X } X } X X vrow += varsetsize; X rrow += rulesetsize; X } X X#ifdef DEBUG X print_fderives(); X#endif X X FREE(firsts); X} X X X X/* set firsts to be an nvars by nvars bit matrix indicating which items X can represent the beginning of the input corresponding to which other items. X For example, if some rule expands symbol 5 into the sequence of symbols 8 3 20, X the symbol 8 can be the beginning of the data for symbol 5, X so the bit [8 - ntokens, 5 - ntokens] in firsts is set. */ X Xset_firsts() X{ X register unsigned *row; X/* register int done; JF unused */ X register int symbol; X register short *sp; X register int rowsize; X X int i; X X varsetsize = rowsize = WORDSIZE(nvars); X X firsts = NEW2(nvars * rowsize, unsigned); X X row = firsts; X for (i = ntokens; i < nsyms; i++) X { X sp = derives[i]; X while (*sp >= 0) X { X symbol = ritem[rrhs[*sp++]]; X if (ISVAR(symbol)) X { X symbol -= ntokens; X SETBIT(row, symbol); X } X } X X row += rowsize; X } X X RTC(firsts, nvars); X X#ifdef DEBUG X print_firsts(); X#endif X} X X X Xclosure(core, n) Xshort *core; Xint n; X{ X register int ruleno; X register unsigned word; X register unsigned mask; X register short *csp; X register unsigned *dsp; X register unsigned *rsp; X X short *csend; X unsigned *rsend; X int symbol; X int itemno; X X rsp = ruleset; X rsend = ruleset + rulesetsize; X csend = core + n; X X if (n == 0) X { X dsp = fderives + start_symbol * rulesetsize; X while (rsp < rsend) X *rsp++ = *dsp++; X } X else X { X while (rsp < rsend) X *rsp++ = 0; X X csp = core; X while (csp < csend) X { X symbol = ritem[*csp++]; X if (ISVAR(symbol)) X { X dsp = fderives + symbol * rulesetsize; X rsp = ruleset; X while (rsp < rsend) X *rsp++ |= *dsp++; X } X } X } X X ruleno = 0; X itemsetend = itemset; X csp = core; X rsp = ruleset; X while (rsp < rsend) X { X word = *rsp++; X if (word == 0) X { X ruleno += BITS_PER_WORD; X } X else X { X mask = 1; X while (mask) X { X if (word & mask) X { X itemno = rrhs[ruleno]; X while (csp < csend && *csp < itemno) X *itemsetend++ = *csp++; X *itemsetend++ = itemno; X } X X mask <<= 1; X ruleno++; X } X } X } X X while (csp < csend) X *itemsetend++ = *csp++; X X#ifdef DEBUG X print_closure(n); X#endif X} X X X Xfinalize_closure() X{ X FREE(itemset); X FREE(ruleset); X FREE(fderives + ntokens * rulesetsize); X} X X X X#ifdef DEBUG X Xprint_closure(n) Xint n; X{ X register short *isp; X X printf("\n\nn = %d\n\n", n); X for (isp = itemset; isp < itemsetend; isp++) X printf(" %d\n", *isp); X} X X X Xprint_firsts() X{ X register int i; X register int j; X register unsigned *rowp; X register unsigned cword; X register unsigned mask; X X extern char **tags; X X printf("\n\n\nFIRSTS\n\n"); X X for (i = ntokens; i < nsyms; i++) X { X printf("\n\n%s firsts\n\n", tags[i]); X X rowp = firsts + ((i - ntokens) * vrowsize); X X cword = *rowp++; X mask = 1; X for (j = 0; j < nsyms; j++) X { X if (cword & mask) X printf(" %s\n", tags[j + ntokens]); X X mask <<= 1; X X if (mask == 0 && j + 1 < nsyms) X { X cword = *rowp++; X mask = 1; X } X } X } X} X X X Xprint_fderives() X{ X register int i; X register int j; X register unsigned *rp; X register unsigned cword; X register unsigned mask; X X extern char **tags; X X printf("\n\n\nFDERIVES\n"); X X for (i = ntokens; i < nsyms; i++) X { X printf("\n\n%s derives\n\n", tags[i]); X rp = fderives + i * rrowsize; X cword = *rp++; X mask = 1; X for (j = 0; j <= nrules; j++) X { X if (cword & mask) X printf(" %d\n", j); X X mask <<= 1; X if (mask == 0 && j + 1 < nrules) X { X cword = *rp++; X mask = 1; X } X } X } X X fflush(stdout); X} X X#endif SHAR_EOF if test 7374 -ne "`wc -c < 'closure.c'`" then echo shar: error transmitting "'closure.c'" '(should have been 7374 characters)' fi fi # end of overwriting check echo shar: extracting "'conflicts.c'" '(14583 characters)' if test -f 'conflicts.c' then echo shar: will not over-write existing file "'conflicts.c'" else sed 's/^X//' << \SHAR_EOF > 'conflicts.c' X/* Find and resolve or report look-ahead conflicts for bison, X Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc. X XBISON is distributed in the hope that it will be useful, but WITHOUT ANY XWARRANTY. No author or distributor accepts responsibility to anyone Xfor the consequences of using it or for whether it serves any Xparticular purpose or works at all, unless he says so in writing. XRefer to the BISON General Public License for full details. X XEveryone is granted permission to copy, modify and redistribute BISON, Xbut only under the conditions described in the BISON General Public XLicense. A copy of this license is supposed to have been given to you Xalong with BISON so you can know your rights and responsibilities. It Xshould be in a file named COPYING. Among other things, the copyright Xnotice and this notice must be preserved on all copies. X X In other words, you are welcome to use, share and improve this program. X You are forbidden to forbid anyone else to use, share and improve X what you give them. Help stamp out software-hoarding! */ X X#include X#include "machine.h" X#include "new.h" X#include "file.h" X#include "gram.h" X#include "state.h" X X Xextern char **tags; Xextern int tokensetsize; Xextern char *consistent; Xextern short *accessing_symbol; Xextern shifts **shift_table; Xextern unsigned *LA; Xextern short *LAruleno; Xextern short *lookaheads; Xextern int verboseflag; X Xchar any_conflicts; Xchar *conflicts; Xerrs **err_table; Xint expected_conflicts; X X Xstatic unsigned *shiftset; Xstatic unsigned *lookaheadset; Xstatic int src_total; Xstatic int rrc_total; Xstatic int src_count; Xstatic int rrc_count; X X X Xinitialize_conflicts() X{ X register int i; X/* register errs *sp; JF unused */ X X conflicts = NEW2(nstates, char); X shiftset = NEW2(tokensetsize, unsigned); X lookaheadset = NEW2(tokensetsize, unsigned); X X err_table = NEW2(nstates, errs *); X X any_conflicts = 0; X X for (i = 0; i < nstates; i++) X set_conflicts(i); X} X X X Xset_conflicts(state) Xint state; X{ X register int i; X register int k; X register shifts *shiftp; X register unsigned *fp2; X register unsigned *fp3; X register unsigned *fp4; X register unsigned *fp1; X register int symbol; X X if (consistent[state]) return; X X for (i = 0; i < tokensetsize; i++) X lookaheadset[i] = 0; X X shiftp = shift_table[state]; X if (shiftp) X { X k = shiftp->nshifts; X for (i = 0; i < k; i++) X { X symbol = accessing_symbol[shiftp->shifts[i]]; X if (ISVAR(symbol)) break; X SETBIT(lookaheadset, symbol); X } X } X X k = lookaheads[state + 1]; X fp4 = lookaheadset + tokensetsize; X X /* loop over all rules which require lookahead in this state */ X /* first check for shift-reduce conflict, and try to resolve using precedence */ X X for (i = lookaheads[state]; i < k; i++) X if (rprec[LAruleno[i]]) X { X fp1 = LA + i * tokensetsize; X fp2 = fp1; X fp3 = lookaheadset; X X while (fp3 < fp4) X { X if (*fp2++ & *fp3++) X { X resolve_sr_conflict(state, i); X break; X } X } X } X X /* loop over all rules which require lookahead in this state */ X /* Check for conflicts not resolved above. */ X X for (i = lookaheads[state]; i < k; i++) X { X fp1 = LA + i * tokensetsize; X fp2 = fp1; X fp3 = lookaheadset; X X while (fp3 < fp4) X { X if (*fp2++ & *fp3++) X { X conflicts[state] = 1; X any_conflicts = 1; X } X } X X fp2 = fp1; X fp3 = lookaheadset; X X while (fp3 < fp4) X *fp3++ |= *fp2++; X } X} X X X X/* Attempt to resolve shift-reduce conflict for one rule Xby means of precedence declarations. XIt has already been checked that the rule has a precedence. XA conflict is resolved by modifying the shift or reduce tables Xso that there is no longer a conflict. */ X Xresolve_sr_conflict(state, lookaheadnum) Xint state; Xint lookaheadnum; X{ X register int i; X register int mask; X register unsigned *fp1; X register unsigned *fp2; X register int redprec; X errs *errp = (errs *) malloc (sizeof(errs) + ntokens * sizeof(short)); X short *errtokens = errp->errs; X X /* find the rule to reduce by to get precedence of reduction */ X redprec = rprec[LAruleno[lookaheadnum]]; X X mask = 1; X fp1 = LA + lookaheadnum * tokensetsize; X fp2 = lookaheadset; X for (i = 0; i < ntokens; i++) X { X if ((mask & *fp2 & *fp1) && sprec[i]) X /* shift-reduce conflict occurs for token number i and it has a precision. X The precedence of shifting is that of token i. */ X { X if (sprec[i] < redprec) X { X if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce"); X *fp2 &= ~mask; /* flush the shift for this token */ X flush_shift(state, i); X } X else if (sprec[i] > redprec) X { X if (verboseflag) log_resolution(state, lookaheadnum, i, "shift"); X *fp1 &= ~mask; /* flush the reduce for this token */ X } X else X { X /* Matching precedence levels. X For left association, keep only the reduction. X For right association, keep only the shift. X For nonassociation, keep neither. */ X X switch (sassoc[i]) X { X X case RIGHT_ASSOC: X if (verboseflag) log_resolution(state, lookaheadnum, i, "shift"); X break; X X case LEFT_ASSOC: X if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce"); X break; X X case NON_ASSOC: X if (verboseflag) log_resolution(state, lookaheadnum, i, "an error"); X break; X } X X if (sassoc[i] != RIGHT_ASSOC) X { X *fp2 &= ~mask; /* flush the shift for this token */ X flush_shift(state, i); X } X if (sassoc[i] != LEFT_ASSOC) X { X *fp1 &= ~mask; /* flush the reduce for this token */ X } X if (sassoc[i] == NON_ASSOC) X { X /* Record an explicit error for this token. */ X *errtokens++ = i; X } X } X } X X mask <<= 1; X if (mask == 0) X { X mask = 1; X fp2++; fp1++; X } X } X errp->nerrs = errtokens - errp->errs; X if (errp->nerrs) X { X /* Some tokens have been explicitly made errors. Allocate X a permanent errs structure for this state, to record them. */ X i = (char *) errtokens - (char *) errp; X err_table[state] = (errs *) allocate ((unsigned int)i); X memcpy(errp, err_table[state], i); X } X else X err_table[state] = 0; X free(errp); X} X X X X/* turn off the shift recorded for the specified token in the specified state. XUsed when we resolve a shift-reduce conflict in favor of the reduction. */ X Xflush_shift(state, token) Xint state; Xint token; X{ X register shifts *shiftp; X register int k, i; X/* register unsigned symbol; JF unused */ X X shiftp = shift_table[state]; X X if (shiftp) X { X k = shiftp->nshifts; X for (i = 0; i < k; i++) X { X if (shiftp->shifts[i] && token == accessing_symbol[shiftp->shifts[i]]) X (shiftp->shifts[i]) = 0; X } X } X} X X X Xlog_resolution(state, LAno, token, resolution) Xint state, LAno, token; Xchar *resolution; X{ X fprintf(foutput, X "Conflict in state %d between rule %d and token %s resolved as %s.\n", X state, LAruleno[LAno], tags[token], resolution); X} X X X Xconflict_log() X{ X register int i; X X src_total = 0; X rrc_total = 0; X X for (i = 0; i < nstates; i++) X { X if (conflicts[i]) X { X count_sr_conflicts(i); X count_rr_conflicts(i); X src_total += src_count; X rrc_total += rrc_count; X } X } X X total_conflicts(); X} X X X Xverbose_conflict_log() X{ X register int i; X X src_total = 0; X rrc_total = 0; X X for (i = 0; i < nstates; i++) X { X if (conflicts[i]) X { X count_sr_conflicts(i); X count_rr_conflicts(i); X src_total += src_count; X rrc_total += rrc_count; X X fprintf(foutput, "State %d contains", i); X X if (src_count == 1) X fprintf(foutput, " 1 shift/reduce conflict"); X else if (src_count > 1) X fprintf(foutput, " %d shift/reduce conflicts", src_count); X X if (src_count > 0 && rrc_count > 0) X fprintf(foutput, " and"); X X if (rrc_count == 1) X fprintf(foutput, " 1 reduce/reduce conflict"); X else if (rrc_count > 1) X fprintf(foutput, " %d reduce/reduce conflicts", rrc_count); X X putc('.', foutput); X putc('\n', foutput); X } X } X X total_conflicts(); X} X X X Xtotal_conflicts() X{ X if (src_total == expected_conflicts && rrc_total == 0) X return; X X fprintf(stderr, "%s contains", infile); X X if (src_total == 1) X fprintf(stderr, " 1 shift/reduce conflict"); X else if (src_total > 1) X fprintf(stderr, " %d shift/reduce conflicts", src_total); X X if (src_total > 0 && rrc_total > 0) X fprintf(stderr, " and"); X X if (rrc_total == 1) X fprintf(stderr, " 1 reduce/reduce conflict"); X else if (rrc_total > 1) X fprintf(stderr, " %d reduce/reduce conflicts", rrc_total); X X putc('.', stderr); X putc('\n', stderr); X} X X X Xcount_sr_conflicts(state) Xint state; X{ X register int i; X register int k; X register int mask; X register shifts *shiftp; X register unsigned *fp1; X register unsigned *fp2; X register unsigned *fp3; X register int symbol; X X src_count = 0; X X shiftp = shift_table[state]; X if (!shiftp) return; X X for (i = 0; i < tokensetsize; i++) X { X shiftset[i] = 0; X lookaheadset[i] = 0; X } X X k = shiftp->nshifts; X for (i = 0; i < k; i++) X { X if (! shiftp->shifts[i]) continue; X symbol = accessing_symbol[shiftp->shifts[i]]; X if (ISVAR(symbol)) break; X SETBIT(shiftset, symbol); X } X X k = lookaheads[state + 1]; X fp3 = lookaheadset + tokensetsize; X X for (i = lookaheads[state]; i < k; i++) X { X fp1 = LA + i * tokensetsize; X fp2 = lookaheadset; X X while (fp2 < fp3) X *fp2++ |= *fp1++; X } X X fp1 = shiftset; X fp2 = lookaheadset; X X while (fp2 < fp3) X *fp2++ &= *fp1++; X X mask = 1; X fp2 = lookaheadset; X for (i = 0; i < ntokens; i++) X { X if (mask & *fp2) X src_count++; X X mask <<= 1; X if (mask == 0) X { X mask = 1; X fp2++; X } X } X} X X X Xcount_rr_conflicts(state) Xint state; X{ X register int i; X register int j; X register int count; X register unsigned mask; X register unsigned *baseword; X register unsigned *wordp; X register int m; X register int n; X X rrc_count = 0; X X m = lookaheads[state]; X n = lookaheads[state + 1]; X X if (n - m < 2) return; X X mask = 1; X baseword = LA + m * tokensetsize; X for (i = 0; i < ntokens; i++) X { X wordp = baseword; X X count = 0; X for (j = m; j < n; j++) X { X if (mask & *wordp) X count++; X X wordp += tokensetsize; X } X X if (count >= 2) rrc_count++; X X mask <<= 1; X if (mask == 0) X { X mask = 1; X baseword++; X } X } X} X X X Xprint_reductions(state) Xint state; X{ X register int i; X register int j; X register int k; X register unsigned *fp1; X register unsigned *fp2; X register unsigned *fp3; X register unsigned *fp4; X register int rule; X register int symbol; X register unsigned mask; X register int m; X register int n; X register int default_LA; X register int default_rule; X register int cmax; X register int count; X register shifts *shiftp; X register errs *errp; X int nodefault = 0; X X for (i = 0; i < tokensetsize; i++) X shiftset[i] = 0; X X shiftp = shift_table[state]; X if (shiftp) X { X k = shiftp->nshifts; X for (i = 0; i < k; i++) X { X if (! shiftp->shifts[i]) continue; X symbol = accessing_symbol[shiftp->shifts[i]]; X if (ISVAR(symbol)) break; X /* if this state has a shift for the error token, X don't use a default rule. */ X if (symbol == error_token_number) nodefault = 1; X SETBIT(shiftset, symbol); X } X } X X errp = err_table[state]; X if (errp) X { X k = errp->nerrs; X for (i = 0; i < k; i++) X { X if (! errp->errs[i]) continue; X symbol = errp->errs[i]; X SETBIT(shiftset, symbol); X } X } X X m = lookaheads[state]; X n = lookaheads[state + 1]; X X if (n - m == 1 && ! nodefault) X { X default_rule = LAruleno[m]; X X fp1 = LA + m * tokensetsize; X fp2 = shiftset; X fp3 = lookaheadset; X fp4 = lookaheadset + tokensetsize; X X while (fp3 < fp4) X *fp3++ = *fp1++ & *fp2++; X X mask = 1; X fp3 = lookaheadset; X X for (i = 0; i < ntokens; i++) X { X if (mask & *fp3) X fprintf(foutput, " %-4s\t[reduce %d (%s)]\n", X tags[i], default_rule, tags[rlhs[default_rule]]); X X mask <<= 1; X if (mask == 0) X { X mask = 1; X fp3++; X } X } X X fprintf(foutput, " $default\treduce %d (%s)\n\n", X default_rule, tags[rlhs[default_rule]]); X } X else if (n - m >= 1) X { X cmax = 0; X default_LA = -1; X fp4 = lookaheadset + tokensetsize; X X if (! nodefault) X for (i = m; i < n; i++) X { X fp1 = LA + i * tokensetsize; X fp2 = shiftset; X fp3 = lookaheadset; X X while (fp3 < fp4) X *fp3++ = *fp1++ & ( ~ (*fp2++)); X X count = 0; X mask = 1; X fp3 = lookaheadset; X for (j = 0; j < ntokens; j++) X { X if (mask & *fp3) X count++; X X mask <<= 1; X if (mask == 0) X { X mask = 1; X fp3++; X } X } X X if (count > cmax) X { X cmax = count; X default_LA = i; X default_rule = LAruleno[i]; X } X X fp2 = shiftset; X fp3 = lookaheadset; X X while (fp3 < fp4) X *fp2++ |= *fp3++; X } X X for (i = 0; i < tokensetsize; i++) X shiftset[i] = 0; X X if (shiftp) X { X k = shiftp->nshifts; X for (i = 0; i < k; i++) X { X if (! shiftp->shifts[i]) continue; X symbol = accessing_symbol[shiftp->shifts[i]]; X if (ISVAR(symbol)) break; X SETBIT(shiftset, symbol); X } X } X X mask = 1; X fp1 = LA + m * tokensetsize; X fp2 = shiftset; X for (i = 0; i < ntokens; i++) X { X int defaulted = 0; X X if (mask & *fp2) X count = 1; X else X count = 0; X X fp3 = fp1; X for (j = m; j < n; j++) X { X if (mask & *fp3) X { X if (count == 0) X { X if (j != default_LA) X { X rule = LAruleno[j]; X fprintf(foutput, " %-4s\treduce %d (%s)\n", X tags[i], rule, tags[rlhs[rule]]); X } X else defaulted = 1; X X count++; X } X else X { X if (defaulted) X { X rule = LAruleno[default_LA]; X fprintf(foutput, " %-4s\treduce %d (%s)\n", X tags[i], rule, tags[rlhs[rule]]); X defaulted = 0; X } X rule = LAruleno[j]; X fprintf(foutput, " %-4s\t[reduce %d (%s)]\n", X tags[i], rule, tags[rlhs[rule]]); X } X } X X fp3 += tokensetsize; X } X X mask <<= 1; X if (mask == 0) X { X mask = 1; X fp1++; X } X } X X if (default_LA >= 0) X { X fprintf(foutput, " $default\treduce %d (%s)\n", X default_rule, tags[rlhs[default_rule]]); X } X X putc('\n', foutput); X } X} X X X Xfinalize_conflicts() X{ X FREE(conflicts); X FREE(shiftset); X FREE(lookaheadset); X} SHAR_EOF if test 14583 -ne "`wc -c < 'conflicts.c'`" then echo shar: error transmitting "'conflicts.c'" '(should have been 14583 characters)' fi fi # end of overwriting check echo shar: extracting "'derives.c'" '(2488 characters)' if test -f 'derives.c' then echo shar: will not over-write existing file "'derives.c'" else sed 's/^X//' << \SHAR_EOF > 'derives.c' X/* Match rules with nonterminals for bison, X Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc. X XBISON is distributed in the hope that it will be useful, but WITHOUT ANY XWARRANTY. No author or distributor accepts responsibility to anyone Xfor the consequences of using it or for whether it serves any Xparticular purpose or works at all, unless he says so in writing. XRefer to the BISON General Public License for full details. X XEveryone is granted permission to copy, modify and redistribute BISON, Xbut only under the conditions described in the BISON General Public XLicense. A copy of this license is supposed to have been given to you Xalong with BISON so you can know your rights and responsibilities. It Xshould be in a file named COPYING. Among other things, the copyright Xnotice and this notice must be preserved on all copies. X X In other words, you are welcome to use, share and improve this program. X You are forbidden to forbid anyone else to use, share and improve X what you give them. Help stamp out software-hoarding! */ X X/* set_derives finds, for each variable (nonterminal), which rules can derive it. X It sets up the value of derives so that X derives[i - ntokens] points to a vector of rule numbers, terminated with a zero. */ X X#include X#include "new.h" X#include "types.h" X#include "gram.h" X X Xshort **derives; X X Xset_derives() X{ X register int i; X register int lhs; X register shorts *p; X register short *q; X register shorts **dset; X register shorts *delts; X X dset = NEW2(nvars, shorts *) - ntokens; X delts = NEW2(nrules + 1, shorts); X X p = delts; X for (i = nrules; i > 0; i--) X { X lhs = rlhs[i]; X p->next = dset[lhs]; X p->value = i; X dset[lhs] = p; X p++; X } X X derives = NEW2(nvars, short *) - ntokens; X q = NEW2(nvars + nrules, short); X X for (i = ntokens; i < nsyms; i++) X { X derives[i] = q; X p = dset[i]; X while (p) X { X *q++ = p->value; X p = p->next; X } X *q++ = -1; X } X X#ifdef DEBUG X print_derives(); X#endif X X FREE(dset + ntokens); X FREE(delts); X} X X Xfree_derives() X{ X FREE(derives[ntokens]); X FREE(derives + ntokens); X} X X X X#ifdef DEBUG X Xprint_derives() X{ X register int i; X register short *sp; X X extern char **tags; X X printf("\n\n\nDERIVES\n\n"); X X for (i = ntokens; i < nsyms; i++) X { X printf("%s derives", tags[i]); X for (sp = derives[i]; *sp > 0; sp++) X { X printf(" %d", *sp); X } X putchar('\n'); X } X X putchar('\n'); X} X X#endif X SHAR_EOF if test 2488 -ne "`wc -c < 'derives.c'`" then echo shar: error transmitting "'derives.c'" '(should have been 2488 characters)' fi fi # end of overwriting check echo shar: extracting "'files.c'" '(7386 characters)' if test -f 'files.c' then echo shar: will not over-write existing file "'files.c'" else sed 's/^X//' << \SHAR_EOF > 'files.c' X/* Open and close files for bison, X Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc. X XBISON is distributed in the hope that it will be useful, but WITHOUT ANY XWARRANTY. No author or distributor accepts responsibility to anyone Xfor the consequences of using it or for whether it serves any Xparticular purpose or works at all, unless he says so in writing. XRefer to the BISON General Public License for full details. X XEveryone is granted permission to copy, modify and redistribute BISON, Xbut only under the conditions described in the BISON General Public XLicense. A copy of this license is supposed to have been given to you Xalong with BISON so you can know your rights and responsibilities. It Xshould be in a file named COPYING. Among other things, the copyright Xnotice and this notice must be preserved on all copies. X X In other words, you are welcome to use, share and improve this program. X You are forbidden to forbid anyone else to use, share and improve X what you give them. Help stamp out software-hoarding! */ X X#define __SEG__ files X X#ifdef VMS X#include X#define unlink delete X#define XPFILE "GNU_BISON:[000000]BISON.SIMPLE" X#define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY" X#endif X X#include X#include "file.h" X#include "new.h" X#include "gram.h" X#include "Events.h" X#include "Files.h" /* get mac file stuff as opposed to Bison file stuff*/ X XFILE *finput = NULL; XFILE *foutput = NULL; XFILE *fdefines = NULL; XFILE *ftable = NULL; XFILE *fattrs = NULL; XFILE *fguard = NULL; XFILE *faction = NULL; XFILE *fparser = NULL; X X/* File name specified with -o for the output file, or 0 if no -o. */ Xchar *spec_outfile; X Xchar *infile; Xchar *outfile; Xchar *defsfile; Xchar *tabfile; Xchar *attrsfile; Xchar *guardfile; Xchar *actfile; Xchar *tmpattrsfile; Xchar *tmptabfile; X XFILE *tryopen(); /* This might be a good idea */ X Xextern int verboseflag; Xextern int definesflag; Xint fixed_outfiles = 0; X X Xchar* Xstringappend(string1, end1, string2) Xchar *string1; Xint end1; Xchar *string2; X{ X register char *ostring; X register char *cp, *cp1; X register int i; X X cp = string2; i = 0; X while (*cp++) i++; X X ostring = NEW2(i+end1+1, char); X X cp = ostring; X cp1 = string1; X for (i = 0; i < end1; i++) X *cp++ = *cp1++; X X cp1 = string2; X while (*cp++ = *cp1++) ; X X return ostring; X} X X X/* JF this has been hacked to death. Nowaday it sets up the file names for X the output files, and opens the tmp files and the parser */ Xopenfiles() X{ X char *name_base; X register char *cp; X char *filename; X int base_length; X int short_base_length; X X long time; X X#ifdef VMS X char *tmp_base = "sys$scratch:b_"; X#else X char *tmp_base = "Bison."; X#endif X X char holding[50]; X int tmp_len; X X tmp_len = strlen (tmp_base); X X if (spec_outfile) X { X /* -o was specified. The precise -o name will be used for ftable. X For other output files, remove the ".c" or ".tab.c" suffix. */ X name_base = spec_outfile; X /* BASE_LENGTH includes ".tab" but not ".c". */ X base_length = strlen (name_base); X if (!strcmp (name_base + base_length - 2, ".c")) X base_length -= 2; X /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ X short_base_length = base_length; X if (!strcmp (name_base + short_base_length - 4, ".tab")) X short_base_length -= 4; X else if (!strcmp (name_base + short_base_length - 4, "_tab")) X short_base_length -= 4; X } X else X { X /* -o was not specified; compute output file name from input X or use y.tab.c, etc., if -y was specified. */ X X name_base = fixed_outfiles ? "y.y" : infile; X X /* Discard any directory names from the input file name X to make the base of the output. */ X cp = name_base; X while (*cp) X { X if (*cp == '/') X name_base = cp+1; X cp++; X } X X /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */ X X base_length = strlen (name_base); X if (!strcmp (name_base + base_length - 2, ".y")) X base_length -= 2; X short_base_length = base_length; X X#ifdef VMS X name_base = stringappend(name_base, short_base_length, "_tab"); X#else X name_base = stringappend(name_base, short_base_length, ".tab"); X#endif X base_length = short_base_length + 4; X } X X finput = tryopen(infile, "r"); X X filename = (char *) getenv ("BISON_SIMPLE"); X fparser = tryopen(filename ? filename : PFILE, "r"); X X if (verboseflag) X { X outfile = stringappend(name_base, short_base_length, ".output"); X foutput = tryopen(outfile, "w"); X } X X if (definesflag) X { X defsfile = stringappend(name_base, base_length, ".h"); X fdefines = tryopen(defsfile, "w"); X } X time=TickCount(); X sprintf(holding,"%s%u","act.",time); X actfile = stringappend(tmp_base, tmp_len, holding); X faction = tryopen(actfile, "w+"); X unlink(actfile); X X sprintf(holding,"%s%u","attrs.",time); X tmpattrsfile = stringappend(tmp_base, tmp_len, holding); X fattrs = tryopen(tmpattrsfile,"w+"); X unlink(tmpattrsfile); X X sprintf(holding,"%s%u","tab.",time); X tmptabfile = stringappend(tmp_base, tmp_len, holding); X ftable = tryopen(tmptabfile, "w+"); X unlink(tmptabfile); X X /* These are opened by `done' or `open_extra_files', if at all */ X if (spec_outfile) X tabfile = spec_outfile; X else X tabfile = stringappend(name_base, base_length, ".c"); X X#ifdef VMS X attrsfile = stringappend(name_base, short_base_length, "_stype.h"); X guardfile = stringappend(name_base, short_base_length, "_guard.c"); X#else X attrsfile = stringappend(name_base, short_base_length, ".stype.h"); X guardfile = stringappend(name_base, short_base_length, ".guard.c"); X#endif X} X X X X/* open the output files needed only for the semantic parser. XThis is done when %semantic_parser is seen in the declarations section. */ Xopen_extra_files() X{ X FILE *ftmp; X int c; X char *filename; X /* JF change open parser file */ X fclose(fparser); X filename = (char *) getenv ("BISON_HAIRY"); X fparser= tryopen(filename ? filename : PFILE1, "r"); X X /* JF change from inline attrs file to separate one */ X ftmp = tryopen(attrsfile, "w"); X rewind(fattrs); X while((c=getc(fattrs))!=EOF) /* Thank god for buffering */ X putc(c,ftmp); X fclose(fattrs); X fattrs=ftmp; X X fguard = tryopen(guardfile, "w"); X X} X X /* JF to make file opening easier. This func tries to open file X NAME with mode MODE, and prints an error message if it fails. */ XFILE * Xtryopen(name, mode) Xchar *name; Xchar *mode; X{ X FILE *ptr; X Create(name,0,'MPS ','TEXT'); X ptr = fopen(name, mode); X if (ptr == NULL) X { X fprintf(stderr, "bison cannot find: "); X fprintf(stderr, name); X done(2); X } X return ptr; X} X Xdone(k) Xint k; X{ X if (faction) X fclose(faction); X X if (fattrs) X fclose(fattrs); X X if (fguard) X fclose(fguard); X X if (finput) X fclose(finput); X X if (fparser) X fclose(fparser); X X/* JF we don't need this anymore X if (fparser1) X fclose(fparser1); */ X X if (foutput) X fclose(foutput); X X /* JF write out the output file */ X if (k == 0 && ftable) X { X FILE *ftmp; X register int c; X X ftmp=tryopen(tabfile, "w"); X rewind(ftable); X while((c=getc(ftable)) != EOF) X putc(c,ftmp); X fclose(ftmp); X fclose(ftable); X } X X#ifdef VMS X delete(actfile); X delete(tmpattrsfile); X delete(tmptabfile); X if (k==0) sys$exit(SS$_NORMAL); X sys$exit(SS$_ABORT); X#else X exit(k); X#endif X} SHAR_EOF if test 7386 -ne "`wc -c < 'files.c'`" then echo shar: error transmitting "'files.c'" '(should have been 7386 characters)' fi fi # end of overwriting check echo shar: extracting "'getargs.c'" '(1879 characters)' if test -f 'getargs.c' then echo shar: will not over-write existing file "'getargs.c'" else sed 's/^X//' << \SHAR_EOF > 'getargs.c' X/* Parse command line arguments for bison, X Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc. X XBISON is distributed in the hope that it will be useful, but WITHOUT ANY XWARRANTY. No author or distributor accepts responsibility to anyone Xfor the consequences of using it or for whether it serves any Xparticular purpose or works at all, unless he says so in writing. XRefer to the BISON General Public License for full details. X XEveryone is granted permission to copy, modify and redistribute BISON, Xbut only under the conditions described in the BISON General Public XLicense. A copy of this license is supposed to have been given to you Xalong with BISON so you can know your rights and responsibilities. It Xshould be in a file named COPYING. Among other things, the copyright Xnotice and this notice must be preserved on all copies. X X In other words, you are welcome to use, share and improve this program. X You are forbidden to forbid anyone else to use, share and improve X what you give them. Help stamp out software-hoarding! */ X X#include X#include "file.h" X X#define __SEG__ files X Xint verboseflag; Xint definesflag; Xextern int fixed_outfiles;/* JF */ X X Xgetargs(argc, argv) Xint argc; Xchar *argv[]; X{ X register int c; X X extern int optind; X extern char *optarg; X X verboseflag = 0; X definesflag = 0; X fixed_outfiles = 0; X X while ((c = getopt (argc, argv, "yYvVdDo:O:")) != EOF) X switch (c) X { X case 'y': X case 'Y': X fixed_outfiles = 1; X break; X X case 'v': X case 'V': X verboseflag = 1; X break; X X case 'd': X case 'D': X definesflag = 1; X break; X X case 'o': X case 'O': X spec_outfile = optarg; X } X X if (optind == argc) X fatal("grammar file not specified"); X else X infile = argv[optind]; X X if (optind < argc - 1) X fprintf(stderr, "bison: warning: extra arguments ignored\n"); X} SHAR_EOF if test 1879 -ne "`wc -c < 'getargs.c'`" then echo shar: error transmitting "'getargs.c'" '(should have been 1879 characters)' fi fi # end of overwriting check # End of shell archive exit 0 --- end of part 2 ---