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 ??
112 #include "complain.h"
116 extern int *user_toknums
;
117 extern int tokensetsize
;
118 extern int final_state
;
119 extern core
**state_table
;
120 extern shifts
**shift_table
;
121 extern errs
**err_table
;
122 extern reductions
**reduction_table
;
123 extern short *accessing_symbol
;
125 extern short *LAruleno
;
126 extern short *lookaheads
;
127 extern char *consistent
;
128 extern short *goto_map
;
129 extern short *from_state
;
130 extern short *to_state
;
132 extern void output_headers
PARAMS ((void));
133 extern void output_trailers
PARAMS ((void));
134 extern void output
PARAMS ((void));
136 static void output_token_translations
PARAMS ((void));
137 static void output_gram
PARAMS ((void));
138 static void output_stos
PARAMS ((void));
139 static void output_rule_data
PARAMS ((void));
140 static void output_defines
PARAMS ((void));
141 static void output_actions
PARAMS ((void));
142 static void token_actions
PARAMS ((void));
143 static void save_row
PARAMS ((int));
144 static void goto_actions
PARAMS ((void));
145 static void save_column
PARAMS ((int, int));
146 static void sort_actions
PARAMS ((void));
147 static void pack_table
PARAMS ((void));
148 static void output_base
PARAMS ((void));
149 static void output_table
PARAMS ((void));
150 static void output_check
PARAMS ((void));
151 static void output_parser
PARAMS ((void));
152 static void output_program
PARAMS ((void));
153 static void free_shifts
PARAMS ((void));
154 static void free_reductions
PARAMS ((void));
155 static void free_itemsets
PARAMS ((void));
156 static int action_row
PARAMS ((int));
157 static int default_goto
PARAMS ((int));
158 static int matching_state
PARAMS ((int));
159 static int pack_vector
PARAMS ((int));
161 extern void berror
PARAMS ((const char *));
162 extern void reader_output_yylsp
PARAMS ((FILE *));
166 static short **froms
;
170 static short *actrow
;
171 static short *state_count
;
185 extern int yyerror;\n\
186 extern int yycost;\n\
187 extern char * yymsg;\n\
188 extern YYSTYPE yyval;\n\
190 yyguard(n, yyvsp, yylsp)\n\
192 register YYSTYPE *yyvsp;\n\
193 register YYLTYPE *yylsp;\n\
204 extern YYSTYPE yyval;\n\
205 extern int yychar;\n\
207 yyaction(n, yyvsp, yylsp)\n\
209 register YYSTYPE *yyvsp;\n\
210 register YYLTYPE *yylsp;\n\
215 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
219 output_headers (void)
222 fprintf (fguard
, GUARDSTR
, attrsfile
);
227 fprintf (faction
, (semantic_parser
? ACTSTR
: ACTSTR_SIMPLE
), attrsfile
);
228 /* if (semantic_parser) JF moved this below
229 fprintf(ftable, "#include \"%s\"\n", attrsfile);
230 fprintf(ftable, "#include <stdio.h>\n\n");
233 /* Rename certain symbols if -p was specified. */
234 if (spec_name_prefix
)
236 fprintf (ftable
, "#define yyparse %sparse\n", spec_name_prefix
);
237 fprintf (ftable
, "#define yylex %slex\n", spec_name_prefix
);
238 fprintf (ftable
, "#define yyerror %serror\n", spec_name_prefix
);
239 fprintf (ftable
, "#define yylval %slval\n", spec_name_prefix
);
240 fprintf (ftable
, "#define yychar %schar\n", spec_name_prefix
);
241 fprintf (ftable
, "#define yydebug %sdebug\n", spec_name_prefix
);
242 fprintf (ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
);
248 output_trailers (void)
251 fprintf (fguard
, "\n }\n}\n");
253 fprintf (faction
, "\n");
259 fprintf (faction
, " }\n");
260 fprintf (faction
, "}\n");
269 /* output_token_defines(ftable); / * JF put out token defines FIRST */
270 if (!semantic_parser
) /* JF Put out other stuff */
273 while ((c
= getc (fattrs
)) != EOF
)
276 reader_output_yylsp (ftable
);
286 fprintf (ftable
, "#include \"%s\"\n", attrsfile
);
289 fprintf (ftable
, "#include <stdio.h>\n\n");
291 /* Make "const" do nothing if not in ANSI C. */
293 #ifndef __cplusplus\n\
303 output_token_translations ();
304 /* if (semantic_parser) */
305 /* This is now unconditional because debugging printouts can use it. */
319 output_token_translations (void)
322 /* register short *sp; JF unused */
327 "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
328 max_user_token_number
, nsyms
);
330 if (ntokens
< 127) /* play it very safe; check maximum element value. */
331 fprintf (ftable
, "\nstatic const char yytranslate[] = { 0");
333 fprintf (ftable
, "\nstatic const short yytranslate[] = { 0");
336 for (i
= 1; i
<= max_user_token_number
; i
++)
350 fprintf (ftable
, "%6d", token_translations
[i
]);
353 fprintf (ftable
, "\n};\n");
357 fprintf (ftable
, "\n#define YYTRANSLATE(x) (x)\n");
369 /* With the ordinary parser,
370 yyprhs and yyrhs are needed only for yydebug. */
371 /* With the noparser option, all tables are generated */
372 if (!semantic_parser
&& !noparserflag
)
373 fprintf (ftable
, "\n#if YYDEBUG != 0");
375 fprintf (ftable
, "\nstatic const short yyprhs[] = { 0");
378 for (i
= 1; i
<= nrules
; i
++)
392 fprintf (ftable
, "%6d", rrhs
[i
]);
395 fprintf (ftable
, "\n};\n");
397 fprintf (ftable
, "\nstatic const short yyrhs[] = {%6d", ritem
[0]);
400 for (sp
= ritem
+ 1; *sp
; sp
++)
415 fprintf (ftable
, "%6d", *sp
);
417 fprintf (ftable
, " 0");
420 fprintf (ftable
, "\n};\n");
422 if (!semantic_parser
&& !noparserflag
)
423 fprintf (ftable
, "\n#endif\n");
433 fprintf (ftable
, "\nstatic const short yystos[] = { 0");
436 for (i
= 1; i
< nstates
; i
++)
450 fprintf (ftable
, "%6d", accessing_symbol
[i
]);
453 fprintf (ftable
, "\n};\n");
458 output_rule_data (void)
465 /* YYRLINE[yyn]: source line where rule number YYN was defined. */\n\
466 static const short yyrline[] = { 0", ftable
);
469 for (i
= 1; i
<= nrules
; i
++)
483 fprintf (ftable
, "%6d", rline
[i
]);
485 fprintf (ftable
, "\n};\n#endif\n\n");
487 if (toknumflag
|| noparserflag
)
489 fprintf (ftable
, "#define YYNTOKENS %d\n", ntokens
);
490 fprintf (ftable
, "#define YYNNTS %d\n", nvars
);
491 fprintf (ftable
, "#define YYNRULES %d\n", nrules
);
492 fprintf (ftable
, "#define YYNSTATES %d\n", nstates
);
493 fprintf (ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
496 if (!toknumflag
&& !noparserflag
)
497 fprintf (ftable
, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
499 /* Output the table of symbol names. */
502 "static const char * const yytname[] = { \"%s\"", tags
[0]);
504 j
= strlen (tags
[0]) + 44;
505 for (i
= 1; i
< nsyms
; i
++)
506 /* this used to be i<=nsyms, but that output a final "" symbol
507 almost by accident */
522 for (p
= tags
[i
]; p
&& *p
; p
++)
524 if (*p
== '"' || *p
== '\\')
526 fprintf (ftable
, "\\%c", *p
);
531 fprintf (ftable
, "\\n");
536 fprintf (ftable
, "\\t");
541 fprintf (ftable
, "\\b");
544 else if (*p
< 040 || *p
>= 0177)
546 fprintf (ftable
, "\\%03o", *p
);
559 /* add a NULL entry to list of tokens */
560 fprintf (ftable
, ", NULL\n};\n");
562 if (!toknumflag
&& !noparserflag
)
563 fprintf (ftable
, "#endif\n\n");
565 /* Output YYTOKNUM. */
568 fprintf (ftable
, "static const short yytoknum[] = { 0");
570 for (i
= 1; i
<= ntokens
; i
++)
580 fprintf (ftable
, "%6d", user_toknums
[i
]);
582 fprintf (ftable
, "\n};\n\n");
587 /* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n\
588 static const short yyr1[] = { 0", ftable
);
591 for (i
= 1; i
<= nrules
; i
++)
605 fprintf (ftable
, "%6d", rlhs
[i
]);
614 /* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\
615 static const short yyr2[] = { 0", ftable
);
617 for (i
= 1; i
< nrules
; i
++)
631 fprintf (ftable
, "%6d", rrhs
[i
+ 1] - rrhs
[i
] - 1);
638 fprintf (ftable
, "%6d\n};\n", nitems
- rrhs
[nrules
] - 1);
644 output_defines (void)
646 fprintf (ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
647 fprintf (ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
648 fprintf (ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
653 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */
656 output_actions (void)
658 nvectors
= nstates
+ nvars
;
660 froms
= NEW2 (nvectors
, short *);
661 tos
= NEW2 (nvectors
, short *);
662 tally
= NEW2 (nvectors
, short);
663 width
= NEW2 (nvectors
, short);
671 FREE (accessing_symbol
);
674 FREE (goto_map
+ ntokens
);
687 /* Figure out the actions for the specified state, indexed by
688 lookahead token type.
690 The yydefact table is output now. The detailed info is saved for
691 putting into yytable later. */
700 actrow
= NEW2 (ntokens
, short);
703 fprintf (ftable
, "\nstatic const short yydefact[] = {%6d", k
);
707 for (i
= 1; i
< nstates
; i
++)
722 fprintf (ftable
, "%6d", k
);
726 fprintf (ftable
, "\n};\n");
732 /* Decide what to do for each type of token if seen as the lookahead
733 token in specified state. The value returned is used as the
734 default action (yydefact) for the state. In addition, actrow is
735 filled with what to do for each kind of token, index by symbol
736 number, with zero meaning do the default action. The value
737 MINSHORT, a very negative number, means this situation is an error.
738 The parser recognizes this value specially.
740 This is where conflicts are resolved. The loop over lookahead
741 rules considered lower-numbered rules last, and the last rule
742 considered that likes a token gets to handle it. */
745 action_row (int state
)
753 register int default_rule
;
757 register int shift_state
;
759 register unsigned mask
;
760 register unsigned *wordp
;
761 register reductions
*redp
;
762 register shifts
*shiftp
;
764 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
766 for (i
= 0; i
< ntokens
; i
++)
771 redp
= reduction_table
[state
];
779 /* loop over all the rules available here which require
781 m
= lookaheads
[state
];
782 n
= lookaheads
[state
+ 1];
784 for (i
= n
- 1; i
>= m
; i
--)
787 wordp
= LA
+ i
* tokensetsize
;
790 /* and find each token which the rule finds acceptable
792 for (j
= 0; j
< ntokens
; j
++)
794 /* and record this rule as the rule to use if that
810 shiftp
= shift_table
[state
];
812 /* Now see which tokens are allowed for shifts in this state. For
813 them, record the shift as the thing to do. So shift is preferred
820 for (i
= 0; i
< k
; i
++)
822 shift_state
= shiftp
->shifts
[i
];
826 symbol
= accessing_symbol
[shift_state
];
831 actrow
[symbol
] = shift_state
;
833 /* Do not use any default reduction if there is a shift for
835 if (symbol
== error_token_number
)
840 errp
= err_table
[state
];
842 /* See which tokens are an explicit error in this state (due to
843 %nonassoc). For them, record MINSHORT as the action. */
849 for (i
= 0; i
< k
; i
++)
851 symbol
= errp
->errs
[i
];
852 actrow
[symbol
] = MINSHORT
;
856 /* Now find the most common reduction and make it the default action
859 if (nreds
>= 1 && !nodefault
)
861 if (consistent
[state
])
862 default_rule
= redp
->rules
[0];
866 for (i
= m
; i
< n
; i
++)
871 for (j
= 0; j
< ntokens
; j
++)
873 if (actrow
[j
] == rule
)
884 /* actions which match the default are replaced with zero,
885 which means "use the default" */
889 for (j
= 0; j
< ntokens
; j
++)
891 if (actrow
[j
] == default_rule
)
895 default_rule
= -default_rule
;
900 /* If have no default rule, the default is an error.
901 So replace any action which says "error" with "use default". */
903 if (default_rule
== 0)
904 for (j
= 0; j
< ntokens
; j
++)
906 if (actrow
[j
] == MINSHORT
)
924 for (i
= 0; i
< ntokens
; i
++)
933 froms
[state
] = sp1
= sp
= NEW2 (count
, short);
934 tos
[state
] = sp2
= NEW2 (count
, short);
936 for (i
= 0; i
< ntokens
; i
++)
945 tally
[state
] = count
;
946 width
[state
] = sp1
[-1] - sp
[0] + 1;
951 /* figure out what to do after reducing with each rule,
952 depending on the saved state from before the beginning
953 of parsing the data that matched this rule.
955 The yydefgoto table is output now. The detailed info
956 is saved for putting into yytable later. */
965 state_count
= NEW2 (nstates
, short);
967 k
= default_goto (ntokens
);
968 fprintf (ftable
, "\nstatic const short yydefgoto[] = {%6d", k
);
969 save_column (ntokens
, k
);
972 for (i
= ntokens
+ 1; i
< nsyms
; i
++)
986 k
= default_goto (i
);
987 fprintf (ftable
, "%6d", k
);
991 fprintf (ftable
, "\n};\n");
998 default_goto (int symbol
)
1003 register int default_state
;
1006 m
= goto_map
[symbol
];
1007 n
= goto_map
[symbol
+ 1];
1012 for (i
= 0; i
< nstates
; i
++)
1015 for (i
= m
; i
< n
; i
++)
1016 state_count
[to_state
[i
]]++;
1021 for (i
= 0; i
< nstates
; i
++)
1023 if (state_count
[i
] > max
)
1025 max
= state_count
[i
];
1030 return default_state
;
1035 save_column (int symbol
, int default_state
)
1041 register short *sp1
;
1042 register short *sp2
;
1046 m
= goto_map
[symbol
];
1047 n
= goto_map
[symbol
+ 1];
1050 for (i
= m
; i
< n
; i
++)
1052 if (to_state
[i
] != default_state
)
1059 symno
= symbol
- ntokens
+ nstates
;
1061 froms
[symno
] = sp1
= sp
= NEW2 (count
, short);
1062 tos
[symno
] = sp2
= NEW2 (count
, short);
1064 for (i
= m
; i
< n
; i
++)
1066 if (to_state
[i
] != default_state
)
1068 *sp1
++ = from_state
[i
];
1069 *sp2
++ = to_state
[i
];
1073 tally
[symno
] = count
;
1074 width
[symno
] = sp1
[-1] - sp
[0] + 1;
1079 /* The next few functions decide how to pack the actions and gotos
1080 information into yytable. */
1091 order
= NEW2 (nvectors
, short);
1094 for (i
= 0; i
< nvectors
; i
++)
1102 while (j
>= 0 && (width
[order
[j
]] < w
))
1105 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
1108 for (k
= nentries
- 1; k
> j
; k
--)
1109 order
[k
+ 1] = order
[k
];
1125 base
= NEW2 (nvectors
, short);
1126 pos
= NEW2 (nentries
, short);
1127 table
= NEW2 (MAXTABLE
, short);
1128 check
= NEW2 (MAXTABLE
, short);
1133 for (i
= 0; i
< nvectors
; i
++)
1136 for (i
= 0; i
< MAXTABLE
; i
++)
1139 for (i
= 0; i
< nentries
; i
++)
1141 state
= matching_state (i
);
1144 place
= pack_vector (i
);
1146 place
= base
[state
];
1149 base
[order
[i
]] = place
;
1152 for (i
= 0; i
< nvectors
; i
++)
1168 matching_state (int vector
)
1185 for (prev
= vector
- 1; prev
>= 0; prev
--)
1188 if (width
[j
] != w
|| tally
[j
] != t
)
1192 for (k
= 0; match
&& k
< t
; k
++)
1194 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
1208 pack_vector (int vector
)
1214 register int loc
= 0;
1216 register short *from
;
1223 berror ("pack_vector");
1228 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
1232 for (k
= 0; ok
&& k
< t
; k
++)
1236 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
1238 if (table
[loc
] != 0)
1242 for (k
= 0; ok
&& k
< vector
; k
++)
1250 for (k
= 0; k
< t
; k
++)
1254 check
[loc
] = from
[k
];
1257 while (table
[lowzero
] != 0)
1267 berror ("pack_vector");
1268 return 0; /* JF keep lint happy */
1273 /* the following functions output yytable, yycheck
1274 and the vectors whose elements index the portion starts */
1282 fprintf (ftable
, "\nstatic const short yypact[] = {%6d", base
[0]);
1285 for (i
= 1; i
< nstates
; i
++)
1291 putc ('\n', ftable
);
1299 fprintf (ftable
, "%6d", base
[i
]);
1302 fprintf (ftable
, "\n};\n\nstatic const short yypgoto[] = {%6d",
1306 for (i
= nstates
+ 1; i
< nvectors
; i
++)
1312 putc ('\n', ftable
);
1320 fprintf (ftable
, "%6d", base
[i
]);
1323 fprintf (ftable
, "\n};\n");
1334 fprintf (ftable
, "\n\n#define\tYYLAST\t\t%d\n\n", high
);
1335 fprintf (ftable
, "\nstatic const short yytable[] = {%6d", table
[0]);
1338 for (i
= 1; i
<= high
; i
++)
1344 putc ('\n', ftable
);
1352 fprintf (ftable
, "%6d", table
[i
]);
1355 fprintf (ftable
, "\n};\n");
1366 fprintf (ftable
, "\nstatic const short yycheck[] = {%6d", check
[0]);
1369 for (i
= 1; i
<= high
; i
++)
1375 putc ('\n', ftable
);
1383 fprintf (ftable
, "%6d", check
[i
]);
1386 fprintf (ftable
, "\n};\n");
1392 /* copy the parser code into the ftable file at the end. */
1395 output_parser (void)
1401 #define fpars fparser
1405 fprintf (ftable
, "#define YYPURE 1\n\n");
1407 #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1408 currently open parser from bison.simple to bison.hairy */
1409 if (semantic_parser
)
1415 /* Loop over lines in the standard parser file. */
1423 /* See if the line starts with `#line.
1424 If so, set write_line to 0. */
1441 fprintf (ftable
, "#lin");
1444 fprintf (ftable
, "#li");
1447 fprintf (ftable
, "#l");
1450 fprintf (ftable
, "#");
1453 /* now write out the line... */
1454 for (; c
!= '\n' && c
!= EOF
; c
= getc (fpars
))
1459 /* `$' in the parser file indicates where to put the actions.
1460 Copy them in at this point. */
1462 for (c
= getc (faction
); c
!= EOF
; c
= getc (faction
))
1475 output_program (void)
1480 fprintf (ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1492 free_itemsets (void)
1494 register core
*cp
, *cptmp
;
1498 for (cp
= first_state
; cp
; cp
= cptmp
)
1509 register shifts
*sp
, *sptmp
; /* JF derefrenced freed ptr */
1513 for (sp
= first_shift
; sp
; sp
= sptmp
)
1522 free_reductions (void)
1524 register reductions
*rp
, *rptmp
; /* JF fixed freed ptr */
1526 FREE (reduction_table
);
1528 for (rp
= first_reduction
; rp
; rp
= rptmp
)