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

📄 output.c

📁 生成C++词法/语法分析的GNU语法分析器
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Output the generated parsing program for bison,   Copyright (C) 1984, 1986, 1989 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.  *//* functions to output parsing data to various files.  Entries are:  output_headers ()Output constant strings to the beginning of certain files.  output_trailers()Output constant strings to the ends of certain files.  output ()Output the parsing tables and the parser code to ftable.The parser tables consist of these tables.Starred ones needed only for the semantic parser.yytranslate = vector mapping yylex's token numbers into bison's token numbers.yytname = vector of string-names indexed by bison token numberyyrline = vector of line-numbers of all rules.  For yydebug printouts.yyrhs = vector of items of all rules.        This is exactly what ritems contains.  For yydebug and for semantic	parser.yyprhs[r] = index in yyrhs of first item for rule r.yyr1[r] = symbol number of symbol that rule r derives.yyr2[r] = number of symbols composing right hand side of rule r.* yystos[s] = the symbol number of the symbol that leads to state s.yydefact[s] = default rule to reduce with in state s,	      when yytable doesn't specify something else to do.	      Zero means the default is an error.yydefgoto[i] = default state to go to after a reduction of a rule that	       generates variable ntokens + i, except when yytable	       specifies something else to do.yypact[s] = index in yytable of the portion describing state s.            The lookahead token's type is used to index that portion            to find out what to do.	    If the value in yytable is positive,	    we shift the token and go to that state.	    If the value is negative, it is minus a rule number to reduce by.	    If the value is zero, the default action from yydefact[s] is used.yypgoto[i] = the index in yytable of the portion describing              what to do after reducing a rule that derives variable i + ntokens.             This portion is indexed by the parser state number	     as of before the text for this nonterminal was read.	     The value from yytable is the state to go to.yytable = a vector filled with portions for different uses,          found via yypact and yypgoto.yycheck = a vector indexed in parallel with yytable.	  It indicates, in a roundabout way, the bounds of the	  portion you are trying to examine.	  Suppose that the portion of yytable starts at index p	  and the index to be examined within the portion is i.	  Then if yycheck[p+i] != i, i is outside the bounds	  of what is actually allocated, and the default	  (from yydefact or yydefgoto) should be used.	  Otherwise, yytable[p+i] should be used.YYFINAL = the state number of the termination state.YYFLAG = most negative short int.  Used to flag ??YYNTBASE = ntokens.*/#include <stdio.h>#include "system.h"#include "machine.h"#include "new.h"#include "files.h"#include "gram.h"#include "state.h"#include "symtab.h"extern int debugflag;extern int nolinesflag;extern int definesflag;extern char **tags;extern int tokensetsize;extern int final_state;extern core **state_table;extern shifts **shift_table;extern errs **err_table;extern reductions **reduction_table;extern short *accessing_symbol;extern unsigned *LA;extern short *LAruleno;extern short *lookaheads;extern char *consistent;extern short *goto_map;extern short *from_state;extern short *to_state;void output_token_translations();void output_gram();void output_stos();void output_rule_data();void output_defines();void output_actions();void token_actions();void save_row();void goto_actions();void save_column();void sort_actions();void pack_table();void output_base();void output_table();void output_check();void output_parser();void output_program();void free_itemset();void free_shifts();void free_reductions();void free_itemsets();int action_row();int default_goto();int matching_state();int pack_vector();extern void berror();extern void fatals();static int nvectors;static int nentries;static short **froms;static short **tos;static short *tally;static short *width;static short *actrow;static short *state_count;static short *order;static short *base;static short *pos;static short *table;static short *check;static int lowzero;static int high;voidoutput_section();voidoutput_token_defines_fmt();extern bucket *errtoken;void output_token_defines();void output_token_const_def();void output_token_const_decl();void output_token_enum();extern int line_fparser;extern int line_fhskel;extern char *parser_fname;extern char *hskel_fname;extern char *version_string;#define	GUARDSTR	"\n#include \"%s\"\nextern int yyerror;\n\extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\register YYLTYPE *yylsp;\n\{\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\nswitch (n)\n    {"#define	ACTSTR		"\n#include \"%s\"\nextern YYSTYPE yyval;\\nextern int yychar;\yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\register YYLTYPE *yylsp;\n{\n  switch (n)\n{"#define	ACTSTR_SIMPLE	"\n  switch (yyn) {\n"voidoutput_before_read(){   fprintf(ftable, "\n/*  A Bison++ parser, made from %s  */\n\n", infile);  fprintf(ftable, " /* with Bison++ version %s  */\n\n", version_string);  output_section(fparser,ftable);  if(definesflag) output_section(fhskel,fdefines);};voidoutput_headers(){  if(definesflag) output_section(fhskel,fdefines);  output_section(fparser,ftable);  if (pure_parser)    {     fprintf(ftable, "#define YY_%s_PURE 1\n",parser_name);     if(definesflag) fprintf(fdefines, "#define YY_%s_PURE 1\n\n",parser_name);    }  /* start writing the guard and action files, if they are needed.  */  if (semantic_parser)    fprintf(fguard, GUARDSTR, attrsfile);  fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);  if(definesflag) output_section(fhskel,fdefines);  output_section(fparser,ftable);  /* Rename certain symbols if -p was specified.  */  if (spec_name_prefix)    {      fprintf(ftable, "#define YY_%s_PARSE %sparse\n",          parser_name, spec_name_prefix);      fprintf(ftable, "#define YY_%s_LEX %slex\n",          parser_name, spec_name_prefix);      fprintf(ftable, "#define YY_%s_ERROR %serror\n",          parser_name, spec_name_prefix);      fprintf(ftable, "#define YY_%s_LVAL %slval\n",          parser_name, spec_name_prefix);      fprintf(ftable, "#define YY_%s_CHAR %schar\n",          parser_name, spec_name_prefix);      fprintf(ftable, "#define YY_%s_DEBUG %sdebug\n",          parser_name, spec_name_prefix);    }  if (spec_name_prefix && definesflag)    {      fprintf(fdefines, "#define YY_%s_PARSE %sparse\n",          parser_name, spec_name_prefix);      fprintf(fdefines, "#define YY_%s_LEX %slex\n",          parser_name, spec_name_prefix);      fprintf(fdefines, "#define YY_%s_ERROR %serror\n",          parser_name, spec_name_prefix);      fprintf(fdefines, "#define YY_%s_LVAL %slval\n",          parser_name, spec_name_prefix);      fprintf(fdefines, "#define YY_%s_CHAR %schar\n",          parser_name, spec_name_prefix);      fprintf(fdefines, "#define YY_%s_DEBUG %sdebug\n",          parser_name, spec_name_prefix);    }}voidoutput_trailers(){  if(definesflag) output_section(fhskel,fdefines);  output_section(fparser,ftable);  /* output the definition of YYLTYPE into the fattrs and fdefines files.  */  if(debugflag)   {fprintf(ftable,     "#define YY_%s_DEBUG %d\n"	    ,parser_name,!!debugflag);    if (definesflag)     fprintf(fdefines,       "#define YY_%s_DEBUG %d\n",            parser_name,!!debugflag);    }  if(definesflag) output_section(fhskel,fdefines);  output_section(fparser,ftable);  /* Now we know whether we need the line-number stack.     If we do, write its type into the .tab.h file.  */  if (yylsp_needed)    {     /* fattrs winds up in the .tab.c file, before bison.simple.  */      fprintf(ftable, "#define YYLSP_%s_NEEDED\n",parser_name);      if (debugflag)      if (definesflag)	{         fprintf(fdefines,              "#define YY_%s_LSP_NEEDED\n",              parser_name);        }    }  if (semantic_parser)    {      fprintf(fguard, "\n    }\n}\n");      fprintf(faction, "\n    }\n}\n");    }  else    fprintf(faction, "\n}\n");}voidoutput(){  int c;   if (!semantic_parser)		/* JF Put out other stuff */    {      rewind(fattrs);      while ((c=getc(fattrs))!=EOF)        putc(c,ftable);    }  if (semantic_parser)    fprintf(ftable, "#include \"%s\"\n", attrsfile);  free_itemsets();  output_defines();  output_token_translations();/*   if (semantic_parser) */  /* This is now unconditional because debugging printouts can use it.  */  output_gram();  FREE(ritem);  if (semantic_parser)    output_stos();  output_rule_data();  output_actions();  output_parser();  output_program();}voidoutput_token_translations(){  register int i, j;/*   register short *sp; JF unused */  if (translations)    {      fprintf(ftable,	      "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",	      max_user_token_number, nsyms);          if (ntokens < 127)  /* play it very safe; check maximum element value.  */        fprintf(ftable, "\nstatic const char yytranslate[] = {     0");      else	fprintf(ftable, "\nstatic const short yytranslate[] = {     0");          j = 10;      for (i = 1; i <= max_user_token_number; i++)	{	  putc(',', ftable);    	  if (j >= 10)	    {	      putc('\n', ftable);	      j = 1;	    }	  else	    {	      j++;	    }    	  fprintf(ftable, "%6d", token_translations[i]);	}          fprintf(ftable, "\n};\n");    }  else    {      fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");    } }voidoutput_gram(){  register int i;  register int j;  register short *sp;  /* With the ordinary parser,     yyprhs and yyrhs are needed only for yydebug.  */  if (!semantic_parser)    fprintf(ftable, "\n#if YY_%s_DEBUG != 0",parser_name);  fprintf(ftable, "\nstatic const short yyprhs[] = {     0");  j = 10;  for (i = 1; i <= nrules; i++)    {      putc(',', ftable);      if (j >= 10)	{	  putc('\n', ftable);	  j = 1;	}      else	{	  j++;	}      fprintf(ftable, "%6d", rrhs[i]);    }  fprintf(ftable, "\n};\n");  fprintf(ftable, "\nstatic const short yyrhs[] = {%6d", ritem[0]);  j = 10;  for (sp = ritem + 1; *sp; sp++)    {      putc(',', ftable);      if (j >= 10)	{	  putc('\n', ftable);	  j = 1;	}      else	{	  j++;	}      if (*sp > 0)	fprintf(ftable, "%6d", *sp);      else	fprintf(ftable, "     0");    }  fprintf(ftable, "\n};\n");  if(!semantic_parser)    fprintf(ftable, "\n#endif\n");}voidoutput_stos(){  register int i;  register int j;  fprintf(ftable, "\nstatic const short yystos[] = {     0");  j = 10;  for (i = 1; i < nstates; i++)    {      putc(',', ftable);      if (j >= 10)	{	  putc('\n', ftable);	  j = 1;	}      else	{	  j++;	}      fprintf(ftable, "%6d", accessing_symbol[i]);    }  fprintf(ftable, "\n};\n");}voidoutput_rule_data(){  register int i;  register int j;  fprintf(ftable,      "\n#if YY_%s_DEBUG != 0\nstatic const short yyrline[] = { 0",parser_name);  j = 10;  for (i = 1; i <= nrules; i++)    {      putc(',', ftable);      if (j >= 10)	{	  putc('\n', ftable);	  j = 1;	}      else	{	  j++;	}      fprintf(ftable, "%6d", rline[i]);    }  /* Output the table of symbol names.  */  fprintf(ftable,          "\n};\n\nstatic const char * const yytname[] = {   \"%s\"",          tags[0]);  j = strlen (tags[0]) + 44;  for (i = 1; i <= nsyms; i++)    {      register char *p;      putc(',', ftable);      j++;      if (j > 75)	{	  putc('\n', ftable);	  j = 0;	}      putc ('\"', ftable);      j++;      for (p = tags[i]; p && *p; p++)	{	  if (*p == '"' || *p == '\\')	    {	      fprintf(ftable, "\\%c", *p);	      j += 2;	    }	  else if (*p == '\n')	    {	      fprintf(ftable, "\\n");	      j += 2;	    }	  else if (*p == '\t')	    {	      fprintf(ftable, "\\t");	      j += 2;	    }	  else if (*p == '\b')	    {	      fprintf(ftable, "\\b");	      j += 2;	    }

⌨️ 快捷键说明

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