]>
git.saurik.com Git - bison.git/blob - src/output.c
1 /* Output the generated parsing program for bison,
2 Copyright 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 ??
100 #include "complain.h"
104 #include "conflicts.h"
106 extern void berror
PARAMS((const char *));
112 static short **froms
;
116 static short *actrow
;
117 static short *state_count
;
129 output_short_or_char_table (FILE *out
,
132 const char *table_name
,
135 short begin
, short end
)
140 fprintf (out
, "/* %s. */\n", comment
);
142 fprintf (out
, "static const %s %s[] =\n{\n %6d",
143 type
, table_name
, first_value
);
146 for (i
= begin
; i
< end
; i
++)
160 fprintf (out
, "%6d", short_table
[i
]);
163 fprintf (out
, "\n};\n");
168 output_short_table (FILE *out
,
170 const char *table_name
,
173 short begin
, short end
)
175 output_short_or_char_table (out
, comment
, "short", table_name
, short_table
,
176 first_value
, begin
, end
);
180 /*--------------------------------------------------------------.
181 | output_headers -- Output constant strings to the beginning of |
183 `--------------------------------------------------------------*/
188 extern int yyerror;\n\
189 extern int yycost;\n\
190 extern char * yymsg;\n\
191 extern YYSTYPE yyval;\n\
193 yyguard(n, yyvsp, yylsp)\n\
195 register YYSTYPE *yyvsp;\n\
196 register YYLTYPE *yylsp;\n\
207 extern YYSTYPE yyval;\n\
208 extern int yychar;\n\
210 yyaction(n, yyvsp, yylsp)\n\
212 register YYSTYPE *yyvsp;\n\
213 register YYLTYPE *yylsp;\n\
218 #define ACTSTR_PROLOGUE \
222 #define ACTSTR_EPILOGUE \
224 extern YYSTYPE yyval;\n\
225 extern int yychar;\n\
227 yyaction(n, yyvsp, yylsp)\n\
229 register YYSTYPE *yyvsp;\n\
230 register YYLTYPE *yylsp;\n\
235 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
238 output_headers (void)
241 fprintf (fguard
, GUARDSTR
, attrsfile
);
248 obstack_grow_literal_string (&action_obstack
,
250 obstack_grow (&action_obstack
,
251 attrsfile
, strlen (attrsfile
));
252 obstack_grow_literal_string (&action_obstack
,
257 obstack_grow_literal_string (&action_obstack
, ACTSTR_SIMPLE
);
260 /* if (semantic_parser) JF moved this below
261 fprintf(ftable, "#include \"%s\"\n", attrsfile);
262 fprintf(ftable, "#include <stdio.h>\n\n");
265 /* Rename certain symbols if -p was specified. */
266 if (spec_name_prefix
)
268 fprintf (ftable
, "#define yyparse %sparse\n", spec_name_prefix
);
269 fprintf (ftable
, "#define yylex %slex\n", spec_name_prefix
);
270 fprintf (ftable
, "#define yyerror %serror\n", spec_name_prefix
);
271 fprintf (ftable
, "#define yylval %slval\n", spec_name_prefix
);
272 fprintf (ftable
, "#define yychar %schar\n", spec_name_prefix
);
273 fprintf (ftable
, "#define yydebug %sdebug\n", spec_name_prefix
);
274 fprintf (ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
);
279 /*-------------------------------------------------------.
280 | Output constant strings to the ends of certain files. |
281 `-------------------------------------------------------*/
284 output_trailers (void)
287 fprintf (fguard
, "\n }\n}\n");
289 obstack_1grow (&action_obstack
, '\n');
296 obstack_grow_literal_string (&action_obstack
,
299 obstack_grow_literal_string (&action_obstack
,
306 output_token_translations (void)
310 /* YYRANSLATE(YYLEX) -- Bison token number corresponding to YYLEX. */\n",
315 "#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\
318 max_user_token_number
, nsyms
);
320 output_short_or_char_table (ftable
,
321 "YYRANSLATE[YYLEX] -- Bison token number corresponding to YYLEX",
322 ntokens
< 127 ? "char" : "short",
323 "yytranslate", token_translations
,
324 0, 1, max_user_token_number
+ 1);
328 fprintf (ftable
, "\n#define YYTRANSLATE(x) (x)\n");
336 /* With the ordinary parser,
337 yyprhs and yyrhs are needed only for yydebug. */
338 /* With the no_parser option, all tables are generated */
339 if (!semantic_parser
&& !no_parser_flag
)
340 fprintf (ftable
, "\n#if YYDEBUG != 0\n");
342 output_short_table (ftable
, NULL
, "yyprhs", rrhs
,
346 size_t yyrhs_size
= 1;
350 for (sp
= ritem
+ 1; *sp
; sp
++)
352 yyrhs
= XMALLOC (short, yyrhs_size
);
354 for (sp
= ritem
+ 1, i
= 1; *sp
; ++sp
, ++i
)
355 yyrhs
[i
] = *sp
> 0 ? *sp
: 0;
357 output_short_table (ftable
, NULL
, "yyrhs", yyrhs
,
358 ritem
[0], 1, yyrhs_size
);
362 if (!semantic_parser
&& !no_parser_flag
)
363 fprintf (ftable
, "\n#endif\n");
370 output_short_table (ftable
, NULL
, "yystos", accessing_symbol
,
376 output_rule_data (void)
380 short *short_tab
= NULL
;
386 output_short_table (ftable
,
387 "YYRLINE[YYN] -- source line where rule number YYN was defined",
391 fputs ("#endif\n\n", ftable
);
393 if (token_table_flag
|| no_parser_flag
)
395 fprintf (ftable
, "#define YYNTOKENS %d\n", ntokens
);
396 fprintf (ftable
, "#define YYNNTS %d\n", nvars
);
397 fprintf (ftable
, "#define YYNRULES %d\n", nrules
);
398 fprintf (ftable
, "#define YYNSTATES %d\n", nstates
);
399 fprintf (ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
402 /* Output the table of symbol names. */
403 if (!token_table_flag
&& !no_parser_flag
)
404 fputs ("\n#if YYDEBUG != 0 || defined YYERROR_VERBOSE\n\n", ftable
);
406 /* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */\n",
409 "static const char *const yytname[] =\n{\n ");
412 for (i
= 0; i
< nsyms
; i
++)
413 /* this used to be i<=nsyms, but that output a final "" symbol
414 almost by accident */
416 /* Width of the next token, including the two quotes, the coma
421 for (p
= tags
[i
]; p
&& *p
; p
++)
422 if (*p
== '"' || *p
== '\\' || *p
== '\n' || *p
== '\t'
425 else if (*p
< 040 || *p
>= 0177)
430 if (j
+ strsize
> 75)
432 fputs ("\n ", ftable
);
437 for (p
= tags
[i
]; p
&& *p
; p
++)
439 if (*p
== '"' || *p
== '\\')
441 fprintf (ftable
, "\\%c", *p
);
445 fprintf (ftable
, "\\n");
449 fprintf (ftable
, "\\t");
453 fprintf (ftable
, "\\b");
455 else if (*p
< 040 || *p
>= 0177)
457 fprintf (ftable
, "\\%03o", *p
);
465 fputs ("\", ", ftable
);
468 /* add a NULL entry to list of tokens */
469 fprintf (ftable
, "NULL\n};\n");
471 if (!token_table_flag
&& !no_parser_flag
)
472 fprintf (ftable
, "#endif\n\n");
474 /* Output YYTOKNUM. */
475 if (token_table_flag
)
477 output_short_table (ftable
,
478 "YYTOKNUM[YYLEX] -- Index in YYTNAME corresponding to YYLEX",
479 "yytoknum", user_toknums
,
484 output_short_table (ftable
,
485 "YYR1[YYN] -- Symbol number of symbol that rule YYN derives",
493 short_tab
= XMALLOC (short, nrules
+ 1);
494 for (i
= 1; i
< nrules
; i
++)
495 short_tab
[i
] = rrhs
[i
+ 1] - rrhs
[i
] - 1;
496 short_tab
[nrules
] = nitems
- rrhs
[nrules
] - 1;
497 output_short_table (ftable
,
498 "YYR2[YYN] -- Number of symbols composing right hand side of rule YYN",
510 output_defines (void)
512 fprintf (ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
513 fprintf (ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
514 fprintf (ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
518 /*------------------------------------------------------------------.
519 | Decide what to do for each type of token if seen as the lookahead |
520 | token in specified state. The value returned is used as the |
521 | default action (yydefact) for the state. In addition, actrow is |
522 | filled with what to do for each kind of token, index by symbol |
523 | number, with zero meaning do the default action. The value |
524 | MINSHORT, a very negative number, means this situation is an |
525 | error. The parser recognizes this value specially. |
527 | This is where conflicts are resolved. The loop over lookahead |
528 | rules considered lower-numbered rules last, and the last rule |
529 | considered that likes a token gets to handle it. |
530 `------------------------------------------------------------------*/
533 action_row (int state
)
552 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
554 for (i
= 0; i
< ntokens
; i
++)
559 redp
= reduction_table
[state
];
567 /* loop over all the rules available here which require
569 m
= lookaheads
[state
];
570 n
= lookaheads
[state
+ 1];
572 for (i
= n
- 1; i
>= m
; i
--)
575 wordp
= LA
+ i
* tokensetsize
;
578 /* and find each token which the rule finds acceptable
580 for (j
= 0; j
< ntokens
; j
++)
582 /* and record this rule as the rule to use if that
598 shiftp
= shift_table
[state
];
600 /* Now see which tokens are allowed for shifts in this state. For
601 them, record the shift as the thing to do. So shift is preferred
608 for (i
= 0; i
< k
; i
++)
610 shift_state
= shiftp
->shifts
[i
];
614 symbol
= accessing_symbol
[shift_state
];
619 actrow
[symbol
] = shift_state
;
621 /* Do not use any default reduction if there is a shift for
623 if (symbol
== error_token_number
)
628 errp
= err_table
[state
];
630 /* See which tokens are an explicit error in this state (due to
631 %nonassoc). For them, record MINSHORT as the action. */
637 for (i
= 0; i
< k
; i
++)
639 symbol
= errp
->errs
[i
];
640 actrow
[symbol
] = MINSHORT
;
644 /* Now find the most common reduction and make it the default action
647 if (nreds
>= 1 && !nodefault
)
649 if (consistent
[state
])
650 default_rule
= redp
->rules
[0];
654 for (i
= m
; i
< n
; i
++)
659 for (j
= 0; j
< ntokens
; j
++)
661 if (actrow
[j
] == rule
)
672 /* actions which match the default are replaced with zero,
673 which means "use the default" */
677 for (j
= 0; j
< ntokens
; j
++)
679 if (actrow
[j
] == default_rule
)
683 default_rule
= -default_rule
;
688 /* If have no default rule, the default is an error.
689 So replace any action which says "error" with "use default". */
691 if (default_rule
== 0)
692 for (j
= 0; j
< ntokens
; j
++)
694 if (actrow
[j
] == MINSHORT
)
712 for (i
= 0; i
< ntokens
; i
++)
721 froms
[state
] = sp1
= sp
= XCALLOC (short, count
);
722 tos
[state
] = sp2
= XCALLOC (short, count
);
724 for (i
= 0; i
< ntokens
; i
++)
733 tally
[state
] = count
;
734 width
[state
] = sp1
[-1] - sp
[0] + 1;
738 /*------------------------------------------------------------------.
739 | Figure out the actions for the specified state, indexed by |
740 | lookahead token type. |
742 | The YYDEFACT table is output now. The detailed info is saved for |
743 | putting into YYTABLE later. |
744 `------------------------------------------------------------------*/
750 short *yydefact
= XCALLOC (short, nstates
);
752 actrow
= XCALLOC (short, ntokens
);
753 for (i
= 0; i
< nstates
; ++i
)
755 yydefact
[i
] = action_row (i
);
760 output_short_table (ftable
,
761 "YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE\n\
762 doesn't specify something else to do. Zero means the default is an\n\
764 "yydefact", yydefact
,
765 yydefact
[0], 1, nstates
);
774 shifts
*sp
, *sptmp
; /* JF derefrenced freed ptr */
778 for (sp
= first_shift
; sp
; sp
= sptmp
)
787 free_reductions (void)
789 reductions
*rp
, *rptmp
; /* JF fixed freed ptr */
791 XFREE (reduction_table
);
793 for (rp
= first_reduction
; rp
; rp
= rptmp
)
803 save_column (int symbol
, int default_state
)
812 short begin
= goto_map
[symbol
];
813 short end
= goto_map
[symbol
+ 1];
816 for (i
= begin
; i
< end
; i
++)
818 if (to_state
[i
] != default_state
)
825 symno
= symbol
- ntokens
+ nstates
;
827 froms
[symno
] = sp1
= sp
= XCALLOC (short, count
);
828 tos
[symno
] = sp2
= XCALLOC (short, count
);
830 for (i
= begin
; i
< end
; i
++)
832 if (to_state
[i
] != default_state
)
834 *sp1
++ = from_state
[i
];
835 *sp2
++ = to_state
[i
];
839 tally
[symno
] = count
;
840 width
[symno
] = sp1
[-1] - sp
[0] + 1;
844 default_goto (int symbol
)
852 m
= goto_map
[symbol
];
853 n
= goto_map
[symbol
+ 1];
858 for (i
= 0; i
< nstates
; i
++)
861 for (i
= m
; i
< n
; i
++)
862 state_count
[to_state
[i
]]++;
867 for (i
= 0; i
< nstates
; i
++)
869 if (state_count
[i
] > max
)
871 max
= state_count
[i
];
876 return default_state
;
880 /*-------------------------------------------------------------------.
881 | Figure out what to do after reducing with each rule, depending on |
882 | the saved state from before the beginning of parsing the data that |
883 | matched this rule. |
885 | The YYDEFGOTO table is output now. The detailed info is saved for |
886 | putting into YYTABLE later. |
887 `-------------------------------------------------------------------*/
894 short *yydefgoto
= XMALLOC (short, nsyms
- ntokens
);
895 state_count
= XCALLOC (short, nstates
);
897 for (i
= ntokens
; i
< nsyms
; ++i
)
899 int default_state
= default_goto (i
);
900 save_column (i
, default_state
);
901 yydefgoto
[i
- ntokens
] = default_state
;
904 output_short_table (ftable
, NULL
, "yydefgoto", yydefgoto
,
905 yydefgoto
[0], 1, nsyms
- ntokens
);
912 /* The next few functions decide how to pack the actions and gotos
913 information into yytable. */
924 order
= XCALLOC (short, nvectors
);
927 for (i
= 0; i
< nvectors
; i
++)
935 while (j
>= 0 && (width
[order
[j
]] < w
))
938 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
941 for (k
= nentries
- 1; k
> j
; k
--)
942 order
[k
+ 1] = order
[k
];
952 matching_state (int vector
)
969 for (prev
= vector
- 1; prev
>= 0; prev
--)
972 if (width
[j
] != w
|| tally
[j
] != t
)
976 for (k
= 0; match
&& k
< t
; k
++)
978 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
991 pack_vector (int vector
)
1010 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
1014 for (k
= 0; ok
&& k
< t
; k
++)
1018 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
1020 if (table
[loc
] != 0)
1024 for (k
= 0; ok
&& k
< vector
; k
++)
1032 for (k
= 0; k
< t
; k
++)
1036 check
[loc
] = from
[k
];
1039 while (table
[lowzero
] != 0)
1049 berror ("pack_vector");
1050 return 0; /* JF keep lint happy */
1061 base
= XCALLOC (short, nvectors
);
1062 pos
= XCALLOC (short, nentries
);
1063 table
= XCALLOC (short, MAXTABLE
);
1064 check
= XCALLOC (short, MAXTABLE
);
1069 for (i
= 0; i
< nvectors
; i
++)
1072 for (i
= 0; i
< MAXTABLE
; i
++)
1075 for (i
= 0; i
< nentries
; i
++)
1077 state
= matching_state (i
);
1080 place
= pack_vector (i
);
1082 place
= base
[state
];
1085 base
[order
[i
]] = place
;
1088 for (i
= 0; i
< nvectors
; i
++)
1101 /* the following functions output yytable, yycheck
1102 and the vectors whose elements index the portion starts */
1107 output_short_table (ftable
, NULL
, "yypact", base
,
1108 base
[0], 1, nstates
);
1110 putc ('\n', ftable
);
1112 output_short_table (ftable
, NULL
, "yypgoto", base
,
1113 base
[nstates
], nstates
+ 1, nvectors
);
1122 fprintf (ftable
, "\n\n#define\tYYLAST\t\t%d\n\n\n", high
);
1123 output_short_table (ftable
, NULL
, "yytable", table
,
1124 table
[0], 1, high
+ 1);
1132 output_short_table (ftable
, NULL
, "yycheck", check
,
1133 check
[0], 1, high
+ 1);
1137 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
1141 output_actions (void)
1143 nvectors
= nstates
+ nvars
;
1145 froms
= XCALLOC (short *, nvectors
);
1146 tos
= XCALLOC (short *, nvectors
);
1147 tally
= XCALLOC (short, nvectors
);
1148 width
= XCALLOC (short, nvectors
);
1156 XFREE (accessing_symbol
);
1159 XFREE (goto_map
+ ntokens
);
1165 putc ('\n', ftable
);
1168 putc ('\n', ftable
);
1172 /* copy the parser code into the ftable file at the end. */
1175 output_parser (void)
1178 static int number_of_dollar_signs
= 0;
1182 #define fpars fparser
1186 fprintf (ftable
, "#define YYPURE 1\n\n");
1189 /* JF no longer needed 'cuz open_extra_files changes the currently
1190 open parser from bison.simple to bison.hairy */
1191 if (semantic_parser
)
1197 /* Loop over lines in the standard parser file. */
1205 /* See if the line starts with `#line.
1206 If so, set write_line to 0. */
1223 fprintf (ftable
, "#lin");
1226 fprintf (ftable
, "#li");
1229 fprintf (ftable
, "#l");
1232 fprintf (ftable
, "#");
1235 /* now write out the line... */
1236 for (; c
!= '\n' && c
!= EOF
; c
= getc (fpars
))
1239 /* `$' in the parser file indicates where to put the
1240 actions. Copy them in at this point. */
1243 size_t size
= obstack_object_size (&action_obstack
);
1245 number_of_dollar_signs
++;
1246 assert (number_of_dollar_signs
== 1);
1247 fwrite (obstack_finish (&action_obstack
),
1250 /* Skip the end of the line containing `$'. */
1260 assert (number_of_dollar_signs
== 1);
1264 output_program (void)
1269 fprintf (ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1281 free_itemsets (void)
1285 XFREE (state_table
);
1287 for (cp
= first_state
; cp
; cp
= cptmp
)
1295 /*----------------------------------------------------------.
1296 | Output the parsing tables and the parser code to ftable. |
1297 `----------------------------------------------------------*/
1304 /* output_token_defines(ftable); / * JF put out token defines FIRST */
1306 /* If using a simple parser the definition of YYSTYPE are put into
1308 if (!semantic_parser
)
1310 size_t size
= obstack_object_size (&attrs_obstack
);
1311 fwrite (obstack_finish (&attrs_obstack
), 1, size
, ftable
);
1313 reader_output_yylsp (ftable
);
1317 # define YYDEBUG 1\n\
1322 if (semantic_parser
)
1323 fprintf (ftable
, "#include \"%s\"\n", attrsfile
);
1325 if (!no_parser_flag
)
1326 fprintf (ftable
, "#include <stdio.h>\n\n");
1328 /* Make "const" do nothing if not in ANSI C. */
1330 #ifndef __cplusplus\n\
1331 # ifndef __STDC__\n\
1340 output_token_translations ();
1341 /* if (semantic_parser) */
1342 /* This is now unconditional because debugging printouts can use it. */
1345 if (semantic_parser
)
1347 output_rule_data ();
1349 if (!no_parser_flag
)