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 void output_headers
PARAMS((void));
139 void output_trailers
PARAMS((void));
140 void output
PARAMS((void));
141 void output_token_translations
PARAMS((void));
142 void output_gram
PARAMS((void));
143 void output_stos
PARAMS((void));
144 void output_rule_data
PARAMS((void));
145 void output_defines
PARAMS((void));
146 void output_actions
PARAMS((void));
147 void token_actions
PARAMS((void));
148 void save_row
PARAMS((int));
149 void goto_actions
PARAMS((void));
150 void save_column
PARAMS((int, int));
151 void sort_actions
PARAMS((void));
152 void pack_table
PARAMS((void));
153 void output_base
PARAMS((void));
154 void output_table
PARAMS((void));
155 void output_check
PARAMS((void));
156 void output_parser
PARAMS((void));
157 void output_program
PARAMS((void));
158 void free_shifts
PARAMS((void));
159 void free_reductions
PARAMS((void));
160 void free_itemsets
PARAMS((void));
161 int action_row
PARAMS((int));
162 int default_goto
PARAMS((int));
163 int matching_state
PARAMS((int));
164 int pack_vector
PARAMS((int));
166 extern void berror
PARAMS((char *));
167 extern void reader_output_yylsp
PARAMS((FILE *));
171 static short **froms
;
175 static short *actrow
;
176 static short *state_count
;
187 #define GUARDSTR "\n#include \"%s\"\nextern int yyerror;\n\
188 extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
189 yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
190 register YYLTYPE *yylsp;\n\
191 {\n yyerror = 0;\nyycost = 0;\n yymsg = 0;\nswitch (n)\n {"
193 #define ACTSTR "\n#include \"%s\"\nextern YYSTYPE yyval;\
194 \nextern int yychar;\
195 yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
196 register YYLTYPE *yylsp;\n{\n switch (n)\n{"
198 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
202 output_headers (void)
205 fprintf(fguard
, GUARDSTR
, attrsfile
);
210 fprintf(faction
, (semantic_parser
? ACTSTR
: ACTSTR_SIMPLE
), attrsfile
);
211 /* if (semantic_parser) JF moved this below
212 fprintf(ftable, "#include \"%s\"\n", attrsfile);
213 fprintf(ftable, "#include <stdio.h>\n\n");
216 /* Rename certain symbols if -p was specified. */
217 if (spec_name_prefix
)
219 fprintf(ftable
, "#define yyparse %sparse\n", spec_name_prefix
);
220 fprintf(ftable
, "#define yylex %slex\n", spec_name_prefix
);
221 fprintf(ftable
, "#define yyerror %serror\n", spec_name_prefix
);
222 fprintf(ftable
, "#define yylval %slval\n", spec_name_prefix
);
223 fprintf(ftable
, "#define yychar %schar\n", spec_name_prefix
);
224 fprintf(ftable
, "#define yydebug %sdebug\n", spec_name_prefix
);
225 fprintf(ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
);
231 output_trailers (void)
234 fprintf(fguard
, "\n }\n}\n");
236 fprintf(faction
, "\n");
242 fprintf(faction
, " }\n");
243 fprintf(faction
, "}\n");
252 /* output_token_defines(ftable); / * JF put out token defines FIRST */
253 if (!semantic_parser
) /* JF Put out other stuff */
256 while ((c
=getc(fattrs
))!=EOF
)
259 reader_output_yylsp(ftable
);
261 fprintf(ftable
, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n",
265 fprintf(ftable
, "#include \"%s\"\n", attrsfile
);
268 fprintf(ftable
, "#include <stdio.h>\n\n");
270 /* Make "const" do nothing if not in ANSI C. */
271 fprintf (ftable
, "#ifndef __cplusplus\n#ifndef __STDC__\n#define const\n#endif\n#endif\n\n");
275 output_token_translations();
276 /* if (semantic_parser) */
277 /* This is now unconditional because debugging printouts can use it. */
291 output_token_translations (void)
294 /* register short *sp; JF unused */
299 "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
300 max_user_token_number
, nsyms
);
302 if (ntokens
< 127) /* play it very safe; check maximum element value. */
303 fprintf(ftable
, "\nstatic const char yytranslate[] = { 0");
305 fprintf(ftable
, "\nstatic const short yytranslate[] = { 0");
308 for (i
= 1; i
<= max_user_token_number
; i
++)
322 fprintf(ftable
, "%6d", token_translations
[i
]);
325 fprintf(ftable
, "\n};\n");
329 fprintf(ftable
, "\n#define YYTRANSLATE(x) (x)\n");
341 /* With the ordinary parser,
342 yyprhs and yyrhs are needed only for yydebug. */
343 /* With the noparser option, all tables are generated */
344 if (! semantic_parser
&& ! noparserflag
)
345 fprintf(ftable
, "\n#if YYDEBUG != 0");
347 fprintf(ftable
, "\nstatic const short yyprhs[] = { 0");
350 for (i
= 1; i
<= nrules
; i
++)
364 fprintf(ftable
, "%6d", rrhs
[i
]);
367 fprintf(ftable
, "\n};\n");
369 fprintf(ftable
, "\nstatic const short yyrhs[] = {%6d", ritem
[0]);
372 for (sp
= ritem
+ 1; *sp
; sp
++)
387 fprintf(ftable
, "%6d", *sp
);
389 fprintf(ftable
, " 0");
392 fprintf(ftable
, "\n};\n");
394 if (! semantic_parser
&& ! noparserflag
)
395 fprintf(ftable
, "\n#endif\n");
405 fprintf(ftable
, "\nstatic const short yystos[] = { 0");
408 for (i
= 1; i
< nstates
; i
++)
422 fprintf(ftable
, "%6d", accessing_symbol
[i
]);
425 fprintf(ftable
, "\n};\n");
430 output_rule_data (void)
437 /* YYRLINE[yyn]: source line where rule number YYN was defined. */\n\
438 static const short yyrline[] = { 0", ftable
);
441 for (i
= 1; i
<= nrules
; i
++)
455 fprintf(ftable
, "%6d", rline
[i
]);
457 fprintf(ftable
, "\n};\n#endif\n\n");
459 if (toknumflag
|| noparserflag
)
461 fprintf(ftable
, "#define YYNTOKENS %d\n", ntokens
);
462 fprintf(ftable
, "#define YYNNTS %d\n", nvars
);
463 fprintf(ftable
, "#define YYNRULES %d\n", nrules
);
464 fprintf(ftable
, "#define YYNSTATES %d\n", nstates
);
465 fprintf(ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
468 if (! toknumflag
&& ! noparserflag
)
469 fprintf(ftable
, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
471 /* Output the table of symbol names. */
474 "static const char * const yytname[] = { \"%s\"",
477 j
= strlen (tags
[0]) + 44;
478 for (i
= 1; i
< nsyms
; i
++)
479 /* this used to be i<=nsyms, but that output a final "" symbol
480 almost by accident */
495 for (p
= tags
[i
]; p
&& *p
; p
++)
497 if (*p
== '"' || *p
== '\\')
499 fprintf(ftable
, "\\%c", *p
);
504 fprintf(ftable
, "\\n");
509 fprintf(ftable
, "\\t");
514 fprintf(ftable
, "\\b");
517 else if (*p
< 040 || *p
>= 0177)
519 fprintf(ftable
, "\\%03o", *p
);
532 /* add a NULL entry to list of tokens */
533 fprintf (ftable
, ", NULL\n};\n");
535 if (! toknumflag
&& ! noparserflag
)
536 fprintf (ftable
, "#endif\n\n");
538 /* Output YYTOKNUM. */
541 fprintf(ftable
, "static const short yytoknum[] = { 0");
543 for (i
= 1; i
<= ntokens
; i
++) {
552 fprintf(ftable
, "%6d", user_toknums
[i
]);
554 fprintf(ftable
, "\n};\n\n");
559 /* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n\
560 static const short yyr1[] = { 0", ftable
);
563 for (i
= 1; i
<= nrules
; i
++)
577 fprintf(ftable
, "%6d", rlhs
[i
]);
586 /* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\
587 static const short yyr2[] = { 0", ftable
);
589 for (i
= 1; i
< nrules
; i
++)
603 fprintf(ftable
, "%6d", rrhs
[i
+ 1] - rrhs
[i
] - 1);
610 fprintf(ftable
, "%6d\n};\n", nitems
- rrhs
[nrules
] - 1);
616 output_defines (void)
618 fprintf(ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
619 fprintf(ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
620 fprintf(ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
625 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */
628 output_actions (void)
630 nvectors
= nstates
+ nvars
;
632 froms
= NEW2(nvectors
, short *);
633 tos
= NEW2(nvectors
, short *);
634 tally
= NEW2(nvectors
, short);
635 width
= NEW2(nvectors
, short);
643 FREE(accessing_symbol
);
646 FREE(goto_map
+ ntokens
);
659 /* figure out the actions for the specified state, indexed by lookahead token type.
661 The yydefact table is output now. The detailed info
662 is saved for putting into yytable later. */
671 actrow
= NEW2(ntokens
, short);
674 fprintf(ftable
, "\nstatic const short yydefact[] = {%6d", k
);
678 for (i
= 1; i
< nstates
; i
++)
693 fprintf(ftable
, "%6d", k
);
697 fprintf(ftable
, "\n};\n");
703 /* Decide what to do for each type of token if seen as the lookahead token in specified state.
704 The value returned is used as the default action (yydefact) for the state.
705 In addition, actrow is filled with what to do for each kind of token,
706 index by symbol number, with zero meaning do the default action.
707 The value MINSHORT, a very negative number, means this situation
708 is an error. The parser recognizes this value specially.
710 This is where conflicts are resolved. The loop over lookahead rules
711 considered lower-numbered rules last, and the last rule considered that likes
712 a token gets to handle it. */
715 action_row (int state
)
723 register int default_rule
;
727 register int shift_state
;
729 register unsigned mask
;
730 register unsigned *wordp
;
731 register reductions
*redp
;
732 register shifts
*shiftp
;
734 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
736 for (i
= 0; i
< ntokens
; i
++)
741 redp
= reduction_table
[state
];
749 /* loop over all the rules available here which require lookahead */
750 m
= lookaheads
[state
];
751 n
= lookaheads
[state
+ 1];
753 for (i
= n
- 1; i
>= m
; i
--)
755 rule
= - LAruleno
[i
];
756 wordp
= LA
+ i
* tokensetsize
;
759 /* and find each token which the rule finds acceptable to come next */
760 for (j
= 0; j
< ntokens
; j
++)
762 /* and record this rule as the rule to use if that token follows. */
777 shiftp
= shift_table
[state
];
779 /* now see which tokens are allowed for shifts in this state.
780 For them, record the shift as the thing to do. So shift is preferred to reduce. */
786 for (i
= 0; i
< k
; i
++)
788 shift_state
= shiftp
->shifts
[i
];
789 if (! shift_state
) continue;
791 symbol
= accessing_symbol
[shift_state
];
796 actrow
[symbol
] = shift_state
;
798 /* do not use any default reduction if there is a shift for error */
800 if (symbol
== error_token_number
) nodefault
= 1;
804 errp
= err_table
[state
];
806 /* See which tokens are an explicit error in this state
807 (due to %nonassoc). For them, record MINSHORT as the action. */
813 for (i
= 0; i
< k
; i
++)
815 symbol
= errp
->errs
[i
];
816 actrow
[symbol
] = MINSHORT
;
820 /* now find the most common reduction and make it the default action for this state. */
822 if (nreds
>= 1 && ! nodefault
)
824 if (consistent
[state
])
825 default_rule
= redp
->rules
[0];
829 for (i
= m
; i
< n
; i
++)
832 rule
= - LAruleno
[i
];
834 for (j
= 0; j
< ntokens
; j
++)
836 if (actrow
[j
] == rule
)
847 /* actions which match the default are replaced with zero,
848 which means "use the default" */
852 for (j
= 0; j
< ntokens
; j
++)
854 if (actrow
[j
] == default_rule
)
858 default_rule
= - default_rule
;
863 /* If have no default rule, the default is an error.
864 So replace any action which says "error" with "use default". */
866 if (default_rule
== 0)
867 for (j
= 0; j
< ntokens
; j
++)
869 if (actrow
[j
] == MINSHORT
)
873 return (default_rule
);
887 for (i
= 0; i
< ntokens
; i
++)
896 froms
[state
] = sp1
= sp
= NEW2(count
, short);
897 tos
[state
] = sp2
= NEW2(count
, short);
899 for (i
= 0; i
< ntokens
; i
++)
908 tally
[state
] = count
;
909 width
[state
] = sp1
[-1] - sp
[0] + 1;
914 /* figure out what to do after reducing with each rule,
915 depending on the saved state from before the beginning
916 of parsing the data that matched this rule.
918 The yydefgoto table is output now. The detailed info
919 is saved for putting into yytable later. */
928 state_count
= NEW2(nstates
, short);
930 k
= default_goto(ntokens
);
931 fprintf(ftable
, "\nstatic const short yydefgoto[] = {%6d", k
);
932 save_column(ntokens
, k
);
935 for (i
= ntokens
+ 1; i
< nsyms
; i
++)
950 fprintf(ftable
, "%6d", k
);
954 fprintf(ftable
, "\n};\n");
961 default_goto (int symbol
)
966 register int default_state
;
969 m
= goto_map
[symbol
];
970 n
= goto_map
[symbol
+ 1];
975 for (i
= 0; i
< nstates
; i
++)
978 for (i
= m
; i
< n
; i
++)
979 state_count
[to_state
[i
]]++;
984 for (i
= 0; i
< nstates
; i
++)
986 if (state_count
[i
] > max
)
988 max
= state_count
[i
];
993 return (default_state
);
998 save_column (int symbol
, int default_state
)
1004 register short *sp1
;
1005 register short *sp2
;
1009 m
= goto_map
[symbol
];
1010 n
= goto_map
[symbol
+ 1];
1013 for (i
= m
; i
< n
; i
++)
1015 if (to_state
[i
] != default_state
)
1022 symno
= symbol
- ntokens
+ nstates
;
1024 froms
[symno
] = sp1
= sp
= NEW2(count
, short);
1025 tos
[symno
] = sp2
= NEW2(count
, short);
1027 for (i
= m
; i
< n
; i
++)
1029 if (to_state
[i
] != default_state
)
1031 *sp1
++ = from_state
[i
];
1032 *sp2
++ = to_state
[i
];
1036 tally
[symno
] = count
;
1037 width
[symno
] = sp1
[-1] - sp
[0] + 1;
1042 /* The next few functions decide how to pack the actions and gotos
1043 information into yytable. */
1054 order
= NEW2(nvectors
, short);
1057 for (i
= 0; i
< nvectors
; i
++)
1065 while (j
>= 0 && (width
[order
[j
]] < w
))
1068 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
1071 for (k
= nentries
- 1; k
> j
; k
--)
1072 order
[k
+ 1] = order
[k
];
1088 base
= NEW2(nvectors
, short);
1089 pos
= NEW2(nentries
, short);
1090 table
= NEW2(MAXTABLE
, short);
1091 check
= NEW2(MAXTABLE
, short);
1096 for (i
= 0; i
< nvectors
; i
++)
1099 for (i
= 0; i
< MAXTABLE
; i
++)
1102 for (i
= 0; i
< nentries
; i
++)
1104 state
= matching_state(i
);
1107 place
= pack_vector(i
);
1109 place
= base
[state
];
1112 base
[order
[i
]] = place
;
1115 for (i
= 0; i
< nvectors
; i
++)
1131 matching_state (int vector
)
1148 for (prev
= vector
- 1; prev
>= 0; prev
--)
1151 if (width
[j
] != w
|| tally
[j
] != t
)
1155 for (k
= 0; match
&& k
< t
; k
++)
1157 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
1171 pack_vector (int vector
)
1177 register int loc
= 0;
1179 register short *from
;
1186 berror("pack_vector");
1191 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
1195 for (k
= 0; ok
&& k
< t
; k
++)
1199 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
1201 if (table
[loc
] != 0)
1205 for (k
= 0; ok
&& k
< vector
; k
++)
1213 for (k
= 0; k
< t
; k
++)
1217 check
[loc
] = from
[k
];
1220 while (table
[lowzero
] != 0)
1230 berror("pack_vector");
1231 return 0; /* JF keep lint happy */
1236 /* the following functions output yytable, yycheck
1237 and the vectors whose elements index the portion starts */
1245 fprintf(ftable
, "\nstatic const short yypact[] = {%6d", base
[0]);
1248 for (i
= 1; i
< nstates
; i
++)
1262 fprintf(ftable
, "%6d", base
[i
]);
1265 fprintf(ftable
, "\n};\n\nstatic const short yypgoto[] = {%6d", base
[nstates
]);
1268 for (i
= nstates
+ 1; i
< nvectors
; i
++)
1282 fprintf(ftable
, "%6d", base
[i
]);
1285 fprintf(ftable
, "\n};\n");
1296 fprintf(ftable
, "\n\n#define\tYYLAST\t\t%d\n\n", high
);
1297 fprintf(ftable
, "\nstatic const short yytable[] = {%6d", table
[0]);
1300 for (i
= 1; i
<= high
; i
++)
1314 fprintf(ftable
, "%6d", table
[i
]);
1317 fprintf(ftable
, "\n};\n");
1328 fprintf(ftable
, "\nstatic const short yycheck[] = {%6d", check
[0]);
1331 for (i
= 1; i
<= high
; i
++)
1345 fprintf(ftable
, "%6d", check
[i
]);
1348 fprintf(ftable
, "\n};\n");
1354 /* copy the parser code into the ftable file at the end. */
1357 output_parser (void)
1363 #define fpars fparser
1367 fprintf(ftable
, "#define YYPURE 1\n\n");
1369 #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1370 currently open parser from bison.simple to bison.hairy */
1371 if (semantic_parser
)
1373 else fpars
= fparser1
;
1376 /* Loop over lines in the standard parser file. */
1384 /* See if the line starts with `#line.
1385 If so, set write_line to 0. */
1402 fprintf(ftable
, "#lin");
1405 fprintf(ftable
, "#li");
1408 fprintf(ftable
, "#l");
1411 fprintf(ftable
, "#");
1414 /* now write out the line... */
1415 for (; c
!= '\n' && c
!= EOF
; c
= getc(fpars
))
1419 /* `$' in the parser file indicates where to put the actions.
1420 Copy them in at this point. */
1422 for(c
=getc(faction
);c
!=EOF
;c
=getc(faction
))
1435 output_program (void)
1440 fprintf(ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1452 free_itemsets (void)
1454 register core
*cp
,*cptmp
;
1458 for (cp
= first_state
; cp
; cp
= cptmp
) {
1468 register shifts
*sp
,*sptmp
;/* JF derefrenced freed ptr */
1472 for (sp
= first_shift
; sp
; sp
= sptmp
) {
1480 free_reductions (void)
1482 register reductions
*rp
,*rptmp
;/* JF fixed freed ptr */
1484 FREE(reduction_table
);
1486 for (rp
= first_reduction
; rp
; rp
= rptmp
) {