]>
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, 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 /* The parser tables consist of these tables.
23 Starred ones needed only for the semantic parser.
24 Double starred are output only if switches are set.
26 yytranslate = vector mapping yylex's token numbers into bison's token
29 ** yytname = vector of string-names indexed by bison token number
31 ** yytoknum = vector of yylex token numbers corresponding to entries
34 yyrline = vector of line-numbers of all rules. For yydebug printouts.
36 yyrhs = vector of items of all rules.
37 This is exactly what ritems contains. For yydebug and for semantic
40 yyprhs[r] = index in yyrhs of first item for rule r.
42 yyr1[r] = symbol number of symbol that rule r derives.
44 yyr2[r] = number of symbols composing right hand side of rule r.
46 * yystos[s] = the symbol number of the symbol that leads to state s.
48 yydefact[s] = default rule to reduce with in state s,
49 when yytable doesn't specify something else to do.
50 Zero means the default is an error.
52 yydefgoto[i] = default state to go to after a reduction of a rule that
53 generates variable ntokens + i, except when yytable
54 specifies something else to do.
56 yypact[s] = index in yytable of the portion describing state s.
57 The lookahead token's type is used to index that portion
58 to find out what to do.
60 If the value in yytable is positive,
61 we shift the token and go to that state.
63 If the value is negative, it is minus a rule number to reduce by.
65 If the value is zero, the default action from yydefact[s] is used.
67 yypgoto[i] = the index in yytable of the portion describing
68 what to do after reducing a rule that derives variable i + ntokens.
69 This portion is indexed by the parser state number, s,
70 as of before the text for this nonterminal was read.
71 The value from yytable is the state to go to if
72 the corresponding value in yycheck is s.
74 yytable = a vector filled with portions for different uses,
75 found via yypact and yypgoto.
77 yycheck = a vector indexed in parallel with yytable.
78 It indicates, in a roundabout way, the bounds of the
79 portion you are trying to examine.
81 Suppose that the portion of yytable starts at index p
82 and the index to be examined within the portion is i.
83 Then if yycheck[p+i] != i, i is outside the bounds
84 of what is actually allocated, and the default
85 (from yydefact or yydefgoto) should be used.
86 Otherwise, yytable[p+i] should be used.
88 YYFINAL = the state number of the termination state.
89 YYFLAG = most negative short int. Used to flag ??
103 #include "conflicts.h"
105 extern void berror
PARAMS((const char *));
111 static short **froms
;
115 static short *actrow
;
116 static short *state_count
;
128 output_short_or_char_table (FILE *out
,
131 const char *table_name
,
134 short begin
, short end
)
139 fprintf (out
, "/* %s. */\n", comment
);
141 fprintf (out
, "static const %s %s[] =\n{\n %6d",
142 type
, table_name
, first_value
);
145 for (i
= begin
; i
< end
; i
++)
159 fprintf (out
, "%6d", short_table
[i
]);
162 fprintf (out
, "\n};\n");
167 output_short_table (FILE *out
,
169 const char *table_name
,
172 short begin
, short end
)
174 output_short_or_char_table (out
, comment
, "short", table_name
, short_table
,
175 first_value
, begin
, end
);
179 /*--------------------------------------------------------------.
180 | output_headers -- Output constant strings to the beginning of |
182 `--------------------------------------------------------------*/
187 extern int yyerror;\n\
188 extern int yycost;\n\
189 extern char * yymsg;\n\
190 extern YYSTYPE yyval;\n\
192 yyguard(n, yyvsp, yylsp)\n\
194 register YYSTYPE *yyvsp;\n\
195 register YYLTYPE *yylsp;\n\
206 extern YYSTYPE yyval;\n\
207 extern int yychar;\n\
209 yyaction(n, yyvsp, yylsp)\n\
211 register YYSTYPE *yyvsp;\n\
212 register YYLTYPE *yylsp;\n\
217 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
220 output_headers (void)
223 fprintf (fguard
, GUARDSTR
, attrsfile
);
228 fprintf (faction
, (semantic_parser
? ACTSTR
: ACTSTR_SIMPLE
), attrsfile
);
229 /* if (semantic_parser) JF moved this below
230 fprintf(ftable, "#include \"%s\"\n", attrsfile);
231 fprintf(ftable, "#include <stdio.h>\n\n");
234 /* Rename certain symbols if -p was specified. */
235 if (spec_name_prefix
)
237 fprintf (ftable
, "#define yyparse %sparse\n", spec_name_prefix
);
238 fprintf (ftable
, "#define yylex %slex\n", spec_name_prefix
);
239 fprintf (ftable
, "#define yyerror %serror\n", spec_name_prefix
);
240 fprintf (ftable
, "#define yylval %slval\n", spec_name_prefix
);
241 fprintf (ftable
, "#define yychar %schar\n", spec_name_prefix
);
242 fprintf (ftable
, "#define yydebug %sdebug\n", spec_name_prefix
);
243 fprintf (ftable
, "#define yynerrs %snerrs\n", spec_name_prefix
);
248 /*-------------------------------------------------------.
249 | Output constant strings to the ends of certain files. |
250 `-------------------------------------------------------*/
253 output_trailers (void)
256 fprintf (fguard
, "\n }\n}\n");
258 fprintf (faction
, "\n");
264 fprintf (faction
, " }\n");
265 fprintf (faction
, "}\n");
271 output_token_translations (void)
275 /* YYRANSLATE(YYLEX) -- Bison token number corresponding to YYLEX. */\n",
280 "#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\
283 max_user_token_number
, nsyms
);
285 output_short_or_char_table (ftable
,
286 "YYRANSLATE[YYLEX] -- Bison token number corresponding to YYLEX",
287 ntokens
< 127 ? "char" : "short",
288 "yytranslate", token_translations
,
289 0, 1, max_user_token_number
+ 1);
293 fprintf (ftable
, "\n#define YYTRANSLATE(x) (x)\n");
301 /* With the ordinary parser,
302 yyprhs and yyrhs are needed only for yydebug. */
303 /* With the no_parser option, all tables are generated */
304 if (!semantic_parser
&& !no_parser_flag
)
305 fprintf (ftable
, "\n#if YYDEBUG != 0\n");
307 output_short_table (ftable
, NULL
, "yyprhs", rrhs
,
311 size_t yyrhs_size
= 1;
315 for (sp
= ritem
+ 1; *sp
; sp
++)
317 yyrhs
= XMALLOC (short, yyrhs_size
);
319 for (sp
= ritem
+ 1, i
= 1; *sp
; ++sp
, ++i
)
320 yyrhs
[i
] = *sp
> 0 ? *sp
: 0;
322 output_short_table (ftable
, NULL
, "yyrhs", yyrhs
,
323 ritem
[0], 1, yyrhs_size
);
327 if (!semantic_parser
&& !no_parser_flag
)
328 fprintf (ftable
, "\n#endif\n");
335 output_short_table (ftable
, NULL
, "yystos", accessing_symbol
,
341 output_rule_data (void)
345 short *short_tab
= NULL
;
351 output_short_table (ftable
,
352 "YYRLINE[YYN] -- source line where rule number YYN was defined",
356 fputs ("#endif\n\n", ftable
);
358 if (token_table_flag
|| no_parser_flag
)
360 fprintf (ftable
, "#define YYNTOKENS %d\n", ntokens
);
361 fprintf (ftable
, "#define YYNNTS %d\n", nvars
);
362 fprintf (ftable
, "#define YYNRULES %d\n", nrules
);
363 fprintf (ftable
, "#define YYNSTATES %d\n", nstates
);
364 fprintf (ftable
, "#define YYMAXUTOK %d\n\n", max_user_token_number
);
367 /* Output the table of symbol names. */
368 if (!token_table_flag
&& !no_parser_flag
)
369 fputs ("\n#if YYDEBUG != 0 || defined YYERROR_VERBOSE\n\n", ftable
);
371 /* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */\n",
374 "static const char *const yytname[] =\n{\n ");
377 for (i
= 0; i
< nsyms
; i
++)
378 /* this used to be i<=nsyms, but that output a final "" symbol
379 almost by accident */
381 /* Width of the next token, including the two quotes, the coma
386 for (p
= tags
[i
]; p
&& *p
; p
++)
387 if (*p
== '"' || *p
== '\\' || *p
== '\n' || *p
== '\t'
390 else if (*p
< 040 || *p
>= 0177)
395 if (j
+ strsize
> 75)
397 fputs ("\n ", ftable
);
402 for (p
= tags
[i
]; p
&& *p
; p
++)
404 if (*p
== '"' || *p
== '\\')
406 fprintf (ftable
, "\\%c", *p
);
410 fprintf (ftable
, "\\n");
414 fprintf (ftable
, "\\t");
418 fprintf (ftable
, "\\b");
420 else if (*p
< 040 || *p
>= 0177)
422 fprintf (ftable
, "\\%03o", *p
);
430 fputs ("\", ", ftable
);
433 /* add a NULL entry to list of tokens */
434 fprintf (ftable
, "NULL\n};\n");
436 if (!token_table_flag
&& !no_parser_flag
)
437 fprintf (ftable
, "#endif\n\n");
439 /* Output YYTOKNUM. */
440 if (token_table_flag
)
442 output_short_table (ftable
,
443 "YYTOKNUM[YYLEX] -- Index in YYTNAME corresponding to YYLEX",
444 "yytoknum", user_toknums
,
449 output_short_table (ftable
,
450 "YYR1[YYN] -- Symbol number of symbol that rule YYN derives",
458 short_tab
= XMALLOC (short, nrules
+ 1);
459 for (i
= 1; i
< nrules
; i
++)
460 short_tab
[i
] = rrhs
[i
+ 1] - rrhs
[i
] - 1;
461 short_tab
[nrules
] = nitems
- rrhs
[nrules
] - 1;
462 output_short_table (ftable
,
463 "YYR2[YYN] -- Number of symbols composing right hand side of rule YYN",
475 output_defines (void)
477 fprintf (ftable
, "\n\n#define\tYYFINAL\t\t%d\n", final_state
);
478 fprintf (ftable
, "#define\tYYFLAG\t\t%d\n", MINSHORT
);
479 fprintf (ftable
, "#define\tYYNTBASE\t%d\n", ntokens
);
483 /*------------------------------------------------------------------.
484 | Decide what to do for each type of token if seen as the lookahead |
485 | token in specified state. The value returned is used as the |
486 | default action (yydefact) for the state. In addition, actrow is |
487 | filled with what to do for each kind of token, index by symbol |
488 | number, with zero meaning do the default action. The value |
489 | MINSHORT, a very negative number, means this situation is an |
490 | error. The parser recognizes this value specially. |
492 | This is where conflicts are resolved. The loop over lookahead |
493 | rules considered lower-numbered rules last, and the last rule |
494 | considered that likes a token gets to handle it. |
495 `------------------------------------------------------------------*/
498 action_row (int state
)
517 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
519 for (i
= 0; i
< ntokens
; i
++)
524 redp
= reduction_table
[state
];
532 /* loop over all the rules available here which require
534 m
= lookaheads
[state
];
535 n
= lookaheads
[state
+ 1];
537 for (i
= n
- 1; i
>= m
; i
--)
540 wordp
= LA
+ i
* tokensetsize
;
543 /* and find each token which the rule finds acceptable
545 for (j
= 0; j
< ntokens
; j
++)
547 /* and record this rule as the rule to use if that
563 shiftp
= shift_table
[state
];
565 /* Now see which tokens are allowed for shifts in this state. For
566 them, record the shift as the thing to do. So shift is preferred
573 for (i
= 0; i
< k
; i
++)
575 shift_state
= shiftp
->shifts
[i
];
579 symbol
= accessing_symbol
[shift_state
];
584 actrow
[symbol
] = shift_state
;
586 /* Do not use any default reduction if there is a shift for
588 if (symbol
== error_token_number
)
593 errp
= err_table
[state
];
595 /* See which tokens are an explicit error in this state (due to
596 %nonassoc). For them, record MINSHORT as the action. */
602 for (i
= 0; i
< k
; i
++)
604 symbol
= errp
->errs
[i
];
605 actrow
[symbol
] = MINSHORT
;
609 /* Now find the most common reduction and make it the default action
612 if (nreds
>= 1 && !nodefault
)
614 if (consistent
[state
])
615 default_rule
= redp
->rules
[0];
619 for (i
= m
; i
< n
; i
++)
624 for (j
= 0; j
< ntokens
; j
++)
626 if (actrow
[j
] == rule
)
637 /* actions which match the default are replaced with zero,
638 which means "use the default" */
642 for (j
= 0; j
< ntokens
; j
++)
644 if (actrow
[j
] == default_rule
)
648 default_rule
= -default_rule
;
653 /* If have no default rule, the default is an error.
654 So replace any action which says "error" with "use default". */
656 if (default_rule
== 0)
657 for (j
= 0; j
< ntokens
; j
++)
659 if (actrow
[j
] == MINSHORT
)
677 for (i
= 0; i
< ntokens
; i
++)
686 froms
[state
] = sp1
= sp
= XCALLOC (short, count
);
687 tos
[state
] = sp2
= XCALLOC (short, count
);
689 for (i
= 0; i
< ntokens
; i
++)
698 tally
[state
] = count
;
699 width
[state
] = sp1
[-1] - sp
[0] + 1;
703 /*------------------------------------------------------------------.
704 | Figure out the actions for the specified state, indexed by |
705 | lookahead token type. |
707 | The YYDEFACT table is output now. The detailed info is saved for |
708 | putting into YYTABLE later. |
709 `------------------------------------------------------------------*/
715 short *yydefact
= XCALLOC (short, nstates
);
717 actrow
= XCALLOC (short, ntokens
);
718 for (i
= 0; i
< nstates
; ++i
)
720 yydefact
[i
] = action_row (i
);
725 output_short_table (ftable
,
726 "YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE\n\
727 doesn't specify something else to do. Zero means the default is an\n\
729 "yydefact", yydefact
,
730 yydefact
[0], 1, nstates
);
739 shifts
*sp
, *sptmp
; /* JF derefrenced freed ptr */
743 for (sp
= first_shift
; sp
; sp
= sptmp
)
752 free_reductions (void)
754 reductions
*rp
, *rptmp
; /* JF fixed freed ptr */
756 XFREE (reduction_table
);
758 for (rp
= first_reduction
; rp
; rp
= rptmp
)
768 save_column (int symbol
, int default_state
)
777 short begin
= goto_map
[symbol
];
778 short end
= goto_map
[symbol
+ 1];
781 for (i
= begin
; i
< end
; i
++)
783 if (to_state
[i
] != default_state
)
790 symno
= symbol
- ntokens
+ nstates
;
792 froms
[symno
] = sp1
= sp
= XCALLOC (short, count
);
793 tos
[symno
] = sp2
= XCALLOC (short, count
);
795 for (i
= begin
; i
< end
; i
++)
797 if (to_state
[i
] != default_state
)
799 *sp1
++ = from_state
[i
];
800 *sp2
++ = to_state
[i
];
804 tally
[symno
] = count
;
805 width
[symno
] = sp1
[-1] - sp
[0] + 1;
809 default_goto (int symbol
)
817 m
= goto_map
[symbol
];
818 n
= goto_map
[symbol
+ 1];
823 for (i
= 0; i
< nstates
; i
++)
826 for (i
= m
; i
< n
; i
++)
827 state_count
[to_state
[i
]]++;
832 for (i
= 0; i
< nstates
; i
++)
834 if (state_count
[i
] > max
)
836 max
= state_count
[i
];
841 return default_state
;
845 /*-------------------------------------------------------------------.
846 | Figure out what to do after reducing with each rule, depending on |
847 | the saved state from before the beginning of parsing the data that |
848 | matched this rule. |
850 | The YYDEFGOTO table is output now. The detailed info is saved for |
851 | putting into YYTABLE later. |
852 `-------------------------------------------------------------------*/
859 short *yydefgoto
= XMALLOC (short, nsyms
- ntokens
);
860 state_count
= XCALLOC (short, nstates
);
862 for (i
= ntokens
; i
< nsyms
; ++i
)
864 int default_state
= default_goto (i
);
865 save_column (i
, default_state
);
866 yydefgoto
[i
- ntokens
] = default_state
;
869 output_short_table (ftable
, NULL
, "yydefgoto", yydefgoto
,
870 yydefgoto
[0], 1, nsyms
- ntokens
);
877 /* The next few functions decide how to pack the actions and gotos
878 information into yytable. */
889 order
= XCALLOC (short, nvectors
);
892 for (i
= 0; i
< nvectors
; i
++)
900 while (j
>= 0 && (width
[order
[j
]] < w
))
903 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
906 for (k
= nentries
- 1; k
> j
; k
--)
907 order
[k
+ 1] = order
[k
];
917 matching_state (int vector
)
934 for (prev
= vector
- 1; prev
>= 0; prev
--)
937 if (width
[j
] != w
|| tally
[j
] != t
)
941 for (k
= 0; match
&& k
< t
; k
++)
943 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
956 pack_vector (int vector
)
975 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
979 for (k
= 0; ok
&& k
< t
; k
++)
983 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
989 for (k
= 0; ok
&& k
< vector
; k
++)
997 for (k
= 0; k
< t
; k
++)
1001 check
[loc
] = from
[k
];
1004 while (table
[lowzero
] != 0)
1014 berror ("pack_vector");
1015 return 0; /* JF keep lint happy */
1026 base
= XCALLOC (short, nvectors
);
1027 pos
= XCALLOC (short, nentries
);
1028 table
= XCALLOC (short, MAXTABLE
);
1029 check
= XCALLOC (short, MAXTABLE
);
1034 for (i
= 0; i
< nvectors
; i
++)
1037 for (i
= 0; i
< MAXTABLE
; i
++)
1040 for (i
= 0; i
< nentries
; i
++)
1042 state
= matching_state (i
);
1045 place
= pack_vector (i
);
1047 place
= base
[state
];
1050 base
[order
[i
]] = place
;
1053 for (i
= 0; i
< nvectors
; i
++)
1066 /* the following functions output yytable, yycheck
1067 and the vectors whose elements index the portion starts */
1072 output_short_table (ftable
, NULL
, "yypact", base
,
1073 base
[0], 1, nstates
);
1075 putc ('\n', ftable
);
1077 output_short_table (ftable
, NULL
, "yypgoto", base
,
1078 base
[nstates
], nstates
+ 1, nvectors
);
1087 fprintf (ftable
, "\n\n#define\tYYLAST\t\t%d\n\n\n", high
);
1088 output_short_table (ftable
, NULL
, "yytable", table
,
1089 table
[0], 1, high
+ 1);
1097 output_short_table (ftable
, NULL
, "yycheck", check
,
1098 check
[0], 1, high
+ 1);
1102 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
1106 output_actions (void)
1108 nvectors
= nstates
+ nvars
;
1110 froms
= XCALLOC (short *, nvectors
);
1111 tos
= XCALLOC (short *, nvectors
);
1112 tally
= XCALLOC (short, nvectors
);
1113 width
= XCALLOC (short, nvectors
);
1121 XFREE (accessing_symbol
);
1124 XFREE (goto_map
+ ntokens
);
1130 putc ('\n', ftable
);
1133 putc ('\n', ftable
);
1137 /* copy the parser code into the ftable file at the end. */
1140 output_parser (void)
1143 static int number_of_dollar_signs
= 0;
1147 #define fpars fparser
1151 fprintf (ftable
, "#define YYPURE 1\n\n");
1154 /* JF no longer needed 'cuz open_extra_files changes the currently
1155 open parser from bison.simple to bison.hairy */
1156 if (semantic_parser
)
1162 /* Loop over lines in the standard parser file. */
1170 /* See if the line starts with `#line.
1171 If so, set write_line to 0. */
1188 fprintf (ftable
, "#lin");
1191 fprintf (ftable
, "#li");
1194 fprintf (ftable
, "#l");
1197 fprintf (ftable
, "#");
1200 /* now write out the line... */
1201 for (; c
!= '\n' && c
!= EOF
; c
= getc (fpars
))
1206 number_of_dollar_signs
++;
1207 assert (number_of_dollar_signs
== 1);
1208 /* `$' in the parser file indicates where to put the actions.
1209 Copy them in at this point. */
1211 for (c
= getc (faction
); c
!= EOF
; c
= getc (faction
))
1213 /* Skip the end of the line containing `$'. */
1223 assert (number_of_dollar_signs
== 1);
1227 output_program (void)
1232 fprintf (ftable
, "#line %d \"%s\"\n", lineno
, infile
);
1244 free_itemsets (void)
1248 XFREE (state_table
);
1250 for (cp
= first_state
; cp
; cp
= cptmp
)
1258 /*----------------------------------------------------------.
1259 | Output the parsing tables and the parser code to ftable. |
1260 `----------------------------------------------------------*/
1267 /* output_token_defines(ftable); / * JF put out token defines FIRST */
1268 if (!semantic_parser
) /* JF Put out other stuff */
1271 while ((c
= getc (fattrs
)) != EOF
)
1274 reader_output_yylsp (ftable
);
1278 # define YYDEBUG 1\n\
1283 if (semantic_parser
)
1284 fprintf (ftable
, "#include \"%s\"\n", attrsfile
);
1286 if (!no_parser_flag
)
1287 fprintf (ftable
, "#include <stdio.h>\n\n");
1289 /* Make "const" do nothing if not in ANSI C. */
1291 #ifndef __cplusplus\n\
1292 # ifndef __STDC__\n\
1301 output_token_translations ();
1302 /* if (semantic_parser) */
1303 /* This is now unconditional because debugging printouts can use it. */
1306 if (semantic_parser
)
1308 output_rule_data ();
1310 if (!no_parser_flag
)