]>
git.saurik.com Git - bison.git/blob - src/output.c
1 /* Output the generated parsing program for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001 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 ??
101 #include "complain.h"
105 #include "conflicts.h"
106 #include "macrotab.h"
108 extern void berror
PARAMS((const char *));
112 static short **froms
;
116 static short *actrow
;
117 static short *state_count
;
126 struct obstack macro_obstack
;
127 struct obstack output_obstack
;
132 output_table_data (struct obstack
* oout
,
141 obstack_fgrow1 (oout
, "%6d", first
);
142 for (i
= begin
; i
< end
; ++i
)
144 obstack_1grow (oout
, ',');
147 obstack_sgrow (oout
, "\n ");
152 obstack_fgrow1 (oout
, "%6d", table
[i
]);
154 obstack_1grow (oout
, 0);
159 output_token_translations (void)
161 output_table_data (&output_obstack
, token_translations
,
162 0, 1, max_user_token_number
+ 1);
163 macro_insert ("translate", obstack_finish (&output_obstack
));
170 output_table_data (&output_obstack
, rrhs
,
172 macro_insert ("prhs", obstack_finish (&output_obstack
));
175 size_t yyrhs_size
= 1;
179 for (sp
= ritem
+ 1; *sp
; sp
++)
181 yyrhs
= XMALLOC (short, yyrhs_size
);
183 for (sp
= ritem
+ 1, i
= 1; *sp
; ++sp
, ++i
)
184 yyrhs
[i
] = *sp
> 0 ? *sp
: 0;
186 output_table_data (&output_obstack
, yyrhs
,
187 ritem
[0], 1, yyrhs_size
);
188 macro_insert ("rhs", obstack_finish (&output_obstack
));
193 /* if (!semantic_parser && !no_parser_flag)
194 obstack_sgrow (&table_obstack, "\n#endif\n"); */
201 output_table_data (&output_obstack
, accessing_symbol
,
203 macro_insert ("stos", obstack_finish (&output_obstack
));
208 output_rule_data (void)
212 short *short_tab
= NULL
;
214 output_table_data (&output_obstack
, rline
,
216 macro_insert ("rline", obstack_finish (&output_obstack
));
219 for (i
= 0; i
< nsyms
; i
++)
220 /* this used to be i<=nsyms, but that output a final "" symbol
221 almost by accident */
223 /* Width of the next token, including the two quotes, the coma
228 for (p
= tags
[i
]; p
&& *p
; p
++)
229 if (*p
== '"' || *p
== '\\' || *p
== '\n' || *p
== '\t'
232 else if (*p
< 040 || *p
>= 0177)
237 if (j
+ strsize
> 75)
239 obstack_sgrow (&output_obstack
, "\n ");
243 obstack_1grow (&output_obstack
, '\"');
244 for (p
= tags
[i
]; p
&& *p
; p
++)
246 if (*p
== '"' || *p
== '\\')
247 obstack_fgrow1 (&output_obstack
, "\\%c", *p
);
249 obstack_sgrow (&output_obstack
, "\\n");
251 obstack_sgrow (&output_obstack
, "\\t");
253 obstack_sgrow (&output_obstack
, "\\b");
254 else if (*p
< 040 || *p
>= 0177)
255 obstack_fgrow1 (&output_obstack
, "\\%03o", *p
);
257 obstack_1grow (&output_obstack
, *p
);
260 obstack_sgrow (&output_obstack
, "\", ");
263 /* add a NULL entry to list of tokens */
264 obstack_sgrow (&output_obstack
, "NULL");
266 /* Finish table and store. */
267 obstack_1grow (&output_obstack
, 0);
268 macro_insert ("tname", obstack_finish (&output_obstack
));
270 /* Output YYTOKNUM. */
271 output_table_data (&output_obstack
, user_toknums
,
273 macro_insert ("toknum", obstack_finish (&output_obstack
));
276 output_table_data (&output_obstack
, rlhs
,
278 macro_insert ("r1", obstack_finish (&output_obstack
));
282 short_tab
= XMALLOC (short, nrules
+ 1);
283 for (i
= 1; i
< nrules
; i
++)
284 short_tab
[i
] = rrhs
[i
+ 1] - rrhs
[i
] - 1;
285 short_tab
[nrules
] = nitems
- rrhs
[nrules
] - 1;
286 output_table_data (&output_obstack
, short_tab
,
288 macro_insert ("r2", obstack_finish (&output_obstack
));
294 /*------------------------------------------------------------------.
295 | Decide what to do for each type of token if seen as the lookahead |
296 | token in specified state. The value returned is used as the |
297 | default action (yydefact) for the state. In addition, actrow is |
298 | filled with what to do for each kind of token, index by symbol |
299 | number, with zero meaning do the default action. The value |
300 | MINSHORT, a very negative number, means this situation is an |
301 | error. The parser recognizes this value specially. |
303 | This is where conflicts are resolved. The loop over lookahead |
304 | rules considered lower-numbered rules last, and the last rule |
305 | considered that likes a token gets to handle it. |
306 `------------------------------------------------------------------*/
309 action_row (int state
)
328 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
330 for (i
= 0; i
< ntokens
; i
++)
335 redp
= reduction_table
[state
];
343 /* loop over all the rules available here which require
345 m
= lookaheads
[state
];
346 n
= lookaheads
[state
+ 1];
348 for (i
= n
- 1; i
>= m
; i
--)
351 wordp
= LA
+ i
* tokensetsize
;
354 /* and find each token which the rule finds acceptable
356 for (j
= 0; j
< ntokens
; j
++)
358 /* and record this rule as the rule to use if that
374 shiftp
= shift_table
[state
];
376 /* Now see which tokens are allowed for shifts in this state. For
377 them, record the shift as the thing to do. So shift is preferred
384 for (i
= 0; i
< k
; i
++)
386 shift_state
= shiftp
->shifts
[i
];
390 symbol
= accessing_symbol
[shift_state
];
395 actrow
[symbol
] = shift_state
;
397 /* Do not use any default reduction if there is a shift for
399 if (symbol
== error_token_number
)
404 errp
= err_table
[state
];
406 /* See which tokens are an explicit error in this state (due to
407 %nonassoc). For them, record MINSHORT as the action. */
413 for (i
= 0; i
< k
; i
++)
415 symbol
= errp
->errs
[i
];
416 actrow
[symbol
] = MINSHORT
;
420 /* Now find the most common reduction and make it the default action
423 if (nreds
>= 1 && !nodefault
)
425 if (consistent
[state
])
426 default_rule
= redp
->rules
[0];
430 for (i
= m
; i
< n
; i
++)
435 for (j
= 0; j
< ntokens
; j
++)
437 if (actrow
[j
] == rule
)
448 /* actions which match the default are replaced with zero,
449 which means "use the default" */
453 for (j
= 0; j
< ntokens
; j
++)
455 if (actrow
[j
] == default_rule
)
459 default_rule
= -default_rule
;
464 /* If have no default rule, the default is an error.
465 So replace any action which says "error" with "use default". */
467 if (default_rule
== 0)
468 for (j
= 0; j
< ntokens
; j
++)
470 if (actrow
[j
] == MINSHORT
)
488 for (i
= 0; i
< ntokens
; i
++)
497 froms
[state
] = sp1
= sp
= XCALLOC (short, count
);
498 tos
[state
] = sp2
= XCALLOC (short, count
);
500 for (i
= 0; i
< ntokens
; i
++)
509 tally
[state
] = count
;
510 width
[state
] = sp1
[-1] - sp
[0] + 1;
514 /*------------------------------------------------------------------.
515 | Figure out the actions for the specified state, indexed by |
516 | lookahead token type. |
518 | The YYDEFACT table is output now. The detailed info is saved for |
519 | putting into YYTABLE later. |
520 `------------------------------------------------------------------*/
526 short *yydefact
= XCALLOC (short, nstates
);
528 actrow
= XCALLOC (short, ntokens
);
529 for (i
= 0; i
< nstates
; ++i
)
531 yydefact
[i
] = action_row (i
);
535 output_table_data (&output_obstack
, yydefact
,
536 yydefact
[0], 1, nstates
);
537 macro_insert ("defact", obstack_finish (&output_obstack
));
547 shifts
*sp
, *sptmp
; /* JF derefrenced freed ptr */
551 for (sp
= first_shift
; sp
; sp
= sptmp
)
560 free_reductions (void)
562 reductions
*rp
, *rptmp
; /* JF fixed freed ptr */
564 XFREE (reduction_table
);
566 for (rp
= first_reduction
; rp
; rp
= rptmp
)
576 save_column (int symbol
, int default_state
)
585 short begin
= goto_map
[symbol
];
586 short end
= goto_map
[symbol
+ 1];
589 for (i
= begin
; i
< end
; i
++)
591 if (to_state
[i
] != default_state
)
598 symno
= symbol
- ntokens
+ nstates
;
600 froms
[symno
] = sp1
= sp
= XCALLOC (short, count
);
601 tos
[symno
] = sp2
= XCALLOC (short, count
);
603 for (i
= begin
; i
< end
; i
++)
605 if (to_state
[i
] != default_state
)
607 *sp1
++ = from_state
[i
];
608 *sp2
++ = to_state
[i
];
612 tally
[symno
] = count
;
613 width
[symno
] = sp1
[-1] - sp
[0] + 1;
617 default_goto (int symbol
)
625 m
= goto_map
[symbol
];
626 n
= goto_map
[symbol
+ 1];
631 for (i
= 0; i
< nstates
; i
++)
634 for (i
= m
; i
< n
; i
++)
635 state_count
[to_state
[i
]]++;
640 for (i
= 0; i
< nstates
; i
++)
642 if (state_count
[i
] > max
)
644 max
= state_count
[i
];
649 return default_state
;
653 /*-------------------------------------------------------------------.
654 | Figure out what to do after reducing with each rule, depending on |
655 | the saved state from before the beginning of parsing the data that |
656 | matched this rule. |
658 | The YYDEFGOTO table is output now. The detailed info is saved for |
659 | putting into YYTABLE later. |
660 `-------------------------------------------------------------------*/
666 short *yydefgoto
= XMALLOC (short, nsyms
- ntokens
);
668 state_count
= XCALLOC (short, nstates
);
669 for (i
= ntokens
; i
< nsyms
; ++i
)
671 int default_state
= default_goto (i
);
672 save_column (i
, default_state
);
673 yydefgoto
[i
- ntokens
] = default_state
;
676 output_table_data (&output_obstack
, yydefgoto
,
677 yydefgoto
[0], 1, nsyms
- ntokens
);
678 macro_insert ("defgoto", obstack_finish (&output_obstack
));
685 /* The next few functions decide how to pack the actions and gotos
686 information into yytable. */
697 order
= XCALLOC (short, nvectors
);
700 for (i
= 0; i
< nvectors
; i
++)
708 while (j
>= 0 && (width
[order
[j
]] < w
))
711 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
714 for (k
= nentries
- 1; k
> j
; k
--)
715 order
[k
+ 1] = order
[k
];
725 matching_state (int vector
)
742 for (prev
= vector
- 1; prev
>= 0; prev
--)
745 if (width
[j
] != w
|| tally
[j
] != t
)
749 for (k
= 0; match
&& k
< t
; k
++)
751 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
764 pack_vector (int vector
)
783 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
787 for (k
= 0; ok
&& k
< t
; k
++)
791 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
797 for (k
= 0; ok
&& k
< vector
; k
++)
805 for (k
= 0; k
< t
; k
++)
809 check
[loc
] = from
[k
];
812 while (table
[lowzero
] != 0)
822 berror ("pack_vector");
823 return 0; /* JF keep lint happy */
834 base
= XCALLOC (short, nvectors
);
835 pos
= XCALLOC (short, nentries
);
836 table
= XCALLOC (short, MAXTABLE
);
837 check
= XCALLOC (short, MAXTABLE
);
842 for (i
= 0; i
< nvectors
; i
++)
845 for (i
= 0; i
< MAXTABLE
; i
++)
848 for (i
= 0; i
< nentries
; i
++)
850 state
= matching_state (i
);
853 place
= pack_vector (i
);
858 base
[order
[i
]] = place
;
861 for (i
= 0; i
< nvectors
; i
++)
874 /* the following functions output yytable, yycheck
875 and the vectors whose elements index the portion starts */
881 output_table_data (&output_obstack
, base
,
882 base
[0], 1, nstates
);
883 macro_insert ("pact", obstack_finish (&output_obstack
));
886 output_table_data (&output_obstack
, base
,
887 base
[nstates
], nstates
+ 1, nvectors
);
888 macro_insert ("pgoto", obstack_finish (&output_obstack
));
897 output_table_data (&output_obstack
, table
,
898 table
[0], 1, high
+ 1);
899 macro_insert ("table", obstack_finish (&output_obstack
));
907 output_table_data (&output_obstack
, check
,
908 check
[0], 1, high
+ 1);
909 macro_insert ("check", obstack_finish (&output_obstack
));
913 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
917 output_actions (void)
919 nvectors
= nstates
+ nvars
;
921 froms
= XCALLOC (short *, nvectors
);
922 tos
= XCALLOC (short *, nvectors
);
923 tally
= XCALLOC (short, nvectors
);
924 width
= XCALLOC (short, nvectors
);
932 XFREE (accessing_symbol
);
935 XFREE (goto_map
+ ntokens
);
941 /* FIXME: See if this is useful. */
942 /* obstack_1grow (&table_obstack, '\n'); */
945 /* FIXME: See if this is useful. */
946 /* obstack_1grow (&table_obstack, '\n'); */
950 /*------------------------------------------.
951 | Copy the parser code into TABLE_OBSTACK. |
952 `------------------------------------------*/
960 int actions_dumped
= 0;
962 /* Loop over lines in the standard parser file. */
966 skeleton
= skeleton_find ("BISON_HAIRY", BISON_HAIRY
);
968 skeleton
= skeleton_find ("BISON_SIMPLE", BISON_SIMPLE
);
970 fskel
= xfopen (skeleton
, "r");
972 /* New output code. */
981 obstack_1grow (&table_obstack
, c
);
984 else if ((c
= getc (fskel
)) == '%')
986 /* Read the macro. */
988 char* macro_value
= 0;
989 while (isalnum (c
= getc (fskel
)) || c
== '_')
990 obstack_1grow (¯o_obstack
, c
);
991 obstack_1grow (¯o_obstack
, 0);
993 /* Output the right value, or see if it's something special. */
994 macro_key
= obstack_finish (¯o_obstack
);
995 macro_value
= macro_find (macro_key
);
997 obstack_sgrow (&table_obstack
, macro_value
);
998 else if (!strcmp (macro_key
, "line"))
999 obstack_fgrow1 (&table_obstack
, "%d", line
+ 1);
1000 else if (!strcmp (macro_key
, "action"))
1002 size_t size
= obstack_object_size (&action_obstack
);
1003 obstack_grow (&table_obstack
,
1004 obstack_finish (&action_obstack
), size
);
1008 obstack_sgrow (&table_obstack
, "%%");
1009 obstack_sgrow (&table_obstack
, macro_key
);
1019 output_program (void)
1023 while ((c
= getc (finput
)) != EOF
)
1024 obstack_1grow (&table_obstack
, c
);
1029 free_itemsets (void)
1033 XFREE (state_table
);
1035 for (cp
= first_state
; cp
; cp
= cptmp
)
1044 #define MACRO_INSERT_INT(Key, Value) \
1046 obstack_fgrow1 (¯o_obstack, "%d", Value); \
1047 obstack_1grow (¯o_obstack, 0); \
1048 macro_insert (Key, obstack_finish (¯o_obstack)); \
1051 #define MACRO_INSERT_STRING(Key, Value) \
1053 obstack_sgrow (¯o_obstack, Value); \
1054 obstack_1grow (¯o_obstack, 0); \
1055 macro_insert (Key, obstack_finish (¯o_obstack)); \
1058 #define MACRO_INSERT_PREFIX(Key, Value) \
1060 obstack_fgrow2 (¯o_obstack, "%s%s", spec_name_prefix, Value); \
1061 obstack_1grow (¯o_obstack, 0); \
1062 macro_insert (Key, obstack_finish (¯o_obstack)); \
1068 MACRO_INSERT_INT ("last", high
);
1069 MACRO_INSERT_INT ("flag", MINSHORT
);
1070 MACRO_INSERT_INT ("pure", pure_parser
);
1071 MACRO_INSERT_INT ("nsym", nsyms
);
1072 MACRO_INSERT_INT ("debug", debug_flag
);
1073 MACRO_INSERT_INT ("final", final_state
);
1074 MACRO_INSERT_INT ("maxtok", max_user_token_number
);
1075 MACRO_INSERT_INT ("ntbase", ntokens
);
1076 MACRO_INSERT_INT ("verbose", 0);
1078 MACRO_INSERT_STRING ("filename", infile
);
1080 MACRO_INSERT_INT ("nnts", nvars
);
1081 MACRO_INSERT_INT ("nrules", nrules
);
1082 MACRO_INSERT_INT ("nstates", nstates
);
1083 MACRO_INSERT_INT ("ntokens", ntokens
);
1085 if (spec_name_prefix
)
1087 MACRO_INSERT_PREFIX ("yylex", "lex");
1088 MACRO_INSERT_PREFIX ("yychar", "char");
1089 MACRO_INSERT_PREFIX ("yylval", "lval");
1090 MACRO_INSERT_PREFIX ("yydebug", "debug");
1091 MACRO_INSERT_PREFIX ("yyerror", "error");
1092 MACRO_INSERT_PREFIX ("yynerrs", "nerrs");
1093 MACRO_INSERT_PREFIX ("yyparse", "parse");
1097 /*----------------------------------------------------------.
1098 | Output the parsing tables and the parser code to ftable. |
1099 `----------------------------------------------------------*/
1104 obstack_init (¯o_obstack
);
1105 obstack_init (&output_obstack
);
1108 /* If using a simple parser the definition of YYSTYPE are put into
1110 if (!semantic_parser
)
1112 size_t size
= obstack_object_size (&attrs_obstack
);
1113 obstack_grow (&table_obstack
, obstack_finish (&attrs_obstack
), size
);
1117 /* reader_output_yylsp (&table_obstack); */
1120 output_token_translations ();
1124 if (semantic_parser
)
1126 output_rule_data ();
1129 /* if (!no_parser_flag) */
1134 obstack_free (¯o_obstack
, 0);
1135 obstack_free (&output_obstack
, 0);