Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!nrl-cmf!ames!pasteur!ucbvax!decwrl!hplabs!felix!dhw68k!macintosh From: wetter@tybalt.caltech.edu (Pierce T. Wetter) Newsgroups: comp.sources.mac Subject: Bison Macintosh Sources (part 5 of 6) Message-ID: <8064@dhw68k.cts.com> Date: 18 May 88 02:01:26 GMT References: <7986@dhw68k.cts.com> <8002@dhw68k.cts.com> <8034@dhw68k.cts.com> <8052@dhw68k.cts.com> Sender: macintosh@dhw68k.cts.com Organization: California Institute of Technology Lines: 1727 Approved: bytebug@dhw68k.cts.com (Roger L. Long) [Bison Macintosh Sources - part 5 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/reader.c # This archive created: Sat May 14 02:42:03 1988 # By: Roger L. Long (bytebug@dhw68k.cts.com) export PATH; PATH=/bin:$PATH echo shar: extracting "'reader.c'" '(34877 characters)' if test -f 'reader.c' then echo shar: will not over-write existing file "'reader.c'" else sed 's/^X//' << \SHAR_EOF > 'reader.c' X/* Input 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/* read in the grammar specification and record it in the format described in gram.h. X All guards are copied into the fguard file and all actions into faction, X in each case forming the body of a C function (yyguard or yyaction) X which contains a switch statement to decide which guard or action to execute. X XThe entry point is reader(). */ X X#include X#include X#include "file.h" X#include "new.h" X#include "symtab.h" X#include "lex.h" X#include "gram.h" X X#define __SEG__ files X X X#define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\ X {\n int timestamp;\n int first_line;\n int first_column;\n\ X int last_line;\n int last_column;\n char *text;\n }\n\ X yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n" X X/* Number of slots allocated (but not necessarily used yet) in `rline' */ Xint rline_allocated; X Xextern int definesflag; Xextern bucket *symval; Xextern int numval; Xextern int failure; Xextern int expected_conflicts; X Xtypedef X struct symbol_list X { X struct symbol_list *next; X bucket *sym; X bucket *ruleprec; X } X symbol_list; X X X Xint lineno; Xbucket *symval; Xsymbol_list *grammar; Xint start_flag; Xbucket *startval; Xchar **tags; X Xstatic int typed; /* nonzero if %union has been seen. */ X Xstatic int lastprec; /* incremented for each %left, %right or %nonassoc seen */ X Xstatic int gensym_count; /* incremented for each generated symbol */ X Xstatic bucket *errtoken; X Xreader() X{ X X start_flag = 0; X startval = NULL; /* start symbol not specified yet. */ X X translations = 0; /* initially assume token number translation not needed. */ X X nsyms = 1; X nvars = 0; X nrules = 0; X nitems = 0; X rline_allocated = 10; X rline = NEW2(rline_allocated, short); X X typed = 0; X lastprec = 0; X X gensym_count = 0; X X semantic_parser = 0; X pure_parser = 0; X X grammar = NULL; X X init_lex(); X lineno = 1; X X /* initialize the symbol table. */ X tabinit(); X /* construct the error token */ X errtoken = getsym("error"); X errtoken->class = STOKEN; X /* construct a token that represents all undefined literal tokens. */ X /* it is always token number 2. */ X getsym("$illegal.")->class = STOKEN; X /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file. X Also notice any %token, %left, etc. found there. */ X fprintf(ftable, "\n/* A Bison parser, made from %s */\n\n", infile); X read_declarations(); X /* output the definition of YYLTYPE into the fattrs and fdefines files. */ X output_ltype(); X /* start writing the guard and action files, if they are needed. */ X output_headers(); X /* read in the grammar, build grammar in list form. write out guards and actions. */ X readgram(); X /* write closing delimiters for actions and guards. */ X output_trailers(); X /* assign the symbols their symbol numbers. X Write #defines for the token symbols into fdefines if requested. */ X packsymbols(); X /* convert the grammar into the format described in gram.h. */ X packgram(); X /* free the symbol table data structure X since symbols are now all referred to by symbol number. */ X free_symtab(); X} X X X X/* read from finput until %% is seen. Discard the %%. XHandle any % declarations, Xand copy the contents of any %{ ... %} groups to fattrs. */ X Xread_declarations () X{ X register int c; X register int tok; X X for (;;) X { X c = skip_white_space(); X X if (c == '%') X { X tok = parse_percent_token(); X X switch (tok) X { X case TWO_PERCENTS: X return; X X case PERCENT_LEFT_CURLY: X copy_definition(); X break; X X case TOKEN: X parse_token_decl (STOKEN, SNTERM); X break; X X case NTERM: X parse_token_decl (SNTERM, STOKEN); X break; X X case TYPE: X parse_type_decl(); X break; X X case START: X parse_start_decl(); X break; X X case UNION: X parse_union_decl(); X break; X X case EXPECT: X parse_expect_decl(); X break; X X case LEFT: X parse_assoc_decl(LEFT_ASSOC); X break; X X case RIGHT: X parse_assoc_decl(RIGHT_ASSOC); X break; X X case NONASSOC: X parse_assoc_decl(NON_ASSOC); X break; X X case SEMANTIC_PARSER: X semantic_parser = 1; X open_extra_files(); X break; X X case PURE_PARSER: X pure_parser = 1; X break; X X default: X fatal("junk after % in definition section"); X } X } X else if (c == EOF) X fatal("no input grammar"); X else/* JF changed msg */ X fatals("Unrecognized char '%c' in declaration section",c); X X } X} X X X/* copy the contents of a %{ ... %} into the definitions file. XThe %{ has already been read. Return after reading the %}. */ Xcopy_definition () X{ X register int c; X register int match; X register int ended; X register int after_percent; /* -1 while reading a character if prev char was % */ X X fprintf(fattrs, "#line %d \"%s\"\n", lineno, infile); X X after_percent = 0; X X c = getc(finput); X X for (;;) X { X switch (c) X { X case '\n': X putc(c, fattrs); X lineno++; X break; X X case '%': X after_percent = -1; X break; X X case '\'': X case '"': X match = c; X putc(c, fattrs); X c = getc(finput); X X while (c != match) X { X if (c == EOF || c == '\n') X fatal("unterminated string"); X X putc(c, fattrs); X X if (c == '\\') X { X c = getc(finput); X if (c == EOF || c == '\n') X fatal("unterminated string"); X putc(c, fattrs); X if (c == '\n') X lineno++; X } X X c = getc(finput); X } X X putc(c, fattrs); X break; X X case '/': X putc(c, fattrs); X c = getc(finput); X if (c != '*') X continue; X X putc(c, fattrs); X c = getc(finput); X X ended = 0; X while (!ended) X { X if (c == '*') X { X while (c == '*') X { X putc(c, fattrs); X c = getc(finput); X } X X if (c == '/') X { X putc(c, fattrs); X ended = 1; X } X } X else if (c == '\n') X { X lineno++; X putc(c, fattrs); X c = getc(finput); X } X else if (c == EOF) X fatal("unterminated comment in %{ definition"); X else X { X putc(c, fattrs); X c = getc(finput); X } X } X X break; X X case EOF: X fatal("unterminated %{ definition"); X X default: X putc(c, fattrs); X } X X c = getc(finput); X X if (after_percent) X { X if (c == '}') X return; X putc('%', fattrs); X } X after_percent = 0; X X } X X} X X X X/* parse what comes after %token or %nterm. XFor %token, what_is is STOKEN and what_is_not is SNTERM. XFor %nterm, the arguments are reversed. */ X Xparse_token_decl (what_is, what_is_not) X int what_is, what_is_not; X{ X/* register int start_lineno; JF */ X register int token = 0; X register int prev; X register char *typename = 0; X int k; X extern char token_buffer[]; X X/* start_lineno = lineno; JF */ X X for (;;) X { X if(ungetc(skip_white_space(), finput) == '%') X return; X X/* if (lineno != start_lineno) X return; JF */ X X /* we have not passed a newline, so the token now starting is in this declaration */ X prev = token; X X if ((token = lex()) == TYPENAME) X { X k = strlen(token_buffer); X if (typename) free (typename); X typename = NEW2(k + 1, char); X strcpy(typename, token_buffer); X } X else if (token == IDENTIFIER) X { X if (symval->class == what_is_not) X fatals("symbol %s redefined", symval->tag); X symval->class = what_is; X if (what_is == SNTERM) X symval->value = nvars++; X X if (typename) X { X if (symval->type_name == NULL) X symval->type_name = typename; X else X fatals("type redeclaration for %s", symval->tag); X } X } X else if (prev == IDENTIFIER && token == NUMBER) X { X symval->user_token_number = numval; X translations = 1; X } X else X fatal("invalid text in %token or %nterm declaration"); X } X X} X X X X/* parse what comes after %start */ X Xparse_start_decl () X{ X if (start_flag) X fatal("multiple %start declarations"); X start_flag = 1; X if (lex() != IDENTIFIER) X fatal("invalid %start declaration"); X startval = symval; X} X X X X/* read in a %type declaration and record its information for get_type_name to access */ X Xparse_type_decl () X{ X register int k; X register char *name; X/* register int start_lineno; JF */ X X extern char token_buffer[]; X X if (lex() != TYPENAME) X fatal("ill-formed %type declaration"); X X k = strlen(token_buffer); X name = NEW2(k + 1, char); X strcpy(name, token_buffer); X X/* start_lineno = lineno; */ X X for (;;) X { X register int t; X X if(ungetc(skip_white_space(), finput) == '%') X return; X X/* if (lineno != start_lineno) X return; JF */ X X /* we have not passed a newline, so the token now starting is in this declaration */ X X t = lex(); X X switch (t) X { X X case COMMA: X break; X X case IDENTIFIER: X if (symval->type_name == NULL) X symval->type_name = name; X else X fatals("type redeclaration for %s", symval->tag); X X break; X X default: X fatal("invalid %type declaration"); X } X } X} X X X X/* read in a %left, %right or %nonassoc declaration and record its information. */ X/* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */ X Xparse_assoc_decl (assoc) Xint assoc; X{ X register int k; X register char *name = NULL; X/* register int start_lineno; JF */ X register int prev = 0; /* JF added = 0 to keep lint happy */ X X extern char token_buffer[]; X X lastprec++; /* assign a new precedence level. */ X X/* start_lineno = lineno; */ X X for (;;) X { X register int t; X X if(ungetc(skip_white_space(), finput) == '%') X return; X X /* if (lineno != start_lineno) X return; JF */ X X /* we have not passed a newline, so the token now starting is in this declaration */ X X t = lex(); X X switch (t) X { X X case TYPENAME: X k = strlen(token_buffer); X name = NEW2(k + 1, char); X strcpy(name, token_buffer); X break; X X case COMMA: X break; X X case IDENTIFIER: X symval->prec = lastprec; X symval->assoc = assoc; X if (symval->class == SNTERM) X fatals("symbol %s redefined", symval->tag); X symval->class = STOKEN; X if (name) X { /* record the type, if one is specified */ X if (symval->type_name == NULL) X symval->type_name = name; X else X fatals("type redeclaration for %s", symval->tag); X } X break; X X case NUMBER: X if (prev == IDENTIFIER) X { X symval->user_token_number = numval; X translations = 1; X } X else X fatal("invalid text in association declaration"); X break; X X case SEMICOLON: X return; X X default: X fatal("malformatted association declaration"); X } X X prev = t; X X } X} X X X X/* copy the union declaration into fattrs (and fdefines), X where it is made into the X definition of YYSTYPE, the type of elements of the parser value stack. */ X Xparse_union_decl() X{ X register int c; X register int count; X register int in_comment; X X if (typed) X fatal("multiple %union declarations"); X X typed = 1; X X fprintf(fattrs, "\n#line %d \"%s\"\n", lineno, infile); X fprintf(fattrs, "typedef union"); X if (fdefines) X fprintf(fdefines, "typedef union"); X X count = 0; X in_comment = 0; X X c = getc(finput); X X while (c != EOF) X { X putc(c, fattrs); X if (fdefines) X putc(c, fdefines); X X switch (c) X { X case '\n': X lineno++; X break; X X case '/': X c = getc(finput); X if (c != '*') X ungetc(c, finput); X else X { X putc('*', fattrs); X if (fdefines) X putc('*', fdefines); X c = getc(finput); X in_comment = 1; X while (in_comment) X { X if (c == EOF) X fatal("unterminated comment"); X X putc(c, fattrs); X if (fdefines) X putc(c, fdefines); X if (c == '*') X { X c = getc(finput); X if (c == '/') X { X putc('/', fattrs); X if (fdefines) X putc('/', fdefines); X in_comment = 0; X } X } X else X c = getc(finput); X } X } X break; X X X case '{': X count++; X break; X X case '}': X count--; X if (count == 0) X { X fprintf(fattrs, " YYSTYPE;\n"); X if (fdefines) X fprintf(fdefines, " YYSTYPE;\n"); X /* JF don't choke on trailing semi */ X c=skip_white_space(); X if(c!=';') ungetc(c,finput); X return; X } X } X X c = getc(finput); X } X} X X/* parse the declaration %expect N which says to expect N X shift-reduce conflicts. */ X Xparse_expect_decl() X{ X register int c; X register int count; X char buffer[20]; X X c = getc(finput); X while (c == ' ' || c == '\t') X c = getc(finput); X X count = 0; X while (c >= '0' && c <= '9') X { X if (count < 20) X buffer[count++] = c; X c = getc(finput); X } X X while (c != EOF && c != '\n') X c = getc(finput); X X expected_conflicts = atoi (buffer); X} X X/* that's all of parsing the declaration section */ X Xoutput_ltype() X{ X fprintf(fattrs, LTYPESTR);/* JF added YYABORT() */ X if (fdefines) X fprintf(fdefines, LTYPESTR);/* JF added YYABORT() */ X X fprintf(fattrs, "#define\tYYACCEPT\treturn(0)\n"); X fprintf(fattrs, "#define\tYYABORT\treturn(1)\n"); X fprintf(fattrs, "#define\tYYERROR\treturn(1)\n"); X X if (fdefines) X { X fprintf(fdefines, "#define\tYYACCEPT\treturn(0)\n"); X fprintf(fdefines, "#define\tYYABORT\treturn(1)\n"); X fprintf(fdefines, "#define\tYYERROR\treturn(1)\n"); X } X} X X X X/* Get the data type (alternative in the union) of the value for symbol n in rule rule. */ X Xchar * Xget_type_name(n, rule) Xint n; Xsymbol_list *rule; X{ X static char *msg = "invalid $ value"; X X register int i; X register symbol_list *rp; X X if (n < 0) X fatal(msg); X X rp = rule; X i = 0; X X while (i < n) X { X rp = rp->next; X if (rp == NULL || rp->sym == NULL) X fatal(msg); X i++; X } X X return (rp->sym->type_name); X} X X X X/* after %guard is seen in the input file, Xcopy the actual guard into the guards file. XIf the guard is followed by an action, copy that into the actions file. Xstack_offset is the number of values in the current rule so far, Xwhich says where to find $0 with respect to the top of the stack, Xfor the simple parser in which the stack is not popped until after the guard is run. */ X Xcopy_guard(rule, stack_offset) Xsymbol_list *rule; Xint stack_offset; X{ X register int c; X register int n; X register int count; X register int match; X register int ended; X register char *type_name; X extern char token_buffer[]; X X /* offset is always 0 if parser has already popped the stack pointer */ X if (semantic_parser) stack_offset = 0; X X fprintf(fguard, "\ncase %d:\n", nrules); X fprintf(fguard, "#line %d \"%s\"\n", lineno, infile); X putc('{', fguard); X X count = 0; X c = getc(finput); X X while (count > 0 || c != ';' && c != '{') X { X switch (c) X { X case '\n': X putc(c, fguard); X lineno++; X break; X X case '{': X putc(c, fguard); X count++; X break; X X case '}': X putc(c, fguard); X if (count > 0) X count--; X else X fatal("unmatched right brace ('}')"); X X case '\'': X case '"': X match = c; X putc(c, fguard); X c = getc(finput); X X while (c != match) X { X if (c == EOF || c == '\n') X fatal("unterminated string"); X X putc(c, fguard); X X if (c == '\\') X { X c = getc(finput); X if (c == EOF || c == '\n') X fatal("unterminated string"); X putc(c, fguard); X if (c == '\n') X lineno++; X } X X c = getc(finput); X } X X putc(c, fguard); X break; X X case '/': X putc(c, fguard); X c = getc(finput); X if (c != '*') X continue; X X putc(c, fguard); X c = getc(finput); X X ended = 0; X while (!ended) X { X if (c == '*') X { X while (c == '*') X { X putc(c, fguard); X c = getc(finput); X } X X if (c == '/') X { X putc(c, fguard); X ended = 1; X } X } X else if (c == '\n') X { X lineno++; X putc(c, fguard); X c = getc(finput); X } X else if (c == EOF) X fatal("unterminated comment"); X else X { X putc(c, fguard); X c = getc(finput); X } X } X X break; X X case '$': X c = getc(finput); X type_name = NULL; X X if (c == '<') X { X register char *cp = token_buffer; X X while ((c = getc(finput)) != '>' && c > 0) X *cp++ = c; X *cp = 0; X type_name = token_buffer; X X c = getc(finput); X } X X if (c == '$') X { X fprintf(fguard, "yyval"); X if (!type_name) type_name = rule->sym->type_name; X if (type_name) X fprintf(fguard, ".%s", type_name); X if(!type_name && typed) /* JF */ X fprintf(stderr,"%s:%d: warning: $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag); X } X X else if (isdigit(c) || c == '-') X { X ungetc (c, finput); X n = read_signed_integer(finput); X c = getc(finput); X X if (!type_name && n > 0) X type_name = get_type_name(n, rule); X X fprintf(fguard, "yyvsp[%d]", n - stack_offset); X if (type_name) X fprintf(fguard, ".%s", type_name); X if(!type_name && typed) /* JF */ X fprintf(stderr,"%s:%d: warning: $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag); X continue; X } X else X fatals("$%c is invalid",c); /* JF changed style */ X X break; X X case '@': X c = getc(finput); X if (isdigit(c) || c == '-') X { X ungetc (c, finput); X n = read_signed_integer(finput); X c = getc(finput); X } X else X fatals("@%c is invalid",c); /* JF changed style */ X X fprintf(fguard, "yylsp[%d]", n - stack_offset); X X continue; X X case EOF: X fatal("unterminated %guard clause"); X X default: X putc(c, fguard); X } X X c = getc(finput); X } X X fprintf(fguard, ";\n break;}"); X if (c == '{') X copy_action(rule, stack_offset); X else if (c == '=') X { X c = getc(finput); X if (c == '{') X copy_action(rule, stack_offset); X } X X} X X X X/* Assuming that a { has just been seen, copy everything up to the matching } Xinto the actions file. Xstack_offset is the number of values in the current rule so far, Xwhich says where to find $0 with respect to the top of the stack. */ X Xcopy_action(rule, stack_offset) Xsymbol_list *rule; Xint stack_offset; X{ X register int c; X register int n; X register int count; X register int match; X register int ended; X register char *type_name; X extern char token_buffer[]; X X /* offset is always 0 if parser has already popped the stack pointer */ X if (semantic_parser) stack_offset = 0; X X fprintf(faction, "\ncase %d:\n", nrules); X fprintf(faction, "#line %d \"%s\"\n", lineno, infile); X putc('{', faction); X X count = 1; X c = getc(finput); X X while (count > 0) X { X while (c != '}') X { X switch (c) X { X case '\n': X putc(c, faction); X lineno++; X break; X X case '{': X putc(c, faction); X count++; X break; X X case '\'': X case '"': X match = c; X putc(c, faction); X c = getc(finput); X X while (c != match) X { X if (c == EOF || c == '\n') X fatal("unterminated string"); X X putc(c, faction); X X if (c == '\\') X { X c = getc(finput); X if (c == EOF) X fatal("unterminated string"); X putc(c, faction); X if (c == '\n') X lineno++; X } X X c = getc(finput); X } X X putc(c, faction); X break; X X case '/': X putc(c, faction); X c = getc(finput); X if (c != '*') X continue; X X putc(c, faction); X c = getc(finput); X X ended = 0; X while (!ended) X { X if (c == '*') X { X while (c == '*') X { X putc(c, faction); X c = getc(finput); X } X X if (c == '/') X { X putc(c, faction); X ended = 1; X } X } X else if (c == '\n') X { X lineno++; X putc(c, faction); X c = getc(finput); X } X else if (c == EOF) X fatal("unterminated comment"); X else X { X putc(c, faction); X c = getc(finput); X } X } X X break; X X case '$': X c = getc(finput); X type_name = NULL; X X if (c == '<') X { X register char *cp = token_buffer; X X while ((c = getc(finput)) != '>' && c > 0) X *cp++ = c; X *cp = 0; X type_name = token_buffer; X X c = getc(finput); X } X if (c == '$') X { X fprintf(faction, "yyval"); X if (!type_name) type_name = get_type_name(0, rule); X if (type_name) X fprintf(faction, ".%s", type_name); X if(!type_name && typed) /* JF */ X fprintf(stderr,"%s:%d: warning: $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag); X } X else if (isdigit(c) || c == '-') X { X ungetc (c, finput); X n = read_signed_integer(finput); X c = getc(finput); X X if (!type_name && n > 0) X type_name = get_type_name(n, rule); X X fprintf(faction, "yyvsp[%d]", n - stack_offset); X if (type_name) X fprintf(faction, ".%s", type_name); X if(!type_name && typed) /* JF */ X fprintf(stderr,"%s:%d: warning: $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag); X continue; X } X else X fatal("$%c is invalid",c); /* JF changed format */ X X break; X X case '@': X c = getc(finput); X if (isdigit(c) || c == '-') X { X ungetc (c, finput); X n = read_signed_integer(finput); X c = getc(finput); X } X else X fatal("invalid @-construct"); X X fprintf(faction, "yylsp[%d]", n - stack_offset); X X continue; X X case EOF: X fatal("unmatched '{'"); X X default: X putc(c, faction); X } X X c = getc(finput); X } X X /* above loop exits when c is '}' */ X X if (--count) X { X putc(c, faction); X c = getc(finput); X } X } X X fprintf(faction, ";\n break;}"); X} X X X X/* generate a dummy symbol, a nonterminal, Xwhose name cannot conflict with the user's names. */ X Xbucket * Xgensym() X{ X register bucket *sym; X X extern char token_buffer[]; X sprintf (token_buffer, "@%d", ++gensym_count); X sym = getsym(token_buffer); X sym->class = SNTERM; X sym->value = nvars++; X return (sym); X} X X X X/* Parse the input grammar into a one symbol_list structure. XEach rule is represented by a sequence of symbols: the left hand side Xfollowed by the contents of the right hand side, followed by a null pointer Xinstead of a symbol to terminate the rule. XThe next symbol is the lhs of the following rule. X XAll guards and actions are copied out to the appropriate files, Xlabelled by the rule number they apply to. */ X Xreadgram() X{ X register int t; X register bucket *lhs; X register symbol_list *p; X register symbol_list *p1; X register bucket *bp; X X symbol_list *crule; /* points to first symbol_list of current rule. */ X /* its symbol is the lhs of the rule. */ X symbol_list *crule1; /* points to the symbol_list preceding crule. */ X X p1 = NULL; X X t = lex(); X X while (t != TWO_PERCENTS && t != ENDFILE) X { X if (t == IDENTIFIER || t == BAR) X { X register int actionflag = 0; X int rulelength = 0; /* number of symbols in rhs of this rule so far */ X int xactions = 0; /* JF for error checking */ X bucket *first_rhs = 0; X X if (t == IDENTIFIER) X { X lhs = symval; X X t = lex(); X if (t != COLON) X fatal("ill-formed rule"); X } X X if (nrules == 0) X { X if (t == BAR) X fatal("grammar starts with vertical bar"); X X if (!start_flag) X startval = lhs; X } X X /* start a new rule and record its lhs. */ X X nrules++; X nitems++; X X record_rule_line (); X X p = NEW(symbol_list); X p->sym = lhs; X X crule1 = p1; X if (p1) X p1->next = p; X else X grammar = p; X X p1 = p; X crule = p; X X /* mark the rule's lhs as a nonterminal if not already so. */ X X if (lhs->class == SUNKNOWN) X { X lhs->class = SNTERM; X lhs->value = nvars; X nvars++; X } X else if (lhs->class == STOKEN) X fatals("rule given for %s, which is a token", lhs->tag); X X /* read the rhs of the rule. */ X X for (;;) X { X t = lex(); X X if (! (t == IDENTIFIER || t == LEFT_CURLY)) break; X X /* if next token is an identifier, see if a colon follows it. X If one does, exit this rule now. */ X if (t == IDENTIFIER) X { X register bucket *ssave; X register int t1; X X ssave = symval; X t1 = lex(); X unlex(t1); X symval = ssave; X if (t1 == COLON) break; X X if(!first_rhs) /* JF */ X first_rhs = symval; X /* not followed by colon => process as part of this rule's rhs. */ X if (actionflag) X { X register bucket *sdummy; X X /* if this symbol was preceded by an action, */ X /* make a dummy nonterminal to replace that action in this rule */ X /* and make another rule to associate the action to the dummy. */ X /* Since the action was written out with this rule's number, */ X /* we must write give the new rule this number */ X /* by inserting the new rule before it. */ X X /* make a dummy nonterminal, a gensym. */ X sdummy = gensym(); X X /* make a new rule, whose body is empty, before the current one. */ X /* so that the action just read can belong to it. */ X nrules++; X nitems++; X record_rule_line (); X p = NEW(symbol_list); X if (crule1) X crule1->next = p; X else grammar = p; X p->sym = sdummy; X crule1 = NEW(symbol_list); X p->next = crule1; X crule1->next = crule; X X /* insert the dummy generated by that rule into this rule. */ X nitems++; X p = NEW(symbol_list); X p->sym = sdummy; X p1->next = p; X p1 = p; X X actionflag = 0; X } X nitems++; X p = NEW(symbol_list); X p->sym = symval; X p1->next = p; X p1 = p; X } X else /* handle an action. */ X { X copy_action(crule, rulelength); X actionflag = 1; X xactions++; /* JF */ X } X rulelength++; X } X X /* Put an empty link in the list to mark the end of this rule */ X p = NEW(symbol_list); X p1->next = p; X p1 = p; X X if (t == PREC) X { X t = lex(); X crule->ruleprec = symval; X t = lex(); X } X if (t == GUARD) X { X if (! semantic_parser) X fatal("%guard present but %semantic_parser not specified"); X X copy_guard(crule, rulelength); X t = lex(); X } X else if (t == LEFT_CURLY) X { X if (actionflag) fatal("two actions at end of one rule"); X copy_action(crule, rulelength); X t = lex(); X } X /* JF if we'd end up using default, get a warning */ X else if(!xactions && first_rhs && lhs->type_name!=first_rhs->type_name) { X if(lhs->type_name == 0 || first_rhs->type_name == 0 || X strcmp(lhs->type_name,first_rhs->type_name)) X fprintf(stderr,"%s:%d: warning: type clash ('%s' '%s') on default action\n", X infile, X lineno, X lhs->type_name ? lhs->type_name : "", X first_rhs->type_name ? first_rhs->type_name : ""); X } X if (t == SEMICOLON) X t = lex(); X } X /* these things can appear as alternatives to rules. */ X else if (t == TOKEN) X { X parse_token_decl(STOKEN, SNTERM); X t = lex(); X } X else if (t == NTERM) X { X parse_token_decl(SNTERM, STOKEN); X t = lex(); X } X else if (t == TYPE) X { X t = get_type(); X } X else if (t == UNION) X { X parse_union_decl(); X t = lex(); X } X else if (t == EXPECT) X { X parse_expect_decl(); X t = lex(); X } X else if (t == START) X { X parse_start_decl(); X t = lex(); X } X else X fatal("invalid input"); X } X X if (nrules == 0) X fatal("no input grammar"); X X if (typed == 0)/* JF put out same default YYSTYPE as YACC does */ X { X fprintf(fattrs, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n"); X if (fdefines) X fprintf(fdefines, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n"); X } X X /* Report any undefined symbols and consider them nonterminals. */ X X for (bp = firstsymbol; bp; bp = bp->next) X if (bp->class == SUNKNOWN) X { X fprintf(stderr, "symbol %s used, not defined as token, and no rules for it\n", X bp->tag); X failure = 1; X bp->class = SNTERM; X bp->value = nvars++; X } X X ntokens = nsyms - nvars; X} X X Xrecord_rule_line () X{ X /* Record each rule's source line number in rline table. */ X X if (nrules >= rline_allocated) X { X rline_allocated = nrules * 2; X rline = (short *) realloc (rline, X rline_allocated * sizeof (short)); X if (rline == 0) X { X fprintf (stderr, "bison: memory exhausted\n"); X done (1); X } X } X rline[nrules] = lineno; X} X X X/* read in a %type declaration and record its information for get_type_name to access */ X Xint Xget_type() X{ X register int k; X register int t; X register char *name; X X extern char token_buffer[]; X X t = lex(); X X if (t != TYPENAME) X fatal("ill-formed %type declaration"); X X k = strlen(token_buffer); X name = NEW2(k + 1, char); X strcpy(name, token_buffer); X X for (;;) X { X t = lex(); X X switch (t) X { X case SEMICOLON: X return (lex()); X X case COMMA: X break; X X case IDENTIFIER: X if (symval->type_name == NULL) X symval->type_name = name; X else X fatals("type redeclaration for %s", symval->tag); X X break; X X default: X return (t); X } X } X} X X X X/* assign symbol numbers, and write definition of token names into fdefines. XSet up vectors tags and sprec of names and precedences of symbols. */ X Xpacksymbols() X{ X register bucket *bp; X register int tokno = 1; X register int i; X register int last_user_token_number; X X /* int lossage = 0; JF set but not used */ X X tags = NEW2(nsyms + 1, char *); X tags[0] = "$"; X X sprec = NEW2(nsyms, short); X sassoc = NEW2(nsyms, short); X X max_user_token_number = 255; X last_user_token_number = 255; X X for (bp = firstsymbol; bp; bp = bp->next) X { X if (bp->class == SNTERM) X { X bp->value += ntokens; X } X else X { X if (translations && !(bp->user_token_number)) X bp->user_token_number = ++last_user_token_number; X if (bp->user_token_number > max_user_token_number) X max_user_token_number = bp->user_token_number; X bp->value = tokno++; X } X X tags[bp->value] = bp->tag; X sprec[bp->value] = bp->prec; X sassoc[bp->value] = bp->assoc; X X } X X if (translations) X { X register int i; X X token_translations = NEW2(max_user_token_number+1, short); X X /* initialize all entries for literal tokens to 2, X the internal token number for $illegal., which represents all invalid inputs. */ X for (i = 0; i <= max_user_token_number; i++) X token_translations[i] = 2; X } X X for (bp = firstsymbol; bp; bp = bp->next) X { X if (bp->value >= ntokens) continue; X if (translations) X { X if (token_translations[bp->user_token_number] != 2) X { X /* JF made this a call to fatals() */ X fatals( "tokens %s and %s both assigned number %d", X tags[token_translations[bp->user_token_number]], X bp->tag, X bp->user_token_number); X } X token_translations[bp->user_token_number] = bp->value; X } X } X X error_token_number = errtoken->value; X X output_token_defines(ftable); X X if (startval->class == SUNKNOWN) X fatals("the start symbol %s is undefined", startval->tag); X else if (startval->class == STOKEN) X fatals("the start symbol %s is a token", startval->tag); X X start_symbol = startval->value; X X if (definesflag) X { X output_token_defines(fdefines); X X if (semantic_parser) X for (i = ntokens; i < nsyms; i++) X { X /* don't make these for dummy nonterminals made by gensym. */ X if (*tags[i] != '@') X fprintf(fdefines, "#define\tNT%s\t%d\n", tags[i], i); X } X X fclose(fdefines); X fdefines = NULL; X } X} X X Xoutput_token_defines(file) XFILE *file; X{ X bucket *bp; X X for (bp = firstsymbol; bp; bp = bp->next) X { X if (bp->value >= ntokens) continue; X X /* For named tokens, but not literal ones, define the name. */ X /* The value is the user token number. */ X X if ('\'' != *tags[bp->value] && bp != errtoken) X { X register char *cp = tags[bp->value]; X register char c; X X /* Don't #define nonliteral tokens whose names contain periods. */ X X while ((c = *cp++) && c != '.'); X if (!c) X { X fprintf(file, "#define\t%s\t%d\n", tags[bp->value], X (translations ? bp->user_token_number : bp->value)); X if (semantic_parser) X fprintf(file, "#define\tT%s\t%d\n", tags[bp->value], X bp->value); X } X } X } X X putc('\n', file); X} X X X X/* convert the rules into the representation using rrhs, rlhs and ritems. */ X Xpackgram() X{ X register int itemno; X register int ruleno; X register symbol_list *p; X/* register bucket *bp; JF unused */ X X bucket *ruleprec; X X ritem = NEW2(nitems + 1, short); X rlhs = NEW2(nrules, short) - 1; X rrhs = NEW2(nrules, short) - 1; X rprec = NEW2(nrules, short) - 1; X rassoc = NEW2(nrules, short) - 1; X X itemno = 0; X ruleno = 1; X X p = grammar; X while (p) X { X rlhs[ruleno] = p->sym->value; X rrhs[ruleno] = itemno; X ruleprec = p->ruleprec; X X p = p->next; X while (p && p->sym) X { X ritem[itemno++] = p->sym->value; X /* a rule gets the precedence and associativity of the last token in it. */ X if (p->sym->class == STOKEN) X { X rprec[ruleno] = p->sym->prec; X rassoc[ruleno] = p->sym->assoc; X } X if (p) p = p->next; X } X X /* if this rule has a %prec, specified symbol's precedence replaces the default */ X if (ruleprec) X { X rprec[ruleno] = ruleprec->prec; X rassoc[ruleno] = ruleprec->assoc; X } X X ritem[itemno++] = -ruleno; X ruleno++; X X if (p) p = p->next; X } X X ritem[itemno] = 0; X} X X/* Read a signed integer from STREAM and return its value. */ X Xread_signed_integer (stream) X FILE *stream; X{ X register int c = getc(stream); X register int sign = 1; X register int n; X X if (c == '-') X { X c = getc(stream); X sign = -1; X } X n = 0; X while (isdigit(c)) X { X n = 10*n + (c - '0'); X c = getc(stream); X } X X ungetc(c, stream); X X return n * sign; X} SHAR_EOF if test 34877 -ne "`wc -c < 'reader.c'`" then echo shar: error transmitting "'reader.c'" '(should have been 34877 characters)' fi fi # end of overwriting check # End of shell archive exit 0 --- end of part 5 ---