1 /* Output the generated parsing program for bison,
2 Copyright (C) 1984, 1986, 1989, 1992 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.
37 Double starred are output only if switches are set.
39 yytranslate = vector mapping yylex's token numbers into bison's token numbers.
41 ** yytname = vector of string-names indexed by bison token number
43 ** yytoknum = vector of yylex token numbers corresponding to entries in yytname
45 yyrline = vector of line-numbers of all rules. For yydebug printouts.
47 yyrhs = vector of items of all rules.
48 This is exactly what ritems contains. For yydebug and for semantic
51 yyprhs[r] = index in yyrhs of first item for rule r.
53 yyr1[r] = symbol number of symbol that rule r derives.
55 yyr2[r] = number of symbols composing right hand side of rule r.
57 * yystos[s] = the symbol number of the symbol that leads to state s.
59 yydefact[s] = default rule to reduce with in state s,
60 when yytable doesn't specify something else to do.
61 Zero means the default is an error.
63 yydefgoto[i] = default state to go to after a reduction of a rule that
64 generates variable ntokens + i, except when yytable
65 specifies something else to do.
67 yypact[s] = index in yytable of the portion describing state s.
68 The lookahead token's type is used to index that portion
69 to find out what to do.
71 If the value in yytable is positive,
72 we shift the token and go to that state.
74 If the value is negative, it is minus a rule number to reduce by.
76 If the value is zero, the default action from yydefact[s] is used.
78 yypgoto[i] = the index in yytable of the portion describing
79 what to do after reducing a rule that derives variable i + ntokens.
80 This portion is indexed by the parser state number, s,
81 as of before the text for this nonterminal was read.
82 The value from yytable is the state to go to if
83 the corresponding value in yycheck is s.
85 yytable = a vector filled with portions for different uses,
86 found via yypact and yypgoto.
88 yycheck = a vector indexed in parallel with yytable.
89 It indicates, in a roundabout way, the bounds of the
90 portion you are trying to examine.
92 Suppose that the portion of yytable starts at index p
93 and the index to be examined within the portion is i.
94 Then if yycheck[p+i] != i, i is outside the bounds
95 of what is actually allocated, and the default
96 (from yydefact or yydefgoto) should be used.
97 Otherwise, yytable[p+i] should be used.
99 YYFINAL = the state number of the termination state.
100 YYFLAG = most negative short int. Used to flag ??
114 extern int debugflag
;
115 extern int nolinesflag
;
116 extern int noparserflag
;
117 extern int toknumflag
;
120 extern int *user_toknums
;
121 extern int tokensetsize
;
122 extern int final_state
;
123 extern core
**state_table
;
124 extern shifts
**shift_table
;
125 extern errs
**err_table
;
126 extern reductions
**reduction_table
;
127 extern short *accessing_symbol
;
129 extern short *LAruleno
;
130 extern short *lookaheads
;
131 extern char *consistent
;
132 extern short *goto_map
;
133 extern short *from_state
;
134 extern short *to_state
;
137 void output_headers
PARAMS((void));
138 void output_trailers
PARAMS((void));
139 void output
PARAMS((void));
140 void output_token_translations
PARAMS((void));
141 void output_gram
PARAMS((void));
142 void output_stos
PARAMS((void));
143 void output_rule_data
PARAMS((void));
144 void output_defines
PARAMS((void));
145 void output_actions
PARAMS((void));
146 void token_actions
PARAMS((void));
147 void save_row
PARAMS((int));
148 void goto_actions
PARAMS((void));
149 void save_column
PARAMS((int, int));
150 void sort_actions
PARAMS((void));
151 void pack_table
PARAMS((void));
152 void output_base
PARAMS((void));
153 void output_table
PARAMS((void));
154 void output_check
PARAMS((void));
155 void output_parser
PARAMS((void));
156 void output_program
PARAMS((void));
157 void free_shifts
PARAMS((void));
158 void free_reductions
PARAMS((void));
159 void free_itemsets
PARAMS((void));
160 int action_row
PARAMS((int));
161 int default_goto
PARAMS((int));
162 int matching_state
PARAMS((int));
163 int pack_vector
PARAMS((int));
165 extern void berror
PARAMS((char *));
166 extern void fatals
PARAMS((char *, char *));
167 extern char *int_to_string
PARAMS((int));
168 extern void reader_output_yylsp
PARAMS((FILE *));
172 static short **froms
;
176 static short *actrow
;
177 static short *state_count
;
188 #define GUARDSTR "\n#include \"%s\"\nextern int yyerror;\n\
189 extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
190 yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
191 register YYLTYPE *yylsp;\n\
192 {\n yyerror = 0;\nyycost = 0;\n yymsg = 0;\nswitch (n)\n {"
194 #define ACTSTR "\n#include \"%s\"\nextern YYSTYPE yyval;\
195 \nextern int yychar;\
196 yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
197 register YYLTYPE *yylsp;\n{\n switch (n)\n{"
199 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
203 output_headers (void)
206 fprintf(fguard
, GUARDSTR
, attrsfile
);
211 fprintf(faction
, (semantic_parser
? ACTSTR
: ACTSTR_SIMPLE
), attrsfile
);
212 /* if (semantic_parser) JF moved this below
213 fprintf(ftable, "#include \"%s\"\n", attrsfile);
214 fprintf(ftable, "#include <stdio.h>\n\n");
217 /* Rename certain symbols if -p was specified. */
218 if (spec_name_prefix
)
220 fprintf(ftable
, "#define yyparse %sparse\n", spec_name_prefix
);
221 fprintf(ftable
, "#define yylex %slex\n", spec_name_prefix
);
222 fprintf(ftable
, "#define yyerror %serror\n", spec_name_prefix
);
223 fprintf(ftable
, "#define yylval %slval\n", spec_name_prefix
);
224 fprintf(ftable
, "#define yychar %schar\n", spec_name_prefix
);
225 fprintf(ftable
, "#define yydebug %sdebug\n", spec_name_prefix
);
226 fprintf(ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
);
232 output_trailers (void)
235 fprintf(fguard
, "\n }\n}\n");
237 fprintf(faction
, "\n");
243 fprintf(faction
, " }\n");
244 fprintf(faction
, "}\n");
253 /* output_token_defines(ftable); / * JF put out token defines FIRST */
254 if (!semantic_parser
) /* JF Put out other stuff */
257 while ((c
=getc(fattrs
))!=EOF
)
260 reader_output_yylsp(ftable
);
262 fprintf(ftable
, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n",
266 fprintf(ftable
, "#include \"%s\"\n", attrsfile
);
269 fprintf(ftable
, "#include <stdio.h>\n\n");
271 /* Make "const" do nothing if not in ANSI C. */
272 fprintf (ftable
, "#ifndef __cplusplus\n#ifndef __STDC__\n#define const\n#endif\n#endif\n\n");
276 output_token_translations();
277 /* if (semantic_parser) */
278 /* This is now unconditional because debugging printouts can use it. */
292 output_token_translations (void)
295 /* register short *sp; JF unused */
300 "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
301 max_user_token_number
, nsyms
);
303 if (ntokens
< 127) /* play it very safe; check maximum element value. */
304 fprintf(ftable
, "\nstatic const char yytranslate[] = { 0");
306 fprintf(ftable
, "\nstatic const short yytranslate[] = { 0");
309 for (i
= 1; i
<= max_user_token_number
; i
++)
323 fprintf(ftable
, "%6d", token_translations
[i
]);
326 fprintf(ftable
, "\n};\n");
330 fprintf(ftable
, "\n#define YYTRANSLATE(x) (x)\n");
342 /* With the ordinary parser,
343 yyprhs and yyrhs are needed only for yydebug. */
344 /* With the noparser option, all tables are generated */
345 if (! semantic_parser
&& ! noparserflag
)
346 fprintf(ftable
, "\n#if YYDEBUG != 0");
348 fprintf(ftable
, "\nstatic const short yyprhs[] = { 0");
351 for (i
= 1; i
<= nrules
; i
++)
365 fprintf(ftable
, "%6d", rrhs
[i
]);
368 fprintf(ftable
, "\n};\n");
370 fprintf(ftable
, "\nstatic const short yyrhs[] = {%6d", ritem
[0]);
373 for (sp
= ritem
+ 1; *sp
; sp
++)
388 fprintf(ftable
, "%6d", *sp
);
390 fprintf(ftable
, " 0");
393 fprintf(ftable
, "\n};\n");
395 if (! semantic_parser
&& ! noparserflag
)
396 fprintf(ftable
, "\n#endif\n");
406 fprintf(ftable
, "\nstatic const short yystos[] = { 0");
409 for (i
= 1; i
< nstates
; i
++)
423 fprintf(ftable
, "%6d", accessing_symbol
[i
]);
426 fprintf(ftable
, "\n};\n");
431 output_rule_data (void)
436 fprintf(ftable
, "\n#if YYDEBUG != 0\n");
437 fprintf(ftable
, "static const short yyrline[] = { 0");
440 for (i
= 1; i
<= nrules
; i
++)
454 fprintf(ftable
, "%6d", rline
[i
]);
456 fprintf(ftable
, "\n};\n#endif\n\n");
458 if (toknumflag
|| noparserflag
)
460 fprintf(ftable
, "#define YYNTOKENS %d\n", ntokens
);
461 fprintf(ftable
, "#define YYNNTS %d\n", nvars
);
462 fprintf(ftable
, "#define YYNRULES %d\n", nrules
);
463 fprintf(ftable
, "#define YYNSTATES %d\n", nstates
);
464 fprintf(ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
467 if (! toknumflag
&& ! noparserflag
)
468 fprintf(ftable
, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
470 /* Output the table of symbol names. */
473 "static const char * const yytname[] = { \"%s\"",
476 j
= strlen (tags
[0]) + 44;
477 for (i
= 1; i
< nsyms
; i
++)
478 /* this used to be i<=nsyms, but that output a final "" symbol
479 almost by accident */
494 for (p
= tags
[i
]; p
&& *p
; p
++)
496 if (*p
== '"' || *p
== '\\')
498 fprintf(ftable
, "\\%c", *p
);
503 fprintf(ftable
, "\\n");
508 fprintf(ftable
, "\\t");
513 fprintf(ftable
, "\\b");
516 else if (*p
< 040 || *p
>= 0177)
518 fprintf(ftable
, "\\%03o", *p
);
531 fprintf(ftable
, ", NULL\n};\n"); /* add a NULL entry to list of tokens */
533 if (! toknumflag
&& ! noparserflag
)
534 fprintf(ftable
, "#endif\n\n");
538 fprintf(ftable
, "static const short yytoknum[] = { 0");
540 for (i
= 1; i
<= ntokens
; i
++) {
549 fprintf(ftable
, "%6d", user_toknums
[i
]);
551 fprintf(ftable
, "\n};\n\n");
554 fprintf(ftable
, "static const short yyr1[] = { 0");
557 for (i
= 1; i
<= nrules
; i
++)
571 fprintf(ftable
, "%6d", rlhs
[i
]);
576 fprintf(ftable
, "\n};\n\nstatic const short yyr2[] = { 0");
579 for (i
= 1; i
< nrules
; i
++)
593 fprintf(ftable
, "%6d", rrhs
[i
+ 1] - rrhs
[i
] - 1);
600 fprintf(ftable
, "%6d\n};\n", nitems
- rrhs
[nrules
] - 1);
606 output_defines (void)
608 fprintf(ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
609 fprintf(ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
610 fprintf(ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
615 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */
618 output_actions (void)
620 nvectors
= nstates
+ nvars
;
622 froms
= NEW2(nvectors
, short *);
623 tos
= NEW2(nvectors
, short *);
624 tally
= NEW2(nvectors
, short);
625 width
= NEW2(nvectors
, short);
633 FREE(accessing_symbol
);
636 FREE(goto_map
+ ntokens
);
649 /* figure out the actions for the specified state, indexed by lookahead token type.
651 The yydefact table is output now. The detailed info
652 is saved for putting into yytable later. */
661 actrow
= NEW2(ntokens
, short);
664 fprintf(ftable
, "\nstatic const short yydefact[] = {%6d", k
);
668 for (i
= 1; i
< nstates
; i
++)
683 fprintf(ftable
, "%6d", k
);
687 fprintf(ftable
, "\n};\n");
693 /* Decide what to do for each type of token if seen as the lookahead token in specified state.
694 The value returned is used as the default action (yydefact) for the state.
695 In addition, actrow is filled with what to do for each kind of token,
696 index by symbol number, with zero meaning do the default action.
697 The value MINSHORT, a very negative number, means this situation
698 is an error. The parser recognizes this value specially.
700 This is where conflicts are resolved. The loop over lookahead rules
701 considered lower-numbered rules last, and the last rule considered that likes
702 a token gets to handle it. */
705 action_row (int state
)
713 register int default_rule
;
717 register int shift_state
;
719 register unsigned mask
;
720 register unsigned *wordp
;
721 register reductions
*redp
;
722 register shifts
*shiftp
;
724 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
726 for (i
= 0; i
< ntokens
; i
++)
731 redp
= reduction_table
[state
];
739 /* loop over all the rules available here which require lookahead */
740 m
= lookaheads
[state
];
741 n
= lookaheads
[state
+ 1];
743 for (i
= n
- 1; i
>= m
; i
--)
745 rule
= - LAruleno
[i
];
746 wordp
= LA
+ i
* tokensetsize
;
749 /* and find each token which the rule finds acceptable to come next */
750 for (j
= 0; j
< ntokens
; j
++)
752 /* and record this rule as the rule to use if that token follows. */
767 shiftp
= shift_table
[state
];
769 /* now see which tokens are allowed for shifts in this state.
770 For them, record the shift as the thing to do. So shift is preferred to reduce. */
776 for (i
= 0; i
< k
; i
++)
778 shift_state
= shiftp
->shifts
[i
];
779 if (! shift_state
) continue;
781 symbol
= accessing_symbol
[shift_state
];
786 actrow
[symbol
] = shift_state
;
788 /* do not use any default reduction if there is a shift for error */
790 if (symbol
== error_token_number
) nodefault
= 1;
794 errp
= err_table
[state
];
796 /* See which tokens are an explicit error in this state
797 (due to %nonassoc). For them, record MINSHORT as the action. */
803 for (i
= 0; i
< k
; i
++)
805 symbol
= errp
->errs
[i
];
806 actrow
[symbol
] = MINSHORT
;
810 /* now find the most common reduction and make it the default action for this state. */
812 if (nreds
>= 1 && ! nodefault
)
814 if (consistent
[state
])
815 default_rule
= redp
->rules
[0];
819 for (i
= m
; i
< n
; i
++)
822 rule
= - LAruleno
[i
];
824 for (j
= 0; j
< ntokens
; j
++)
826 if (actrow
[j
] == rule
)
837 /* actions which match the default are replaced with zero,
838 which means "use the default" */
842 for (j
= 0; j
< ntokens
; j
++)
844 if (actrow
[j
] == default_rule
)
848 default_rule
= - default_rule
;
853 /* If have no default rule, the default is an error.
854 So replace any action which says "error" with "use default". */
856 if (default_rule
== 0)
857 for (j
= 0; j
< ntokens
; j
++)
859 if (actrow
[j
] == MINSHORT
)
863 return (default_rule
);
877 for (i
= 0; i
< ntokens
; i
++)
886 froms
[state
] = sp1
= sp
= NEW2(count
, short);
887 tos
[state
] = sp2
= NEW2(count
, short);
889 for (i
= 0; i
< ntokens
; i
++)
898 tally
[state
] = count
;
899 width
[state
] = sp1
[-1] - sp
[0] + 1;
904 /* figure out what to do after reducing with each rule,
905 depending on the saved state from before the beginning
906 of parsing the data that matched this rule.
908 The yydefgoto table is output now. The detailed info
909 is saved for putting into yytable later. */
918 state_count
= NEW2(nstates
, short);
920 k
= default_goto(ntokens
);
921 fprintf(ftable
, "\nstatic const short yydefgoto[] = {%6d", k
);
922 save_column(ntokens
, k
);
925 for (i
= ntokens
+ 1; i
< nsyms
; i
++)
940 fprintf(ftable
, "%6d", k
);
944 fprintf(ftable
, "\n};\n");
951 default_goto (int symbol
)
956 register int default_state
;
959 m
= goto_map
[symbol
];
960 n
= goto_map
[symbol
+ 1];
965 for (i
= 0; i
< nstates
; i
++)
968 for (i
= m
; i
< n
; i
++)
969 state_count
[to_state
[i
]]++;
974 for (i
= 0; i
< nstates
; i
++)
976 if (state_count
[i
] > max
)
978 max
= state_count
[i
];
983 return (default_state
);
988 save_column (int symbol
, int default_state
)
999 m
= goto_map
[symbol
];
1000 n
= goto_map
[symbol
+ 1];
1003 for (i
= m
; i
< n
; i
++)
1005 if (to_state
[i
] != default_state
)
1012 symno
= symbol
- ntokens
+ nstates
;
1014 froms
[symno
] = sp1
= sp
= NEW2(count
, short);
1015 tos
[symno
] = sp2
= NEW2(count
, short);
1017 for (i
= m
; i
< n
; i
++)
1019 if (to_state
[i
] != default_state
)
1021 *sp1
++ = from_state
[i
];
1022 *sp2
++ = to_state
[i
];
1026 tally
[symno
] = count
;
1027 width
[symno
] = sp1
[-1] - sp
[0] + 1;
1032 /* the next few functions decide how to pack
1033 the actions and gotos information into yytable. */
1044 order
= NEW2(nvectors
, short);
1047 for (i
= 0; i
< nvectors
; i
++)
1055 while (j
>= 0 && (width
[order
[j
]] < w
))
1058 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
1061 for (k
= nentries
- 1; k
> j
; k
--)
1062 order
[k
+ 1] = order
[k
];
1078 base
= NEW2(nvectors
, short);
1079 pos
= NEW2(nentries
, short);
1080 table
= NEW2(MAXTABLE
, short);
1081 check
= NEW2(MAXTABLE
, short);
1086 for (i
= 0; i
< nvectors
; i
++)
1089 for (i
= 0; i
< MAXTABLE
; i
++)
1092 for (i
= 0; i
< nentries
; i
++)
1094 state
= matching_state(i
);
1097 place
= pack_vector(i
);
1099 place
= base
[state
];
1102 base
[order
[i
]] = place
;
1105 for (i
= 0; i
< nvectors
; i
++)
1121 matching_state (int vector
)
1138 for (prev
= vector
- 1; prev
>= 0; prev
--)
1141 if (width
[j
] != w
|| tally
[j
] != t
)
1145 for (k
= 0; match
&& k
< t
; k
++)
1147 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
1161 pack_vector (int vector
)
1169 register short *from
;
1176 berror("pack_vector");
1181 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
1185 for (k
= 0; ok
&& k
< t
; k
++)
1189 fatals(_("maximum table size (%s) exceeded"), int_to_string(MAXTABLE
));
1191 if (table
[loc
] != 0)
1195 for (k
= 0; ok
&& k
< vector
; k
++)
1203 for (k
= 0; k
< t
; k
++)
1207 check
[loc
] = from
[k
];
1210 while (table
[lowzero
] != 0)
1220 berror("pack_vector");
1221 return 0; /* JF keep lint happy */
1226 /* the following functions output yytable, yycheck
1227 and the vectors whose elements index the portion starts */
1235 fprintf(ftable
, "\nstatic const short yypact[] = {%6d", base
[0]);
1238 for (i
= 1; i
< nstates
; i
++)
1252 fprintf(ftable
, "%6d", base
[i
]);
1255 fprintf(ftable
, "\n};\n\nstatic const short yypgoto[] = {%6d", base
[nstates
]);
1258 for (i
= nstates
+ 1; i
< nvectors
; i
++)
1272 fprintf(ftable
, "%6d", base
[i
]);
1275 fprintf(ftable
, "\n};\n");
1286 fprintf(ftable
, "\n\n#define\tYYLAST\t\t%d\n\n", high
);
1287 fprintf(ftable
, "\nstatic const short yytable[] = {%6d", table
[0]);
1290 for (i
= 1; i
<= high
; i
++)
1304 fprintf(ftable
, "%6d", table
[i
]);
1307 fprintf(ftable
, "\n};\n");
1318 fprintf(ftable
, "\nstatic const short yycheck[] = {%6d", check
[0]);
1321 for (i
= 1; i
<= high
; i
++)
1335 fprintf(ftable
, "%6d", check
[i
]);
1338 fprintf(ftable
, "\n};\n");
1344 /* copy the parser code into the ftable file at the end. */
1347 output_parser (void)
1353 #define fpars fparser
1357 fprintf(ftable
, "#define YYPURE 1\n\n");
1359 #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1360 currently open parser from bison.simple to bison.hairy */
1361 if (semantic_parser
)
1363 else fpars
= fparser1
;
1366 /* Loop over lines in the standard parser file. */
1374 /* See if the line starts with `#line.
1375 If so, set write_line to 0. */
1392 fprintf(ftable
, "#lin");
1395 fprintf(ftable
, "#li");
1398 fprintf(ftable
, "#l");
1401 fprintf(ftable
, "#");
1404 /* now write out the line... */
1405 for (; c
!= '\n' && c
!= EOF
; c
= getc(fpars
))
1409 /* `$' in the parser file indicates where to put the actions.
1410 Copy them in at this point. */
1412 for(c
=getc(faction
);c
!=EOF
;c
=getc(faction
))
1425 output_program (void)
1430 fprintf(ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1442 free_itemsets (void)
1444 register core
*cp
,*cptmp
;
1448 for (cp
= first_state
; cp
; cp
= cptmp
) {
1458 register shifts
*sp
,*sptmp
;/* JF derefrenced freed ptr */
1462 for (sp
= first_shift
; sp
; sp
= sptmp
) {
1470 free_reductions (void)
1472 register reductions
*rp
,*rptmp
;/* JF fixed freed ptr */
1474 FREE(reduction_table
);
1476 for (rp
= first_reduction
; rp
; rp
= rptmp
) {