]>
git.saurik.com Git - bison.git/blob - src/output.c
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
;
136 void output_token_translations();
139 void output_rule_data();
140 void output_defines();
141 void output_actions();
142 void token_actions();
151 void output_parser();
152 void output_program();
155 void free_reductions();
156 void free_itemsets();
159 int matching_state();
162 extern void berror();
163 extern void fatals();
164 extern char *int_to_string();
165 extern void reader_output_yylsp();
169 static short **froms
;
173 static short *actrow
;
174 static short *state_count
;
185 #define GUARDSTR "\n#include \"%s\"\nextern int yyerror;\n\
186 extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
187 yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
188 register YYLTYPE *yylsp;\n\
189 {\n yyerror = 0;\nyycost = 0;\n yymsg = 0;\nswitch (n)\n {"
191 #define ACTSTR "\n#include \"%s\"\nextern YYSTYPE yyval;\
192 \nextern int yychar;\
193 yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
194 register YYLTYPE *yylsp;\n{\n switch (n)\n{"
196 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
203 fprintf(fguard
, GUARDSTR
, attrsfile
);
208 fprintf(faction
, (semantic_parser
? ACTSTR
: ACTSTR_SIMPLE
), attrsfile
);
209 /* if (semantic_parser) JF moved this below
210 fprintf(ftable, "#include \"%s\"\n", attrsfile);
211 fprintf(ftable, "#include <stdio.h>\n\n");
214 /* Rename certain symbols if -p was specified. */
215 if (spec_name_prefix
)
217 fprintf(ftable
, "#define yyparse %sparse\n", spec_name_prefix
);
218 fprintf(ftable
, "#define yylex %slex\n", spec_name_prefix
);
219 fprintf(ftable
, "#define yyerror %serror\n", spec_name_prefix
);
220 fprintf(ftable
, "#define yylval %slval\n", spec_name_prefix
);
221 fprintf(ftable
, "#define yychar %schar\n", spec_name_prefix
);
222 fprintf(ftable
, "#define yydebug %sdebug\n", spec_name_prefix
);
223 fprintf(ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
);
232 fprintf(fguard
, "\n }\n}\n");
234 fprintf(faction
, "\n");
240 fprintf(faction
, " }\n");
241 fprintf(faction
, "}\n");
250 /* output_token_defines(ftable); / * JF put out token defines FIRST */
251 if (!semantic_parser
) /* JF Put out other stuff */
254 while ((c
=getc(fattrs
))!=EOF
)
257 reader_output_yylsp(ftable
);
259 fprintf(ftable
, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n",
263 fprintf(ftable
, "#include \"%s\"\n", attrsfile
);
266 fprintf(ftable
, "#include <stdio.h>\n\n");
268 /* Make "const" do nothing if not in ANSI C. */
269 fprintf (ftable
, "#ifndef __cplusplus\n#ifndef __STDC__\n#define const\n#endif\n#endif\n\n");
273 output_token_translations();
274 /* if (semantic_parser) */
275 /* This is now unconditional because debugging printouts can use it. */
289 output_token_translations()
292 /* register short *sp; JF unused */
297 "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
298 max_user_token_number
, nsyms
);
300 if (ntokens
< 127) /* play it very safe; check maximum element value. */
301 fprintf(ftable
, "\nstatic const char yytranslate[] = { 0");
303 fprintf(ftable
, "\nstatic const short yytranslate[] = { 0");
306 for (i
= 1; i
<= max_user_token_number
; i
++)
320 fprintf(ftable
, "%6d", token_translations
[i
]);
323 fprintf(ftable
, "\n};\n");
327 fprintf(ftable
, "\n#define YYTRANSLATE(x) (x)\n");
339 /* With the ordinary parser,
340 yyprhs and yyrhs are needed only for yydebug. */
341 /* With the noparser option, all tables are generated */
342 if (! semantic_parser
&& ! noparserflag
)
343 fprintf(ftable
, "\n#if YYDEBUG != 0");
345 fprintf(ftable
, "\nstatic const short yyprhs[] = { 0");
348 for (i
= 1; i
<= nrules
; i
++)
362 fprintf(ftable
, "%6d", rrhs
[i
]);
365 fprintf(ftable
, "\n};\n");
367 fprintf(ftable
, "\nstatic const short yyrhs[] = {%6d", ritem
[0]);
370 for (sp
= ritem
+ 1; *sp
; sp
++)
385 fprintf(ftable
, "%6d", *sp
);
387 fprintf(ftable
, " 0");
390 fprintf(ftable
, "\n};\n");
392 if (! semantic_parser
&& ! noparserflag
)
393 fprintf(ftable
, "\n#endif\n");
403 fprintf(ftable
, "\nstatic const short yystos[] = { 0");
406 for (i
= 1; i
< nstates
; i
++)
420 fprintf(ftable
, "%6d", accessing_symbol
[i
]);
423 fprintf(ftable
, "\n};\n");
433 fprintf(ftable
, "\n#if YYDEBUG != 0\n");
434 fprintf(ftable
, "static const short yyrline[] = { 0");
437 for (i
= 1; i
<= nrules
; i
++)
451 fprintf(ftable
, "%6d", rline
[i
]);
453 fprintf(ftable
, "\n};\n#endif\n\n");
455 if (toknumflag
|| noparserflag
)
457 fprintf(ftable
, "#define YYNTOKENS %d\n", ntokens
);
458 fprintf(ftable
, "#define YYNNTS %d\n", nvars
);
459 fprintf(ftable
, "#define YYNRULES %d\n", nrules
);
460 fprintf(ftable
, "#define YYNSTATES %d\n", nstates
);
461 fprintf(ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
464 if (! toknumflag
&& ! noparserflag
)
465 fprintf(ftable
, "\n#if YYDEBUG != 0\n\n");
467 /* Output the table of symbol names. */
470 "static const char * const yytname[] = { \"%s\"",
473 j
= strlen (tags
[0]) + 44;
474 for (i
= 1; i
< nsyms
; i
++)
475 /* this used to be i<=nsyms, but that output a final "" symbol
476 almost by accident */
491 for (p
= tags
[i
]; p
&& *p
; p
++)
493 if (*p
== '"' || *p
== '\\')
495 fprintf(ftable
, "\\%c", *p
);
500 fprintf(ftable
, "\\n");
505 fprintf(ftable
, "\\t");
510 fprintf(ftable
, "\\b");
513 else if (*p
< 040 || *p
>= 0177)
515 fprintf(ftable
, "\\%03o", *p
);
528 fprintf(ftable
, ", NULL\n};\n"); /* add a NULL entry to list of tokens */
530 if (! toknumflag
&& ! noparserflag
)
531 fprintf(ftable
, "#endif\n\n");
535 fprintf(ftable
, "static const short yytoknum[] = { 0");
537 for (i
= 1; i
<= ntokens
; i
++) {
546 fprintf(ftable
, "%6d", user_toknums
[i
]);
548 fprintf(ftable
, "\n};\n\n");
551 fprintf(ftable
, "static const short yyr1[] = { 0");
554 for (i
= 1; i
<= nrules
; i
++)
568 fprintf(ftable
, "%6d", rlhs
[i
]);
573 fprintf(ftable
, "\n};\n\nstatic const short yyr2[] = { 0");
576 for (i
= 1; i
< nrules
; i
++)
590 fprintf(ftable
, "%6d", rrhs
[i
+ 1] - rrhs
[i
] - 1);
597 fprintf(ftable
, "%6d\n};\n", nitems
- rrhs
[nrules
] - 1);
605 fprintf(ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
606 fprintf(ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
607 fprintf(ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
612 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */
617 nvectors
= nstates
+ nvars
;
619 froms
= NEW2(nvectors
, short *);
620 tos
= NEW2(nvectors
, short *);
621 tally
= NEW2(nvectors
, short);
622 width
= NEW2(nvectors
, short);
630 FREE(accessing_symbol
);
633 FREE(goto_map
+ ntokens
);
646 /* figure out the actions for the specified state, indexed by lookahead token type.
648 The yydefact table is output now. The detailed info
649 is saved for putting into yytable later. */
658 actrow
= NEW2(ntokens
, short);
661 fprintf(ftable
, "\nstatic const short yydefact[] = {%6d", k
);
665 for (i
= 1; i
< nstates
; i
++)
680 fprintf(ftable
, "%6d", k
);
684 fprintf(ftable
, "\n};\n");
690 /* Decide what to do for each type of token if seen as the lookahead token in specified state.
691 The value returned is used as the default action (yydefact) for the state.
692 In addition, actrow is filled with what to do for each kind of token,
693 index by symbol number, with zero meaning do the default action.
694 The value MINSHORT, a very negative number, means this situation
695 is an error. The parser recognizes this value specially.
697 This is where conflicts are resolved. The loop over lookahead rules
698 considered lower-numbered rules last, and the last rule considered that likes
699 a token gets to handle it. */
711 register int default_rule
;
715 register int shift_state
;
717 register unsigned mask
;
718 register unsigned *wordp
;
719 register reductions
*redp
;
720 register shifts
*shiftp
;
722 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
724 for (i
= 0; i
< ntokens
; i
++)
729 redp
= reduction_table
[state
];
737 /* loop over all the rules available here which require lookahead */
738 m
= lookaheads
[state
];
739 n
= lookaheads
[state
+ 1];
741 for (i
= n
- 1; i
>= m
; i
--)
743 rule
= - LAruleno
[i
];
744 wordp
= LA
+ i
* tokensetsize
;
747 /* and find each token which the rule finds acceptable to come next */
748 for (j
= 0; j
< ntokens
; j
++)
750 /* and record this rule as the rule to use if that token follows. */
765 shiftp
= shift_table
[state
];
767 /* now see which tokens are allowed for shifts in this state.
768 For them, record the shift as the thing to do. So shift is preferred to reduce. */
774 for (i
= 0; i
< k
; i
++)
776 shift_state
= shiftp
->shifts
[i
];
777 if (! shift_state
) continue;
779 symbol
= accessing_symbol
[shift_state
];
784 actrow
[symbol
] = shift_state
;
786 /* do not use any default reduction if there is a shift for error */
788 if (symbol
== error_token_number
) nodefault
= 1;
792 errp
= err_table
[state
];
794 /* See which tokens are an explicit error in this state
795 (due to %nonassoc). For them, record MINSHORT as the action. */
801 for (i
= 0; i
< k
; i
++)
803 symbol
= errp
->errs
[i
];
804 actrow
[symbol
] = MINSHORT
;
808 /* now find the most common reduction and make it the default action for this state. */
810 if (nreds
>= 1 && ! nodefault
)
812 if (consistent
[state
])
813 default_rule
= redp
->rules
[0];
817 for (i
= m
; i
< n
; i
++)
820 rule
= - LAruleno
[i
];
822 for (j
= 0; j
< ntokens
; j
++)
824 if (actrow
[j
] == rule
)
835 /* actions which match the default are replaced with zero,
836 which means "use the default" */
840 for (j
= 0; j
< ntokens
; j
++)
842 if (actrow
[j
] == default_rule
)
846 default_rule
= - default_rule
;
851 /* If have no default rule, the default is an error.
852 So replace any action which says "error" with "use default". */
854 if (default_rule
== 0)
855 for (j
= 0; j
< ntokens
; j
++)
857 if (actrow
[j
] == MINSHORT
)
861 return (default_rule
);
876 for (i
= 0; i
< ntokens
; i
++)
885 froms
[state
] = sp1
= sp
= NEW2(count
, short);
886 tos
[state
] = sp2
= NEW2(count
, short);
888 for (i
= 0; i
< ntokens
; i
++)
897 tally
[state
] = count
;
898 width
[state
] = sp1
[-1] - sp
[0] + 1;
903 /* figure out what to do after reducing with each rule,
904 depending on the saved state from before the beginning
905 of parsing the data that matched this rule.
907 The yydefgoto table is output now. The detailed info
908 is saved for putting into yytable later. */
917 state_count
= NEW2(nstates
, short);
919 k
= default_goto(ntokens
);
920 fprintf(ftable
, "\nstatic const short yydefgoto[] = {%6d", k
);
921 save_column(ntokens
, k
);
924 for (i
= ntokens
+ 1; i
< nsyms
; i
++)
939 fprintf(ftable
, "%6d", k
);
943 fprintf(ftable
, "\n};\n");
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(symbol
, default_state
)
1001 m
= goto_map
[symbol
];
1002 n
= goto_map
[symbol
+ 1];
1005 for (i
= m
; i
< n
; i
++)
1007 if (to_state
[i
] != default_state
)
1014 symno
= symbol
- ntokens
+ nstates
;
1016 froms
[symno
] = sp1
= sp
= NEW2(count
, short);
1017 tos
[symno
] = sp2
= NEW2(count
, short);
1019 for (i
= m
; i
< n
; i
++)
1021 if (to_state
[i
] != default_state
)
1023 *sp1
++ = from_state
[i
];
1024 *sp2
++ = to_state
[i
];
1028 tally
[symno
] = count
;
1029 width
[symno
] = sp1
[-1] - sp
[0] + 1;
1034 /* the next few functions decide how to pack
1035 the actions and gotos information into yytable. */
1046 order
= NEW2(nvectors
, short);
1049 for (i
= 0; i
< nvectors
; i
++)
1057 while (j
>= 0 && (width
[order
[j
]] < w
))
1060 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
1063 for (k
= nentries
- 1; k
> j
; k
--)
1064 order
[k
+ 1] = order
[k
];
1080 base
= NEW2(nvectors
, short);
1081 pos
= NEW2(nentries
, short);
1082 table
= NEW2(MAXTABLE
, short);
1083 check
= NEW2(MAXTABLE
, short);
1088 for (i
= 0; i
< nvectors
; i
++)
1091 for (i
= 0; i
< MAXTABLE
; i
++)
1094 for (i
= 0; i
< nentries
; i
++)
1096 state
= matching_state(i
);
1099 place
= pack_vector(i
);
1101 place
= base
[state
];
1104 base
[order
[i
]] = place
;
1107 for (i
= 0; i
< nvectors
; i
++)
1123 matching_state(vector
)
1141 for (prev
= vector
- 1; prev
>= 0; prev
--)
1144 if (width
[j
] != w
|| tally
[j
] != t
)
1148 for (k
= 0; match
&& k
< t
; k
++)
1150 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
1173 register short *from
;
1180 berror("pack_vector");
1185 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
1189 for (k
= 0; ok
&& k
< t
; k
++)
1193 fatals("maximum table size (%s) exceeded", int_to_string(MAXTABLE
));
1195 if (table
[loc
] != 0)
1199 for (k
= 0; ok
&& k
< vector
; k
++)
1207 for (k
= 0; k
< t
; k
++)
1211 check
[loc
] = from
[k
];
1214 while (table
[lowzero
] != 0)
1224 berror("pack_vector");
1225 return 0; /* JF keep lint happy */
1230 /* the following functions output yytable, yycheck
1231 and the vectors whose elements index the portion starts */
1239 fprintf(ftable
, "\nstatic const short yypact[] = {%6d", base
[0]);
1242 for (i
= 1; i
< nstates
; i
++)
1256 fprintf(ftable
, "%6d", base
[i
]);
1259 fprintf(ftable
, "\n};\n\nstatic const short yypgoto[] = {%6d", base
[nstates
]);
1262 for (i
= nstates
+ 1; i
< nvectors
; i
++)
1276 fprintf(ftable
, "%6d", base
[i
]);
1279 fprintf(ftable
, "\n};\n");
1290 fprintf(ftable
, "\n\n#define\tYYLAST\t\t%d\n\n", high
);
1291 fprintf(ftable
, "\nstatic const short yytable[] = {%6d", table
[0]);
1294 for (i
= 1; i
<= high
; i
++)
1308 fprintf(ftable
, "%6d", table
[i
]);
1311 fprintf(ftable
, "\n};\n");
1322 fprintf(ftable
, "\nstatic const short yycheck[] = {%6d", check
[0]);
1325 for (i
= 1; i
<= high
; i
++)
1339 fprintf(ftable
, "%6d", check
[i
]);
1342 fprintf(ftable
, "\n};\n");
1348 /* copy the parser code into the ftable file at the end. */
1357 #define fpars fparser
1361 fprintf(ftable
, "#define YYPURE 1\n\n");
1363 #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1364 currently open parser from bison.simple to bison.hairy */
1365 if (semantic_parser
)
1367 else fpars
= fparser1
;
1370 /* Loop over lines in the standard parser file. */
1378 /* See if the line starts with `#line.
1379 If so, set write_line to 0. */
1396 fprintf(ftable
, "#lin");
1399 fprintf(ftable
, "#li");
1402 fprintf(ftable
, "#l");
1405 fprintf(ftable
, "#");
1408 /* now write out the line... */
1409 for (; c
!= '\n' && c
!= EOF
; c
= getc(fpars
))
1413 /* `$' in the parser file indicates where to put the actions.
1414 Copy them in at this point. */
1416 for(c
=getc(faction
);c
!=EOF
;c
=getc(faction
))
1434 fprintf(ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1448 register core
*cp
,*cptmp
;
1452 for (cp
= first_state
; cp
; cp
= cptmp
) {
1462 register shifts
*sp
,*sptmp
;/* JF derefrenced freed ptr */
1466 for (sp
= first_shift
; sp
; sp
= sptmp
) {
1476 register reductions
*rp
,*rptmp
;/* JF fixed freed ptr */
1478 FREE(reduction_table
);
1480 for (rp
= first_reduction
; rp
; rp
= rptmp
) {