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 /* functions to output parsing data to various files. Entries are:
26 Output constant strings to the beginning of certain files.
30 Output constant strings to the ends of certain files.
34 Output the parsing tables and the parser code to ftable.
36 The parser tables consist of these tables.
37 Starred ones needed only for the semantic parser.
38 Double starred are output only if switches are set.
40 yytranslate = vector mapping yylex's token numbers into bison's token numbers.
42 ** yytname = vector of string-names indexed by bison token number
44 ** yytoknum = vector of yylex token numbers corresponding to entries in yytname
46 yyrline = vector of line-numbers of all rules. For yydebug printouts.
48 yyrhs = vector of items of all rules.
49 This is exactly what ritems contains. For yydebug and for semantic
52 yyprhs[r] = index in yyrhs of first item for rule r.
54 yyr1[r] = symbol number of symbol that rule r derives.
56 yyr2[r] = number of symbols composing right hand side of rule r.
58 * yystos[s] = the symbol number of the symbol that leads to state s.
60 yydefact[s] = default rule to reduce with in state s,
61 when yytable doesn't specify something else to do.
62 Zero means the default is an error.
64 yydefgoto[i] = default state to go to after a reduction of a rule that
65 generates variable ntokens + i, except when yytable
66 specifies something else to do.
68 yypact[s] = index in yytable of the portion describing state s.
69 The lookahead token's type is used to index that portion
70 to find out what to do.
72 If the value in yytable is positive,
73 we shift the token and go to that state.
75 If the value is negative, it is minus a rule number to reduce by.
77 If the value is zero, the default action from yydefact[s] is used.
79 yypgoto[i] = the index in yytable of the portion describing
80 what to do after reducing a rule that derives variable i + ntokens.
81 This portion is indexed by the parser state number, s,
82 as of before the text for this nonterminal was read.
83 The value from yytable is the state to go to if
84 the corresponding value in yycheck is s.
86 yytable = a vector filled with portions for different uses,
87 found via yypact and yypgoto.
89 yycheck = a vector indexed in parallel with yytable.
90 It indicates, in a roundabout way, the bounds of the
91 portion you are trying to examine.
93 Suppose that the portion of yytable starts at index p
94 and the index to be examined within the portion is i.
95 Then if yycheck[p+i] != i, i is outside the bounds
96 of what is actually allocated, and the default
97 (from yydefact or yydefgoto) should be used.
98 Otherwise, yytable[p+i] should be used.
100 YYFINAL = the state number of the termination state.
101 YYFLAG = most negative short int. Used to flag ??
113 #include "complain.h"
116 extern int debugflag
;
117 extern int nolinesflag
;
118 extern int noparserflag
;
119 extern int toknumflag
;
122 extern int *user_toknums
;
123 extern int tokensetsize
;
124 extern int final_state
;
125 extern core
**state_table
;
126 extern shifts
**shift_table
;
127 extern errs
**err_table
;
128 extern reductions
**reduction_table
;
129 extern short *accessing_symbol
;
131 extern short *LAruleno
;
132 extern short *lookaheads
;
133 extern char *consistent
;
134 extern short *goto_map
;
135 extern short *from_state
;
136 extern short *to_state
;
138 extern void output_headers
PARAMS((void));
139 extern void output_trailers
PARAMS((void));
140 extern void output
PARAMS((void));
142 static void output_token_translations
PARAMS((void));
143 static void output_gram
PARAMS((void));
144 static void output_stos
PARAMS((void));
145 static void output_rule_data
PARAMS((void));
146 static void output_defines
PARAMS((void));
147 static void output_actions
PARAMS((void));
148 static void token_actions
PARAMS((void));
149 static void save_row
PARAMS((int));
150 static void goto_actions
PARAMS((void));
151 static void save_column
PARAMS((int, int));
152 static void sort_actions
PARAMS((void));
153 static void pack_table
PARAMS((void));
154 static void output_base
PARAMS((void));
155 static void output_table
PARAMS((void));
156 static void output_check
PARAMS((void));
157 static void output_parser
PARAMS((void));
158 static void output_program
PARAMS((void));
159 static void free_shifts
PARAMS((void));
160 static void free_reductions
PARAMS((void));
161 static void free_itemsets
PARAMS((void));
162 static int action_row
PARAMS((int));
163 static int default_goto
PARAMS((int));
164 static int matching_state
PARAMS((int));
165 static int pack_vector
PARAMS((int));
167 extern void berror
PARAMS((const char *));
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)
438 /* YYRLINE[yyn]: source line where rule number YYN was defined. */\n\
439 static const short yyrline[] = { 0", ftable
);
442 for (i
= 1; i
<= nrules
; i
++)
456 fprintf(ftable
, "%6d", rline
[i
]);
458 fprintf(ftable
, "\n};\n#endif\n\n");
460 if (toknumflag
|| noparserflag
)
462 fprintf(ftable
, "#define YYNTOKENS %d\n", ntokens
);
463 fprintf(ftable
, "#define YYNNTS %d\n", nvars
);
464 fprintf(ftable
, "#define YYNRULES %d\n", nrules
);
465 fprintf(ftable
, "#define YYNSTATES %d\n", nstates
);
466 fprintf(ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
469 if (! toknumflag
&& ! noparserflag
)
470 fprintf(ftable
, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
472 /* Output the table of symbol names. */
475 "static const char * const yytname[] = { \"%s\"",
478 j
= strlen (tags
[0]) + 44;
479 for (i
= 1; i
< nsyms
; i
++)
480 /* this used to be i<=nsyms, but that output a final "" symbol
481 almost by accident */
496 for (p
= tags
[i
]; p
&& *p
; p
++)
498 if (*p
== '"' || *p
== '\\')
500 fprintf(ftable
, "\\%c", *p
);
505 fprintf(ftable
, "\\n");
510 fprintf(ftable
, "\\t");
515 fprintf(ftable
, "\\b");
518 else if (*p
< 040 || *p
>= 0177)
520 fprintf(ftable
, "\\%03o", *p
);
533 /* add a NULL entry to list of tokens */
534 fprintf (ftable
, ", NULL\n};\n");
536 if (! toknumflag
&& ! noparserflag
)
537 fprintf (ftable
, "#endif\n\n");
539 /* Output YYTOKNUM. */
542 fprintf(ftable
, "static const short yytoknum[] = { 0");
544 for (i
= 1; i
<= ntokens
; i
++) {
553 fprintf(ftable
, "%6d", user_toknums
[i
]);
555 fprintf(ftable
, "\n};\n\n");
560 /* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n\
561 static const short yyr1[] = { 0", ftable
);
564 for (i
= 1; i
<= nrules
; i
++)
578 fprintf(ftable
, "%6d", rlhs
[i
]);
587 /* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\
588 static const short yyr2[] = { 0", ftable
);
590 for (i
= 1; i
< nrules
; i
++)
604 fprintf(ftable
, "%6d", rrhs
[i
+ 1] - rrhs
[i
] - 1);
611 fprintf(ftable
, "%6d\n};\n", nitems
- rrhs
[nrules
] - 1);
617 output_defines (void)
619 fprintf(ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
620 fprintf(ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
621 fprintf(ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
626 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */
629 output_actions (void)
631 nvectors
= nstates
+ nvars
;
633 froms
= NEW2(nvectors
, short *);
634 tos
= NEW2(nvectors
, short *);
635 tally
= NEW2(nvectors
, short);
636 width
= NEW2(nvectors
, short);
644 FREE(accessing_symbol
);
647 FREE(goto_map
+ ntokens
);
660 /* Figure out the actions for the specified state, indexed by
661 lookahead token type.
663 The yydefact table is output now. The detailed info is saved for
664 putting into yytable later. */
673 actrow
= NEW2(ntokens
, short);
676 fprintf(ftable
, "\nstatic const short yydefact[] = {%6d", k
);
680 for (i
= 1; i
< nstates
; i
++)
695 fprintf(ftable
, "%6d", k
);
699 fprintf(ftable
, "\n};\n");
705 /* Decide what to do for each type of token if seen as the lookahead
706 token in specified state. The value returned is used as the
707 default action (yydefact) for the state. In addition, actrow is
708 filled with what to do for each kind of token, index by symbol
709 number, with zero meaning do the default action. The value
710 MINSHORT, a very negative number, means this situation is an error.
711 The parser recognizes this value specially.
713 This is where conflicts are resolved. The loop over lookahead
714 rules considered lower-numbered rules last, and the last rule
715 considered that likes a token gets to handle it. */
718 action_row (int state
)
726 register int default_rule
;
730 register int shift_state
;
732 register unsigned mask
;
733 register unsigned *wordp
;
734 register reductions
*redp
;
735 register shifts
*shiftp
;
737 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
739 for (i
= 0; i
< ntokens
; i
++)
744 redp
= reduction_table
[state
];
752 /* loop over all the rules available here which require
754 m
= lookaheads
[state
];
755 n
= lookaheads
[state
+ 1];
757 for (i
= n
- 1; i
>= m
; i
--)
759 rule
= - LAruleno
[i
];
760 wordp
= LA
+ i
* tokensetsize
;
763 /* and find each token which the rule finds acceptable
765 for (j
= 0; j
< ntokens
; j
++)
767 /* and record this rule as the rule to use if that
783 shiftp
= shift_table
[state
];
785 /* Now see which tokens are allowed for shifts in this state. For
786 them, record the shift as the thing to do. So shift is preferred
793 for (i
= 0; i
< k
; i
++)
795 shift_state
= shiftp
->shifts
[i
];
796 if (! shift_state
) continue;
798 symbol
= accessing_symbol
[shift_state
];
803 actrow
[symbol
] = shift_state
;
805 /* Do not use any default reduction if there is a shift for
807 if (symbol
== error_token_number
)
812 errp
= err_table
[state
];
814 /* See which tokens are an explicit error in this state (due to
815 %nonassoc). For them, record MINSHORT as the action. */
821 for (i
= 0; i
< k
; i
++)
823 symbol
= errp
->errs
[i
];
824 actrow
[symbol
] = MINSHORT
;
828 /* Now find the most common reduction and make it the default action
831 if (nreds
>= 1 && ! nodefault
)
833 if (consistent
[state
])
834 default_rule
= redp
->rules
[0];
838 for (i
= m
; i
< n
; i
++)
841 rule
= - LAruleno
[i
];
843 for (j
= 0; j
< ntokens
; j
++)
845 if (actrow
[j
] == rule
)
856 /* actions which match the default are replaced with zero,
857 which means "use the default" */
861 for (j
= 0; j
< ntokens
; j
++)
863 if (actrow
[j
] == default_rule
)
867 default_rule
= - default_rule
;
872 /* If have no default rule, the default is an error.
873 So replace any action which says "error" with "use default". */
875 if (default_rule
== 0)
876 for (j
= 0; j
< ntokens
; j
++)
878 if (actrow
[j
] == MINSHORT
)
896 for (i
= 0; i
< ntokens
; i
++)
905 froms
[state
] = sp1
= sp
= NEW2(count
, short);
906 tos
[state
] = sp2
= NEW2(count
, short);
908 for (i
= 0; i
< ntokens
; i
++)
917 tally
[state
] = count
;
918 width
[state
] = sp1
[-1] - sp
[0] + 1;
923 /* figure out what to do after reducing with each rule,
924 depending on the saved state from before the beginning
925 of parsing the data that matched this rule.
927 The yydefgoto table is output now. The detailed info
928 is saved for putting into yytable later. */
937 state_count
= NEW2(nstates
, short);
939 k
= default_goto(ntokens
);
940 fprintf(ftable
, "\nstatic const short yydefgoto[] = {%6d", k
);
941 save_column(ntokens
, k
);
944 for (i
= ntokens
+ 1; i
< nsyms
; i
++)
959 fprintf(ftable
, "%6d", k
);
963 fprintf(ftable
, "\n};\n");
970 default_goto (int symbol
)
975 register int default_state
;
978 m
= goto_map
[symbol
];
979 n
= goto_map
[symbol
+ 1];
984 for (i
= 0; i
< nstates
; i
++)
987 for (i
= m
; i
< n
; i
++)
988 state_count
[to_state
[i
]]++;
993 for (i
= 0; i
< nstates
; i
++)
995 if (state_count
[i
] > max
)
997 max
= state_count
[i
];
1002 return default_state
;
1007 save_column (int symbol
, int default_state
)
1013 register short *sp1
;
1014 register short *sp2
;
1018 m
= goto_map
[symbol
];
1019 n
= goto_map
[symbol
+ 1];
1022 for (i
= m
; i
< n
; i
++)
1024 if (to_state
[i
] != default_state
)
1031 symno
= symbol
- ntokens
+ nstates
;
1033 froms
[symno
] = sp1
= sp
= NEW2(count
, short);
1034 tos
[symno
] = sp2
= NEW2(count
, short);
1036 for (i
= m
; i
< n
; i
++)
1038 if (to_state
[i
] != default_state
)
1040 *sp1
++ = from_state
[i
];
1041 *sp2
++ = to_state
[i
];
1045 tally
[symno
] = count
;
1046 width
[symno
] = sp1
[-1] - sp
[0] + 1;
1051 /* The next few functions decide how to pack the actions and gotos
1052 information into yytable. */
1063 order
= NEW2(nvectors
, short);
1066 for (i
= 0; i
< nvectors
; i
++)
1074 while (j
>= 0 && (width
[order
[j
]] < w
))
1077 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
1080 for (k
= nentries
- 1; k
> j
; k
--)
1081 order
[k
+ 1] = order
[k
];
1097 base
= NEW2(nvectors
, short);
1098 pos
= NEW2(nentries
, short);
1099 table
= NEW2(MAXTABLE
, short);
1100 check
= NEW2(MAXTABLE
, short);
1105 for (i
= 0; i
< nvectors
; i
++)
1108 for (i
= 0; i
< MAXTABLE
; i
++)
1111 for (i
= 0; i
< nentries
; i
++)
1113 state
= matching_state(i
);
1116 place
= pack_vector(i
);
1118 place
= base
[state
];
1121 base
[order
[i
]] = place
;
1124 for (i
= 0; i
< nvectors
; i
++)
1140 matching_state (int vector
)
1157 for (prev
= vector
- 1; prev
>= 0; prev
--)
1160 if (width
[j
] != w
|| tally
[j
] != t
)
1164 for (k
= 0; match
&& k
< t
; k
++)
1166 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
1180 pack_vector (int vector
)
1186 register int loc
= 0;
1188 register short *from
;
1195 berror("pack_vector");
1200 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
1204 for (k
= 0; ok
&& k
< t
; k
++)
1208 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
1210 if (table
[loc
] != 0)
1214 for (k
= 0; ok
&& k
< vector
; k
++)
1222 for (k
= 0; k
< t
; k
++)
1226 check
[loc
] = from
[k
];
1229 while (table
[lowzero
] != 0)
1239 berror("pack_vector");
1240 return 0; /* JF keep lint happy */
1245 /* the following functions output yytable, yycheck
1246 and the vectors whose elements index the portion starts */
1254 fprintf(ftable
, "\nstatic const short yypact[] = {%6d", base
[0]);
1257 for (i
= 1; i
< nstates
; i
++)
1271 fprintf(ftable
, "%6d", base
[i
]);
1274 fprintf(ftable
, "\n};\n\nstatic const short yypgoto[] = {%6d", base
[nstates
]);
1277 for (i
= nstates
+ 1; i
< nvectors
; i
++)
1291 fprintf(ftable
, "%6d", base
[i
]);
1294 fprintf(ftable
, "\n};\n");
1305 fprintf(ftable
, "\n\n#define\tYYLAST\t\t%d\n\n", high
);
1306 fprintf(ftable
, "\nstatic const short yytable[] = {%6d", table
[0]);
1309 for (i
= 1; i
<= high
; i
++)
1323 fprintf(ftable
, "%6d", table
[i
]);
1326 fprintf(ftable
, "\n};\n");
1337 fprintf(ftable
, "\nstatic const short yycheck[] = {%6d", check
[0]);
1340 for (i
= 1; i
<= high
; i
++)
1354 fprintf(ftable
, "%6d", check
[i
]);
1357 fprintf(ftable
, "\n};\n");
1363 /* copy the parser code into the ftable file at the end. */
1366 output_parser (void)
1372 #define fpars fparser
1376 fprintf(ftable
, "#define YYPURE 1\n\n");
1378 #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1379 currently open parser from bison.simple to bison.hairy */
1380 if (semantic_parser
)
1382 else fpars
= fparser1
;
1385 /* Loop over lines in the standard parser file. */
1393 /* See if the line starts with `#line.
1394 If so, set write_line to 0. */
1411 fprintf(ftable
, "#lin");
1414 fprintf(ftable
, "#li");
1417 fprintf(ftable
, "#l");
1420 fprintf(ftable
, "#");
1423 /* now write out the line... */
1424 for (; c
!= '\n' && c
!= EOF
; c
= getc(fpars
))
1428 /* `$' in the parser file indicates where to put the actions.
1429 Copy them in at this point. */
1431 for(c
=getc(faction
);c
!=EOF
;c
=getc(faction
))
1444 output_program (void)
1449 fprintf(ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1461 free_itemsets (void)
1463 register core
*cp
,*cptmp
;
1467 for (cp
= first_state
; cp
; cp
= cptmp
)
1478 register shifts
*sp
,*sptmp
;/* JF derefrenced freed ptr */
1482 for (sp
= first_shift
; sp
; sp
= sptmp
)
1491 free_reductions (void)
1493 register reductions
*rp
,*rptmp
;/* JF fixed freed ptr */
1495 FREE(reduction_table
);
1497 for (rp
= first_reduction
; rp
; rp
= rptmp
)