⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reader.c

📁 Bison语法分析器
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Input parser for bison   Copyright (C) 1984, 1986, 1989, 1992, 1998 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, Inc., 59 Temple Place - Suite 330,Boston, MA 02111-1307, 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 "system.h"#include "files.h"#include "alloc.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 int maxtoken;extern void init_lex PARAMS((void));extern char *grow_token_buffer PARAMS((char *));extern void tabinit PARAMS((void));extern void output_headers PARAMS((void));extern void output_trailers PARAMS((void));extern void free_symtab PARAMS((void));extern void open_extra_files PARAMS((void));extern char *int_to_string PARAMS((int));extern char *printable_version PARAMS((int));extern void fatal PARAMS((char *));extern void fatals PARAMS((char *, char *));extern void warn PARAMS((char *));extern void warni PARAMS((char *, int));extern void warns PARAMS((char *, char *));extern void warnss PARAMS((char *, char *, char *));extern void warnsss PARAMS((char *, char *, char *, char *));extern void unlex PARAMS((int));extern void done PARAMS((int));extern int skip_white_space PARAMS((void));extern int parse_percent_token PARAMS((void));extern int lex PARAMS((void));typedef  struct symbol_list    {      struct symbol_list *next;      bucket *sym;      bucket *ruleprec;    }  symbol_list;void reader PARAMS((void));void reader_output_yylsp PARAMS((FILE *));void read_declarations PARAMS((void));void copy_definition PARAMS((void));void parse_token_decl PARAMS((int, int));void parse_start_decl PARAMS((void));void parse_type_decl PARAMS((void));void parse_assoc_decl PARAMS((int));void parse_union_decl PARAMS((void));void parse_expect_decl PARAMS((void));char *get_type_name PARAMS((int, symbol_list *));void copy_guard PARAMS((symbol_list *, int));void parse_thong_decl PARAMS((void));void copy_action PARAMS((symbol_list *, int));bucket *gensym PARAMS((void));void readgram PARAMS((void));void record_rule_line PARAMS((void));void packsymbols PARAMS((void));void output_token_defines PARAMS((FILE *));void packgram PARAMS((void));int read_signed_integer PARAMS((FILE *));#if 0static int get_type PARAMS((void));#endifint 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;static bucket *undeftoken;/* Nonzero if any action or guard uses the @n construct.  */static int yylsp_needed;extern char *version_string;static voidskip_to_char (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 (void){  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.  */  undeftoken = getsym("$undefined.");  undeftoken->class = STOKEN;  undeftoken->user_token_number = 2;  /* 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 (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 (void){  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 (void){  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 (int what_is, int what_is_not){  register int token = 0;  register char *typename = 0;  register struct bucket *symbol = NULL;  /* pts to symbol being defined */  int k;  for (;;)    {      int tmp_char = ungetc (skip_white_space (), finput);      if (tmp_char == '%')	return;      if (tmp_char == EOF)	fatals ("Premature EOF after %s", token_buffer);      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 (void){  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 (void){  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 (void){  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;      int tmp_char = ungetc (skip_white_space (), finput);      if (tmp_char == '%')	return;      if (tmp_char == EOF)	fatals ("Premature EOF after %s", token_buffer);      t = lex();      switch (t)	{	case COMMA:	case SEMICOLON:	  break;	case IDENTIFIER:	  if (symval->type_name == NULL)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -