Path: utzoo!attcan!uunet!husc6!mailrus!ames!pasteur!ucbvax!hplabs!felix!dhw68k!macintosh From: wetter@tybalt.caltech.edu (Pierce T. Wetter) Newsgroups: comp.sources.mac Subject: Bison Macintosh Sources (part 4 of 6) Message-ID: <8052@dhw68k.cts.com> Date: 17 May 88 04:14:27 GMT References: <7986@dhw68k.cts.com> <8002@dhw68k.cts.com> <8034@dhw68k.cts.com> Sender: macintosh@dhw68k.cts.com Organization: California Institute of Technology Lines: 1841 Approved: bytebug@dhw68k.cts.com (Roger L. Long) [Bison Macintosh Sources - part 4 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/main.c # Bison/nullable.c # Bison/output.c # Bison/print.c # This archive created: Sat May 14 02:41:58 1988 # By: Roger L. Long (bytebug@dhw68k.cts.com) export PATH; PATH=/bin:$PATH echo shar: extracting "'main.c'" '(3142 characters)' if test -f 'main.c' then echo shar: will not over-write existing file "'main.c'" else sed 's/^X//' << \SHAR_EOF > 'main.c' X/* Top level entry point of 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 "machine.h" /* JF for MAXSHORT */ X Xextern int lineno; Xextern int verboseflag; X X/* Nonzero means failure has been detected; don't write a parser file. */ Xint failure; X Xmain(argc, argv) Xint argc; Xchar *argv[]; X{ X failure = 0; X lineno = 0; X getargs(argc, argv); X openfiles(); X X /* read the input. Copy some parts of it to fguard, faction, ftable and fattrs. X In file reader. X The other parts are recorded in the grammar; see gram.h. */ X reader(); X X /* record other info about the grammar. In files derives and nullable. */ X set_derives(); X set_nullable(); X X /* convert to nondeterministic finite state machine. In file LR0. X See state.h for more info. */ X generate_states(); X X /* make it deterministic. In file lalr. */ X lalr(); X X /* Find and record any conflicts: places where one token of lookahead is not X enough to disambiguate the parsing. In file conflicts. X Currently this does not do anything to resolve them; X the trivial form of conflict resolution that exists is done in output. */ X initialize_conflicts(); X X /* print information about results, if requested. In file print. */ X if (verboseflag) X verbose(); X else X terse(); X X /* output the tables and the parser to ftable. In file output. */ X output(); X done(failure); X} X X/* functions to report errors which prevent a parser from being generated */ X Xfatal(s) Xchar *s; X{ X extern char *infile; X X if (infile == 0) X fprintf(stderr, "fatal error: %s\n", s); X else X fprintf(stderr, "\"%s\", line %d: %s\n", infile, lineno, s); X done(1); X} X X X/* JF changed to accept/deal with variable args. Is a real kludge since X we don't support _doprnt calls */ X/*VARARGS1*/ Xfatals(fmt,x1,x2,x3,x4,x5,x6,x7,x8) Xchar *fmt; X{ X char buffer[200]; X X sprintf(buffer, fmt, x1,x2,x3,x4,x5,x6,x7,x8); X fatal(buffer); X} X X X Xtoomany(s) Xchar *s; X{ X char buffer[200]; X X /* JF new msg */ X sprintf(buffer, "limit of %d exceeded, too many %s", MAXSHORT, s); X fatal(buffer); X} X X X Xberror(s) Xchar *s; X{ X fprintf(stderr, "internal error, %s\n", s); X exit(1); X} SHAR_EOF if test 3142 -ne "`wc -c < 'main.c'`" then echo shar: error transmitting "'main.c'" '(should have been 3142 characters)' fi fi # end of overwriting check echo shar: extracting "'nullable.c'" '(2824 characters)' if test -f 'nullable.c' then echo shar: will not over-write existing file "'nullable.c'" else sed 's/^X//' << \SHAR_EOF > 'nullable.c' X/* Part of the bison parser generator, 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 up nullable, a vector saying which nonterminals can expand into the null string. X nullable[i - ntokens] is nonzero if symbol i can do so. */ X X#include X#include "types.h" X#include "gram.h" X#include "new.h" X X Xchar *nullable; X X Xset_nullable() X{ X register short *r; X register short *s1; X register short *s2; X register int ruleno; X register int symbol; X register shorts *p; X X short *squeue; X short *rcount; X shorts **rsets; X shorts *relts; X char any_tokens; X short *r1; X X#ifdef TRACE X fprintf(stderr, "Entering set_nullable"); X#endif X X nullable = NEW2(nvars, char) - ntokens; X X squeue = NEW2(nvars, short); X s1 = s2 = squeue; X X rcount = NEW2(nrules + 1, short); X rsets = NEW2(nvars, shorts *) - ntokens; X relts = NEW2(nitems + nvars + 1, shorts); X p = relts; X X r = ritem; X while (*r) X { X if (*r < 0) X { X symbol = rlhs[-(*r++)]; X if (!nullable[symbol]) X { X nullable[symbol] = 1; X *s2++ = symbol; X } X } X else X { X r1 = r; X any_tokens = 0; X for (symbol = *r++; symbol > 0; symbol = *r++) X { X if (ISTOKEN(symbol)) X any_tokens = 1; X } X X if (!any_tokens) X { X ruleno = -symbol; X r = r1; X for (symbol = *r++; symbol > 0; symbol = *r++) X { X rcount[ruleno]++; X p->next = rsets[symbol]; X p->value = ruleno; X rsets[symbol] = p; X p++; X } X } X } X } X X while (s1 < s2) X { X p = rsets[*s1++]; X while (p) X { X ruleno = p->value; X p = p->next; X if (--rcount[ruleno] == 0) X { X symbol = rlhs[ruleno]; X if (!nullable[symbol]) X { X nullable[symbol] = 1; X *s2++ = symbol; X } X } X } X } X X FREE(squeue); X FREE(rcount); X FREE(rsets + ntokens); X FREE(relts); X} X X Xfree_nullable() X{ X FREE(nullable + ntokens); X} SHAR_EOF if test 2824 -ne "`wc -c < 'nullable.c'`" then echo shar: error transmitting "'nullable.c'" '(should have been 2824 characters)' fi fi # end of overwriting check echo shar: extracting "'output.c'" '(24116 characters)' if test -f 'output.c' then echo shar: will not over-write existing file "'output.c'" else sed 's/^X//' << \SHAR_EOF > 'output.c' X/* Output the generated parsing program 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/* functions to output parsing data to various files. Entries are: X X output_headers () X XOutput constant strings to the beginning of certain files. X X output_trailers() X XOutput constant strings to the ends of certain files. X X output () X XOutput the parsing tables and the parser code to ftable. X XThe parser tables consist of: (starred ones needed only for the semantic parser) X Xyytranslate = vector mapping yylex's token numbers into bison's token numbers. X Xyytname = vector of string-names indexed by bison token number X Xyyrline = vector of line-numbers of all rules. For yydebug printouts. X X* yyrhs = vector of items of all rules. X This is exactly what ritems contains. X X* yyprhs[r] = index in yyrhs of first item for rule r. X Xyyr1[r] = symbol number of symbol that rule r derives. X Xyyr2[r] = number of symbols composing right hand side of rule r. X X* yystos[s] = the symbol number of the symbol that leads to state s. X Xyydefact[s] = default rule to reduce with in state s, X when yytable doesn't specify something else to do. X Zero means the default is an error. X Xyydefgoto[i] = default state to go to after a reduction of a rule that X generates variable ntokens + i, except when yytable X specifies something else to do. X Xyypact[s] = index in yytable of the portion describing state s. X The lookahed token's type is used to index that portion X to find out what to do. X X If the value in yytable is positive, X we shift the token and go to that state. X X If the value is negative, it is minus a rule number to reduce by. X X If the value is zero, the default action from yydefact[s] is used. X Xyypgoto[i] = the index in yytable of the portion describing X what to do after reducing a rule that derives variable i + ntokens. X This portion is indexed by the parser state number X as of before the text for this nonterminal was read. X The value from yytable is the state to go to. X Xyytable = a vector filled with portions for different uses, X found via yypact and yypgoto. X Xyycheck = a vector indexed in parallel with yytable. X It indicates, in a roundabout way, the bounds of the X portion you are trying to examine. X X Suppose that the portion of yytable starts at index p X and the index to be examined within the portion is i. X Then if yycheck[p+i] != i, i is outside the bounds X of what is actually allocated, and the default X (from yydefact or yydefgoto) should be used. X Otherwise, yytable[p+i] should be used. X XYYFINAL = the state number of the termination state. XYYFLAG = most negative short int. Used to flag ?? XYYNTBASE = ntokens. X X*/ 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#define MAXTABLE 32767 X X Xextern char **tags; Xextern int tokensetsize; Xextern int final_state; Xextern core **state_table; Xextern shifts **shift_table; Xextern errs **err_table; Xextern reductions **reduction_table; Xextern short *accessing_symbol; Xextern unsigned *LA; Xextern short *LAruleno; Xextern short *lookaheads; Xextern char *consistent; Xextern short *goto_map; Xextern short *from_state; Xextern short *to_state; X X Xstatic int nvectors; Xstatic int nentries; Xstatic short **froms; Xstatic short **tos; Xstatic short *tally; Xstatic short *width; Xstatic short *actrow; Xstatic short *state_count; Xstatic short *order; Xstatic short *base; Xstatic short *pos; Xstatic short *table; Xstatic short *check; Xstatic int lowzero; Xstatic int high; X X X X#define GUARDSTR "\n#include \"%s\"\nextern int yyerror;\n\ Xextern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\ Xyyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\ Xregister YYLTYPE *yylsp;\n\ X{\n yyerror = 0;\nyycost = 0;\n yymsg = 0;\nswitch (n)\n {" X X#define ACTSTR "\n#include \"%s\"\nextern YYSTYPE yyval;\ X\nextern int yychar;\ Xyyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\ Xregister YYLTYPE *yylsp;\n{\n switch (n)\n{" X X#define ACTSTR_SIMPLE "\n switch (yyn) {\n" X X X Xoutput_headers() X{ X if (semantic_parser) X fprintf(fguard, GUARDSTR, attrsfile); X fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile); X/* if (semantic_parser) JF moved this below X fprintf(ftable, "#include \"%s\"\n", attrsfile); X fprintf(ftable, "#include \n\n"); */ X} X Xoutput_trailers() X{ X if (semantic_parser) X { X fprintf(fguard, "\n }\n}\n"); X fprintf(faction, "\n }\n}\n"); X } X else X fprintf(faction, "\n}\n"); X} X X Xoutput() X{ X int c; X X /* output_token_defines(ftable); /* JF put out token defines FIRST */ X if (!semantic_parser) /* JF Put out other stuff */ X { X rewind(fattrs); X while ((c=getc(fattrs))!=EOF) X putc(c,ftable); X } X X if (semantic_parser) X fprintf(ftable, "#include \"%s\"\n", attrsfile); X fprintf(ftable, "#include \n\n"); X X /* Make "const" do nothing if not in ANSI C. */ X fprintf (ftable, "#ifndef __STDC__\n#define const\n#endif\n\n"); X X free_itemsets(); X output_defines(); X output_token_translations(); X if (semantic_parser) X output_gram(); X FREE(ritem); X if (semantic_parser) X output_stos(); X output_rule_data(); X output_actions(); X output_parser(); X output_program(); X} X Xoutput_token_translations() X{ X register int i, j; X/* register short *sp; JF unused */ X X if (translations) X { X fprintf(ftable, X "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n", X max_user_token_number, nsyms); X X if (ntokens < 127) /* play it very safe; check maximum element value. */ X fprintf(ftable, "\nstatic const char yytranslate[] = { 0"); X else X fprintf(ftable, "\nstatic const short yytranslate[] = { 0"); X X j = 10; X for (i = 1; i <= max_user_token_number; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", token_translations[i]); X } X X fprintf(ftable, "\n};\n"); X } X else X { X fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n"); X } X} X X X Xoutput_gram() X{ X register int i; X register int j; X register short *sp; X X fprintf(ftable, "\nstatic const short yyprhs[] = { 0"); X X j = 10; X for (i = 1; i <= nrules; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", rrhs[i]); X } X X fprintf(ftable, "\n};\n\nstatic const short yyrhs[] = {%6d", ritem[0]); X X j = 10; X for (sp = ritem + 1; *sp; sp++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X if (*sp > 0) X fprintf(ftable, "%6d", *sp); X else X fprintf(ftable, " 0"); X } X X fprintf(ftable, "\n};\n"); X} X X X Xoutput_stos() X{ X register int i; X register int j; X X fprintf(ftable, "\nstatic const short yystos[] = { 0"); X X j = 10; X for (i = 1; i < nstates; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", accessing_symbol[i]); X } X X fprintf(ftable, "\n};\n"); X} X X X Xoutput_rule_data() X{ X register int i; X register int j; X X fprintf(ftable, "\nstatic const short yyrline[] = { 0"); X X j = 10; X for (i = 1; i <= nrules; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", rline[i]); X } X X /* Output the table of token names. */ X X fprintf(ftable, "\n};\n\nstatic const char * const yytname[] = { 0"); X X j = 10; X for (i = 1; i <= ntokens; i++) X { X register char *p; X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X putc ('\"', ftable); X X for (p = tags[i]; *p; p++) X if (*p == '"' || *p == '\\') X fprintf(ftable, "\\%c", *p); X else if (*p == '\n') X fprintf(ftable, "\\n"); X else if (*p == '\t') X fprintf(ftable, "\\t"); X else if (*p == '\b') X fprintf(ftable, "\\b"); X else if (*p < 040 || *p >= 0177) X fprintf(ftable, "\\%03o", *p); X else X putc(*p, ftable); X X putc ('\"', ftable); X } X X fprintf(ftable, "\n};\n\nstatic const short yyr1[] = { 0"); X X j = 10; X for (i = 1; i <= nrules; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", rlhs[i]); X } X X FREE(rlhs + 1); X X fprintf(ftable, "\n};\n\nstatic const short yyr2[] = { 0"); X X j = 10; X for (i = 1; i < nrules; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1); X } X X putc(',', ftable); X if (j >= 10) X putc('\n', ftable); X X fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1); X FREE(rrhs + 1); X} X X X Xoutput_defines() X{ X fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state); X fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT); X fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens); X} X X X X/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */ X Xoutput_actions() X{ X nvectors = nstates + nvars; X X froms = NEW2(nvectors, short *); X tos = NEW2(nvectors, short *); X tally = NEW2(nvectors, short); X width = NEW2(nvectors, short); X X token_actions(); X free_shifts(); X free_reductions(); X FREE(lookaheads); X FREE(LA); X FREE(LAruleno); X FREE(accessing_symbol); X X goto_actions(); X FREE(goto_map + ntokens); X FREE(from_state); X FREE(to_state); X X sort_actions(); X pack_table(); X output_base(); X output_table(); X output_check(); X} X X X X/* figure out the actions for the specified state, indexed by lookahead token type. X X The yydefact table is output now. The detailed info X is saved for putting into yytable later. */ X Xtoken_actions() X{ X register int i; X register int j; X register int k; X X actrow = NEW2(ntokens, short); X X k = action_row(0); X fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k); X save_row(0); X X j = 10; X for (i = 1; i < nstates; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X k = action_row(i); X fprintf(ftable, "%6d", k); X save_row(i); X } X X fprintf(ftable, "\n};\n"); X FREE(actrow); X} X X X X/* Decide what to do for each type of token if seen as the lookahead token in specified state. X The value returned is used as the default action (yydefact) for the state. X In addition, actrow is filled with what to do for each kind of token, X index by symbol number, with zero meaning do the default action. X The value MINSHORT, a very negative number, means this situation X is an error. The parser recognizes this value specially. X X This is where conflicts are resolved. The loop over lookahead rules X considered lower-numbered rules last, and the last rule considered that likes X a token gets to handle it. */ X Xint Xaction_row(state) Xint state; X{ X register int i; X register int j; X register int k; X register int m; X register int n; X register int count; X register int default_rule; X register int nreds; X register int max; X register int rule; X register int shift_state; X register int symbol; X register unsigned mask; X register unsigned *wordp; X register reductions *redp; X register shifts *shiftp; X register errs *errp; X int nodefault = 0; /* set nonzero to inhibit having any default reduction */ X X for (i = 0; i < ntokens; i++) X actrow[i] = 0; X X default_rule = 0; X nreds = 0; X redp = reduction_table[state]; X X if (redp) X { X nreds = redp->nreds; X X if (nreds >= 1) X { X /* loop over all the rules available here which require lookahead */ X m = lookaheads[state]; X n = lookaheads[state + 1]; X X for (i = n - 1; i >= m; i--) X { X rule = - LAruleno[i]; X wordp = LA + i * tokensetsize; X mask = 1; X X /* and find each token which the rule finds acceptable to come next */ X for (j = 0; j < ntokens; j++) X { X /* and record this rule as the rule to use if that token follows. */ X if (mask & *wordp) X actrow[j] = rule; X X mask <<= 1; X if (mask == 0) X { X mask = 1; X wordp++; X } X } X } X } X } X X shiftp = shift_table[state]; X X /* now see which tokens are allowed for shifts in this state. X For them, record the shift as the thing to do. So shift is preferred to reduce. */ X X if (shiftp) X { X k = shiftp->nshifts; X X for (i = 0; i < k; i++) X { X shift_state = shiftp->shifts[i]; X if (! shift_state) continue; X X symbol = accessing_symbol[shift_state]; X X if (ISVAR(symbol)) X break; X X actrow[symbol] = shift_state; X X /* do not use any default reduction if there is a shift for error */ X X if (symbol == error_token_number) nodefault = 1; X } X } X X errp = err_table[state]; X X /* See which tokens are an explicit error in this state X (due to %nonassoc). For them, record MINSHORT as the action. */ X X if (errp) X { X k = errp->nerrs; X X for (i = 0; i < k; i++) X { X symbol = errp->errs[i]; X actrow[symbol] = MINSHORT; X } X } X X /* now find the most common reduction and make it the default action for this state. */ X X if (nreds >= 1 && ! nodefault) X { X if (consistent[state]) X default_rule = redp->rules[0]; X else X { X max = 0; X for (i = m; i < n; i++) X { X count = 0; X rule = - LAruleno[i]; X X for (j = 0; j < ntokens; j++) X { X if (actrow[j] == rule) X count++; X } X X if (count > max) X { X max = count; X default_rule = rule; X } X } X X /* actions which match the default are replaced with zero, X which means "use the default" */ X X if (max > 0) X { X for (j = 0; j < ntokens; j++) X { X if (actrow[j] == default_rule) X actrow[j] = 0; X } X X default_rule = - default_rule; X } X } X } X X /* If have no default rule, the default is an error. X So replace any action which says "error" with "use default". */ X X if (default_rule == 0) X for (j = 0; j < ntokens; j++) X { X if (actrow[j] == MINSHORT) X actrow[j] = 0; X } X X return (default_rule); X} X X X Xsave_row(state) Xint state; X{ X register int i; X register int count; X register short *sp; X register short *sp1; X register short *sp2; X X count = 0; X for (i = 0; i < ntokens; i++) X { X if (actrow[i] != 0) X count++; X } X X if (count == 0) X return; X X froms[state] = sp1 = sp = NEW2(count, short); X tos[state] = sp2 = NEW2(count, short); X X for (i = 0; i < ntokens; i++) X { X if (actrow[i] != 0) X { X *sp1++ = i; X *sp2++ = actrow[i]; X } X } X X tally[state] = count; X width[state] = sp1[-1] - sp[0] + 1; X} X X X X/* figure out what to do after reducing with each rule, X depending on the saved state from before the beginning X of parsing the data that matched this rule. X X The yydefgoto table is output now. The detailed info X is saved for putting into yytable later. */ X Xgoto_actions() X{ X register int i; X register int j; X register int k; X X state_count = NEW2(nstates, short); X X k = default_goto(ntokens); X fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k); X save_column(ntokens, k); X X j = 10; X for (i = ntokens + 1; i < nsyms; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X k = default_goto(i); X fprintf(ftable, "%6d", k); X save_column(i, k); X } X X fprintf(ftable, "\n};\n"); X FREE(state_count); X} X X X Xint Xdefault_goto(symbol) Xint symbol; X{ X register int i; X register int m; X register int n; X register int default_state; X register int max; X X m = goto_map[symbol]; X n = goto_map[symbol + 1]; X X if (m == n) X return (-1); X X for (i = 0; i < nstates; i++) X state_count[i] = 0; X X for (i = m; i < n; i++) X state_count[to_state[i]]++; X X max = 0; X default_state = -1; X X for (i = 0; i < nstates; i++) X { X if (state_count[i] > max) X { X max = state_count[i]; X default_state = i; X } X } X X return (default_state); X} X X X Xsave_column(symbol, default_state) Xint symbol; Xint default_state; X{ X register int i; X register int m; X register int n; X register short *sp; X register short *sp1; X register short *sp2; X register int count; X register int symno; X X m = goto_map[symbol]; X n = goto_map[symbol + 1]; X X count = 0; X for (i = m; i < n; i++) X { X if (to_state[i] != default_state) X count++; X } X X if (count == 0) X return; X X symno = symbol - ntokens + nstates; X X froms[symno] = sp1 = sp = NEW2(count, short); X tos[symno] = sp2 = NEW2(count, short); X X for (i = m; i < n; i++) X { X if (to_state[i] != default_state) X { X *sp1++ = from_state[i]; X *sp2++ = to_state[i]; X } X } X X tally[symno] = count; X width[symno] = sp1[-1] - sp[0] + 1; X} X X X X/* the next few functions decide how to pack X the actions and gotos information into yytable. */ X Xsort_actions() X{ X register int i; X register int j; X register int k; X register int t; X register int w; X X order = NEW2(nvectors, short); X nentries = 0; X X for (i = 0; i < nvectors; i++) X { X if (tally[i] > 0) X { X t = tally[i]; X w = width[i]; X j = nentries - 1; X X while (j >= 0 && (width[order[j]] < w)) X j--; X X while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t)) X j--; X X for (k = nentries - 1; k > j; k--) X order[k + 1] = order[k]; X X order[j + 1] = i; X nentries++; X } X } X} X X X Xpack_table() X{ X register int i; X register int place; X register int state; X X base = NEW2(nvectors, short); X pos = NEW2(nentries, short); X table = NEW2(MAXTABLE, short); X check = NEW2(MAXTABLE, short); X X lowzero = 0; X high = 0; X X for (i = 0; i < nvectors; i++) X base[i] = MINSHORT; X X for (i = 0; i < MAXTABLE; i++) X check[i] = -1; X X for (i = 0; i < nentries; i++) X { X state = matching_state(i); X X if (state < 0) X place = pack_vector(i); X else X place = base[state]; X X pos[i] = place; X base[order[i]] = place; X } X X for (i = 0; i < nvectors; i++) X { X FREE(froms[i]); X FREE(tos[i]); X } X X FREE(froms); X FREE(tos); X FREE(pos); X} X X X Xint Xmatching_state(vector) Xint vector; X{ X register int i; X register int j; X register int k; X register int t; X register int w; X register int match; X register int prev; X X i = order[vector]; X if (i >= nstates) X return (-1); X X t = tally[i]; X w = width[i]; X X for (prev = vector - 1; prev >= 0; prev--) X { X j = order[prev]; X if (width[j] != w || tally[j] != t) X return (-1); X X match = 1; X for (k = 0; match && k < t; k++) X { X if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k]) X match = 0; X } X X if (match) X return (j); X } X X return (-1); X} X X X Xint Xpack_vector(vector) Xint vector; X{ X register int i; X register int j; X register int k; X register int t; X register int loc; X register int ok; X register short *from; X register short *to; X X i = order[vector]; X t = tally[i]; X X if (t == 0) X berror("pack_vector"); X X from = froms[i]; X to = tos[i]; X X for (j = lowzero - from[0]; j < MAXTABLE; j++) X { X ok = 1; X X for (k = 0; ok && k < t; k++) X { X loc = j + from[k]; X if (loc > MAXTABLE) X fatals("maximum table size (%d) exceeded",MAXTABLE); X X if (table[loc] != 0) X ok = 0; X } X X for (k = 0; ok && k < vector; k++) X { X if (pos[k] == j) X ok = 0; X } X X if (ok) X { X for (k = 0; k < t; k++) X { X loc = j + from[k]; X table[loc] = to[k]; X check[loc] = from[k]; X } X X while (table[lowzero] != 0) X lowzero++; X X if (loc > high) X high = loc; X X return (j); X } X } X X berror("pack_vector"); X return 0; /* JF keep lint happy */ X} X X X X/* the following functions output yytable, yycheck X and the vectors whose elements index the portion starts */ X Xoutput_base() X{ X register int i; X register int j; X X fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]); X X j = 10; X for (i = 1; i < nstates; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", base[i]); X } X X fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]); X X j = 10; X for (i = nstates + 1; i < nvectors; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", base[i]); X } X X fprintf(ftable, "\n};\n"); X FREE(base); X} X X X Xoutput_table() X{ X register int i; X register int j; X X fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high); X fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]); X X j = 10; X for (i = 1; i <= high; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", table[i]); X } X X fprintf(ftable, "\n};\n"); X FREE(table); X} X X X Xoutput_check() X{ X register int i; X register int j; X X fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]); X X j = 10; X for (i = 1; i <= high; i++) X { X putc(',', ftable); X X if (j >= 10) X { X putc('\n', ftable); X j = 1; X } X else X { X j++; X } X X fprintf(ftable, "%6d", check[i]); X } X X fprintf(ftable, "\n};\n"); X FREE(check); X} X X X X/* copy the parser code into the ftable file at the end. */ X Xoutput_parser() X{ X register int c; X#ifdef DONTDEF X FILE *fpars; X#else X#define fpars fparser X#endif X X if (pure_parser) X fprintf(ftable, "#define YYIMPURE 1\n\n"); X else X fprintf(ftable, "#define YYPURE 1\n\n"); X X#ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the X currently open parser from bison.simple to bison.hairy */ X if (semantic_parser) X fpars = fparser; X else fpars = fparser1; X#endif X X c = getc(fpars); X while (c != EOF) X { X if (c == '$') { X#ifdef DONTDEF X fprintf(ftable, "#include \"%s\"\n", actfile); X#else X /* JF don't #include the action file. Stuff it right in. */ X rewind(faction); X for(c=getc(faction);c!=EOF;c=getc(faction)) X putc(c,ftable); X#endif X } else X putc(c, ftable); X c = getc(fpars); X } X} X X X Xoutput_program() X{ X register int c; X extern int lineno; X X fprintf(ftable, "#line %d \"%s\"\n", lineno, infile); X X c = getc(finput); X while (c != EOF) X { X putc(c, ftable); X c = getc(finput); X } X} X X X Xfree_itemsets() X{ X register core *cp,*cptmp; X X FREE(state_table); X X for (cp = first_state; cp; cp = cptmp) { X cptmp=cp->next; X FREE(cp); X } X} X X X Xfree_shifts() X{ X register shifts *sp,*sptmp;/* JF derefrenced freed ptr */ X X FREE(shift_table); X X for (sp = first_shift; sp; sp = sptmp) { X sptmp=sp->next; X FREE(sp); X } X} X X X Xfree_reductions() X{ X register reductions *rp,*rptmp;/* JF fixed freed ptr */ X X FREE(reduction_table); X X for (rp = first_reduction; rp; rp = rptmp) { X rptmp=rp->next; X FREE(rp); X } X} SHAR_EOF if test 24116 -ne "`wc -c < 'output.c'`" then echo shar: error transmitting "'output.c'" '(should have been 24116 characters)' fi fi # end of overwriting check echo shar: extracting "'print.c'" '(4598 characters)' if test -f 'print.c' then echo shar: will not over-write existing file "'print.c'" else sed 's/^X//' << \SHAR_EOF > 'print.c' X/* Print information on generated parser, 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 "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 nstates; Xextern short *accessing_symbol; Xextern core **state_table; Xextern shifts **shift_table; Xextern errs **err_table; Xextern reductions **reduction_table; Xextern char *consistent; Xextern char any_conflicts; Xextern char *conflicts; X X X Xterse() X{ X if (any_conflicts) X { X conflict_log(); X } X} X X X Xverbose() X{ X register int i; X X if (any_conflicts) X verbose_conflict_log(); X X fprintf(foutput, "\n\ntoken types:\n"); X print_token (-1, 0); X if (translations) X { X for (i = 0; i <= max_user_token_number; i++) X /* Don't mention all the meaningless ones. */ X if (token_translations[i] != 2) X print_token (i, token_translations[i]); X } X else X for (i = 1; i < ntokens; i++) X print_token (i, i); X X for (i = 0; i < nstates; i++) X { X print_state(i); X } X} X X X Xprint_token(extnum, token) Xint extnum, token; X{ X fprintf(foutput, " type %d is %s\n", extnum, tags[token]); X} X X X Xprint_state(state) Xint state; X{ X fprintf(foutput, "\n\nstate %d\n\n", state); X print_core(state); X print_actions(state); X} X X X Xprint_core(state) Xint state; X{ X register int i; X register int k; X register int rule; X register core *statep; X register short *sp; X register short *sp1; X X statep = state_table[state]; X k = statep->nitems; X X if (k == 0) return; X X for (i = 0; i < k; i++) X { X sp1 = sp = ritem + statep->items[i]; X X while (*sp > 0) X sp++; X X rule = -(*sp); X fprintf(foutput, " %s -> ", tags[rlhs[rule]]); X X for (sp = ritem + rrhs[rule]; sp < sp1; sp++) X { X fprintf(foutput, "%s ", tags[*sp]); X } X X putc('.', foutput); X X while (*sp > 0) X { X fprintf(foutput, " %s", tags[*sp]); X sp++; X } X X fprintf (foutput, " (%d)", rule); X putc('\n', foutput); X } X X putc('\n', foutput); X} X X X Xprint_actions(state) Xint state; X{ X register int i; X register int k; X register int state1; X register int symbol; X register shifts *shiftp; X register errs *errp; X register reductions *redp; X register int rule; X X shiftp = shift_table[state]; X redp = reduction_table[state]; X errp = err_table[state]; X X if (!shiftp && !redp) X { X fprintf(foutput, " NO ACTIONS\n"); X return; X } X X if (shiftp) X { X k = shiftp->nshifts; X X for (i = 0; i < k; i++) X { X if (! shiftp->shifts[i]) continue; X state1 = shiftp->shifts[i]; X symbol = accessing_symbol[state1]; X/* if (ISVAR(symbol)) break; */ X fprintf(foutput, " %-4s\tshift %d\n", tags[symbol], state1); X } X X if (i > 0) X putc('\n', foutput); X } X else X { X i = 0; X k = 0; X } X X if (errp) X { X k = errp->nerrs; X X for (i = 0; i < k; i++) X { X if (! errp->errs[i]) continue; X symbol = errp->errs[i]; X fprintf(foutput, " %-4s\terror (nonassociative)\n", tags[symbol]); X } X X if (i > 0) X putc('\n', foutput); X } X else X { X i = 0; X k = 0; X } X X if (consistent[state] && redp) X { X rule = redp->rules[0]; X symbol = rlhs[rule]; X fprintf(foutput, " $default\treduce %d (%s)\n\n", X rule, tags[symbol]); X } X else if (redp) X { X print_reductions(state); X } X X if (i < k) X { X for (; i < k; i++) X { X if (! shiftp->shifts[i]) continue; X state1 = shiftp->shifts[i]; X symbol = accessing_symbol[state1]; X fprintf(foutput, " %-4s\tgoto %d\n", tags[symbol], state1); X } X X putc('\n', foutput); X } X} SHAR_EOF if test 4598 -ne "`wc -c < 'print.c'`" then echo shar: error transmitting "'print.c'" '(should have been 4598 characters)' fi fi # end of overwriting check # End of shell archive exit 0 --- end of part 4 ---