]>
git.saurik.com Git - bison.git/blob - src/output.c
c6cb9ba83fa74395ed8abccbc2630ba861daf24d
   1 /* Output the generated parsing program for bison, 
   2    Copyright (C) 1984, 1986, 1989 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 
   7 it 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, 
  12 but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14 GNU 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 
  18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */ 
  21 /* functions to output parsing data to various files.  Entries are: 
  25 Output constant strings to the beginning of certain files. 
  29 Output constant strings to the ends of certain files. 
  33 Output the parsing tables and the parser code to ftable. 
  35 The parser tables consist of these tables. 
  36 Starred ones needed only for the semantic parser. 
  38 yytranslate = vector mapping yylex's token numbers into bison's token numbers. 
  40 yytname = vector of string-names indexed by bison token number 
  42 yyrline = vector of line-numbers of all rules.  For yydebug printouts. 
  44 yyrhs = vector of items of all rules. 
  45         This is exactly what ritems contains.  For yydebug and for semantic 
  48 yyprhs[r] = index in yyrhs of first item for rule r. 
  50 yyr1[r] = symbol number of symbol that rule r derives. 
  52 yyr2[r] = number of symbols composing right hand side of rule r. 
  54 * yystos[s] = the symbol number of the symbol that leads to state s. 
  56 yydefact[s] = default rule to reduce with in state s, 
  57               when yytable doesn't specify something else to do. 
  58               Zero means the default is an error. 
  60 yydefgoto[i] = default state to go to after a reduction of a rule that 
  61                generates variable ntokens + i, except when yytable 
  62                specifies something else to do. 
  64 yypact[s] = index in yytable of the portion describing state s. 
  65             The lookahead token's type is used to index that portion 
  66             to find out what to do. 
  68             If the value in yytable is positive, 
  69             we shift the token and go to that state. 
  71             If the value is negative, it is minus a rule number to reduce by. 
  73             If the value is zero, the default action from yydefact[s] is used. 
  75 yypgoto[i] = the index in yytable of the portion describing  
  76              what to do after reducing a rule that derives variable i + ntokens. 
  77              This portion is indexed by the parser state number 
  78              as of before the text for this nonterminal was read. 
  79              The value from yytable is the state to go to. 
  81 yytable = a vector filled with portions for different uses, 
  82           found via yypact and yypgoto. 
  84 yycheck = a vector indexed in parallel with yytable. 
  85           It indicates, in a roundabout way, the bounds of the 
  86           portion you are trying to examine. 
  88           Suppose that the portion of yytable starts at index p 
  89           and the index to be examined within the portion is i. 
  90           Then if yycheck[p+i] != i, i is outside the bounds 
  91           of what is actually allocated, and the default 
  92           (from yydefact or yydefgoto) should be used. 
  93           Otherwise, yytable[p+i] should be used. 
  95 YYFINAL = the state number of the termination state. 
  96 YYFLAG = most negative short int.  Used to flag ?? 
 110 extern int debugflag
; 
 111 extern int nolinesflag
; 
 114 extern int tokensetsize
; 
 115 extern int final_state
; 
 116 extern core 
**state_table
; 
 117 extern shifts 
**shift_table
; 
 118 extern errs 
**err_table
; 
 119 extern reductions 
**reduction_table
; 
 120 extern short *accessing_symbol
; 
 122 extern short *LAruleno
; 
 123 extern short *lookaheads
; 
 124 extern char *consistent
; 
 125 extern short *goto_map
; 
 126 extern short *from_state
; 
 127 extern short *to_state
; 
 129 void output_token_translations(); 
 132 void output_rule_data(); 
 133 void output_defines(); 
 134 void output_actions(); 
 135 void token_actions(); 
 144 void output_parser(); 
 145 void output_program(); 
 148 void free_reductions(); 
 149 void free_itemsets(); 
 152 int matching_state(); 
 155 extern void berror(); 
 156 extern void fatals(); 
 160 static short **froms
; 
 164 static short *actrow
; 
 165 static short *state_count
; 
 176 #define GUARDSTR        "\n#include \"%s\"\nextern int yyerror;\n\ 
 177 extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\ 
 178 yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\ 
 179 register YYLTYPE *yylsp;\n\ 
 180 {\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\nswitch (n)\n    {" 
 182 #define ACTSTR          "\n#include \"%s\"\nextern YYSTYPE yyval;\ 
 183 \nextern int yychar;\ 
 184 yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\ 
 185 register YYLTYPE *yylsp;\n{\n  switch (n)\n{" 
 187 #define ACTSTR_SIMPLE   "\n  switch (yyn) {\n" 
 194     fprintf(fguard
, GUARDSTR
, attrsfile
); 
 195   fprintf(faction
, (semantic_parser 
? ACTSTR 
: ACTSTR_SIMPLE
), attrsfile
); 
 196 /*  if (semantic_parser)        JF moved this below 
 197     fprintf(ftable, "#include \"%s\"\n", attrsfile); 
 198   fprintf(ftable, "#include <stdio.h>\n\n"); 
 201   /* Rename certain symbols if -p was specified.  */ 
 202   if (spec_name_prefix
) 
 204       fprintf(ftable
, "#define yyparse %sparse\n", spec_name_prefix
); 
 205       fprintf(ftable
, "#define yylex %slex\n", spec_name_prefix
); 
 206       fprintf(ftable
, "#define yyerror %serror\n", spec_name_prefix
); 
 207       fprintf(ftable
, "#define yylval %slval\n", spec_name_prefix
); 
 208       fprintf(ftable
, "#define yychar %schar\n", spec_name_prefix
); 
 209       fprintf(ftable
, "#define yydebug %sdebug\n", spec_name_prefix
); 
 210       fprintf(ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
); 
 220       fprintf(fguard
, "\n    }\n}\n"); 
 221       fprintf(faction
, "\n    }\n}\n"); 
 224     fprintf(faction
, "\n}\n"); 
 233   /* output_token_defines(ftable);      / * JF put out token defines FIRST */ 
 234   if (!semantic_parser
)         /* JF Put out other stuff */ 
 237       while ((c
=getc(fattrs
))!=EOF
) 
 242     fprintf(ftable
, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n", 
 246     fprintf(ftable
, "#include \"%s\"\n", attrsfile
); 
 247   fprintf(ftable
, "#include <stdio.h>\n\n"); 
 249   /* Make "const" do nothing if not in ANSI C.  */ 
 250   fprintf (ftable
, "#ifndef __cplusplus\n#ifndef __STDC__\n#define const\n#endif\n#endif\n\n"); 
 254   output_token_translations(); 
 255 /*   if (semantic_parser) */ 
 256   /* This is now unconditional because debugging printouts can use it.  */ 
 269 output_token_translations() 
 272 /*   register short *sp; JF unused */ 
 277               "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n", 
 278               max_user_token_number
, nsyms
); 
 280       if (ntokens 
< 127)  /* play it very safe; check maximum element value.  */ 
 281         fprintf(ftable
, "\nstatic const char yytranslate[] = {     0"); 
 283         fprintf(ftable
, "\nstatic const short yytranslate[] = {     0"); 
 286       for (i 
= 1; i 
<= max_user_token_number
; i
++) 
 300           fprintf(ftable
, "%6d", token_translations
[i
]); 
 303       fprintf(ftable
, "\n};\n"); 
 307       fprintf(ftable
, "\n#define YYTRANSLATE(x) (x)\n"); 
 319   /* With the ordinary parser, 
 320      yyprhs and yyrhs are needed only for yydebug.  */ 
 321   if (!semantic_parser
) 
 322     fprintf(ftable
, "\n#if YYDEBUG != 0"); 
 324   fprintf(ftable
, "\nstatic const short yyprhs[] = {     0"); 
 327   for (i 
= 1; i 
<= nrules
; i
++) 
 341       fprintf(ftable
, "%6d", rrhs
[i
]); 
 344   fprintf(ftable
, "\n};\n"); 
 346   fprintf(ftable
, "\nstatic const short yyrhs[] = {%6d", ritem
[0]); 
 349   for (sp 
= ritem 
+ 1; *sp
; sp
++) 
 364         fprintf(ftable
, "%6d", *sp
); 
 366         fprintf(ftable
, "     0"); 
 369   fprintf(ftable
, "\n};\n"); 
 372     fprintf(ftable
, "\n#endif\n"); 
 382   fprintf(ftable
, "\nstatic const short yystos[] = {     0"); 
 385   for (i 
= 1; i 
< nstates
; i
++) 
 399       fprintf(ftable
, "%6d", accessing_symbol
[i
]); 
 402   fprintf(ftable
, "\n};\n"); 
 412   fprintf(ftable
, "\n#if YYDEBUG != 0\nstatic const short yyrline[] = { 0"); 
 415   for (i 
= 1; i 
<= nrules
; i
++) 
 429       fprintf(ftable
, "%6d", rline
[i
]); 
 432   /* Output the table of symbol names.  */ 
 435           "\n};\n\nstatic const char * const yytname[] = {   \"%s\"", 
 438   j 
= strlen (tags
[0]) + 44; 
 439   for (i 
= 1; i 
<= nsyms
; i
++) 
 454       for (p 
= tags
[i
]; p 
&& *p
; p
++) 
 456           if (*p 
== '"' || *p 
== '\\') 
 458               fprintf(ftable
, "\\%c", *p
); 
 463               fprintf(ftable
, "\\n"); 
 468               fprintf(ftable
, "\\t"); 
 473               fprintf(ftable
, "\\b"); 
 476           else if (*p 
< 040 || *p 
>= 0177) 
 478               fprintf(ftable
, "\\%03o", *p
); 
 492   fprintf(ftable
, "\n};\n#endif\n\nstatic const short yyr1[] = {     0"); 
 495   for (i 
= 1; i 
<= nrules
; i
++) 
 509       fprintf(ftable
, "%6d", rlhs
[i
]); 
 514   fprintf(ftable
, "\n};\n\nstatic const short yyr2[] = {     0"); 
 517   for (i 
= 1; i 
< nrules
; i
++) 
 531       fprintf(ftable
, "%6d", rrhs
[i 
+ 1] - rrhs
[i
] - 1); 
 538   fprintf(ftable
, "%6d\n};\n", nitems 
- rrhs
[nrules
] - 1); 
 546   fprintf(ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
); 
 547   fprintf(ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
); 
 548   fprintf(ftable
, "#define\tYYNTBASE\t%d\n", ntokens
); 
 553 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck.  */ 
 558   nvectors 
= nstates 
+ nvars
; 
 560   froms 
= NEW2(nvectors
, short *); 
 561   tos 
= NEW2(nvectors
, short *); 
 562   tally 
= NEW2(nvectors
, short); 
 563   width 
= NEW2(nvectors
, short); 
 571   FREE(accessing_symbol
); 
 574   FREE(goto_map 
+ ntokens
); 
 587 /* figure out the actions for the specified state, indexed by lookahead token type. 
 589    The yydefact table is output now.  The detailed info 
 590    is saved for putting into yytable later.  */ 
 599   actrow 
= NEW2(ntokens
, short); 
 602   fprintf(ftable
, "\nstatic const short yydefact[] = {%6d", k
); 
 606   for (i 
= 1; i 
< nstates
; i
++) 
 621       fprintf(ftable
, "%6d", k
); 
 625   fprintf(ftable
, "\n};\n"); 
 631 /* Decide what to do for each type of token if seen as the lookahead token in specified state. 
 632    The value returned is used as the default action (yydefact) for the state. 
 633    In addition, actrow is filled with what to do for each kind of token, 
 634    index by symbol number, with zero meaning do the default action. 
 635    The value MINSHORT, a very negative number, means this situation 
 636    is an error.  The parser recognizes this value specially. 
 638    This is where conflicts are resolved.  The loop over lookahead rules 
 639    considered lower-numbered rules last, and the last rule considered that likes 
 640    a token gets to handle it.  */ 
 652   register int default_rule
; 
 656   register int shift_state
; 
 658   register unsigned mask
; 
 659   register unsigned *wordp
; 
 660   register reductions 
*redp
; 
 661   register shifts 
*shiftp
; 
 663   int nodefault 
= 0;  /* set nonzero to inhibit having any default reduction */ 
 665   for (i 
= 0; i 
< ntokens
; i
++) 
 670   redp 
= reduction_table
[state
]; 
 678           /* loop over all the rules available here which require lookahead */ 
 679           m 
= lookaheads
[state
]; 
 680           n 
= lookaheads
[state 
+ 1]; 
 682           for (i 
= n 
- 1; i 
>= m
; i
--) 
 684               rule 
= - LAruleno
[i
]; 
 685               wordp 
= LA 
+ i 
* tokensetsize
; 
 688               /* and find each token which the rule finds acceptable to come next */ 
 689               for (j 
= 0; j 
< ntokens
; j
++) 
 691                   /* and record this rule as the rule to use if that token follows.  */ 
 706   shiftp 
= shift_table
[state
]; 
 708   /* now see which tokens are allowed for shifts in this state. 
 709      For them, record the shift as the thing to do.  So shift is preferred to reduce.  */ 
 715       for (i 
= 0; i 
< k
; i
++) 
 717           shift_state 
= shiftp
->shifts
[i
]; 
 718           if (! shift_state
) continue; 
 720           symbol 
= accessing_symbol
[shift_state
]; 
 725           actrow
[symbol
] = shift_state
; 
 727           /* do not use any default reduction if there is a shift for error */ 
 729           if (symbol 
== error_token_number
) nodefault 
= 1; 
 733   errp 
= err_table
[state
]; 
 735   /* See which tokens are an explicit error in this state 
 736      (due to %nonassoc).  For them, record MINSHORT as the action.  */ 
 742       for (i 
= 0; i 
< k
; i
++) 
 744           symbol 
= errp
->errs
[i
]; 
 745           actrow
[symbol
] = MINSHORT
; 
 749   /* now find the most common reduction and make it the default action for this state.  */ 
 751   if (nreds 
>= 1 && ! nodefault
) 
 753       if (consistent
[state
]) 
 754         default_rule 
= redp
->rules
[0]; 
 758           for (i 
= m
; i 
< n
; i
++) 
 761               rule 
= - LAruleno
[i
]; 
 763               for (j 
= 0; j 
< ntokens
; j
++) 
 765                   if (actrow
[j
] == rule
) 
 776           /* actions which match the default are replaced with zero, 
 777              which means "use the default" */ 
 781               for (j 
= 0; j 
< ntokens
; j
++) 
 783                   if (actrow
[j
] == default_rule
) 
 787               default_rule 
= - default_rule
; 
 792   /* If have no default rule, the default is an error. 
 793      So replace any action which says "error" with "use default".  */ 
 795   if (default_rule 
== 0) 
 796     for (j 
= 0; j 
< ntokens
; j
++) 
 798         if (actrow
[j
] == MINSHORT
) 
 802   return (default_rule
); 
 817   for (i 
= 0; i 
< ntokens
; i
++) 
 826   froms
[state
] = sp1 
= sp 
= NEW2(count
, short); 
 827   tos
[state
] = sp2 
= NEW2(count
, short); 
 829   for (i 
= 0; i 
< ntokens
; i
++) 
 838   tally
[state
] = count
; 
 839   width
[state
] = sp1
[-1] - sp
[0] + 1; 
 844 /* figure out what to do after reducing with each rule, 
 845    depending on the saved state from before the beginning 
 846    of parsing the data that matched this rule. 
 848    The yydefgoto table is output now.  The detailed info 
 849    is saved for putting into yytable later.  */ 
 858   state_count 
= NEW2(nstates
, short); 
 860   k 
= default_goto(ntokens
); 
 861   fprintf(ftable
, "\nstatic const short yydefgoto[] = {%6d", k
); 
 862   save_column(ntokens
, k
); 
 865   for (i 
= ntokens 
+ 1; i 
< nsyms
; i
++) 
 880       fprintf(ftable
, "%6d", k
); 
 884   fprintf(ftable
, "\n};\n"); 
 897   register int default_state
; 
 900   m 
= goto_map
[symbol
]; 
 901   n 
= goto_map
[symbol 
+ 1]; 
 906   for (i 
= 0; i 
< nstates
; i
++) 
 909   for (i 
= m
; i 
< n
; i
++) 
 910     state_count
[to_state
[i
]]++; 
 915   for (i 
= 0; i 
< nstates
; i
++) 
 917       if (state_count
[i
] > max
) 
 919           max 
= state_count
[i
]; 
 924   return (default_state
); 
 929 save_column(symbol
, default_state
) 
 942   m 
= goto_map
[symbol
]; 
 943   n 
= goto_map
[symbol 
+ 1]; 
 946   for (i 
= m
; i 
< n
; i
++) 
 948       if (to_state
[i
] != default_state
) 
 955   symno 
= symbol 
- ntokens 
+ nstates
; 
 957   froms
[symno
] = sp1 
= sp 
= NEW2(count
, short); 
 958   tos
[symno
] = sp2 
= NEW2(count
, short); 
 960   for (i 
= m
; i 
< n
; i
++) 
 962       if (to_state
[i
] != default_state
) 
 964           *sp1
++ = from_state
[i
]; 
 965           *sp2
++ = to_state
[i
]; 
 969   tally
[symno
] = count
; 
 970   width
[symno
] = sp1
[-1] - sp
[0] + 1; 
 975 /* the next few functions decide how to pack  
 976    the actions and gotos information into yytable. */ 
 987   order 
= NEW2(nvectors
, short); 
 990   for (i 
= 0; i 
< nvectors
; i
++) 
 998           while (j 
>= 0 && (width
[order
[j
]] < w
)) 
1001           while (j 
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
)) 
1004           for (k 
= nentries 
- 1; k 
> j
; k
--) 
1005             order
[k 
+ 1] = order
[k
]; 
1021   base 
= NEW2(nvectors
, short); 
1022   pos 
= NEW2(nentries
, short); 
1023   table 
= NEW2(MAXTABLE
, short); 
1024   check 
= NEW2(MAXTABLE
, short); 
1029   for (i 
= 0; i 
< nvectors
; i
++) 
1032   for (i 
= 0; i 
< MAXTABLE
; i
++) 
1035   for (i 
= 0; i 
< nentries
; i
++) 
1037       state 
= matching_state(i
); 
1040         place 
= pack_vector(i
); 
1042         place 
= base
[state
]; 
1045       base
[order
[i
]] = place
; 
1048   for (i 
= 0; i 
< nvectors
; i
++) 
1064 matching_state(vector
) 
1082   for (prev 
= vector 
- 1; prev 
>= 0; prev
--) 
1085       if (width
[j
] != w 
|| tally
[j
] != t
) 
1089       for (k 
= 0; match 
&& k 
< t
; k
++) 
1091           if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
]) 
1114   register short *from
; 
1121     berror("pack_vector"); 
1126   for (j 
= lowzero 
- from
[0]; j 
< MAXTABLE
; j
++) 
1130       for (k 
= 0; ok 
&& k 
< t
; k
++) 
1134             fatals("maximum table size (%d) exceeded",MAXTABLE
); 
1136           if (table
[loc
] != 0) 
1140       for (k 
= 0; ok 
&& k 
< vector
; k
++) 
1148           for (k 
= 0; k 
< t
; k
++) 
1152               check
[loc
] = from
[k
]; 
1155           while (table
[lowzero
] != 0) 
1165   berror("pack_vector"); 
1166   return 0;     /* JF keep lint happy */ 
1171 /* the following functions output yytable, yycheck 
1172    and the vectors whose elements index the portion starts */ 
1180   fprintf(ftable
, "\nstatic const short yypact[] = {%6d", base
[0]); 
1183   for (i 
= 1; i 
< nstates
; i
++) 
1197       fprintf(ftable
, "%6d", base
[i
]); 
1200   fprintf(ftable
, "\n};\n\nstatic const short yypgoto[] = {%6d", base
[nstates
]); 
1203   for (i 
= nstates 
+ 1; i 
< nvectors
; i
++) 
1217       fprintf(ftable
, "%6d", base
[i
]); 
1220   fprintf(ftable
, "\n};\n"); 
1231   fprintf(ftable
, "\n\n#define\tYYLAST\t\t%d\n\n", high
); 
1232   fprintf(ftable
, "\nstatic const short yytable[] = {%6d", table
[0]); 
1235   for (i 
= 1; i 
<= high
; i
++) 
1249       fprintf(ftable
, "%6d", table
[i
]); 
1252   fprintf(ftable
, "\n};\n"); 
1263   fprintf(ftable
, "\nstatic const short yycheck[] = {%6d", check
[0]); 
1266   for (i 
= 1; i 
<= high
; i
++) 
1280       fprintf(ftable
, "%6d", check
[i
]); 
1283   fprintf(ftable
, "\n};\n"); 
1289 /* copy the parser code into the ftable file at the end.  */ 
1298 #define fpars fparser 
1302     fprintf(ftable
, "#define YYPURE 1\n\n"); 
1304 #ifdef DONTDEF  /* JF no longer needed 'cuz open_extra_files changes the 
1305                    currently open parser from bison.simple to bison.hairy */ 
1306   if (semantic_parser
) 
1308   else fpars 
= fparser1
; 
1311   /* Loop over lines in the standard parser file.  */ 
1319       /* See if the line starts with `#line. 
1320          If so, set write_line to 0.  */ 
1337                           fprintf(ftable
, "#lin"); 
1340                       fprintf(ftable
, "#li"); 
1343                   fprintf(ftable
, "#l"); 
1346               fprintf(ftable
, "#"); 
1349       /* now write out the line... */ 
1350       for ( ; c 
!= '\n' && c 
!= EOF
; c 
= getc(fpars
)) 
1354               /* `$' in the parser file indicates where to put the actions. 
1355                  Copy them in at this point.  */ 
1357               for(c
=getc(faction
);c
!=EOF
;c
=getc(faction
)) 
1376     fprintf(ftable
, "#line %d \"%s\"\n", lineno
, infile
); 
1390   register core 
*cp
,*cptmp
; 
1394   for (cp 
= first_state
; cp
; cp 
= cptmp
) { 
1404   register shifts 
*sp
,*sptmp
;/* JF derefrenced freed ptr */ 
1408   for (sp 
= first_shift
; sp
; sp 
= sptmp
) { 
1418   register reductions 
*rp
,*rptmp
;/* JF fixed freed ptr */ 
1420   FREE(reduction_table
); 
1422   for (rp 
= first_reduction
; rp
; rp 
= rptmp
) {