]>
git.saurik.com Git - bison.git/blob - src/output.c
54ec5c19a89806cbcd4ffdd31bc74008108629cb
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
,
130 const char *table_name
,
133 short begin
, short end
)
138 fprintf (out
, "/* %s. */\n", comment
);
140 fprintf (out
, "static const short %s[] =\n{\n %6d",
141 table_name
, first_value
);
144 for (i
= begin
; i
< end
; i
++)
158 fprintf (out
, "%6d", short_table
[i
]);
161 fprintf (out
, "\n};\n");
165 /*--------------------------------------------------------------.
166 | output_headers -- Output constant strings to the beginning of |
168 `--------------------------------------------------------------*/
173 extern int yyerror;\n\
174 extern int yycost;\n\
175 extern char * yymsg;\n\
176 extern YYSTYPE yyval;\n\
178 yyguard(n, yyvsp, yylsp)\n\
180 register YYSTYPE *yyvsp;\n\
181 register YYLTYPE *yylsp;\n\
192 extern YYSTYPE yyval;\n\
193 extern int yychar;\n\
195 yyaction(n, yyvsp, yylsp)\n\
197 register YYSTYPE *yyvsp;\n\
198 register YYLTYPE *yylsp;\n\
203 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
206 output_headers (void)
209 fprintf (fguard
, GUARDSTR
, attrsfile
);
214 fprintf (faction
, (semantic_parser
? ACTSTR
: ACTSTR_SIMPLE
), attrsfile
);
215 /* if (semantic_parser) JF moved this below
216 fprintf(ftable, "#include \"%s\"\n", attrsfile);
217 fprintf(ftable, "#include <stdio.h>\n\n");
220 /* Rename certain symbols if -p was specified. */
221 if (spec_name_prefix
)
223 fprintf (ftable
, "#define yyparse %sparse\n", spec_name_prefix
);
224 fprintf (ftable
, "#define yylex %slex\n", spec_name_prefix
);
225 fprintf (ftable
, "#define yyerror %serror\n", spec_name_prefix
);
226 fprintf (ftable
, "#define yylval %slval\n", spec_name_prefix
);
227 fprintf (ftable
, "#define yychar %schar\n", spec_name_prefix
);
228 fprintf (ftable
, "#define yydebug %sdebug\n", spec_name_prefix
);
229 fprintf (ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
);
234 /*-------------------------------------------------------.
235 | Output constant strings to the ends of certain files. |
236 `-------------------------------------------------------*/
239 output_trailers (void)
242 fprintf (fguard
, "\n }\n}\n");
244 fprintf (faction
, "\n");
250 fprintf (faction
, " }\n");
251 fprintf (faction
, "}\n");
257 output_token_translations (void)
260 /* short *sp; JF unused */
265 "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
266 max_user_token_number
, nsyms
);
268 if (ntokens
< 127) /* play it very safe; check maximum element value. */
269 fprintf (ftable
, "\nstatic const char yytranslate[] = { 0");
271 fprintf (ftable
, "\nstatic const short yytranslate[] = { 0");
274 for (i
= 1; i
<= max_user_token_number
; i
++)
288 fprintf (ftable
, "%6d", token_translations
[i
]);
291 fprintf (ftable
, "\n};\n");
295 fprintf (ftable
, "\n#define YYTRANSLATE(x) (x)\n");
303 /* With the ordinary parser,
304 yyprhs and yyrhs are needed only for yydebug. */
305 /* With the no_parser option, all tables are generated */
306 if (!semantic_parser
&& !no_parser_flag
)
307 fprintf (ftable
, "\n#if YYDEBUG != 0\n");
309 output_short_table (ftable
, NULL
, "yyprhs", rrhs
,
313 size_t yyrhs_size
= 1;
317 for (sp
= ritem
+ 1; *sp
; sp
++)
319 yyrhs
= XMALLOC (short, yyrhs_size
);
321 for (sp
= ritem
+ 1, i
= 1; *sp
; ++sp
, ++i
)
322 yyrhs
[i
] = *sp
> 0 ? *sp
: 0;
324 output_short_table (ftable
, NULL
, "yyrhs", yyrhs
,
325 ritem
[0], 1, yyrhs_size
);
329 if (!semantic_parser
&& !no_parser_flag
)
330 fprintf (ftable
, "\n#endif\n");
337 output_short_table (ftable
, NULL
, "yystos", accessing_symbol
,
343 output_rule_data (void)
347 short *short_tab
= NULL
;
353 output_short_table (ftable
,
354 "YYRLINE[YYN] -- source line where rule number YYN was defined",
358 fputs ("#endif\n\n", ftable
);
360 if (token_table_flag
|| no_parser_flag
)
362 fprintf (ftable
, "#define YYNTOKENS %d\n", ntokens
);
363 fprintf (ftable
, "#define YYNNTS %d\n", nvars
);
364 fprintf (ftable
, "#define YYNRULES %d\n", nrules
);
365 fprintf (ftable
, "#define YYNSTATES %d\n", nstates
);
366 fprintf (ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
369 /* Output the table of symbol names. */
370 if (!token_table_flag
&& !no_parser_flag
)
371 fputs ("\n#if YYDEBUG != 0 || defined YYERROR_VERBOSE\n\n", ftable
);
373 /* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */\n",
376 "static const char *const yytname[] =\n{\n ");
379 for (i
= 0; i
< nsyms
; i
++)
380 /* this used to be i<=nsyms, but that output a final "" symbol
381 almost by accident */
383 /* Width of the next token, including the two quotes, the coma
388 for (p
= tags
[i
]; p
&& *p
; p
++)
389 if (*p
== '"' || *p
== '\\' || *p
== '\n' || *p
== '\t'
392 else if (*p
< 040 || *p
>= 0177)
397 if (j
+ strsize
> 75)
399 fputs ("\n ", ftable
);
404 for (p
= tags
[i
]; p
&& *p
; p
++)
406 if (*p
== '"' || *p
== '\\')
408 fprintf (ftable
, "\\%c", *p
);
412 fprintf (ftable
, "\\n");
416 fprintf (ftable
, "\\t");
420 fprintf (ftable
, "\\b");
422 else if (*p
< 040 || *p
>= 0177)
424 fprintf (ftable
, "\\%03o", *p
);
432 fputs ("\", ", ftable
);
435 /* add a NULL entry to list of tokens */
436 fprintf (ftable
, "NULL\n};\n");
438 if (!token_table_flag
&& !no_parser_flag
)
439 fprintf (ftable
, "#endif\n\n");
441 /* Output YYTOKNUM. */
442 if (token_table_flag
)
444 output_short_table (ftable
,
445 "YYTOKNUM[YYLEX] -- Index in YYTNAME corresponding to YYLEX",
446 "yytoknum", user_toknums
,
451 output_short_table (ftable
,
452 "YYR1[YYN] -- Symbol number of symbol that rule YYN derives",
460 short_tab
= XMALLOC (short, nrules
+ 1);
461 for (i
= 1; i
< nrules
; i
++)
462 short_tab
[i
] = rrhs
[i
+ 1] - rrhs
[i
] - 1;
463 short_tab
[nrules
] = nitems
- rrhs
[nrules
] - 1;
464 output_short_table (ftable
,
465 "YYR2[YYN] -- Number of symbols composing right hand side of rule YYN",
477 output_defines (void)
479 fprintf (ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
480 fprintf (ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
481 fprintf (ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
485 /*------------------------------------------------------------------.
486 | Decide what to do for each type of token if seen as the lookahead |
487 | token in specified state. The value returned is used as the |
488 | default action (yydefact) for the state. In addition, actrow is |
489 | filled with what to do for each kind of token, index by symbol |
490 | number, with zero meaning do the default action. The value |
491 | MINSHORT, a very negative number, means this situation is an |
492 | error. The parser recognizes this value specially. |
494 | This is where conflicts are resolved. The loop over lookahead |
495 | rules considered lower-numbered rules last, and the last rule |
496 | considered that likes a token gets to handle it. |
497 `------------------------------------------------------------------*/
500 action_row (int state
)
519 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
521 for (i
= 0; i
< ntokens
; i
++)
526 redp
= reduction_table
[state
];
534 /* loop over all the rules available here which require
536 m
= lookaheads
[state
];
537 n
= lookaheads
[state
+ 1];
539 for (i
= n
- 1; i
>= m
; i
--)
542 wordp
= LA
+ i
* tokensetsize
;
545 /* and find each token which the rule finds acceptable
547 for (j
= 0; j
< ntokens
; j
++)
549 /* and record this rule as the rule to use if that
565 shiftp
= shift_table
[state
];
567 /* Now see which tokens are allowed for shifts in this state. For
568 them, record the shift as the thing to do. So shift is preferred
575 for (i
= 0; i
< k
; i
++)
577 shift_state
= shiftp
->shifts
[i
];
581 symbol
= accessing_symbol
[shift_state
];
586 actrow
[symbol
] = shift_state
;
588 /* Do not use any default reduction if there is a shift for
590 if (symbol
== error_token_number
)
595 errp
= err_table
[state
];
597 /* See which tokens are an explicit error in this state (due to
598 %nonassoc). For them, record MINSHORT as the action. */
604 for (i
= 0; i
< k
; i
++)
606 symbol
= errp
->errs
[i
];
607 actrow
[symbol
] = MINSHORT
;
611 /* Now find the most common reduction and make it the default action
614 if (nreds
>= 1 && !nodefault
)
616 if (consistent
[state
])
617 default_rule
= redp
->rules
[0];
621 for (i
= m
; i
< n
; i
++)
626 for (j
= 0; j
< ntokens
; j
++)
628 if (actrow
[j
] == rule
)
639 /* actions which match the default are replaced with zero,
640 which means "use the default" */
644 for (j
= 0; j
< ntokens
; j
++)
646 if (actrow
[j
] == default_rule
)
650 default_rule
= -default_rule
;
655 /* If have no default rule, the default is an error.
656 So replace any action which says "error" with "use default". */
658 if (default_rule
== 0)
659 for (j
= 0; j
< ntokens
; j
++)
661 if (actrow
[j
] == MINSHORT
)
679 for (i
= 0; i
< ntokens
; i
++)
688 froms
[state
] = sp1
= sp
= XCALLOC (short, count
);
689 tos
[state
] = sp2
= XCALLOC (short, count
);
691 for (i
= 0; i
< ntokens
; i
++)
700 tally
[state
] = count
;
701 width
[state
] = sp1
[-1] - sp
[0] + 1;
705 /*------------------------------------------------------------------.
706 | Figure out the actions for the specified state, indexed by |
707 | lookahead token type. |
709 | The YYDEFACT table is output now. The detailed info is saved for |
710 | putting into YYTABLE later. |
711 `------------------------------------------------------------------*/
717 short *yydefact
= XCALLOC (short, nstates
);
719 actrow
= XCALLOC (short, ntokens
);
720 for (i
= 0; i
< nstates
; ++i
)
722 yydefact
[i
] = action_row (i
);
727 output_short_table (ftable
, NULL
, "yydefact", yydefact
,
728 yydefact
[0], 1, nstates
);
736 shifts
*sp
, *sptmp
; /* JF derefrenced freed ptr */
740 for (sp
= first_shift
; sp
; sp
= sptmp
)
749 free_reductions (void)
751 reductions
*rp
, *rptmp
; /* JF fixed freed ptr */
753 XFREE (reduction_table
);
755 for (rp
= first_reduction
; rp
; rp
= rptmp
)
765 save_column (int symbol
, int default_state
)
776 m
= goto_map
[symbol
];
777 n
= goto_map
[symbol
+ 1];
780 for (i
= m
; i
< n
; i
++)
782 if (to_state
[i
] != default_state
)
789 symno
= symbol
- ntokens
+ nstates
;
791 froms
[symno
] = sp1
= sp
= XCALLOC (short, count
);
792 tos
[symno
] = sp2
= XCALLOC (short, count
);
794 for (i
= m
; i
< n
; i
++)
796 if (to_state
[i
] != default_state
)
798 *sp1
++ = from_state
[i
];
799 *sp2
++ = to_state
[i
];
803 tally
[symno
] = count
;
804 width
[symno
] = sp1
[-1] - sp
[0] + 1;
808 default_goto (int symbol
)
816 m
= goto_map
[symbol
];
817 n
= goto_map
[symbol
+ 1];
822 for (i
= 0; i
< nstates
; i
++)
825 for (i
= m
; i
< n
; i
++)
826 state_count
[to_state
[i
]]++;
831 for (i
= 0; i
< nstates
; i
++)
833 if (state_count
[i
] > max
)
835 max
= state_count
[i
];
840 return default_state
;
844 /*-------------------------------------------------------------------.
845 | Figure out what to do after reducing with each rule, depending on |
846 | the saved state from before the beginning of parsing the data that |
847 | matched this rule. |
849 | The YYDEFGOTO table is output now. The detailed info is saved for |
850 | putting into YYTABLE later. |
851 `-------------------------------------------------------------------*/
858 state_count
= XCALLOC (short, nstates
);
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
++)
879 k
= default_goto (i
);
880 fprintf (ftable
, "%6d", k
);
884 fprintf (ftable
, "\n};\n");
889 /* The next few functions decide how to pack the actions and gotos
890 information into yytable. */
901 order
= XCALLOC (short, nvectors
);
904 for (i
= 0; i
< nvectors
; i
++)
912 while (j
>= 0 && (width
[order
[j
]] < w
))
915 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
918 for (k
= nentries
- 1; k
> j
; k
--)
919 order
[k
+ 1] = order
[k
];
929 matching_state (int vector
)
946 for (prev
= vector
- 1; prev
>= 0; prev
--)
949 if (width
[j
] != w
|| tally
[j
] != t
)
953 for (k
= 0; match
&& k
< t
; k
++)
955 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
968 pack_vector (int vector
)
987 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
991 for (k
= 0; ok
&& k
< t
; k
++)
995 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
1001 for (k
= 0; ok
&& k
< vector
; k
++)
1009 for (k
= 0; k
< t
; k
++)
1013 check
[loc
] = from
[k
];
1016 while (table
[lowzero
] != 0)
1026 berror ("pack_vector");
1027 return 0; /* JF keep lint happy */
1038 base
= XCALLOC (short, nvectors
);
1039 pos
= XCALLOC (short, nentries
);
1040 table
= XCALLOC (short, MAXTABLE
);
1041 check
= XCALLOC (short, MAXTABLE
);
1046 for (i
= 0; i
< nvectors
; i
++)
1049 for (i
= 0; i
< MAXTABLE
; i
++)
1052 for (i
= 0; i
< nentries
; i
++)
1054 state
= matching_state (i
);
1057 place
= pack_vector (i
);
1059 place
= base
[state
];
1062 base
[order
[i
]] = place
;
1065 for (i
= 0; i
< nvectors
; i
++)
1078 /* the following functions output yytable, yycheck
1079 and the vectors whose elements index the portion starts */
1084 output_short_table (ftable
, NULL
, "yypact", base
,
1085 base
[0], 1, nstates
);
1087 putc ('\n', ftable
);
1089 output_short_table (ftable
, NULL
, "yypgoto", base
,
1090 base
[nstates
], nstates
+ 1, nvectors
);
1099 fprintf (ftable
, "\n\n#define\tYYLAST\t\t%d\n\n\n", high
);
1100 output_short_table (ftable
, NULL
, "yytable", table
,
1101 table
[0], 1, high
+ 1);
1109 output_short_table (ftable
, NULL
, "yycheck", check
,
1110 check
[0], 1, high
+ 1);
1114 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
1118 output_actions (void)
1120 nvectors
= nstates
+ nvars
;
1122 froms
= XCALLOC (short *, nvectors
);
1123 tos
= XCALLOC (short *, nvectors
);
1124 tally
= XCALLOC (short, nvectors
);
1125 width
= XCALLOC (short, nvectors
);
1133 XFREE (accessing_symbol
);
1136 XFREE (goto_map
+ ntokens
);
1142 putc ('\n', ftable
);
1145 putc ('\n', ftable
);
1149 /* copy the parser code into the ftable file at the end. */
1152 output_parser (void)
1155 static int number_of_dollar_signs
= 0;
1159 #define fpars fparser
1163 fprintf (ftable
, "#define YYPURE 1\n\n");
1166 /* JF no longer needed 'cuz open_extra_files changes the currently
1167 open parser from bison.simple to bison.hairy */
1168 if (semantic_parser
)
1174 /* Loop over lines in the standard parser file. */
1182 /* See if the line starts with `#line.
1183 If so, set write_line to 0. */
1200 fprintf (ftable
, "#lin");
1203 fprintf (ftable
, "#li");
1206 fprintf (ftable
, "#l");
1209 fprintf (ftable
, "#");
1212 /* now write out the line... */
1213 for (; c
!= '\n' && c
!= EOF
; c
= getc (fpars
))
1218 number_of_dollar_signs
++;
1219 assert (number_of_dollar_signs
== 1);
1220 /* `$' in the parser file indicates where to put the actions.
1221 Copy them in at this point. */
1223 for (c
= getc (faction
); c
!= EOF
; c
= getc (faction
))
1225 /* Skip the end of the line containing `$'. */
1235 assert (number_of_dollar_signs
== 1);
1239 output_program (void)
1244 fprintf (ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1256 free_itemsets (void)
1260 XFREE (state_table
);
1262 for (cp
= first_state
; cp
; cp
= cptmp
)
1270 /*----------------------------------------------------------.
1271 | Output the parsing tables and the parser code to ftable. |
1272 `----------------------------------------------------------*/
1279 /* output_token_defines(ftable); / * JF put out token defines FIRST */
1280 if (!semantic_parser
) /* JF Put out other stuff */
1283 while ((c
= getc (fattrs
)) != EOF
)
1286 reader_output_yylsp (ftable
);
1290 # define YYDEBUG 1\n\
1295 if (semantic_parser
)
1296 fprintf (ftable
, "#include \"%s\"\n", attrsfile
);
1298 if (!no_parser_flag
)
1299 fprintf (ftable
, "#include <stdio.h>\n\n");
1301 /* Make "const" do nothing if not in ANSI C. */
1303 #ifndef __cplusplus\n\
1304 # ifndef __STDC__\n\
1313 output_token_translations ();
1314 /* if (semantic_parser) */
1315 /* This is now unconditional because debugging printouts can use it. */
1318 if (semantic_parser
)
1320 output_rule_data ();
1322 if (!no_parser_flag
)