]>
git.saurik.com Git - bison.git/blob - src/output.c
56538f0c1db76c7957292ed8c68cc1651b81b081
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 ??
102 extern void berror
PARAMS((const char *));
105 extern short *user_toknums
;
106 extern int tokensetsize
;
107 extern int final_state
;
108 extern core
**state_table
;
109 extern shifts
**shift_table
;
110 extern errs
**err_table
;
111 extern reductions
**reduction_table
;
112 extern short *accessing_symbol
;
114 extern short *LAruleno
;
115 extern short *lookaheads
;
116 extern char *consistent
;
117 extern short *goto_map
;
118 extern short *from_state
;
119 extern short *to_state
;
121 extern void reader_output_yylsp
PARAMS ((FILE *));
125 static short **froms
;
129 static short *actrow
;
130 static short *state_count
;
142 output_short_table (FILE *out
,
143 const char *table_name
,
146 short begin
, short end
)
150 fprintf (out
, "static const short %s[] = {%6d", table_name
, first_value
);
153 for (i
= begin
; i
< end
; i
++)
167 fprintf (out
, "%6d", short_table
[i
]);
170 fprintf (out
, "\n};\n");
174 /*--------------------------------------------------------------.
175 | output_headers -- Output constant strings to the beginning of |
177 `--------------------------------------------------------------*/
182 extern int yyerror;\n\
183 extern int yycost;\n\
184 extern char * yymsg;\n\
185 extern YYSTYPE yyval;\n\
187 yyguard(n, yyvsp, yylsp)\n\
189 register YYSTYPE *yyvsp;\n\
190 register YYLTYPE *yylsp;\n\
201 extern YYSTYPE yyval;\n\
202 extern int yychar;\n\
204 yyaction(n, yyvsp, yylsp)\n\
206 register YYSTYPE *yyvsp;\n\
207 register YYLTYPE *yylsp;\n\
212 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
215 output_headers (void)
218 fprintf (fguard
, GUARDSTR
, attrsfile
);
223 fprintf (faction
, (semantic_parser
? ACTSTR
: ACTSTR_SIMPLE
), attrsfile
);
224 /* if (semantic_parser) JF moved this below
225 fprintf(ftable, "#include \"%s\"\n", attrsfile);
226 fprintf(ftable, "#include <stdio.h>\n\n");
229 /* Rename certain symbols if -p was specified. */
230 if (spec_name_prefix
)
232 fprintf (ftable
, "#define yyparse %sparse\n", spec_name_prefix
);
233 fprintf (ftable
, "#define yylex %slex\n", spec_name_prefix
);
234 fprintf (ftable
, "#define yyerror %serror\n", spec_name_prefix
);
235 fprintf (ftable
, "#define yylval %slval\n", spec_name_prefix
);
236 fprintf (ftable
, "#define yychar %schar\n", spec_name_prefix
);
237 fprintf (ftable
, "#define yydebug %sdebug\n", spec_name_prefix
);
238 fprintf (ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
);
243 /*-------------------------------------------------------.
244 | Output constant strings to the ends of certain files. |
245 `-------------------------------------------------------*/
248 output_trailers (void)
251 fprintf (fguard
, "\n }\n}\n");
253 fprintf (faction
, "\n");
259 fprintf (faction
, " }\n");
260 fprintf (faction
, "}\n");
266 output_token_translations (void)
269 /* short *sp; JF unused */
274 "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
275 max_user_token_number
, nsyms
);
277 if (ntokens
< 127) /* play it very safe; check maximum element value. */
278 fprintf (ftable
, "\nstatic const char yytranslate[] = { 0");
280 fprintf (ftable
, "\nstatic const short yytranslate[] = { 0");
283 for (i
= 1; i
<= max_user_token_number
; i
++)
297 fprintf (ftable
, "%6d", token_translations
[i
]);
300 fprintf (ftable
, "\n};\n");
304 fprintf (ftable
, "\n#define YYTRANSLATE(x) (x)\n");
315 /* With the ordinary parser,
316 yyprhs and yyrhs are needed only for yydebug. */
317 /* With the noparser option, all tables are generated */
318 if (!semantic_parser
&& !noparserflag
)
319 fprintf (ftable
, "\n#if YYDEBUG != 0\n");
321 output_short_table (ftable
, "yyprhs", rrhs
,
324 fprintf (ftable
, "\nstatic const short yyrhs[] = {%6d", ritem
[0]);
327 for (sp
= ritem
+ 1; *sp
; sp
++)
342 fprintf (ftable
, "%6d", *sp
);
344 fprintf (ftable
, " 0");
347 fprintf (ftable
, "\n};\n");
349 if (!semantic_parser
&& !noparserflag
)
350 fprintf (ftable
, "\n#endif\n");
357 output_short_table (ftable
, "yystos", accessing_symbol
,
363 output_rule_data (void)
370 /* YYRLINE[yyn]: source line where rule number YYN was defined. */\n",
373 output_short_table (ftable
, "yyrline", rline
,
376 fputs ("#endif\n\n", ftable
);
378 if (toknumflag
|| noparserflag
)
380 fprintf (ftable
, "#define YYNTOKENS %d\n", ntokens
);
381 fprintf (ftable
, "#define YYNNTS %d\n", nvars
);
382 fprintf (ftable
, "#define YYNRULES %d\n", nrules
);
383 fprintf (ftable
, "#define YYNSTATES %d\n", nstates
);
384 fprintf (ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
387 if (!toknumflag
&& !noparserflag
)
388 fprintf (ftable
, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
390 /* Output the table of symbol names. */
393 "static const char * const yytname[] = { \"%s\"", tags
[0]);
395 j
= strlen (tags
[0]) + 44;
396 for (i
= 1; i
< nsyms
; i
++)
397 /* this used to be i<=nsyms, but that output a final "" symbol
398 almost by accident */
413 for (p
= tags
[i
]; p
&& *p
; p
++)
415 if (*p
== '"' || *p
== '\\')
417 fprintf (ftable
, "\\%c", *p
);
422 fprintf (ftable
, "\\n");
427 fprintf (ftable
, "\\t");
432 fprintf (ftable
, "\\b");
435 else if (*p
< 040 || *p
>= 0177)
437 fprintf (ftable
, "\\%03o", *p
);
450 /* add a NULL entry to list of tokens */
451 fprintf (ftable
, ", NULL\n};\n");
453 if (!toknumflag
&& !noparserflag
)
454 fprintf (ftable
, "#endif\n\n");
456 /* Output YYTOKNUM. */
459 output_short_table (ftable
, "yytoknum", user_toknums
,
465 /* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n", ftable
);
467 output_short_table (ftable
, "yyr1", rlhs
,
475 /* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\
476 static const short yyr2[] = { 0", ftable
);
478 for (i
= 1; i
< nrules
; i
++)
492 fprintf (ftable
, "%6d", rrhs
[i
+ 1] - rrhs
[i
] - 1);
499 fprintf (ftable
, "%6d\n};\n", nitems
- rrhs
[nrules
] - 1);
505 output_defines (void)
507 fprintf (ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
508 fprintf (ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
509 fprintf (ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
513 /*------------------------------------------------------------------.
514 | Decide what to do for each type of token if seen as the lookahead |
515 | token in specified state. The value returned is used as the |
516 | default action (yydefact) for the state. In addition, actrow is |
517 | filled with what to do for each kind of token, index by symbol |
518 | number, with zero meaning do the default action. The value |
519 | MINSHORT, a very negative number, means this situation is an |
520 | error. The parser recognizes this value specially. |
522 | This is where conflicts are resolved. The loop over lookahead |
523 | rules considered lower-numbered rules last, and the last rule |
524 | considered that likes a token gets to handle it. |
525 `------------------------------------------------------------------*/
528 action_row (int state
)
547 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
549 for (i
= 0; i
< ntokens
; i
++)
554 redp
= reduction_table
[state
];
562 /* loop over all the rules available here which require
564 m
= lookaheads
[state
];
565 n
= lookaheads
[state
+ 1];
567 for (i
= n
- 1; i
>= m
; i
--)
570 wordp
= LA
+ i
* tokensetsize
;
573 /* and find each token which the rule finds acceptable
575 for (j
= 0; j
< ntokens
; j
++)
577 /* and record this rule as the rule to use if that
593 shiftp
= shift_table
[state
];
595 /* Now see which tokens are allowed for shifts in this state. For
596 them, record the shift as the thing to do. So shift is preferred
603 for (i
= 0; i
< k
; i
++)
605 shift_state
= shiftp
->shifts
[i
];
609 symbol
= accessing_symbol
[shift_state
];
614 actrow
[symbol
] = shift_state
;
616 /* Do not use any default reduction if there is a shift for
618 if (symbol
== error_token_number
)
623 errp
= err_table
[state
];
625 /* See which tokens are an explicit error in this state (due to
626 %nonassoc). For them, record MINSHORT as the action. */
632 for (i
= 0; i
< k
; i
++)
634 symbol
= errp
->errs
[i
];
635 actrow
[symbol
] = MINSHORT
;
639 /* Now find the most common reduction and make it the default action
642 if (nreds
>= 1 && !nodefault
)
644 if (consistent
[state
])
645 default_rule
= redp
->rules
[0];
649 for (i
= m
; i
< n
; i
++)
654 for (j
= 0; j
< ntokens
; j
++)
656 if (actrow
[j
] == rule
)
667 /* actions which match the default are replaced with zero,
668 which means "use the default" */
672 for (j
= 0; j
< ntokens
; j
++)
674 if (actrow
[j
] == default_rule
)
678 default_rule
= -default_rule
;
683 /* If have no default rule, the default is an error.
684 So replace any action which says "error" with "use default". */
686 if (default_rule
== 0)
687 for (j
= 0; j
< ntokens
; j
++)
689 if (actrow
[j
] == MINSHORT
)
707 for (i
= 0; i
< ntokens
; i
++)
716 froms
[state
] = sp1
= sp
= NEW2 (count
, short);
717 tos
[state
] = sp2
= NEW2 (count
, short);
719 for (i
= 0; i
< ntokens
; i
++)
728 tally
[state
] = count
;
729 width
[state
] = sp1
[-1] - sp
[0] + 1;
733 /*------------------------------------------------------------------.
734 | Figure out the actions for the specified state, indexed by |
735 | lookahead token type. |
737 | The YYDEFACT table is output now. The detailed info is saved for |
738 | putting into YYTABLE later. |
739 `------------------------------------------------------------------*/
745 short *yydefact
= NEW2 (nstates
, short);
747 actrow
= NEW2 (ntokens
, short);
748 for (i
= 0; i
< nstates
; ++i
)
750 yydefact
[i
] = action_row (i
);
755 output_short_table (ftable
, "yydefact", yydefact
,
756 yydefact
[0], 1, nstates
);
764 shifts
*sp
, *sptmp
; /* JF derefrenced freed ptr */
768 for (sp
= first_shift
; sp
; sp
= sptmp
)
777 free_reductions (void)
779 reductions
*rp
, *rptmp
; /* JF fixed freed ptr */
781 FREE (reduction_table
);
783 for (rp
= first_reduction
; rp
; rp
= rptmp
)
793 save_column (int symbol
, int default_state
)
804 m
= goto_map
[symbol
];
805 n
= goto_map
[symbol
+ 1];
808 for (i
= m
; i
< n
; i
++)
810 if (to_state
[i
] != default_state
)
817 symno
= symbol
- ntokens
+ nstates
;
819 froms
[symno
] = sp1
= sp
= NEW2 (count
, short);
820 tos
[symno
] = sp2
= NEW2 (count
, short);
822 for (i
= m
; i
< n
; i
++)
824 if (to_state
[i
] != default_state
)
826 *sp1
++ = from_state
[i
];
827 *sp2
++ = to_state
[i
];
831 tally
[symno
] = count
;
832 width
[symno
] = sp1
[-1] - sp
[0] + 1;
836 default_goto (int symbol
)
844 m
= goto_map
[symbol
];
845 n
= goto_map
[symbol
+ 1];
850 for (i
= 0; i
< nstates
; i
++)
853 for (i
= m
; i
< n
; i
++)
854 state_count
[to_state
[i
]]++;
859 for (i
= 0; i
< nstates
; i
++)
861 if (state_count
[i
] > max
)
863 max
= state_count
[i
];
868 return default_state
;
872 /*-------------------------------------------------------------------.
873 | Figure out what to do after reducing with each rule, depending on |
874 | the saved state from before the beginning of parsing the data that |
875 | matched this rule. |
877 | The YYDEFGOTO table is output now. The detailed info is saved for |
878 | putting into YYTABLE later. |
879 `-------------------------------------------------------------------*/
886 state_count
= NEW2 (nstates
, short);
888 k
= default_goto (ntokens
);
889 fprintf (ftable
, "\nstatic const short yydefgoto[] = {%6d", k
);
890 save_column (ntokens
, k
);
893 for (i
= ntokens
+ 1; i
< nsyms
; i
++)
907 k
= default_goto (i
);
908 fprintf (ftable
, "%6d", k
);
912 fprintf (ftable
, "\n};\n");
917 /* The next few functions decide how to pack the actions and gotos
918 information into yytable. */
929 order
= NEW2 (nvectors
, short);
932 for (i
= 0; i
< nvectors
; i
++)
940 while (j
>= 0 && (width
[order
[j
]] < w
))
943 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
946 for (k
= nentries
- 1; k
> j
; k
--)
947 order
[k
+ 1] = order
[k
];
957 matching_state (int vector
)
974 for (prev
= vector
- 1; prev
>= 0; prev
--)
977 if (width
[j
] != w
|| tally
[j
] != t
)
981 for (k
= 0; match
&& k
< t
; k
++)
983 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
996 pack_vector (int vector
)
1011 berror ("pack_vector");
1016 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
1020 for (k
= 0; ok
&& k
< t
; k
++)
1024 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
1026 if (table
[loc
] != 0)
1030 for (k
= 0; ok
&& k
< vector
; k
++)
1038 for (k
= 0; k
< t
; k
++)
1042 check
[loc
] = from
[k
];
1045 while (table
[lowzero
] != 0)
1055 berror ("pack_vector");
1056 return 0; /* JF keep lint happy */
1067 base
= NEW2 (nvectors
, short);
1068 pos
= NEW2 (nentries
, short);
1069 table
= NEW2 (MAXTABLE
, short);
1070 check
= NEW2 (MAXTABLE
, short);
1075 for (i
= 0; i
< nvectors
; i
++)
1078 for (i
= 0; i
< MAXTABLE
; i
++)
1081 for (i
= 0; i
< nentries
; i
++)
1083 state
= matching_state (i
);
1086 place
= pack_vector (i
);
1088 place
= base
[state
];
1091 base
[order
[i
]] = place
;
1094 for (i
= 0; i
< nvectors
; i
++)
1107 /* the following functions output yytable, yycheck
1108 and the vectors whose elements index the portion starts */
1113 output_short_table (ftable
, "yypact", base
,
1114 base
[0], 1, nstates
);
1116 putc ('\n', ftable
);
1118 output_short_table (ftable
, "yypgoto", base
,
1119 base
[nstates
], nstates
+ 1, nvectors
);
1128 fprintf (ftable
, "\n\n#define\tYYLAST\t\t%d\n\n\n", high
);
1129 output_short_table (ftable
, "yytable", table
,
1130 table
[0], 1, high
+ 1);
1138 output_short_table (ftable
, "yycheck", check
,
1139 check
[0], 1, high
+ 1);
1143 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
1147 output_actions (void)
1149 nvectors
= nstates
+ nvars
;
1151 froms
= NEW2 (nvectors
, short *);
1152 tos
= NEW2 (nvectors
, short *);
1153 tally
= NEW2 (nvectors
, short);
1154 width
= NEW2 (nvectors
, short);
1162 FREE (accessing_symbol
);
1165 FREE (goto_map
+ ntokens
);
1171 putc ('\n', ftable
);
1174 putc ('\n', ftable
);
1178 /* copy the parser code into the ftable file at the end. */
1181 output_parser (void)
1187 #define fpars fparser
1191 fprintf (ftable
, "#define YYPURE 1\n\n");
1193 #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1194 currently open parser from bison.simple to bison.hairy */
1195 if (semantic_parser
)
1201 /* Loop over lines in the standard parser file. */
1209 /* See if the line starts with `#line.
1210 If so, set write_line to 0. */
1227 fprintf (ftable
, "#lin");
1230 fprintf (ftable
, "#li");
1233 fprintf (ftable
, "#l");
1236 fprintf (ftable
, "#");
1239 /* now write out the line... */
1240 for (; c
!= '\n' && c
!= EOF
; c
= getc (fpars
))
1245 /* `$' in the parser file indicates where to put the actions.
1246 Copy them in at this point. */
1248 for (c
= getc (faction
); c
!= EOF
; c
= getc (faction
))
1261 output_program (void)
1266 fprintf (ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1278 free_itemsets (void)
1284 for (cp
= first_state
; cp
; cp
= cptmp
)
1292 /*----------------------------------------------------------.
1293 | Output the parsing tables and the parser code to ftable. |
1294 `----------------------------------------------------------*/
1301 /* output_token_defines(ftable); / * JF put out token defines FIRST */
1302 if (!semantic_parser
) /* JF Put out other stuff */
1305 while ((c
= getc (fattrs
)) != EOF
)
1308 reader_output_yylsp (ftable
);
1312 #define YYDEBUG 1\n\
1317 if (semantic_parser
)
1318 fprintf (ftable
, "#include \"%s\"\n", attrsfile
);
1321 fprintf (ftable
, "#include <stdio.h>\n\n");
1323 /* Make "const" do nothing if not in ANSI C. */
1325 #ifndef __cplusplus\n\
1326 # ifndef __STDC__\n\
1335 output_token_translations ();
1336 /* if (semantic_parser) */
1337 /* This is now unconditional because debugging printouts can use it. */
1340 if (semantic_parser
)
1342 output_rule_data ();