]>
git.saurik.com Git - bison.git/blob - src/output.c
dabcba1592f563bdc8196e48c57b30732d30c778
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 ??
100 #include "complain.h"
104 #include "conflicts.h"
105 #include "muscle_tab.h"
107 extern void berror
PARAMS((const char *));
111 static short **froms
= NULL
;
112 static short **tos
= NULL
;
113 static short *tally
= NULL
;
114 static short *width
= NULL
;
115 static short *actrow
= NULL
;
116 static short *state_count
= NULL
;
117 static short *order
= NULL
;
118 static short *base
= NULL
;
119 static short *pos
= NULL
;
120 static short *table
= NULL
;
121 static short *check
= NULL
;
125 struct obstack muscle_obstack
;
126 struct obstack output_obstack
;
131 output_table_data (struct obstack
*oout
,
140 obstack_fgrow1 (oout
, "%6d", first
);
141 for (i
= begin
; i
< end
; ++i
)
143 obstack_1grow (oout
, ',');
146 obstack_sgrow (oout
, "\n ");
151 obstack_fgrow1 (oout
, "%6d", table_data
[i
]);
153 obstack_1grow (oout
, 0);
158 output_token_translations (void)
160 output_table_data (&output_obstack
, token_translations
,
161 0, 1, max_user_token_number
+ 1);
162 muscle_insert ("translate", obstack_finish (&output_obstack
));
163 XFREE (token_translations
);
172 short *values
= XCALLOC (short, nrules
+ 1);
173 for (i
= 0; i
< nrules
+ 1; ++i
)
174 values
[i
] = rule_table
[i
].rhs
;
175 output_table_data (&output_obstack
, values
,
180 muscle_insert ("prhs", obstack_finish (&output_obstack
));
183 size_t yyrhs_size
= 1;
187 for (sp
= ritem
+ 1; *sp
; sp
++)
189 yyrhs
= XMALLOC (short, yyrhs_size
);
191 for (sp
= ritem
+ 1, i
= 1; *sp
; ++sp
, ++i
)
192 yyrhs
[i
] = *sp
> 0 ? *sp
: 0;
194 output_table_data (&output_obstack
, yyrhs
,
195 ritem
[0], 1, yyrhs_size
);
196 muscle_insert ("rhs", obstack_finish (&output_obstack
));
202 if (!semantic_parser
&& !no_parser_flag
)
203 obstack_sgrow (&table_obstack
, "\n#endif\n");
212 short *values
= (short *) alloca (sizeof (short) * nstates
);
213 for (i
= 0; i
< nstates
; ++i
)
214 values
[i
] = state_table
[i
].accessing_symbol
;
215 output_table_data (&output_obstack
, values
,
217 muscle_insert ("stos", obstack_finish (&output_obstack
));
222 output_rule_data (void)
226 short *short_tab
= NULL
;
228 output_table_data (&output_obstack
, rline
,
230 muscle_insert ("rline", obstack_finish (&output_obstack
));
233 for (i
= 0; i
< nsyms
; i
++)
234 /* this used to be i<=nsyms, but that output a final "" symbol
235 almost by accident */
237 /* Width of the next token, including the two quotes, the coma
242 for (p
= tags
[i
]; p
&& *p
; p
++)
243 if (*p
== '"' || *p
== '\\' || *p
== '\n' || *p
== '\t'
246 else if (*p
< 040 || *p
>= 0177)
251 if (j
+ strsize
> 75)
253 obstack_sgrow (&output_obstack
, "\n ");
257 obstack_1grow (&output_obstack
, '\"');
258 for (p
= tags
[i
]; p
&& *p
; p
++)
260 if (*p
== '"' || *p
== '\\')
261 obstack_fgrow1 (&output_obstack
, "\\%c", *p
);
263 obstack_sgrow (&output_obstack
, "\\n");
265 obstack_sgrow (&output_obstack
, "\\t");
267 obstack_sgrow (&output_obstack
, "\\b");
268 else if (*p
< 040 || *p
>= 0177)
269 obstack_fgrow1 (&output_obstack
, "\\%03o", *p
);
271 obstack_1grow (&output_obstack
, *p
);
274 obstack_sgrow (&output_obstack
, "\", ");
277 /* add a NULL entry to list of tokens */
278 obstack_sgrow (&output_obstack
, "NULL");
280 /* Finish table and store. */
281 obstack_1grow (&output_obstack
, 0);
282 muscle_insert ("tname", obstack_finish (&output_obstack
));
284 /* Output YYTOKNUM. */
285 output_table_data (&output_obstack
, user_toknums
,
287 muscle_insert ("toknum", obstack_finish (&output_obstack
));
291 short *values
= XCALLOC (short, nrules
+ 1);
292 for (i
= 0; i
< nrules
+ 1; ++i
)
293 values
[i
] = rule_table
[i
].lhs
;
294 output_table_data (&output_obstack
, values
,
296 muscle_insert ("r1", obstack_finish (&output_obstack
));
301 short_tab
= XMALLOC (short, nrules
+ 1);
302 for (i
= 1; i
< nrules
; i
++)
303 short_tab
[i
] = rule_table
[i
+ 1].rhs
- rule_table
[i
].rhs
- 1;
304 short_tab
[nrules
] = nitems
- rule_table
[nrules
].rhs
- 1;
305 output_table_data (&output_obstack
, short_tab
,
307 muscle_insert ("r2", obstack_finish (&output_obstack
));
310 XFREE (rule_table
+ 1);
313 /*------------------------------------------------------------------.
314 | Decide what to do for each type of token if seen as the lookahead |
315 | token in specified state. The value returned is used as the |
316 | default action (yydefact) for the state. In addition, actrow is |
317 | filled with what to do for each kind of token, index by symbol |
318 | number, with zero meaning do the default action. The value |
319 | MINSHORT, a very negative number, means this situation is an |
320 | error. The parser recognizes this value specially. |
322 | This is where conflicts are resolved. The loop over lookahead |
323 | rules considered lower-numbered rules last, and the last rule |
324 | considered that likes a token gets to handle it. |
325 `------------------------------------------------------------------*/
328 action_row (int state
)
347 int nodefault
= 0; /* set nonzero to inhibit having any default reduction */
349 for (i
= 0; i
< ntokens
; i
++)
354 redp
= state_table
[state
].reduction_table
;
362 /* loop over all the rules available here which require
364 m
= state_table
[state
].lookaheads
;
365 n
= state_table
[state
+ 1].lookaheads
;
367 for (i
= n
- 1; i
>= m
; i
--)
373 /* and find each token which the rule finds acceptable
375 for (j
= 0; j
< ntokens
; j
++)
377 /* and record this rule as the rule to use if that
393 shiftp
= state_table
[state
].shift_table
;
395 /* Now see which tokens are allowed for shifts in this state. For
396 them, record the shift as the thing to do. So shift is preferred
403 for (i
= 0; i
< k
; i
++)
405 shift_state
= shiftp
->shifts
[i
];
409 symbol
= state_table
[shift_state
].accessing_symbol
;
414 actrow
[symbol
] = shift_state
;
416 /* Do not use any default reduction if there is a shift for
418 if (symbol
== error_token_number
)
423 errp
= err_table
[state
];
425 /* See which tokens are an explicit error in this state (due to
426 %nonassoc). For them, record MINSHORT as the action. */
432 for (i
= 0; i
< k
; i
++)
434 symbol
= errp
->errs
[i
];
435 actrow
[symbol
] = MINSHORT
;
439 /* Now find the most common reduction and make it the default action
442 if (nreds
>= 1 && !nodefault
)
444 if (state_table
[state
].consistent
)
445 default_rule
= redp
->rules
[0];
449 for (i
= m
; i
< n
; i
++)
454 for (j
= 0; j
< ntokens
; j
++)
456 if (actrow
[j
] == rule
)
467 /* actions which match the default are replaced with zero,
468 which means "use the default" */
472 for (j
= 0; j
< ntokens
; j
++)
474 if (actrow
[j
] == default_rule
)
478 default_rule
= -default_rule
;
483 /* If have no default rule, the default is an error.
484 So replace any action which says "error" with "use default". */
486 if (default_rule
== 0)
487 for (j
= 0; j
< ntokens
; j
++)
489 if (actrow
[j
] == MINSHORT
)
507 for (i
= 0; i
< ntokens
; i
++)
516 froms
[state
] = sp1
= sp
= XCALLOC (short, count
);
517 tos
[state
] = sp2
= XCALLOC (short, count
);
519 for (i
= 0; i
< ntokens
; i
++)
528 tally
[state
] = count
;
529 width
[state
] = sp1
[-1] - sp
[0] + 1;
533 /*------------------------------------------------------------------.
534 | Figure out the actions for the specified state, indexed by |
535 | lookahead token type. |
537 | The YYDEFACT table is output now. The detailed info is saved for |
538 | putting into YYTABLE later. |
539 `------------------------------------------------------------------*/
545 short *yydefact
= XCALLOC (short, nstates
);
547 actrow
= XCALLOC (short, ntokens
);
548 for (i
= 0; i
< nstates
; ++i
)
550 yydefact
[i
] = action_row (i
);
554 output_table_data (&output_obstack
, yydefact
,
555 yydefact
[0], 1, nstates
);
556 muscle_insert ("defact", obstack_finish (&output_obstack
));
566 shifts
*sp
, *sptmp
; /* JF derefrenced freed ptr */
568 for (sp
= first_shift
; sp
; sp
= sptmp
)
577 free_reductions (void)
579 reductions
*rp
, *rptmp
; /* JF fixed freed ptr */
581 for (rp
= first_reduction
; rp
; rp
= rptmp
)
591 save_column (int symbol
, int default_state
)
600 short begin
= goto_map
[symbol
];
601 short end
= goto_map
[symbol
+ 1];
604 for (i
= begin
; i
< end
; i
++)
606 if (to_state
[i
] != default_state
)
613 symno
= symbol
- ntokens
+ nstates
;
615 froms
[symno
] = sp1
= sp
= XCALLOC (short, count
);
616 tos
[symno
] = sp2
= XCALLOC (short, count
);
618 for (i
= begin
; i
< end
; i
++)
620 if (to_state
[i
] != default_state
)
622 *sp1
++ = from_state
[i
];
623 *sp2
++ = to_state
[i
];
627 tally
[symno
] = count
;
628 width
[symno
] = sp1
[-1] - sp
[0] + 1;
632 default_goto (int symbol
)
640 m
= goto_map
[symbol
];
641 n
= goto_map
[symbol
+ 1];
646 for (i
= 0; i
< nstates
; i
++)
649 for (i
= m
; i
< n
; i
++)
650 state_count
[to_state
[i
]]++;
655 for (i
= 0; i
< nstates
; i
++)
657 if (state_count
[i
] > max
)
659 max
= state_count
[i
];
664 return default_state
;
668 /*-------------------------------------------------------------------.
669 | Figure out what to do after reducing with each rule, depending on |
670 | the saved state from before the beginning of parsing the data that |
671 | matched this rule. |
673 | The YYDEFGOTO table is output now. The detailed info is saved for |
674 | putting into YYTABLE later. |
675 `-------------------------------------------------------------------*/
681 short *yydefgoto
= XMALLOC (short, nsyms
- ntokens
);
683 state_count
= XCALLOC (short, nstates
);
684 for (i
= ntokens
; i
< nsyms
; ++i
)
686 int default_state
= default_goto (i
);
687 save_column (i
, default_state
);
688 yydefgoto
[i
- ntokens
] = default_state
;
691 output_table_data (&output_obstack
, yydefgoto
,
692 yydefgoto
[0], 1, nsyms
- ntokens
);
693 muscle_insert ("defgoto", obstack_finish (&output_obstack
));
700 /* The next few functions decide how to pack the actions and gotos
701 information into yytable. */
712 order
= XCALLOC (short, nvectors
);
715 for (i
= 0; i
< nvectors
; i
++)
723 while (j
>= 0 && (width
[order
[j
]] < w
))
726 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
729 for (k
= nentries
- 1; k
> j
; k
--)
730 order
[k
+ 1] = order
[k
];
740 matching_state (int vector
)
757 for (prev
= vector
- 1; prev
>= 0; prev
--)
760 if (width
[j
] != w
|| tally
[j
] != t
)
764 for (k
= 0; match
&& k
< t
; k
++)
766 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
779 pack_vector (int vector
)
798 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
802 for (k
= 0; ok
&& k
< t
; k
++)
806 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
812 for (k
= 0; ok
&& k
< vector
; k
++)
820 for (k
= 0; k
< t
; k
++)
824 check
[loc
] = from
[k
];
827 while (table
[lowzero
] != 0)
837 berror ("pack_vector");
838 return 0; /* JF keep lint happy */
849 base
= XCALLOC (short, nvectors
);
850 pos
= XCALLOC (short, nentries
);
851 table
= XCALLOC (short, MAXTABLE
);
852 check
= XCALLOC (short, MAXTABLE
);
857 for (i
= 0; i
< nvectors
; i
++)
860 for (i
= 0; i
< MAXTABLE
; i
++)
863 for (i
= 0; i
< nentries
; i
++)
865 state
= matching_state (i
);
868 place
= pack_vector (i
);
873 base
[order
[i
]] = place
;
876 for (i
= 0; i
< nvectors
; i
++)
889 /* the following functions output yytable, yycheck
890 and the vectors whose elements index the portion starts */
896 output_table_data (&output_obstack
, base
,
897 base
[0], 1, nstates
);
898 muscle_insert ("pact", obstack_finish (&output_obstack
));
901 output_table_data (&output_obstack
, base
,
902 base
[nstates
], nstates
+ 1, nvectors
);
903 muscle_insert ("pgoto", obstack_finish (&output_obstack
));
912 output_table_data (&output_obstack
, table
,
913 table
[0], 1, high
+ 1);
914 muscle_insert ("table", obstack_finish (&output_obstack
));
922 output_table_data (&output_obstack
, check
,
923 check
[0], 1, high
+ 1);
924 muscle_insert ("check", obstack_finish (&output_obstack
));
928 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
932 output_actions (void)
934 nvectors
= nstates
+ nvars
;
936 froms
= XCALLOC (short *, nvectors
);
937 tos
= XCALLOC (short *, nvectors
);
938 tally
= XCALLOC (short, nvectors
);
939 width
= XCALLOC (short, nvectors
);
948 XFREE (goto_map
+ ntokens
);
963 /*------------------------------------------------------------.
964 | Copy the parser code from SKEL_FILENAME into OOUT obstack. |
965 | and do the muscle substitution. |
966 `------------------------------------------------------------*/
969 output_parser (const char *skel_filename
, struct obstack
*oout
)
975 fskel
= xfopen (skel_filename
, "r");
977 /* New output code. */
986 obstack_1grow (oout
, c
);
989 else if ((c
= getc (fskel
)) == '%')
991 /* Read the muscle. */
992 const char *muscle_key
= 0;
993 const char *muscle_value
= 0;
995 while (isalnum (c
= getc (fskel
)) || c
== '_')
996 obstack_1grow (&muscle_obstack
, c
);
997 obstack_1grow (&muscle_obstack
, 0);
999 /* Output the right value, or see if it's something special. */
1000 muscle_key
= obstack_finish (&muscle_obstack
);
1001 muscle_value
= muscle_find (muscle_key
);
1003 obstack_sgrow (oout
, muscle_value
);
1004 else if (!strcmp (muscle_key
, "line"))
1005 obstack_fgrow1 (oout
, "%d", line
+ 1);
1006 else if (!strcmp (muscle_key
, "input_line"))
1007 obstack_fgrow1 (oout
, "%d", lineno
);
1008 /* FIXME: Insert the code to recognize %%sub-skeleton for exemple. */
1011 obstack_sgrow (oout
, "%%");
1012 obstack_sgrow (oout
, muscle_key
);
1016 obstack_1grow (oout
, '%');
1023 /*----------------------------------------.
1024 | Prepare the master parser to be output |
1025 `----------------------------------------*/
1028 output_master_parser (void)
1032 if (semantic_parser
)
1033 skeleton
= skeleton_find ("BISON_HAIRY", BISON_HAIRY
);
1035 skeleton
= skeleton_find ("BISON_SIMPLE", BISON_SIMPLE
);
1037 output_parser (skeleton
, &table_obstack
);
1042 free_itemsets (void)
1045 for (cp
= first_state
; cp
; cp
= cptmp
)
1054 #define MUSCLE_INSERT_INT(Key, Value) \
1056 obstack_fgrow1 (&muscle_obstack, "%d", Value); \
1057 obstack_1grow (&muscle_obstack, 0); \
1058 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
1061 #define MUSCLE_INSERT_STRING(Key, Value) \
1063 obstack_sgrow (&muscle_obstack, Value); \
1064 obstack_1grow (&muscle_obstack, 0); \
1065 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
1068 #define MUSCLE_INSERT_PREFIX(Key, Value) \
1070 obstack_fgrow2 (&muscle_obstack, "%s%s", spec_name_prefix, Value); \
1071 obstack_1grow (&muscle_obstack, 0); \
1072 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
1078 MUSCLE_INSERT_INT ("last", high
);
1079 MUSCLE_INSERT_INT ("flag", MINSHORT
);
1080 MUSCLE_INSERT_INT ("pure", pure_parser
);
1081 MUSCLE_INSERT_INT ("nsym", nsyms
);
1082 MUSCLE_INSERT_INT ("debug", debug_flag
);
1083 MUSCLE_INSERT_INT ("final", final_state
);
1084 MUSCLE_INSERT_INT ("maxtok", max_user_token_number
);
1085 MUSCLE_INSERT_INT ("ntbase", ntokens
);
1086 MUSCLE_INSERT_INT ("verbose", 0);
1088 MUSCLE_INSERT_INT ("nnts", nvars
);
1089 MUSCLE_INSERT_INT ("nrules", nrules
);
1090 MUSCLE_INSERT_INT ("nstates", nstates
);
1091 MUSCLE_INSERT_INT ("ntokens", ntokens
);
1093 MUSCLE_INSERT_INT ("locations_flag", locations_flag
);
1095 /* We need to save the actions in the muscle %%action. */
1096 muscle_insert ("action", obstack_finish (&action_obstack
));
1098 if (spec_name_prefix
)
1099 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix
);
1102 /*----------------------------------------------------------.
1103 | Output the parsing tables and the parser code to ftable. |
1104 `----------------------------------------------------------*/
1109 obstack_init (&output_obstack
);
1113 output_token_translations ();
1117 if (semantic_parser
)
1119 output_rule_data ();
1120 XFREE (user_toknums
);
1124 if (!no_parser_flag
) */
1127 /* Copy definitions in directive. */
1128 muscle_insert ("prologue", obstack_finish (&attrs_obstack
));
1130 output_master_parser ();
1132 obstack_free (&muscle_obstack
, 0);
1133 obstack_free (&output_obstack
, 0);
1134 obstack_free (&action_obstack
, 0);