]>
git.saurik.com Git - bison.git/blob - src/output.c
f86d2243a37db7b3215d89afe01a91a8aa2a8622
   1 /* Output the generated parsing program for bison, 
   2    Copyright (C) 1984, 1986, 1989, 1992, 2000 Free Software Foundation, Inc. 
   4    This file is part of Bison, the GNU Compiler Compiler. 
   6    Bison is free software; you can redistribute it and/or modify it 
   7    under the terms of the GNU General Public License as published by 
   8    the Free Software Foundation; either version 2, or (at your option) 
  11    Bison is distributed in the hope that it will be useful, but 
  12    WITHOUT ANY WARRANTY; without even the implied warranty of 
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
  14    General Public License for more details. 
  16    You should have received a copy of the GNU General Public License 
  17    along with Bison; see the file COPYING.  If not, write to the Free 
  18    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
  22 /* The parser tables consist of these tables. 
  23    Starred ones needed only for the semantic parser. 
  24    Double starred are output only if switches are set. 
  26    yytranslate = vector mapping yylex's token numbers into bison's token 
  29    ** yytname = vector of string-names indexed by bison token number 
  31    ** yytoknum = vector of yylex token numbers corresponding to entries 
  34    yyrline = vector of line-numbers of all rules.  For yydebug printouts. 
  36    yyrhs = vector of items of all rules. 
  37    This is exactly what ritems contains.  For yydebug and for semantic 
  40    yyprhs[r] = index in yyrhs of first item for rule r. 
  42    yyr1[r] = symbol number of symbol that rule r derives. 
  44    yyr2[r] = number of symbols composing right hand side of rule r. 
  46    * yystos[s] = the symbol number of the symbol that leads to state s. 
  48    yydefact[s] = default rule to reduce with in state s, 
  49    when yytable doesn't specify something else to do. 
  50    Zero means the default is an error. 
  52    yydefgoto[i] = default state to go to after a reduction of a rule that 
  53    generates variable ntokens + i, except when yytable 
  54    specifies something else to do. 
  56    yypact[s] = index in yytable of the portion describing state s. 
  57    The lookahead token's type is used to index that portion 
  58    to find out what to do. 
  60    If the value in yytable is positive, 
  61    we shift the token and go to that state. 
  63    If the value is negative, it is minus a rule number to reduce by. 
  65    If the value is zero, the default action from yydefact[s] is used. 
  67    yypgoto[i] = the index in yytable of the portion describing 
  68    what to do after reducing a rule that derives variable i + ntokens. 
  69    This portion is indexed by the parser state number, s, 
  70    as of before the text for this nonterminal was read. 
  71    The value from yytable is the state to go to if 
  72    the corresponding value in yycheck is s. 
  74    yytable = a vector filled with portions for different uses, 
  75    found via yypact and yypgoto. 
  77    yycheck = a vector indexed in parallel with yytable. 
  78    It indicates, in a roundabout way, the bounds of the 
  79    portion you are trying to examine. 
  81    Suppose that the portion of yytable starts at index p 
  82    and the index to be examined within the portion is i. 
  83    Then if yycheck[p+i] != i, i is outside the bounds 
  84    of what is actually allocated, and the default 
  85    (from yydefact or yydefgoto) should be used. 
  86    Otherwise, yytable[p+i] should be used. 
  88    YYFINAL = the state number of the termination state. 
  89    YYFLAG = most negative short int.  Used to flag ?? 
 103 #include "conflicts.h" 
 105 extern void berror 
PARAMS((const char *)); 
 111 static short **froms
; 
 115 static short *actrow
; 
 116 static short *state_count
; 
 128 output_short_table (FILE *out
, 
 129                     const char *table_name
, 
 132                     short begin
, short end
) 
 136   fprintf (out
, "static const short %s[] = {%6d", table_name
, first_value
); 
 139   for (i 
= begin
; i 
< end
; i
++) 
 153       fprintf (out
, "%6d", short_table
[i
]); 
 156   fprintf (out
, "\n};\n"); 
 160 /*--------------------------------------------------------------. 
 161 | output_headers -- Output constant strings to the beginning of | 
 163 `--------------------------------------------------------------*/ 
 168 extern int yyerror;\n\ 
 169 extern int yycost;\n\ 
 170 extern char * yymsg;\n\ 
 171 extern YYSTYPE yyval;\n\ 
 173 yyguard(n, yyvsp, yylsp)\n\ 
 175 register YYSTYPE *yyvsp;\n\ 
 176 register YYLTYPE *yylsp;\n\ 
 187 extern YYSTYPE yyval;\n\ 
 188 extern int yychar;\n\ 
 190 yyaction(n, yyvsp, yylsp)\n\ 
 192 register YYSTYPE *yyvsp;\n\ 
 193 register YYLTYPE *yylsp;\n\ 
 198 #define ACTSTR_SIMPLE   "\n  switch (yyn) {\n" 
 201 output_headers (void) 
 204     fprintf (fguard
, GUARDSTR
, attrsfile
); 
 209   fprintf (faction
, (semantic_parser 
? ACTSTR 
: ACTSTR_SIMPLE
), attrsfile
); 
 210 /*  if (semantic_parser)        JF moved this below 
 211     fprintf(ftable, "#include \"%s\"\n", attrsfile); 
 212   fprintf(ftable, "#include <stdio.h>\n\n"); 
 215   /* Rename certain symbols if -p was specified.  */ 
 216   if (spec_name_prefix
) 
 218       fprintf (ftable
, "#define yyparse %sparse\n", spec_name_prefix
); 
 219       fprintf (ftable
, "#define yylex %slex\n", spec_name_prefix
); 
 220       fprintf (ftable
, "#define yyerror %serror\n", spec_name_prefix
); 
 221       fprintf (ftable
, "#define yylval %slval\n", spec_name_prefix
); 
 222       fprintf (ftable
, "#define yychar %schar\n", spec_name_prefix
); 
 223       fprintf (ftable
, "#define yydebug %sdebug\n", spec_name_prefix
); 
 224       fprintf (ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
); 
 229 /*-------------------------------------------------------. 
 230 | Output constant strings to the ends of certain files.  | 
 231 `-------------------------------------------------------*/ 
 234 output_trailers (void) 
 237     fprintf (fguard
, "\n    }\n}\n"); 
 239   fprintf (faction
, "\n"); 
 245     fprintf (faction
, "    }\n"); 
 246   fprintf (faction
, "}\n"); 
 252 output_token_translations (void) 
 255 /*   short *sp; JF unused */ 
 260                "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n", 
 261                max_user_token_number
, nsyms
); 
 263       if (ntokens 
< 127)        /* play it very safe; check maximum element value.  */ 
 264         fprintf (ftable
, "\nstatic const char yytranslate[] = {     0"); 
 266         fprintf (ftable
, "\nstatic const short yytranslate[] = {     0"); 
 269       for (i 
= 1; i 
<= max_user_token_number
; i
++) 
 283           fprintf (ftable
, "%6d", token_translations
[i
]); 
 286       fprintf (ftable
, "\n};\n"); 
 290       fprintf (ftable
, "\n#define YYTRANSLATE(x) (x)\n"); 
 301   /* With the ordinary parser, 
 302      yyprhs and yyrhs are needed only for yydebug. */ 
 303   /* With the noparser option, all tables are generated */ 
 304   if (!semantic_parser 
&& !noparserflag
) 
 305     fprintf (ftable
, "\n#if YYDEBUG != 0\n"); 
 307   output_short_table (ftable
, "yyprhs", rrhs
, 
 310   fprintf (ftable
, "\nstatic const short yyrhs[] = {%6d", ritem
[0]); 
 313   for (sp 
= ritem 
+ 1; *sp
; sp
++) 
 328         fprintf (ftable
, "%6d", *sp
); 
 330         fprintf (ftable
, "     0"); 
 333   fprintf (ftable
, "\n};\n"); 
 335   if (!semantic_parser 
&& !noparserflag
) 
 336     fprintf (ftable
, "\n#endif\n"); 
 343   output_short_table (ftable
, "yystos", accessing_symbol
, 
 349 output_rule_data (void) 
 356 /* YYRLINE[yyn]: source line where rule number YYN was defined. */\n", 
 359   output_short_table (ftable
, "yyrline", rline
, 
 362   fputs ("#endif\n\n", ftable
); 
 364   if (toknumflag 
|| noparserflag
) 
 366       fprintf (ftable
, "#define YYNTOKENS %d\n", ntokens
); 
 367       fprintf (ftable
, "#define YYNNTS %d\n", nvars
); 
 368       fprintf (ftable
, "#define YYNRULES %d\n", nrules
); 
 369       fprintf (ftable
, "#define YYNSTATES %d\n", nstates
); 
 370       fprintf (ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
); 
 373   if (!toknumflag 
&& !noparserflag
) 
 374     fprintf (ftable
, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n"); 
 376   /* Output the table of symbol names.  */ 
 379            "static const char * const yytname[] = {   \"%s\"", tags
[0]); 
 381   j 
= strlen (tags
[0]) + 44; 
 382   for (i 
= 1; i 
< nsyms
; i
++) 
 383     /* this used to be i<=nsyms, but that output a final "" symbol 
 384        almost by accident */ 
 399       for (p 
= tags
[i
]; p 
&& *p
; p
++) 
 401           if (*p 
== '"' || *p 
== '\\') 
 403               fprintf (ftable
, "\\%c", *p
); 
 408               fprintf (ftable
, "\\n"); 
 413               fprintf (ftable
, "\\t"); 
 418               fprintf (ftable
, "\\b"); 
 421           else if (*p 
< 040 || *p 
>= 0177) 
 423               fprintf (ftable
, "\\%03o", *p
); 
 436   /* add a NULL entry to list of tokens */ 
 437   fprintf (ftable
, ", NULL\n};\n"); 
 439   if (!toknumflag 
&& !noparserflag
) 
 440     fprintf (ftable
, "#endif\n\n"); 
 442   /* Output YYTOKNUM. */ 
 445       output_short_table (ftable
, "yytoknum", user_toknums
, 
 451 /* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n", ftable
); 
 453   output_short_table (ftable
, "yyr1", rlhs
, 
 461 /* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\ 
 462 static const short yyr2[] = {     0", ftable
); 
 464   for (i 
= 1; i 
< nrules
; i
++) 
 478       fprintf (ftable
, "%6d", rrhs
[i 
+ 1] - rrhs
[i
] - 1); 
 485   fprintf (ftable
, "%6d\n};\n", nitems 
- rrhs
[nrules
] - 1); 
 491 output_defines (void) 
 493   fprintf (ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
); 
 494   fprintf (ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
); 
 495   fprintf (ftable
, "#define\tYYNTBASE\t%d\n", ntokens
); 
 499 /*------------------------------------------------------------------. 
 500 | Decide what to do for each type of token if seen as the lookahead | 
 501 | token in specified state.  The value returned is used as the      | 
 502 | default action (yydefact) for the state.  In addition, actrow is  | 
 503 | filled with what to do for each kind of token, index by symbol    | 
 504 | number, with zero meaning do the default action.  The value       | 
 505 | MINSHORT, a very negative number, means this situation is an      | 
 506 | error.  The parser recognizes this value specially.               | 
 508 | This is where conflicts are resolved.  The loop over lookahead    | 
 509 | rules considered lower-numbered rules last, and the last rule     | 
 510 | considered that likes a token gets to handle it.                  | 
 511 `------------------------------------------------------------------*/ 
 514 action_row (int state
) 
 533   int nodefault 
= 0;            /* set nonzero to inhibit having any default reduction */ 
 535   for (i 
= 0; i 
< ntokens
; i
++) 
 540   redp 
= reduction_table
[state
]; 
 548           /* loop over all the rules available here which require 
 550           m 
= lookaheads
[state
]; 
 551           n 
= lookaheads
[state 
+ 1]; 
 553           for (i 
= n 
- 1; i 
>= m
; i
--) 
 556               wordp 
= LA 
+ i 
* tokensetsize
; 
 559               /* and find each token which the rule finds acceptable 
 561               for (j 
= 0; j 
< ntokens
; j
++) 
 563                   /* and record this rule as the rule to use if that 
 579   shiftp 
= shift_table
[state
]; 
 581   /* Now see which tokens are allowed for shifts in this state.  For 
 582      them, record the shift as the thing to do.  So shift is preferred 
 589       for (i 
= 0; i 
< k
; i
++) 
 591           shift_state 
= shiftp
->shifts
[i
]; 
 595           symbol 
= accessing_symbol
[shift_state
]; 
 600           actrow
[symbol
] = shift_state
; 
 602           /* Do not use any default reduction if there is a shift for 
 604           if (symbol 
== error_token_number
) 
 609   errp 
= err_table
[state
]; 
 611   /* See which tokens are an explicit error in this state (due to 
 612      %nonassoc).  For them, record MINSHORT as the action.  */ 
 618       for (i 
= 0; i 
< k
; i
++) 
 620           symbol 
= errp
->errs
[i
]; 
 621           actrow
[symbol
] = MINSHORT
; 
 625   /* Now find the most common reduction and make it the default action 
 628   if (nreds 
>= 1 && !nodefault
) 
 630       if (consistent
[state
]) 
 631         default_rule 
= redp
->rules
[0]; 
 635           for (i 
= m
; i 
< n
; i
++) 
 640               for (j 
= 0; j 
< ntokens
; j
++) 
 642                   if (actrow
[j
] == rule
) 
 653           /* actions which match the default are replaced with zero, 
 654              which means "use the default" */ 
 658               for (j 
= 0; j 
< ntokens
; j
++) 
 660                   if (actrow
[j
] == default_rule
) 
 664               default_rule 
= -default_rule
; 
 669   /* If have no default rule, the default is an error. 
 670      So replace any action which says "error" with "use default".  */ 
 672   if (default_rule 
== 0) 
 673     for (j 
= 0; j 
< ntokens
; j
++) 
 675         if (actrow
[j
] == MINSHORT
) 
 693   for (i 
= 0; i 
< ntokens
; i
++) 
 702   froms
[state
] = sp1 
= sp 
= XCALLOC (short, count
); 
 703   tos
[state
] = sp2 
= XCALLOC (short, count
); 
 705   for (i 
= 0; i 
< ntokens
; i
++) 
 714   tally
[state
] = count
; 
 715   width
[state
] = sp1
[-1] - sp
[0] + 1; 
 719 /*------------------------------------------------------------------. 
 720 | Figure out the actions for the specified state, indexed by        | 
 721 | lookahead token type.                                             | 
 723 | The YYDEFACT table is output now.  The detailed info is saved for | 
 724 | putting into YYTABLE later.                                       | 
 725 `------------------------------------------------------------------*/ 
 731   short *yydefact 
= XCALLOC (short, nstates
); 
 733   actrow 
= XCALLOC (short, ntokens
); 
 734   for (i 
= 0; i 
< nstates
; ++i
) 
 736       yydefact
[i
] = action_row (i
); 
 741   output_short_table (ftable
, "yydefact", yydefact
, 
 742                       yydefact
[0], 1, nstates
); 
 750   shifts 
*sp
, *sptmp
;   /* JF derefrenced freed ptr */ 
 754   for (sp 
= first_shift
; sp
; sp 
= sptmp
) 
 763 free_reductions (void) 
 765   reductions 
*rp
, *rptmp
;       /* JF fixed freed ptr */ 
 767   XFREE (reduction_table
); 
 769   for (rp 
= first_reduction
; rp
; rp 
= rptmp
) 
 779 save_column (int symbol
, int default_state
) 
 790   m 
= goto_map
[symbol
]; 
 791   n 
= goto_map
[symbol 
+ 1]; 
 794   for (i 
= m
; i 
< n
; i
++) 
 796       if (to_state
[i
] != default_state
) 
 803   symno 
= symbol 
- ntokens 
+ nstates
; 
 805   froms
[symno
] = sp1 
= sp 
= XCALLOC (short, count
); 
 806   tos
[symno
] = sp2 
= XCALLOC (short, count
); 
 808   for (i 
= m
; i 
< n
; i
++) 
 810       if (to_state
[i
] != default_state
) 
 812           *sp1
++ = from_state
[i
]; 
 813           *sp2
++ = to_state
[i
]; 
 817   tally
[symno
] = count
; 
 818   width
[symno
] = sp1
[-1] - sp
[0] + 1; 
 822 default_goto (int symbol
) 
 830   m 
= goto_map
[symbol
]; 
 831   n 
= goto_map
[symbol 
+ 1]; 
 836   for (i 
= 0; i 
< nstates
; i
++) 
 839   for (i 
= m
; i 
< n
; i
++) 
 840     state_count
[to_state
[i
]]++; 
 845   for (i 
= 0; i 
< nstates
; i
++) 
 847       if (state_count
[i
] > max
) 
 849           max 
= state_count
[i
]; 
 854   return default_state
; 
 858 /*-------------------------------------------------------------------. 
 859 | Figure out what to do after reducing with each rule, depending on  | 
 860 | the saved state from before the beginning of parsing the data that | 
 861 | matched this rule.                                                 | 
 863 | The YYDEFGOTO table is output now.  The detailed info is saved for | 
 864 | putting into YYTABLE later.                                        | 
 865 `-------------------------------------------------------------------*/ 
 872   state_count 
= XCALLOC (short, nstates
); 
 874   k 
= default_goto (ntokens
); 
 875   fprintf (ftable
, "\nstatic const short yydefgoto[] = {%6d", k
); 
 876   save_column (ntokens
, k
); 
 879   for (i 
= ntokens 
+ 1; i 
< nsyms
; i
++) 
 893       k 
= default_goto (i
); 
 894       fprintf (ftable
, "%6d", k
); 
 898   fprintf (ftable
, "\n};\n"); 
 903 /* The next few functions decide how to pack the actions and gotos 
 904    information into yytable. */ 
 915   order 
= XCALLOC (short, nvectors
); 
 918   for (i 
= 0; i 
< nvectors
; i
++) 
 926           while (j 
>= 0 && (width
[order
[j
]] < w
)) 
 929           while (j 
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
)) 
 932           for (k 
= nentries 
- 1; k 
> j
; k
--) 
 933             order
[k 
+ 1] = order
[k
]; 
 943 matching_state (int vector
) 
 960   for (prev 
= vector 
- 1; prev 
>= 0; prev
--) 
 963       if (width
[j
] != w 
|| tally
[j
] != t
) 
 967       for (k 
= 0; match 
&& k 
< t
; k
++) 
 969           if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
]) 
 982 pack_vector (int vector
) 
1001   for (j 
= lowzero 
- from
[0]; j 
< MAXTABLE
; j
++) 
1005       for (k 
= 0; ok 
&& k 
< t
; k
++) 
1009             fatal (_("maximum table size (%d) exceeded"), MAXTABLE
); 
1011           if (table
[loc
] != 0) 
1015       for (k 
= 0; ok 
&& k 
< vector
; k
++) 
1023           for (k 
= 0; k 
< t
; k
++) 
1027               check
[loc
] = from
[k
]; 
1030           while (table
[lowzero
] != 0) 
1040   berror ("pack_vector"); 
1041   return 0;                     /* JF keep lint happy */ 
1052   base 
= XCALLOC (short, nvectors
); 
1053   pos 
= XCALLOC (short, nentries
); 
1054   table 
= XCALLOC (short, MAXTABLE
); 
1055   check 
= XCALLOC (short, MAXTABLE
); 
1060   for (i 
= 0; i 
< nvectors
; i
++) 
1063   for (i 
= 0; i 
< MAXTABLE
; i
++) 
1066   for (i 
= 0; i 
< nentries
; i
++) 
1068       state 
= matching_state (i
); 
1071         place 
= pack_vector (i
); 
1073         place 
= base
[state
]; 
1076       base
[order
[i
]] = place
; 
1079   for (i 
= 0; i 
< nvectors
; i
++) 
1092 /* the following functions output yytable, yycheck 
1093    and the vectors whose elements index the portion starts */ 
1098   output_short_table (ftable
, "yypact", base
, 
1099                       base
[0], 1, nstates
); 
1101   putc ('\n', ftable
); 
1103   output_short_table (ftable
, "yypgoto", base
, 
1104                       base
[nstates
], nstates 
+ 1, nvectors
); 
1113   fprintf (ftable
, "\n\n#define\tYYLAST\t\t%d\n\n\n", high
); 
1114   output_short_table (ftable
, "yytable", table
, 
1115                       table
[0], 1, high 
+ 1); 
1123   output_short_table (ftable
, "yycheck", check
, 
1124                       check
[0], 1, high 
+ 1); 
1128 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable 
1132 output_actions (void) 
1134   nvectors 
= nstates 
+ nvars
; 
1136   froms 
= XCALLOC (short *, nvectors
); 
1137   tos 
= XCALLOC (short *, nvectors
); 
1138   tally 
= XCALLOC (short, nvectors
); 
1139   width 
= XCALLOC (short, nvectors
); 
1147   XFREE (accessing_symbol
); 
1150   XFREE (goto_map 
+ ntokens
); 
1156   putc ('\n', ftable
); 
1159   putc ('\n', ftable
); 
1163 /* copy the parser code into the ftable file at the end.  */ 
1166 output_parser (void) 
1172 #define fpars fparser 
1176     fprintf (ftable
, "#define YYPURE 1\n\n"); 
1178 #ifdef DONTDEF                  /* JF no longer needed 'cuz open_extra_files changes the 
1179                                    currently open parser from bison.simple to bison.hairy */ 
1180   if (semantic_parser
) 
1186   /* Loop over lines in the standard parser file.  */ 
1194       /* See if the line starts with `#line. 
1195          If so, set write_line to 0.  */ 
1212                           fprintf (ftable
, "#lin"); 
1215                       fprintf (ftable
, "#li"); 
1218                   fprintf (ftable
, "#l"); 
1221               fprintf (ftable
, "#"); 
1224       /* now write out the line... */ 
1225       for (; c 
!= '\n' && c 
!= EOF
; c 
= getc (fpars
)) 
1230                 /* `$' in the parser file indicates where to put the actions. 
1231                    Copy them in at this point.  */ 
1233                 for (c 
= getc (faction
); c 
!= EOF
; c 
= getc (faction
)) 
1246 output_program (void) 
1251     fprintf (ftable
, "#line %d \"%s\"\n", lineno
, infile
); 
1263 free_itemsets (void) 
1267   XFREE (state_table
); 
1269   for (cp 
= first_state
; cp
; cp 
= cptmp
) 
1277 /*----------------------------------------------------------. 
1278 | Output the parsing tables and the parser code to ftable.  | 
1279 `----------------------------------------------------------*/ 
1286   /* output_token_defines(ftable);      / * JF put out token defines FIRST */ 
1287   if (!semantic_parser
)         /* JF Put out other stuff */ 
1290       while ((c 
= getc (fattrs
)) != EOF
) 
1293   reader_output_yylsp (ftable
); 
1297 #define YYDEBUG 1\n\ 
1302   if (semantic_parser
) 
1303     fprintf (ftable
, "#include \"%s\"\n", attrsfile
); 
1306     fprintf (ftable
, "#include <stdio.h>\n\n"); 
1308   /* Make "const" do nothing if not in ANSI C.  */ 
1310 #ifndef __cplusplus\n\ 
1311 # ifndef __STDC__\n\ 
1320   output_token_translations (); 
1321 /*   if (semantic_parser) */ 
1322   /* This is now unconditional because debugging printouts can use it.  */ 
1325   if (semantic_parser
) 
1327   output_rule_data ();