📄 output.c
字号:
/* Output the generated parsing program 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, Inc., 59 Temple Place - Suite 330,Boston, MA 02111-1307, 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.Double starred are output only if switches are set.yytranslate = vector mapping yylex's token numbers into bison's token numbers.** yytname = vector of string-names indexed by bison token number** yytoknum = vector of yylex token numbers corresponding to entries in yytnameyyrline = 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, s, as of before the text for this nonterminal was read. The value from yytable is the state to go to if the corresponding value in yycheck is s.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 "alloc.h"#include "files.h"#include "gram.h"#include "state.h"extern int debugflag;extern int nolinesflag;extern int noparserflag;extern int toknumflag;extern char **tags;extern int *user_toknums;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;extern int lineno;void output_headers PARAMS((void));void output_trailers PARAMS((void));void output PARAMS((void));void output_token_translations PARAMS((void));void output_gram PARAMS((void));void output_stos PARAMS((void));void output_rule_data PARAMS((void));void output_defines PARAMS((void));void output_actions PARAMS((void));void token_actions PARAMS((void));void save_row PARAMS((int));void goto_actions PARAMS((void));void save_column PARAMS((int, int));void sort_actions PARAMS((void));void pack_table PARAMS((void));void output_base PARAMS((void));void output_table PARAMS((void));void output_check PARAMS((void));void output_parser PARAMS((void));void output_program PARAMS((void));void free_shifts PARAMS((void));void free_reductions PARAMS((void));void free_itemsets PARAMS((void));int action_row PARAMS((int));int default_goto PARAMS((int));int matching_state PARAMS((int));int pack_vector PARAMS((int));extern void berror PARAMS((char *));extern void fatals PARAMS((char *, char *));extern char *int_to_string PARAMS((int));extern void reader_output_yylsp PARAMS((FILE *));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;#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_headers (void){ if (semantic_parser) fprintf(fguard, GUARDSTR, attrsfile); if (noparserflag) return; fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);/* if (semantic_parser) JF moved this below fprintf(ftable, "#include \"%s\"\n", attrsfile); fprintf(ftable, "#include <stdio.h>\n\n");*/ /* Rename certain symbols if -p was specified. */ if (spec_name_prefix) { fprintf(ftable, "#define yyparse %sparse\n", spec_name_prefix); fprintf(ftable, "#define yylex %slex\n", spec_name_prefix); fprintf(ftable, "#define yyerror %serror\n", spec_name_prefix); fprintf(ftable, "#define yylval %slval\n", spec_name_prefix); fprintf(ftable, "#define yychar %schar\n", spec_name_prefix); fprintf(ftable, "#define yydebug %sdebug\n", spec_name_prefix); fprintf(ftable, "#define yynerrs %snerrs\n", spec_name_prefix); }}voidoutput_trailers (void){ if (semantic_parser) fprintf(fguard, "\n }\n}\n"); fprintf(faction, "\n"); if (noparserflag) return; if (semantic_parser) fprintf(faction, " }\n"); fprintf(faction, "}\n");}voidoutput (void){ int c; /* output_token_defines(ftable); / * JF put out token defines FIRST */ if (!semantic_parser) /* JF Put out other stuff */ { rewind(fattrs); while ((c=getc(fattrs))!=EOF) putc(c,ftable); } reader_output_yylsp(ftable); if (debugflag) fprintf(ftable, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n", !!debugflag); if (semantic_parser) fprintf(ftable, "#include \"%s\"\n", attrsfile); if (! noparserflag) fprintf(ftable, "#include <stdio.h>\n\n"); /* Make "const" do nothing if not in ANSI C. */ fprintf (ftable, "#ifndef __cplusplus\n#ifndef __STDC__\n#define const\n#endif\n#endif\n\n"); 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(); if (! noparserflag) output_parser(); output_program();}voidoutput_token_translations (void){ 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 (void){ register int i; register int j; register short *sp; /* With the ordinary parser, yyprhs and yyrhs are needed only for yydebug. */ /* With the noparser option, all tables are generated */ if (! semantic_parser && ! noparserflag) fprintf(ftable, "\n#if YYDEBUG != 0"); 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 && ! noparserflag) fprintf(ftable, "\n#endif\n");}voidoutput_stos (void){ 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 (void){ register int i; register int j; fprintf(ftable, "\n#if YYDEBUG != 0\n"); fprintf(ftable, "static const short yyrline[] = { 0"); 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]); } fprintf(ftable, "\n};\n#endif\n\n"); if (toknumflag || noparserflag) { fprintf(ftable, "#define YYNTOKENS %d\n", ntokens); fprintf(ftable, "#define YYNNTS %d\n", nvars); fprintf(ftable, "#define YYNRULES %d\n", nrules); fprintf(ftable, "#define YYNSTATES %d\n", nstates); fprintf(ftable, "#define YYMAXUTOK %d\n\n", max_user_token_number); } if (! toknumflag && ! noparserflag) fprintf(ftable, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n"); /* Output the table of symbol names. */ fprintf(ftable, "static const char * const yytname[] = { \"%s\"", tags[0]); j = strlen (tags[0]) + 44; for (i = 1; i < nsyms; i++) /* this used to be i<=nsyms, but that output a final "" symbol almost by accident */ { 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; } else if (*p < 040 || *p >= 0177) { fprintf(ftable, "\\%03o", *p); j += 4; } else { putc(*p, ftable); j++; } } putc ('\"', ftable); j++; } fprintf(ftable, ", NULL\n};\n"); /* add a NULL entry to list of tokens */ if (! toknumflag && ! noparserflag) fprintf(ftable, "#endif\n\n"); if (toknumflag) { fprintf(ftable, "static const short yytoknum[] = { 0"); j = 10; for (i = 1; i <= ntokens; i++) { putc(',', ftable); if (j >= 10) { putc('\n', ftable); j = 1; } else j++; fprintf(ftable, "%6d", user_toknums[i]); } fprintf(ftable, "\n};\n\n"); } fprintf(ftable, "static const short yyr1[] = { 0"); j = 10; for (i = 1; i <= nrules; i++) { putc(',', ftable); if (j >= 10) { putc('\n', ftable); j = 1; } else { j++; } fprintf(ftable, "%6d", rlhs[i]); } FREE(rlhs + 1); fprintf(ftable, "\n};\n\nstatic const short yyr2[] = { 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 + 1] - rrhs[i] - 1); } putc(',', ftable); if (j >= 10) putc('\n', ftable); fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1); FREE(rrhs + 1);}voidoutput_defines (void){ fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state); fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT); fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);}/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */voidoutput_actions (void){ nvectors = nstates + nvars; froms = NEW2(nvectors, short *); tos = NEW2(nvectors, short *); tally = NEW2(nvectors, short); width = NEW2(nvectors, short); token_actions(); free_shifts(); free_reductions(); FREE(lookaheads); FREE(LA); FREE(LAruleno); FREE(accessing_symbol); goto_actions(); FREE(goto_map + ntokens); FREE(from_state); FREE(to_state); sort_actions(); pack_table(); output_base(); output_table(); output_check();}/* figure out the actions for the specified state, indexed by lookahead token type. The yydefact table is output now. The detailed info is saved for putting into yytable later. */voidtoken_actions (void){ register int i; register int j; register int k; actrow = NEW2(ntokens, short); k = action_row(0); fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k); save_row(0); j = 10; for (i = 1; i < nstates; i++) { putc(',', ftable); if (j >= 10) { putc('\n', ftable); j = 1; } else { j++; } k = action_row(i); fprintf(ftable, "%6d", k); save_row(i); } fprintf(ftable, "\n};\n"); FREE(actrow);}/* Decide what to do for each type of token if seen as the lookahead token in specified state. The value returned is used as the default action (yydefact) for the state. In addition, actrow is filled with what to do for each kind of token, index by symbol number, with zero meaning do the default action. The value MINSHORT, a very negative number, means this situation is an error. The parser recognizes this value specially. This is where conflicts are resolved. The loop over lookahead rules considered lower-numbered rules last, and the last rule considered that likes a token gets to handle it. */intaction_row (int state){ register int i; register int j; register int k; register int m = 0; register int n = 0; register int count; register int default_rule; register int nreds; register int max; register int rule; register int shift_state; register int symbol; register unsigned mask; register unsigned *wordp; register reductions *redp; register shifts *shiftp; register errs *errp; int nodefault = 0; /* set nonzero to inhibit having any default reduction */ for (i = 0; i < ntokens; i++) actrow[i] = 0; default_rule = 0; nreds = 0; redp = reduction_table[state]; if (redp) { nreds = redp->nreds; if (nreds >= 1) { /* loop over all the rules available here which require lookahead */ m = lookaheads[state];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -