📄 reader.c
字号:
/* Input parser for bison Copyright (C) 1984, 1986, 1989, 1992 Free Software Foundation, Inc.This file is part of Bison, the GNU Compiler Compiler.Bison is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.Bison is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Bison; see the file COPYING. If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. *//* read in the grammar specification and record it in the format described in gram.h. All guards are copied into the fguard file and all actions into faction, in each case forming the body of a C function (yyguard or yyaction) which contains a switch statement to decide which guard or action to execute.The entry point is reader(). */#include <stdio.h>#include <ctype.h>#include "system.h"#include "files.h"#include "new.h"#include "symtab.h"#include "lex.h"#include "gram.h"#include "machine.h"#define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\ {\n int timestamp;\n int first_line;\n int first_column;\\n int last_line;\n int last_column;\n char *text;\n }\n\ yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"/* Number of slots allocated (but not necessarily used yet) in `rline' */int rline_allocated;extern char *program_name;extern int definesflag;extern int nolinesflag;extern int noparserflag;extern int rawtoknumflag;extern bucket *symval;extern int numval;extern int expected_conflicts;extern char *token_buffer;extern void init_lex();extern void tabinit();extern void output_headers();extern void output_trailers();extern void free_symtab();extern void open_extra_files();extern char *int_to_string();extern void fatal();extern void fatals();extern void warn();extern void warni();extern void warns();extern void warnss();extern void warnsss();extern void unlex();extern void done();extern int skip_white_space();extern int parse_percent_token();extern int lex();void reader_output_yylsp();void read_declarations();void copy_definition();void parse_token_decl();void parse_start_decl();void parse_type_decl();void parse_assoc_decl();void parse_union_decl();void parse_expect_decl();void parse_thong_decl();void copy_action();void readgram();void record_rule_line();void packsymbols();void output_token_defines();void packgram();int read_signed_integer();static int get_type();typedef struct symbol_list { struct symbol_list *next; bucket *sym; bucket *ruleprec; } symbol_list;int lineno;symbol_list *grammar;int start_flag;bucket *startval;char **tags;int *user_toknums;/* Nonzero if components of semantic values are used, implying they must be unions. */static int value_components_used;static int typed; /* nonzero if %union has been seen. */static int lastprec; /* incremented for each %left, %right or %nonassoc seen */static int gensym_count; /* incremented for each generated symbol */static bucket *errtoken;/* Nonzero if any action or guard uses the @n construct. */static int yylsp_needed;extern char *version_string;static voidskip_to_char(target) int target;{ int c; if (target == '\n') warn(" Skipping to next \\n"); else warni(" Skipping to next %c", target); do c = skip_white_space(); while (c != target && c != EOF); if (c != EOF) ungetc(c, finput);}voidreader(){ start_flag = 0; startval = NULL; /* start symbol not specified yet. */#if 0 translations = 0; /* initially assume token number translation not needed. */#endif /* Nowadays translations is always set to 1, since we give `error' a user-token-number to satisfy the Posix demand for YYERRCODE==256. */ translations = 1; nsyms = 1; nvars = 0; nrules = 0; nitems = 0; rline_allocated = 10; rline = NEW2(rline_allocated, short); typed = 0; lastprec = 0; gensym_count = 0; semantic_parser = 0; pure_parser = 0; yylsp_needed = 0; grammar = NULL; init_lex(); lineno = 1; /* initialize the symbol table. */ tabinit(); /* construct the error token */ errtoken = getsym("error"); errtoken->class = STOKEN; errtoken->user_token_number = 256; /* Value specified by posix. */ /* construct a token that represents all undefined literal tokens. */ /* it is always token number 2. */ getsym("$undefined.")->class = STOKEN; /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file. Also notice any %token, %left, etc. found there. */ if (noparserflag) fprintf(ftable, "\n/* Bison-generated parse tables, made from %s\n", infile); else fprintf(ftable, "\n/* A Bison parser, made from %s\n", infile); fprintf(ftable, " by %s */\n\n", version_string); fprintf(ftable, "#define YYBISON 1 /* Identify Bison output. */\n\n"); read_declarations(); /* start writing the guard and action files, if they are needed. */ output_headers(); /* read in the grammar, build grammar in list form. write out guards and actions. */ readgram(); /* Now we know whether we need the line-number stack. If we do, write its type into the .tab.h file. */ if (fdefines) reader_output_yylsp(fdefines); /* write closing delimiters for actions and guards. */ output_trailers(); if (yylsp_needed) fprintf(ftable, "#define YYLSP_NEEDED\n\n"); /* assign the symbols their symbol numbers. Write #defines for the token symbols into fdefines if requested. */ packsymbols(); /* convert the grammar into the format described in gram.h. */ packgram(); /* free the symbol table data structure since symbols are now all referred to by symbol number. */ free_symtab();}voidreader_output_yylsp(f) FILE *f;{ if (yylsp_needed) fprintf(f, LTYPESTR);}/* read from finput until %% is seen. Discard the %%.Handle any % declarations,and copy the contents of any %{ ... %} groups to fattrs. */voidread_declarations (){ register int c; register int tok; for (;;) { c = skip_white_space(); if (c == '%') { tok = parse_percent_token(); switch (tok) { case TWO_PERCENTS: return; case PERCENT_LEFT_CURLY: copy_definition(); break; case TOKEN: parse_token_decl (STOKEN, SNTERM); break; case NTERM: parse_token_decl (SNTERM, STOKEN); break; case TYPE: parse_type_decl(); break; case START: parse_start_decl(); break; case UNION: parse_union_decl(); break; case EXPECT: parse_expect_decl(); break; case THONG: parse_thong_decl(); break; case LEFT: parse_assoc_decl(LEFT_ASSOC); break; case RIGHT: parse_assoc_decl(RIGHT_ASSOC); break; case NONASSOC: parse_assoc_decl(NON_ASSOC); break; case SEMANTIC_PARSER: if (semantic_parser == 0) { semantic_parser = 1; open_extra_files(); } break; case PURE_PARSER: pure_parser = 1; break; case NOOP: break; default: warns("unrecognized: %s", token_buffer); skip_to_char('%'); } } else if (c == EOF) fatal("no input grammar"); else { char buff[100]; sprintf(buff, "unknown character: %s", printable_version(c)); warn(buff); skip_to_char('%'); } }}/* copy the contents of a %{ ... %} into the definitions file.The %{ has already been read. Return after reading the %}. */voidcopy_definition (){ register int c; register int match; register int ended; register int after_percent; /* -1 while reading a character if prev char was % */ int cplus_comment; if (!nolinesflag) fprintf(fattrs, "#line %d \"%s\"\n", lineno, infile); after_percent = 0; c = getc(finput); for (;;) { switch (c) { case '\n': putc(c, fattrs); lineno++; break; case '%': after_percent = -1; break; case '\'': case '"': match = c; putc(c, fattrs); c = getc(finput); while (c != match) { if (c == EOF) fatal("unterminated string at end of file"); if (c == '\n') { warn("unterminated string"); ungetc(c, finput); c = match; continue; } putc(c, fattrs); if (c == '\\') { c = getc(finput); if (c == EOF) fatal("unterminated string at end of file"); putc(c, fattrs); if (c == '\n') lineno++; } c = getc(finput); } putc(c, fattrs); break; case '/': putc(c, fattrs); c = getc(finput); if (c != '*' && c != '/') continue; cplus_comment = (c == '/'); putc(c, fattrs); c = getc(finput); ended = 0; while (!ended) { if (!cplus_comment && c == '*') { while (c == '*') { putc(c, fattrs); c = getc(finput); } if (c == '/') { putc(c, fattrs); ended = 1; } } else if (c == '\n') { lineno++; putc(c, fattrs); if (cplus_comment) ended = 1; else c = getc(finput); } else if (c == EOF) fatal("unterminated comment in `%{' definition"); else { putc(c, fattrs); c = getc(finput); } } break; case EOF: fatal("unterminated `%{' definition"); default: putc(c, fattrs); } c = getc(finput); if (after_percent) { if (c == '}') return; putc('%', fattrs); } after_percent = 0; }}/* parse what comes after %token or %nterm.For %token, what_is is STOKEN and what_is_not is SNTERM.For %nterm, the arguments are reversed. */voidparse_token_decl (what_is, what_is_not) int what_is, what_is_not;{ register int token = 0; register char *typename = 0; register struct bucket *symbol = NULL; /* pts to symbol being defined */ int k; for (;;) { if(ungetc(skip_white_space(), finput) == '%') return; token = lex(); if (token == COMMA) { symbol = NULL; continue; } if (token == TYPENAME) { k = strlen(token_buffer); typename = NEW2(k + 1, char); strcpy(typename, token_buffer); value_components_used = 1; symbol = NULL; } else if (token == IDENTIFIER && *symval->tag == '\"' && symbol) { translations = 1; symval->class = STOKEN; symval->type_name = typename; symval->user_token_number = symbol->user_token_number; symbol->user_token_number = SALIAS; symval->alias = symbol; symbol->alias = symval; symbol = NULL; nsyms--; /* symbol and symval combined are only one symbol */ } else if (token == IDENTIFIER) { int oldclass = symval->class; symbol = symval; if (symbol->class == what_is_not) warns("symbol %s redefined", symbol->tag); symbol->class = what_is; if (what_is == SNTERM && oldclass != SNTERM) symbol->value = nvars++; if (typename) { if (symbol->type_name == NULL) symbol->type_name = typename; else if (strcmp(typename, symbol->type_name) != 0) warns("type redeclaration for %s", symbol->tag); } } else if (symbol && token == NUMBER) { symbol->user_token_number = numval; translations = 1; } else { warnss("`%s' is invalid in %s", token_buffer, (what_is == STOKEN) ? "%token" : "%nterm"); skip_to_char('%'); } }}/* parse what comes after %thong the full syntax is %thong <type> token number literal the <type> or number may be omitted. The number specifies the user_token_number. Two symbols are entered in the table, one for the token symbol and one for the literal. Both are given the <type>, if any, from the declaration. The ->user_token_number of the first is SALIAS and the ->user_token_number of the second is set to the number, if any, from the declaration. The two symbols are linked via pointers in their ->alias fields. during output_defines_table, the symbol is reported thereafter, only the literal string is retained it is the literal string that is output to yytname*/voidparse_thong_decl (){ register int token; register struct bucket *symbol; register char *typename = 0; int k, usrtoknum; translations = 1; token = lex(); /* fetch typename or first token */ if (token == TYPENAME) { k = strlen(token_buffer); typename = NEW2(k + 1, char); strcpy(typename, token_buffer); value_components_used = 1; token = lex(); /* fetch first token */ } /* process first token */ if (token != IDENTIFIER) { warns("unrecognized item %s, expected an identifier", token_buffer); skip_to_char('%'); return; } symval->class = STOKEN; symval->type_name = typename; symval->user_token_number = SALIAS; symbol = symval; token = lex(); /* get number or literal string */ if (token == NUMBER) { usrtoknum = numval; token = lex(); /* okay, did number, now get literal */ } else usrtoknum = 0; /* process literal string token */ if (token != IDENTIFIER || *symval->tag != '\"') { warns("expected string constant instead of %s", token_buffer); skip_to_char('%'); return; } symval->class = STOKEN; symval->type_name = typename; symval->user_token_number = usrtoknum; symval->alias = symbol; symbol->alias = symval; nsyms--; /* symbol and symval combined are only one symbol */}/* parse what comes after %start */voidparse_start_decl (){ if (start_flag) warn("multiple %start declarations"); if (lex() != IDENTIFIER) warn("invalid %start declaration"); else { start_flag = 1; startval = symval; }}/* read in a %type declaration and record its information for get_type_name to access */voidparse_type_decl (){ register int k; register char *name; if (lex() != TYPENAME) { warn("%type declaration has no <typename>"); skip_to_char('%'); return; } k = strlen(token_buffer); name = NEW2(k + 1, char); strcpy(name, token_buffer); for (;;) { register int t; if(ungetc(skip_white_space(), finput) == '%') return; t = lex(); switch (t) { case COMMA: case SEMICOLON: break; case IDENTIFIER: if (symval->type_name == NULL) symval->type_name = name; else if (strcmp(name, symval->type_name) != 0) warns("type redeclaration for %s", symval->tag); break; default: warns("invalid %%type declaration due to item: `%s'", token_buffer); skip_to_char('%'); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -