]>
git.saurik.com Git - bison.git/blob - src/output.c
20b33618b8f08e68ca86977415df62a792c6651b
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 ??
104 extern void berror
PARAMS((const char *));
107 extern short *user_toknums
;
108 extern int final_state
;
109 extern errs
**err_table
;
114 static short **froms
;
118 static short *actrow
;
119 static short *state_count
;
131 output_short_table (FILE *out
,
132 const char *table_name
,
135 short begin
, short end
)
139 fprintf (out
, "static const short %s[] = {%6d", table_name
, first_value
);
142 for (i
= begin
; i
< end
; i
++)
156 fprintf (out
, "%6d", short_table
[i
]);
159 fprintf (out
, "\n};\n");
163 /*--------------------------------------------------------------.
164 | output_headers -- Output constant strings to the beginning of |
166 `--------------------------------------------------------------*/
171 extern int yyerror;\n\
172 extern int yycost;\n\
173 extern char * yymsg;\n\
174 extern YYSTYPE yyval;\n\
176 yyguard(n, yyvsp, yylsp)\n\
178 register YYSTYPE *yyvsp;\n\
179 register YYLTYPE *yylsp;\n\
190 extern YYSTYPE yyval;\n\
191 extern int yychar;\n\
193 yyaction(n, yyvsp, yylsp)\n\
195 register YYSTYPE *yyvsp;\n\
196 register YYLTYPE *yylsp;\n\
201 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
204 output_headers (void)
207 fprintf (fguard
, GUARDSTR
, attrsfile
);
212 fprintf (faction
, (semantic_parser
? ACTSTR
: ACTSTR_SIMPLE
), attrsfile
);
213 /* if (semantic_parser) JF moved this below
214 fprintf(ftable, "#include \"%s\"\n", attrsfile);
215 fprintf(ftable, "#include <stdio.h>\n\n");
218 /* Rename certain symbols if -p was specified. */
219 if (spec_name_prefix
)
221 fprintf (ftable
, "#define yyparse %sparse\n", spec_name_prefix
);
222 fprintf (ftable
, "#define yylex %slex\n", spec_name_prefix
);
223 fprintf (ftable
, "#define yyerror %serror\n", spec_name_prefix
);
224 fprintf (ftable
, "#define yylval %slval\n", spec_name_prefix
);
225 fprintf (ftable
, "#define yychar %schar\n", spec_name_prefix
);
226 fprintf (ftable
, "#define yydebug %sdebug\n", spec_name_prefix
);
227 fprintf (ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
);
232 /*-------------------------------------------------------.
233 | Output constant strings to the ends of certain files. |
234 `-------------------------------------------------------*/
237 output_trailers (void)
240 fprintf (fguard
, "\n }\n}\n");
242 fprintf (faction
, "\n");
248 fprintf (faction
, " }\n");
249 fprintf (faction
, "}\n");
255 output_token_translations (void)
258 /* short *sp; JF unused */
263 "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
264 max_user_token_number
, nsyms
);
266 if (ntokens
< 127) /* play it very safe; check maximum element value. */
267 fprintf (ftable
, "\nstatic const char yytranslate[] = { 0");
269 fprintf (ftable
, "\nstatic const short yytranslate[] = { 0");
272 for (i
= 1; i
<= max_user_token_number
; i
++)
286 fprintf (ftable
, "%6d", token_translations
[i
]);
289 fprintf (ftable
, "\n};\n");
293 fprintf (ftable
, "\n#define YYTRANSLATE(x) (x)\n");
304 /* With the ordinary parser,
305 yyprhs and yyrhs are needed only for yydebug. */
306 /* With the noparser option, all tables are generated */
307 if (!semantic_parser
&& !noparserflag
)
308 fprintf (ftable
, "\n#if YYDEBUG != 0\n");
310 output_short_table (ftable
, "yyprhs", rrhs
,
313 fprintf (ftable
, "\nstatic const short yyrhs[] = {%6d", ritem
[0]);
316 for (sp
= ritem
+ 1; *sp
; sp
++)
331 fprintf (ftable
, "%6d", *sp
);
333 fprintf (ftable
, " 0");
336 fprintf (ftable
, "\n};\n");
338 if (!semantic_parser
&& !noparserflag
)
339 fprintf (ftable
, "\n#endif\n");
346 output_short_table (ftable
, "yystos", accessing_symbol
,
352 output_rule_data (void)
359 /* YYRLINE[yyn]: source line where rule number YYN was defined. */\n",
362 output_short_table (ftable
, "yyrline", rline
,
365 fputs ("#endif\n\n", ftable
);
367 if (toknumflag
|| noparserflag
)
369 fprintf (ftable
, "#define YYNTOKENS %d\n", ntokens
);
370 fprintf (ftable
, "#define YYNNTS %d\n", nvars
);
371 fprintf (ftable
, "#define YYNRULES %d\n", nrules
);
372 fprintf (ftable
, "#define YYNSTATES %d\n", nstates
);
373 fprintf (ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
376 if (!toknumflag
&& !noparserflag
)
377 fprintf (ftable
, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
379 /* Output the table of symbol names. */
382 "static const char * const yytname[] = { \"%s\"", tags
[0]);
384 j
= strlen (tags
[0]) + 44;
385 for (i
= 1; i
< nsyms
; i
++)
386 /* this used to be i<=nsyms, but that output a final "" symbol
387 almost by accident */
402 for (p
= tags
[i
]; p
&& *p
; p
++)
404 if (*p
== '"' || *p
== '\\')
406 fprintf (ftable
, "\\%c", *p
);
411 fprintf (ftable
, "\\n");
416 fprintf (ftable
, "\\t");
421 fprintf (ftable
, "\\b");
424 else if (*p
< 040 || *p
>= 0177)
426 fprintf (ftable
, "\\%03o", *p
);
439 /* add a NULL entry to list of tokens */
440 fprintf (ftable
, ", NULL\n};\n");
442 if (!toknumflag
&& !noparserflag
)
443 fprintf (ftable
, "#endif\n\n");
445 /* Output YYTOKNUM. */
448 output_short_table (ftable
, "yytoknum", user_toknums
,
454 /* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n", ftable
);
456 output_short_table (ftable
, "yyr1", rlhs
,
464 /* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\
465 static const short yyr2[] = { 0", ftable
);
467 for (i
= 1; i
< nrules
; i
++)
481 fprintf (ftable
, "%6d", rrhs
[i
+ 1] - rrhs
[i
] - 1);
488 fprintf (ftable
, "%6d\n};\n", nitems
- rrhs
[nrules
] - 1);
494 output_defines (void)
496 fprintf (ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
497 fprintf (ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
498 fprintf (ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
502 /*------------------------------------------------------------------.
503 | Decide what to do for each type of token if seen as the lookahead |
504 | token in specified state. The value returned is used as the |
505 | default action (yydefact) for the state. In addition, actrow is |
506 | filled with what to do for each kind of token, index by symbol |
507 | number, with zero meaning do the default action. The value |
508 | MINSHORT, a very negative number, means this situation is an |
509 | error. The parser recognizes this value specially. |
511 | This is where conflicts are resolved. The loop over lookahead |
512 | rules considered lower-numbered rules last, and the last rule |
513 | considered that likes a token gets to handle it. |
514 `------------------------------------------------------------------*/
517 action_row (int state
)
536 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
538 for (i
= 0; i
< ntokens
; i
++)
543 redp
= reduction_table
[state
];
551 /* loop over all the rules available here which require
553 m
= lookaheads
[state
];
554 n
= lookaheads
[state
+ 1];
556 for (i
= n
- 1; i
>= m
; i
--)
559 wordp
= LA
+ i
* tokensetsize
;
562 /* and find each token which the rule finds acceptable
564 for (j
= 0; j
< ntokens
; j
++)
566 /* and record this rule as the rule to use if that
582 shiftp
= shift_table
[state
];
584 /* Now see which tokens are allowed for shifts in this state. For
585 them, record the shift as the thing to do. So shift is preferred
592 for (i
= 0; i
< k
; i
++)
594 shift_state
= shiftp
->shifts
[i
];
598 symbol
= accessing_symbol
[shift_state
];
603 actrow
[symbol
] = shift_state
;
605 /* Do not use any default reduction if there is a shift for
607 if (symbol
== error_token_number
)
612 errp
= err_table
[state
];
614 /* See which tokens are an explicit error in this state (due to
615 %nonassoc). For them, record MINSHORT as the action. */
621 for (i
= 0; i
< k
; i
++)
623 symbol
= errp
->errs
[i
];
624 actrow
[symbol
] = MINSHORT
;
628 /* Now find the most common reduction and make it the default action
631 if (nreds
>= 1 && !nodefault
)
633 if (consistent
[state
])
634 default_rule
= redp
->rules
[0];
638 for (i
= m
; i
< n
; i
++)
643 for (j
= 0; j
< ntokens
; j
++)
645 if (actrow
[j
] == rule
)
656 /* actions which match the default are replaced with zero,
657 which means "use the default" */
661 for (j
= 0; j
< ntokens
; j
++)
663 if (actrow
[j
] == default_rule
)
667 default_rule
= -default_rule
;
672 /* If have no default rule, the default is an error.
673 So replace any action which says "error" with "use default". */
675 if (default_rule
== 0)
676 for (j
= 0; j
< ntokens
; j
++)
678 if (actrow
[j
] == MINSHORT
)
696 for (i
= 0; i
< ntokens
; i
++)
705 froms
[state
] = sp1
= sp
= NEW2 (count
, short);
706 tos
[state
] = sp2
= NEW2 (count
, short);
708 for (i
= 0; i
< ntokens
; i
++)
717 tally
[state
] = count
;
718 width
[state
] = sp1
[-1] - sp
[0] + 1;
722 /*------------------------------------------------------------------.
723 | Figure out the actions for the specified state, indexed by |
724 | lookahead token type. |
726 | The YYDEFACT table is output now. The detailed info is saved for |
727 | putting into YYTABLE later. |
728 `------------------------------------------------------------------*/
734 short *yydefact
= NEW2 (nstates
, short);
736 actrow
= NEW2 (ntokens
, short);
737 for (i
= 0; i
< nstates
; ++i
)
739 yydefact
[i
] = action_row (i
);
744 output_short_table (ftable
, "yydefact", yydefact
,
745 yydefact
[0], 1, nstates
);
753 shifts
*sp
, *sptmp
; /* JF derefrenced freed ptr */
757 for (sp
= first_shift
; sp
; sp
= sptmp
)
766 free_reductions (void)
768 reductions
*rp
, *rptmp
; /* JF fixed freed ptr */
770 FREE (reduction_table
);
772 for (rp
= first_reduction
; rp
; rp
= rptmp
)
782 save_column (int symbol
, int default_state
)
793 m
= goto_map
[symbol
];
794 n
= goto_map
[symbol
+ 1];
797 for (i
= m
; i
< n
; i
++)
799 if (to_state
[i
] != default_state
)
806 symno
= symbol
- ntokens
+ nstates
;
808 froms
[symno
] = sp1
= sp
= NEW2 (count
, short);
809 tos
[symno
] = sp2
= NEW2 (count
, short);
811 for (i
= m
; i
< n
; i
++)
813 if (to_state
[i
] != default_state
)
815 *sp1
++ = from_state
[i
];
816 *sp2
++ = to_state
[i
];
820 tally
[symno
] = count
;
821 width
[symno
] = sp1
[-1] - sp
[0] + 1;
825 default_goto (int symbol
)
833 m
= goto_map
[symbol
];
834 n
= goto_map
[symbol
+ 1];
839 for (i
= 0; i
< nstates
; i
++)
842 for (i
= m
; i
< n
; i
++)
843 state_count
[to_state
[i
]]++;
848 for (i
= 0; i
< nstates
; i
++)
850 if (state_count
[i
] > max
)
852 max
= state_count
[i
];
857 return default_state
;
861 /*-------------------------------------------------------------------.
862 | Figure out what to do after reducing with each rule, depending on |
863 | the saved state from before the beginning of parsing the data that |
864 | matched this rule. |
866 | The YYDEFGOTO table is output now. The detailed info is saved for |
867 | putting into YYTABLE later. |
868 `-------------------------------------------------------------------*/
875 state_count
= NEW2 (nstates
, short);
877 k
= default_goto (ntokens
);
878 fprintf (ftable
, "\nstatic const short yydefgoto[] = {%6d", k
);
879 save_column (ntokens
, k
);
882 for (i
= ntokens
+ 1; i
< nsyms
; i
++)
896 k
= default_goto (i
);
897 fprintf (ftable
, "%6d", k
);
901 fprintf (ftable
, "\n};\n");
906 /* The next few functions decide how to pack the actions and gotos
907 information into yytable. */
918 order
= NEW2 (nvectors
, short);
921 for (i
= 0; i
< nvectors
; i
++)
929 while (j
>= 0 && (width
[order
[j
]] < w
))
932 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
935 for (k
= nentries
- 1; k
> j
; k
--)
936 order
[k
+ 1] = order
[k
];
946 matching_state (int vector
)
963 for (prev
= vector
- 1; prev
>= 0; prev
--)
966 if (width
[j
] != w
|| tally
[j
] != t
)
970 for (k
= 0; match
&& k
< t
; k
++)
972 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
985 pack_vector (int vector
)
1000 berror ("pack_vector");
1005 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
1009 for (k
= 0; ok
&& k
< t
; k
++)
1013 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
1015 if (table
[loc
] != 0)
1019 for (k
= 0; ok
&& k
< vector
; k
++)
1027 for (k
= 0; k
< t
; k
++)
1031 check
[loc
] = from
[k
];
1034 while (table
[lowzero
] != 0)
1044 berror ("pack_vector");
1045 return 0; /* JF keep lint happy */
1056 base
= NEW2 (nvectors
, short);
1057 pos
= NEW2 (nentries
, short);
1058 table
= NEW2 (MAXTABLE
, short);
1059 check
= NEW2 (MAXTABLE
, short);
1064 for (i
= 0; i
< nvectors
; i
++)
1067 for (i
= 0; i
< MAXTABLE
; i
++)
1070 for (i
= 0; i
< nentries
; i
++)
1072 state
= matching_state (i
);
1075 place
= pack_vector (i
);
1077 place
= base
[state
];
1080 base
[order
[i
]] = place
;
1083 for (i
= 0; i
< nvectors
; i
++)
1096 /* the following functions output yytable, yycheck
1097 and the vectors whose elements index the portion starts */
1102 output_short_table (ftable
, "yypact", base
,
1103 base
[0], 1, nstates
);
1105 putc ('\n', ftable
);
1107 output_short_table (ftable
, "yypgoto", base
,
1108 base
[nstates
], nstates
+ 1, nvectors
);
1117 fprintf (ftable
, "\n\n#define\tYYLAST\t\t%d\n\n\n", high
);
1118 output_short_table (ftable
, "yytable", table
,
1119 table
[0], 1, high
+ 1);
1127 output_short_table (ftable
, "yycheck", check
,
1128 check
[0], 1, high
+ 1);
1132 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
1136 output_actions (void)
1138 nvectors
= nstates
+ nvars
;
1140 froms
= NEW2 (nvectors
, short *);
1141 tos
= NEW2 (nvectors
, short *);
1142 tally
= NEW2 (nvectors
, short);
1143 width
= NEW2 (nvectors
, short);
1151 FREE (accessing_symbol
);
1154 FREE (goto_map
+ ntokens
);
1160 putc ('\n', ftable
);
1163 putc ('\n', ftable
);
1167 /* copy the parser code into the ftable file at the end. */
1170 output_parser (void)
1176 #define fpars fparser
1180 fprintf (ftable
, "#define YYPURE 1\n\n");
1182 #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1183 currently open parser from bison.simple to bison.hairy */
1184 if (semantic_parser
)
1190 /* Loop over lines in the standard parser file. */
1198 /* See if the line starts with `#line.
1199 If so, set write_line to 0. */
1216 fprintf (ftable
, "#lin");
1219 fprintf (ftable
, "#li");
1222 fprintf (ftable
, "#l");
1225 fprintf (ftable
, "#");
1228 /* now write out the line... */
1229 for (; c
!= '\n' && c
!= EOF
; c
= getc (fpars
))
1234 /* `$' in the parser file indicates where to put the actions.
1235 Copy them in at this point. */
1237 for (c
= getc (faction
); c
!= EOF
; c
= getc (faction
))
1250 output_program (void)
1255 fprintf (ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1267 free_itemsets (void)
1273 for (cp
= first_state
; cp
; cp
= cptmp
)
1281 /*----------------------------------------------------------.
1282 | Output the parsing tables and the parser code to ftable. |
1283 `----------------------------------------------------------*/
1290 /* output_token_defines(ftable); / * JF put out token defines FIRST */
1291 if (!semantic_parser
) /* JF Put out other stuff */
1294 while ((c
= getc (fattrs
)) != EOF
)
1297 reader_output_yylsp (ftable
);
1301 #define YYDEBUG 1\n\
1306 if (semantic_parser
)
1307 fprintf (ftable
, "#include \"%s\"\n", attrsfile
);
1310 fprintf (ftable
, "#include <stdio.h>\n\n");
1312 /* Make "const" do nothing if not in ANSI C. */
1314 #ifndef __cplusplus\n\
1315 # ifndef __STDC__\n\
1324 output_token_translations ();
1325 /* if (semantic_parser) */
1326 /* This is now unconditional because debugging printouts can use it. */
1329 if (semantic_parser
)
1331 output_rule_data ();