]>
Commit | Line | Data |
---|---|---|
c3e23647 | 1 | /* Output the generated parsing program for bison, |
8c7ebe49 | 2 | Copyright 1984, 1986, 1989, 1992, 2000 Free Software Foundation, Inc. |
c3e23647 | 3 | |
9ee3c97b | 4 | This file is part of Bison, the GNU Compiler Compiler. |
c3e23647 | 5 | |
9ee3c97b AD |
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) | |
9 | any later version. | |
c3e23647 | 10 | |
9ee3c97b AD |
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. | |
c3e23647 | 15 | |
9ee3c97b AD |
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 | |
19 | 02111-1307, USA. */ | |
c3e23647 RS |
20 | |
21 | ||
6c89f1c1 AD |
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. | |
c3e23647 | 25 | |
6c89f1c1 AD |
26 | yytranslate = vector mapping yylex's token numbers into bison's token |
27 | numbers. | |
c3e23647 | 28 | |
6c89f1c1 | 29 | ** yytname = vector of string-names indexed by bison token number |
c3e23647 | 30 | |
6c89f1c1 AD |
31 | ** yytoknum = vector of yylex token numbers corresponding to entries |
32 | in yytname | |
c3e23647 | 33 | |
6c89f1c1 | 34 | yyrline = vector of line-numbers of all rules. For yydebug printouts. |
c3e23647 | 35 | |
6c89f1c1 AD |
36 | yyrhs = vector of items of all rules. |
37 | This is exactly what ritems contains. For yydebug and for semantic | |
38 | parser. | |
c3e23647 | 39 | |
6c89f1c1 | 40 | yyprhs[r] = index in yyrhs of first item for rule r. |
c3e23647 | 41 | |
6c89f1c1 | 42 | yyr1[r] = symbol number of symbol that rule r derives. |
c3e23647 | 43 | |
6c89f1c1 | 44 | yyr2[r] = number of symbols composing right hand side of rule r. |
c3e23647 | 45 | |
6c89f1c1 | 46 | * yystos[s] = the symbol number of the symbol that leads to state s. |
e372befa | 47 | |
6c89f1c1 AD |
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. | |
c3e23647 | 51 | |
6c89f1c1 AD |
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. | |
c3e23647 | 55 | |
6c89f1c1 AD |
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. | |
c3e23647 | 59 | |
6c89f1c1 AD |
60 | If the value in yytable is positive, |
61 | we shift the token and go to that state. | |
c3e23647 | 62 | |
6c89f1c1 | 63 | If the value is negative, it is minus a rule number to reduce by. |
c3e23647 | 64 | |
6c89f1c1 | 65 | If the value is zero, the default action from yydefact[s] is used. |
c3e23647 | 66 | |
6c89f1c1 AD |
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. | |
c3e23647 | 73 | |
6c89f1c1 AD |
74 | yytable = a vector filled with portions for different uses, |
75 | found via yypact and yypgoto. | |
c3e23647 | 76 | |
6c89f1c1 AD |
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. | |
c3e23647 | 80 | |
6c89f1c1 AD |
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. | |
c3e23647 | 87 | |
6c89f1c1 AD |
88 | YYFINAL = the state number of the termination state. |
89 | YYFLAG = most negative short int. Used to flag ?? | |
90 | YYNTBASE = ntokens. | |
c3e23647 RS |
91 | */ |
92 | ||
c3e23647 | 93 | #include "system.h" |
8c7ebe49 | 94 | #include "obstack.h" |
14d3eb9b | 95 | #include "quotearg.h" |
ceed8467 | 96 | #include "getargs.h" |
d7913476 | 97 | #include "xalloc.h" |
c3e23647 RS |
98 | #include "files.h" |
99 | #include "gram.h" | |
b2ca4022 | 100 | #include "LR0.h" |
a0f6b076 | 101 | #include "complain.h" |
6c89f1c1 | 102 | #include "output.h" |
720d742f | 103 | #include "lalr.h" |
a70083a3 | 104 | #include "reader.h" |
0619caf0 | 105 | #include "conflicts.h" |
c3e23647 | 106 | |
d019d655 AD |
107 | extern void berror PARAMS((const char *)); |
108 | ||
d2729d44 | 109 | |
c3e23647 RS |
110 | |
111 | static int nvectors; | |
112 | static int nentries; | |
113 | static short **froms; | |
114 | static short **tos; | |
115 | static short *tally; | |
116 | static short *width; | |
117 | static short *actrow; | |
118 | static short *state_count; | |
119 | static short *order; | |
120 | static short *base; | |
121 | static short *pos; | |
122 | static short *table; | |
123 | static short *check; | |
124 | static int lowzero; | |
125 | static int high; | |
126 | ||
127 | ||
128 | ||
d019d655 | 129 | static inline void |
896fe5c1 | 130 | output_short_or_char_table (struct obstack *oout, |
43591cec AD |
131 | const char *comment, |
132 | const char *type, | |
133 | const char *table_name, | |
134 | short *short_table, | |
135 | short first_value, | |
136 | short begin, short end) | |
d019d655 AD |
137 | { |
138 | int i, j; | |
139 | ||
1e9798d5 | 140 | if (comment) |
896fe5c1 | 141 | obstack_fgrow1 (oout, "/* %s. */\n", comment); |
d019d655 | 142 | |
896fe5c1 AD |
143 | obstack_fgrow3 (oout, "static const %s %s[] =\n{\n %6d", |
144 | type, table_name, first_value); | |
1e9798d5 AD |
145 | |
146 | j = 1; | |
d019d655 AD |
147 | for (i = begin; i < end; i++) |
148 | { | |
896fe5c1 | 149 | obstack_1grow (oout, ','); |
d019d655 AD |
150 | |
151 | if (j >= 10) | |
152 | { | |
ff4423cc | 153 | obstack_sgrow (oout, "\n "); |
d019d655 AD |
154 | j = 1; |
155 | } | |
156 | else | |
157 | { | |
158 | j++; | |
159 | } | |
160 | ||
896fe5c1 | 161 | obstack_fgrow1 (oout, "%6d", short_table[i]); |
d019d655 AD |
162 | } |
163 | ||
ff4423cc | 164 | obstack_sgrow (oout, "\n};\n"); |
d019d655 AD |
165 | } |
166 | ||
167 | ||
43591cec | 168 | static inline void |
896fe5c1 | 169 | output_short_table (struct obstack *oout, |
43591cec AD |
170 | const char *comment, |
171 | const char *table_name, | |
172 | short *short_table, | |
173 | short first_value, | |
174 | short begin, short end) | |
175 | { | |
896fe5c1 | 176 | output_short_or_char_table (oout, comment, "short", table_name, short_table, |
43591cec AD |
177 | first_value, begin, end); |
178 | } | |
179 | ||
180 | ||
d019d655 AD |
181 | /*--------------------------------------------------------------. |
182 | | output_headers -- Output constant strings to the beginning of | | |
183 | | certain files. | | |
184 | `--------------------------------------------------------------*/ | |
185 | ||
14d3eb9b AD |
186 | /* Don't put the `%s' insides quotes, since it quotearg puts them. */ |
187 | ||
ceed8467 AD |
188 | #define GUARDSTR \ |
189 | "\n\ | |
14d3eb9b | 190 | #include %s\n\ |
ceed8467 AD |
191 | extern int yyerror;\n\ |
192 | extern int yycost;\n\ | |
193 | extern char * yymsg;\n\ | |
194 | extern YYSTYPE yyval;\n\ | |
195 | \n\ | |
196 | yyguard(n, yyvsp, yylsp)\n\ | |
197 | register int n;\n\ | |
198 | register YYSTYPE *yyvsp;\n\ | |
c3e23647 | 199 | register YYLTYPE *yylsp;\n\ |
ceed8467 AD |
200 | {\n\ |
201 | yyerror = 0;\n\ | |
202 | yycost = 0;\n\ | |
203 | yymsg = 0;\n\ | |
204 | switch (n)\n\ | |
205 | {" | |
206 | ||
207 | #define ACTSTR \ | |
208 | "\n\ | |
14d3eb9b | 209 | #include %s\n\ |
8c7ebe49 AD |
210 | extern YYSTYPE yyval;\n\ |
211 | extern int yychar;\n\ | |
212 | \n\ | |
213 | yyaction(n, yyvsp, yylsp)\n\ | |
214 | register int n;\n\ | |
215 | register YYSTYPE *yyvsp;\n\ | |
216 | register YYLTYPE *yylsp;\n\ | |
217 | {\n\ | |
218 | switch (n)\n\ | |
219 | {" | |
220 | ||
c3e23647 RS |
221 | #define ACTSTR_SIMPLE "\n switch (yyn) {\n" |
222 | ||
c3e23647 | 223 | void |
d2729d44 | 224 | output_headers (void) |
c3e23647 | 225 | { |
14d3eb9b AD |
226 | char *attrsfile_quoted = quotearg_style (c_quoting_style, attrsfile); |
227 | ||
c3e23647 | 228 | if (semantic_parser) |
ea5607fd | 229 | obstack_fgrow1 (&guard_obstack, GUARDSTR, attrsfile_quoted); |
e372befa | 230 | |
89cab50d | 231 | if (no_parser_flag) |
ceed8467 | 232 | return; |
e372befa | 233 | |
8c7ebe49 | 234 | if (semantic_parser) |
14d3eb9b | 235 | obstack_fgrow1 (&action_obstack, ACTSTR, attrsfile_quoted); |
8c7ebe49 | 236 | else |
ff4423cc | 237 | obstack_sgrow (&action_obstack, ACTSTR_SIMPLE); |
8c7ebe49 | 238 | |
c3e23647 RS |
239 | /* Rename certain symbols if -p was specified. */ |
240 | if (spec_name_prefix) | |
241 | { | |
896fe5c1 | 242 | obstack_fgrow1 (&table_obstack, |
14d3eb9b | 243 | "#define yyparse %sparse\n", spec_name_prefix); |
896fe5c1 | 244 | obstack_fgrow1 (&table_obstack, |
14d3eb9b | 245 | "#define yylex %slex\n", spec_name_prefix); |
896fe5c1 | 246 | obstack_fgrow1 (&table_obstack, |
14d3eb9b | 247 | "#define yyerror %serror\n", spec_name_prefix); |
896fe5c1 | 248 | obstack_fgrow1 (&table_obstack, |
14d3eb9b | 249 | "#define yylval %slval\n", spec_name_prefix); |
896fe5c1 | 250 | obstack_fgrow1 (&table_obstack, |
14d3eb9b | 251 | "#define yychar %schar\n", spec_name_prefix); |
896fe5c1 | 252 | obstack_fgrow1 (&table_obstack, |
14d3eb9b | 253 | "#define yydebug %sdebug\n", spec_name_prefix); |
896fe5c1 | 254 | obstack_fgrow1 (&table_obstack, |
14d3eb9b | 255 | "#define yynerrs %snerrs\n", spec_name_prefix); |
c3e23647 RS |
256 | } |
257 | } | |
258 | ||
259 | ||
6c89f1c1 AD |
260 | /*-------------------------------------------------------. |
261 | | Output constant strings to the ends of certain files. | | |
262 | `-------------------------------------------------------*/ | |
263 | ||
c3e23647 | 264 | void |
d2729d44 | 265 | output_trailers (void) |
c3e23647 RS |
266 | { |
267 | if (semantic_parser) | |
ff4423cc | 268 | obstack_sgrow (&guard_obstack, "\n }\n}\n"); |
e372befa | 269 | |
8c7ebe49 | 270 | obstack_1grow (&action_obstack, '\n'); |
9ee3c97b | 271 | |
89cab50d | 272 | if (no_parser_flag) |
ceed8467 | 273 | return; |
e372befa RS |
274 | |
275 | if (semantic_parser) | |
ff4423cc | 276 | obstack_sgrow (&action_obstack, " }\n"); |
14d3eb9b | 277 | |
ff4423cc | 278 | obstack_sgrow (&action_obstack, "}\n"); |
c3e23647 RS |
279 | } |
280 | ||
281 | ||
c3e23647 | 282 | |
4a120d45 | 283 | static void |
d2729d44 | 284 | output_token_translations (void) |
c3e23647 | 285 | { |
ff4423cc | 286 | obstack_sgrow (&table_obstack, "\ |
43591cec | 287 | \n\ |
896fe5c1 AD |
288 | /* YYRANSLATE(YYLEX) -- Bison token number corresponding to YYLEX. */\n"); |
289 | ||
c3e23647 RS |
290 | if (translations) |
291 | { | |
896fe5c1 | 292 | obstack_fgrow2 (&table_obstack, |
43591cec AD |
293 | "#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\ |
294 | \n\ | |
295 | \n", | |
ceed8467 | 296 | max_user_token_number, nsyms); |
9ee3c97b | 297 | |
896fe5c1 | 298 | output_short_or_char_table (&table_obstack, |
43591cec AD |
299 | "YYRANSLATE[YYLEX] -- Bison token number corresponding to YYLEX", |
300 | ntokens < 127 ? "char" : "short", | |
301 | "yytranslate", token_translations, | |
302 | 0, 1, max_user_token_number + 1); | |
c3e23647 RS |
303 | } |
304 | else | |
305 | { | |
ff4423cc | 306 | obstack_sgrow (&table_obstack, |
573c1d9f | 307 | "\n#define YYTRANSLATE(x) (x)\n"); |
9ee3c97b | 308 | } |
c3e23647 RS |
309 | } |
310 | ||
311 | ||
4a120d45 | 312 | static void |
d2729d44 | 313 | output_gram (void) |
c3e23647 | 314 | { |
9ee3c97b | 315 | /* With the ordinary parser, |
e372befa | 316 | yyprhs and yyrhs are needed only for yydebug. */ |
89cab50d AD |
317 | /* With the no_parser option, all tables are generated */ |
318 | if (!semantic_parser && !no_parser_flag) | |
ff4423cc | 319 | obstack_sgrow (&table_obstack, "\n#if YYDEBUG != 0\n"); |
c3e23647 | 320 | |
896fe5c1 | 321 | output_short_table (&table_obstack, NULL, "yyprhs", rrhs, |
d019d655 | 322 | 0, 1, nrules + 1); |
c3e23647 | 323 | |
1e9798d5 AD |
324 | { |
325 | size_t yyrhs_size = 1; | |
326 | short *yyrhs, *sp; | |
327 | int i; | |
c3e23647 | 328 | |
1e9798d5 AD |
329 | for (sp = ritem + 1; *sp; sp++) |
330 | ++yyrhs_size; | |
331 | yyrhs = XMALLOC (short, yyrhs_size); | |
c3e23647 | 332 | |
1e9798d5 AD |
333 | for (sp = ritem + 1, i = 1; *sp; ++sp, ++i) |
334 | yyrhs[i] = *sp > 0 ? *sp : 0; | |
c3e23647 | 335 | |
896fe5c1 | 336 | output_short_table (&table_obstack, NULL, "yyrhs", yyrhs, |
1e9798d5 AD |
337 | ritem[0], 1, yyrhs_size); |
338 | XFREE (yyrhs); | |
339 | } | |
c3e23647 | 340 | |
89cab50d | 341 | if (!semantic_parser && !no_parser_flag) |
ff4423cc | 342 | obstack_sgrow (&table_obstack, "\n#endif\n"); |
c3e23647 RS |
343 | } |
344 | ||
345 | ||
4a120d45 | 346 | static void |
d2729d44 | 347 | output_stos (void) |
c3e23647 | 348 | { |
896fe5c1 | 349 | output_short_table (&table_obstack, NULL, "yystos", accessing_symbol, |
d019d655 | 350 | 0, 1, nstates); |
c3e23647 RS |
351 | } |
352 | ||
353 | ||
4a120d45 | 354 | static void |
d2729d44 | 355 | output_rule_data (void) |
c3e23647 | 356 | { |
6c89f1c1 AD |
357 | int i; |
358 | int j; | |
1e9798d5 | 359 | short *short_tab = NULL; |
c3e23647 | 360 | |
ff4423cc | 361 | obstack_sgrow (&table_obstack, "\n\ |
896fe5c1 | 362 | #if YYDEBUG != 0\n"); |
c3e23647 | 363 | |
896fe5c1 | 364 | output_short_table (&table_obstack, |
1e9798d5 AD |
365 | "YYRLINE[YYN] -- source line where rule number YYN was defined", |
366 | "yyrline", rline, | |
d019d655 | 367 | 0, 1, nrules + 1); |
c3e23647 | 368 | |
ff4423cc | 369 | obstack_sgrow (&table_obstack, "#endif\n\n"); |
e372befa | 370 | |
89cab50d | 371 | if (token_table_flag || no_parser_flag) |
e372befa | 372 | { |
896fe5c1 AD |
373 | obstack_fgrow1 (&table_obstack, "#define YYNTOKENS %d\n", ntokens); |
374 | obstack_fgrow1 (&table_obstack, "#define YYNNTS %d\n", nvars); | |
375 | obstack_fgrow1 (&table_obstack, "#define YYNRULES %d\n", nrules); | |
376 | obstack_fgrow1 (&table_obstack, "#define YYNSTATES %d\n", nstates); | |
377 | obstack_fgrow1 (&table_obstack, "#define YYMAXUTOK %d\n\n", | |
378 | max_user_token_number); | |
e372befa RS |
379 | } |
380 | ||
c3e23647 | 381 | /* Output the table of symbol names. */ |
1e9798d5 | 382 | if (!token_table_flag && !no_parser_flag) |
ff4423cc | 383 | obstack_sgrow (&table_obstack, |
573c1d9f | 384 | "\n#if YYDEBUG != 0 || defined YYERROR_VERBOSE\n\n"); |
ff4423cc | 385 | obstack_sgrow (&table_obstack, "\ |
896fe5c1 | 386 | /* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */\n"); |
ff4423cc | 387 | obstack_sgrow (&table_obstack, |
1e9798d5 | 388 | "static const char *const yytname[] =\n{\n "); |
c3e23647 | 389 | |
1e9798d5 AD |
390 | j = 0; |
391 | for (i = 0; i < nsyms; i++) | |
ceed8467 AD |
392 | /* this used to be i<=nsyms, but that output a final "" symbol |
393 | almost by accident */ | |
c3e23647 | 394 | { |
1e9798d5 AD |
395 | /* Width of the next token, including the two quotes, the coma |
396 | and the space. */ | |
397 | int strsize = 4; | |
6c89f1c1 | 398 | char *p; |
c3e23647 | 399 | |
1e9798d5 AD |
400 | for (p = tags[i]; p && *p; p++) |
401 | if (*p == '"' || *p == '\\' || *p == '\n' || *p == '\t' | |
402 | || *p == '\b') | |
403 | strsize += 2; | |
404 | else if (*p < 040 || *p >= 0177) | |
405 | strsize += 4; | |
406 | else | |
407 | strsize++; | |
408 | ||
409 | if (j + strsize > 75) | |
c3e23647 | 410 | { |
ff4423cc | 411 | obstack_sgrow (&table_obstack, "\n "); |
1e9798d5 | 412 | j = 2; |
c3e23647 RS |
413 | } |
414 | ||
896fe5c1 | 415 | obstack_1grow (&table_obstack, '\"'); |
c3e23647 RS |
416 | for (p = tags[i]; p && *p; p++) |
417 | { | |
418 | if (*p == '"' || *p == '\\') | |
573c1d9f | 419 | obstack_fgrow1 (&table_obstack, "\\%c", *p); |
c3e23647 | 420 | else if (*p == '\n') |
ff4423cc | 421 | obstack_sgrow (&table_obstack, "\\n"); |
c3e23647 | 422 | else if (*p == '\t') |
ff4423cc | 423 | obstack_sgrow (&table_obstack, "\\t"); |
c3e23647 | 424 | else if (*p == '\b') |
ff4423cc | 425 | obstack_sgrow (&table_obstack, "\\b"); |
c3e23647 | 426 | else if (*p < 040 || *p >= 0177) |
573c1d9f | 427 | obstack_fgrow1 (&table_obstack, "\\%03o", *p); |
c3e23647 | 428 | else |
573c1d9f | 429 | obstack_1grow (&table_obstack, *p); |
c3e23647 RS |
430 | } |
431 | ||
ff4423cc | 432 | obstack_sgrow (&table_obstack, "\", "); |
1e9798d5 | 433 | j += strsize; |
c3e23647 | 434 | } |
9ee3c97b | 435 | /* add a NULL entry to list of tokens */ |
ff4423cc | 436 | obstack_sgrow (&table_obstack, "NULL\n};\n"); |
c3e23647 | 437 | |
89cab50d | 438 | if (!token_table_flag && !no_parser_flag) |
ff4423cc | 439 | obstack_sgrow (&table_obstack, "#endif\n\n"); |
e372befa | 440 | |
9ee3c97b | 441 | /* Output YYTOKNUM. */ |
89cab50d | 442 | if (token_table_flag) |
e372befa | 443 | { |
896fe5c1 | 444 | output_short_table (&table_obstack, |
1e9798d5 AD |
445 | "YYTOKNUM[YYLEX] -- Index in YYTNAME corresponding to YYLEX", |
446 | "yytoknum", user_toknums, | |
d019d655 | 447 | 0, 1, ntokens + 1); |
e372befa RS |
448 | } |
449 | ||
9ee3c97b | 450 | /* Output YYR1. */ |
896fe5c1 | 451 | output_short_table (&table_obstack, |
1e9798d5 AD |
452 | "YYR1[YYN] -- Symbol number of symbol that rule YYN derives", |
453 | "yyr1", rlhs, | |
d019d655 | 454 | 0, 1, nrules + 1); |
d7913476 | 455 | XFREE (rlhs + 1); |
d019d655 | 456 | |
896fe5c1 | 457 | obstack_1grow (&table_obstack, '\n'); |
9ee3c97b AD |
458 | |
459 | /* Output YYR2. */ | |
1e9798d5 | 460 | short_tab = XMALLOC (short, nrules + 1); |
c3e23647 | 461 | for (i = 1; i < nrules; i++) |
1e9798d5 AD |
462 | short_tab[i] = rrhs[i + 1] - rrhs[i] - 1; |
463 | short_tab[nrules] = nitems - rrhs[nrules] - 1; | |
896fe5c1 | 464 | output_short_table (&table_obstack, |
1e9798d5 AD |
465 | "YYR2[YYN] -- Number of symbols composing right hand side of rule YYN", |
466 | "yyr2", short_tab, | |
467 | 0, 1, nrules + 1); | |
896fe5c1 | 468 | obstack_1grow (&table_obstack, '\n'); |
c3e23647 | 469 | |
1e9798d5 | 470 | XFREE (short_tab); |
c3e23647 | 471 | |
d7913476 | 472 | XFREE (rrhs + 1); |
c3e23647 RS |
473 | } |
474 | ||
475 | ||
4a120d45 | 476 | static void |
d2729d44 | 477 | output_defines (void) |
c3e23647 | 478 | { |
896fe5c1 AD |
479 | obstack_fgrow1 (&table_obstack, "\n\n#define\tYYFINAL\t\t%d\n", final_state); |
480 | obstack_fgrow1 (&table_obstack, "#define\tYYFLAG\t\t%d\n", MINSHORT); | |
481 | obstack_fgrow1 (&table_obstack, "#define\tYYNTBASE\t%d\n", ntokens); | |
c3e23647 RS |
482 | } |
483 | ||
484 | ||
6c89f1c1 AD |
485 | /*------------------------------------------------------------------. |
486 | | Decide what to do for each type of token if seen as the lookahead | | |
487 | | token in specified state. The value returned is used as the | | |
488 | | default action (yydefact) for the state. In addition, actrow is | | |
489 | | filled with what to do for each kind of token, index by symbol | | |
490 | | number, with zero meaning do the default action. The value | | |
491 | | MINSHORT, a very negative number, means this situation is an | | |
492 | | error. The parser recognizes this value specially. | | |
493 | | | | |
494 | | This is where conflicts are resolved. The loop over lookahead | | |
495 | | rules considered lower-numbered rules last, and the last rule | | |
496 | | considered that likes a token gets to handle it. | | |
497 | `------------------------------------------------------------------*/ | |
c3e23647 | 498 | |
4a120d45 | 499 | static int |
d2729d44 | 500 | action_row (int state) |
c3e23647 | 501 | { |
6c89f1c1 AD |
502 | int i; |
503 | int j; | |
504 | int k; | |
505 | int m = 0; | |
506 | int n = 0; | |
507 | int count; | |
508 | int default_rule; | |
509 | int nreds; | |
510 | int max; | |
511 | int rule; | |
512 | int shift_state; | |
513 | int symbol; | |
514 | unsigned mask; | |
515 | unsigned *wordp; | |
516 | reductions *redp; | |
517 | shifts *shiftp; | |
518 | errs *errp; | |
ceed8467 | 519 | int nodefault = 0; /* set nonzero to inhibit having any default reduction */ |
c3e23647 RS |
520 | |
521 | for (i = 0; i < ntokens; i++) | |
522 | actrow[i] = 0; | |
523 | ||
524 | default_rule = 0; | |
525 | nreds = 0; | |
526 | redp = reduction_table[state]; | |
527 | ||
528 | if (redp) | |
529 | { | |
530 | nreds = redp->nreds; | |
531 | ||
532 | if (nreds >= 1) | |
533 | { | |
36281465 AD |
534 | /* loop over all the rules available here which require |
535 | lookahead */ | |
c3e23647 RS |
536 | m = lookaheads[state]; |
537 | n = lookaheads[state + 1]; | |
538 | ||
539 | for (i = n - 1; i >= m; i--) | |
540 | { | |
ceed8467 | 541 | rule = -LAruleno[i]; |
c3e23647 RS |
542 | wordp = LA + i * tokensetsize; |
543 | mask = 1; | |
544 | ||
36281465 | 545 | /* and find each token which the rule finds acceptable |
ceed8467 | 546 | to come next */ |
c3e23647 RS |
547 | for (j = 0; j < ntokens; j++) |
548 | { | |
36281465 AD |
549 | /* and record this rule as the rule to use if that |
550 | token follows. */ | |
c3e23647 RS |
551 | if (mask & *wordp) |
552 | actrow[j] = rule; | |
553 | ||
554 | mask <<= 1; | |
555 | if (mask == 0) | |
556 | { | |
557 | mask = 1; | |
558 | wordp++; | |
559 | } | |
560 | } | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
565 | shiftp = shift_table[state]; | |
566 | ||
36281465 AD |
567 | /* Now see which tokens are allowed for shifts in this state. For |
568 | them, record the shift as the thing to do. So shift is preferred | |
569 | to reduce. */ | |
c3e23647 RS |
570 | |
571 | if (shiftp) | |
572 | { | |
573 | k = shiftp->nshifts; | |
574 | ||
575 | for (i = 0; i < k; i++) | |
576 | { | |
577 | shift_state = shiftp->shifts[i]; | |
ceed8467 AD |
578 | if (!shift_state) |
579 | continue; | |
c3e23647 RS |
580 | |
581 | symbol = accessing_symbol[shift_state]; | |
582 | ||
ceed8467 | 583 | if (ISVAR (symbol)) |
c3e23647 RS |
584 | break; |
585 | ||
586 | actrow[symbol] = shift_state; | |
587 | ||
36281465 AD |
588 | /* Do not use any default reduction if there is a shift for |
589 | error */ | |
590 | if (symbol == error_token_number) | |
591 | nodefault = 1; | |
c3e23647 RS |
592 | } |
593 | } | |
594 | ||
595 | errp = err_table[state]; | |
596 | ||
36281465 AD |
597 | /* See which tokens are an explicit error in this state (due to |
598 | %nonassoc). For them, record MINSHORT as the action. */ | |
c3e23647 RS |
599 | |
600 | if (errp) | |
601 | { | |
602 | k = errp->nerrs; | |
603 | ||
604 | for (i = 0; i < k; i++) | |
605 | { | |
606 | symbol = errp->errs[i]; | |
607 | actrow[symbol] = MINSHORT; | |
608 | } | |
609 | } | |
610 | ||
36281465 AD |
611 | /* Now find the most common reduction and make it the default action |
612 | for this state. */ | |
c3e23647 | 613 | |
ceed8467 | 614 | if (nreds >= 1 && !nodefault) |
c3e23647 RS |
615 | { |
616 | if (consistent[state]) | |
617 | default_rule = redp->rules[0]; | |
618 | else | |
619 | { | |
620 | max = 0; | |
621 | for (i = m; i < n; i++) | |
622 | { | |
623 | count = 0; | |
ceed8467 | 624 | rule = -LAruleno[i]; |
9ee3c97b | 625 | |
c3e23647 RS |
626 | for (j = 0; j < ntokens; j++) |
627 | { | |
628 | if (actrow[j] == rule) | |
629 | count++; | |
630 | } | |
9ee3c97b | 631 | |
c3e23647 RS |
632 | if (count > max) |
633 | { | |
634 | max = count; | |
635 | default_rule = rule; | |
636 | } | |
637 | } | |
9ee3c97b | 638 | |
c3e23647 RS |
639 | /* actions which match the default are replaced with zero, |
640 | which means "use the default" */ | |
9ee3c97b | 641 | |
c3e23647 RS |
642 | if (max > 0) |
643 | { | |
644 | for (j = 0; j < ntokens; j++) | |
645 | { | |
646 | if (actrow[j] == default_rule) | |
647 | actrow[j] = 0; | |
648 | } | |
9ee3c97b | 649 | |
ceed8467 | 650 | default_rule = -default_rule; |
c3e23647 RS |
651 | } |
652 | } | |
653 | } | |
654 | ||
655 | /* If have no default rule, the default is an error. | |
656 | So replace any action which says "error" with "use default". */ | |
657 | ||
658 | if (default_rule == 0) | |
659 | for (j = 0; j < ntokens; j++) | |
660 | { | |
661 | if (actrow[j] == MINSHORT) | |
662 | actrow[j] = 0; | |
663 | } | |
664 | ||
36281465 | 665 | return default_rule; |
c3e23647 RS |
666 | } |
667 | ||
668 | ||
4a120d45 | 669 | static void |
d2729d44 | 670 | save_row (int state) |
c3e23647 | 671 | { |
6c89f1c1 AD |
672 | int i; |
673 | int count; | |
674 | short *sp; | |
675 | short *sp1; | |
676 | short *sp2; | |
c3e23647 RS |
677 | |
678 | count = 0; | |
679 | for (i = 0; i < ntokens; i++) | |
680 | { | |
681 | if (actrow[i] != 0) | |
682 | count++; | |
683 | } | |
684 | ||
685 | if (count == 0) | |
686 | return; | |
687 | ||
d7913476 AD |
688 | froms[state] = sp1 = sp = XCALLOC (short, count); |
689 | tos[state] = sp2 = XCALLOC (short, count); | |
c3e23647 RS |
690 | |
691 | for (i = 0; i < ntokens; i++) | |
692 | { | |
693 | if (actrow[i] != 0) | |
694 | { | |
695 | *sp1++ = i; | |
696 | *sp2++ = actrow[i]; | |
697 | } | |
698 | } | |
699 | ||
700 | tally[state] = count; | |
701 | width[state] = sp1[-1] - sp[0] + 1; | |
702 | } | |
703 | ||
704 | ||
6c89f1c1 AD |
705 | /*------------------------------------------------------------------. |
706 | | Figure out the actions for the specified state, indexed by | | |
707 | | lookahead token type. | | |
708 | | | | |
f2acea59 AD |
709 | | The YYDEFACT table is output now. The detailed info is saved for | |
710 | | putting into YYTABLE later. | | |
6c89f1c1 | 711 | `------------------------------------------------------------------*/ |
c3e23647 | 712 | |
4a120d45 | 713 | static void |
6c89f1c1 | 714 | token_actions (void) |
c3e23647 | 715 | { |
6c89f1c1 | 716 | int i; |
d7913476 | 717 | short *yydefact = XCALLOC (short, nstates); |
c3e23647 | 718 | |
d7913476 | 719 | actrow = XCALLOC (short, ntokens); |
f2acea59 | 720 | for (i = 0; i < nstates; ++i) |
c3e23647 | 721 | { |
f2acea59 | 722 | yydefact[i] = action_row (i); |
6c89f1c1 | 723 | save_row (i); |
c3e23647 | 724 | } |
d7913476 | 725 | XFREE (actrow); |
f2acea59 | 726 | |
896fe5c1 | 727 | output_short_table (&table_obstack, |
43591cec AD |
728 | "YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE\n\ |
729 | doesn't specify something else to do. Zero means the default is an\n\ | |
730 | error", | |
731 | "yydefact", yydefact, | |
f2acea59 | 732 | yydefact[0], 1, nstates); |
896fe5c1 | 733 | obstack_1grow (&table_obstack, '\n'); |
d7913476 | 734 | XFREE (yydefact); |
c3e23647 RS |
735 | } |
736 | ||
737 | ||
6c89f1c1 AD |
738 | static void |
739 | free_shifts (void) | |
c3e23647 | 740 | { |
6c89f1c1 | 741 | shifts *sp, *sptmp; /* JF derefrenced freed ptr */ |
c3e23647 | 742 | |
d7913476 | 743 | XFREE (shift_table); |
c3e23647 | 744 | |
6c89f1c1 AD |
745 | for (sp = first_shift; sp; sp = sptmp) |
746 | { | |
747 | sptmp = sp->next; | |
d7913476 | 748 | XFREE (sp); |
6c89f1c1 AD |
749 | } |
750 | } | |
c3e23647 | 751 | |
c3e23647 | 752 | |
6c89f1c1 AD |
753 | static void |
754 | free_reductions (void) | |
755 | { | |
756 | reductions *rp, *rptmp; /* JF fixed freed ptr */ | |
c3e23647 | 757 | |
d7913476 | 758 | XFREE (reduction_table); |
c3e23647 | 759 | |
6c89f1c1 | 760 | for (rp = first_reduction; rp; rp = rptmp) |
c3e23647 | 761 | { |
6c89f1c1 | 762 | rptmp = rp->next; |
d7913476 | 763 | XFREE (rp); |
c3e23647 | 764 | } |
c3e23647 RS |
765 | } |
766 | ||
767 | ||
6c89f1c1 | 768 | |
4a120d45 | 769 | static void |
d2729d44 | 770 | save_column (int symbol, int default_state) |
c3e23647 | 771 | { |
6c89f1c1 | 772 | int i; |
6c89f1c1 AD |
773 | short *sp; |
774 | short *sp1; | |
775 | short *sp2; | |
776 | int count; | |
777 | int symno; | |
c3e23647 | 778 | |
43591cec AD |
779 | short begin = goto_map[symbol]; |
780 | short end = goto_map[symbol + 1]; | |
c3e23647 RS |
781 | |
782 | count = 0; | |
43591cec | 783 | for (i = begin; i < end; i++) |
c3e23647 RS |
784 | { |
785 | if (to_state[i] != default_state) | |
786 | count++; | |
787 | } | |
788 | ||
789 | if (count == 0) | |
790 | return; | |
791 | ||
792 | symno = symbol - ntokens + nstates; | |
793 | ||
d7913476 AD |
794 | froms[symno] = sp1 = sp = XCALLOC (short, count); |
795 | tos[symno] = sp2 = XCALLOC (short, count); | |
c3e23647 | 796 | |
43591cec | 797 | for (i = begin; i < end; i++) |
c3e23647 RS |
798 | { |
799 | if (to_state[i] != default_state) | |
800 | { | |
801 | *sp1++ = from_state[i]; | |
802 | *sp2++ = to_state[i]; | |
803 | } | |
804 | } | |
805 | ||
806 | tally[symno] = count; | |
807 | width[symno] = sp1[-1] - sp[0] + 1; | |
808 | } | |
809 | ||
6c89f1c1 AD |
810 | static int |
811 | default_goto (int symbol) | |
812 | { | |
813 | int i; | |
814 | int m; | |
815 | int n; | |
816 | int default_state; | |
817 | int max; | |
818 | ||
819 | m = goto_map[symbol]; | |
820 | n = goto_map[symbol + 1]; | |
821 | ||
822 | if (m == n) | |
823 | return -1; | |
824 | ||
825 | for (i = 0; i < nstates; i++) | |
826 | state_count[i] = 0; | |
827 | ||
828 | for (i = m; i < n; i++) | |
829 | state_count[to_state[i]]++; | |
830 | ||
831 | max = 0; | |
832 | default_state = -1; | |
833 | ||
834 | for (i = 0; i < nstates; i++) | |
835 | { | |
836 | if (state_count[i] > max) | |
837 | { | |
838 | max = state_count[i]; | |
839 | default_state = i; | |
840 | } | |
841 | } | |
842 | ||
843 | return default_state; | |
844 | } | |
845 | ||
846 | ||
847 | /*-------------------------------------------------------------------. | |
848 | | Figure out what to do after reducing with each rule, depending on | | |
849 | | the saved state from before the beginning of parsing the data that | | |
850 | | matched this rule. | | |
851 | | | | |
852 | | The YYDEFGOTO table is output now. The detailed info is saved for | | |
853 | | putting into YYTABLE later. | | |
854 | `-------------------------------------------------------------------*/ | |
855 | ||
856 | static void | |
857 | goto_actions (void) | |
858 | { | |
43591cec | 859 | int i; |
6c89f1c1 | 860 | |
43591cec | 861 | short *yydefgoto = XMALLOC (short, nsyms - ntokens); |
d7913476 | 862 | state_count = XCALLOC (short, nstates); |
6c89f1c1 | 863 | |
43591cec | 864 | for (i = ntokens; i < nsyms; ++i) |
6c89f1c1 | 865 | { |
43591cec AD |
866 | int default_state = default_goto (i); |
867 | save_column (i, default_state); | |
868 | yydefgoto[i - ntokens] = default_state; | |
6c89f1c1 AD |
869 | } |
870 | ||
896fe5c1 | 871 | output_short_table (&table_obstack, NULL, "yydefgoto", yydefgoto, |
43591cec AD |
872 | yydefgoto[0], 1, nsyms - ntokens); |
873 | ||
d7913476 | 874 | XFREE (state_count); |
43591cec | 875 | XFREE (yydefgoto); |
6c89f1c1 | 876 | } |
c3e23647 RS |
877 | |
878 | ||
9ee3c97b AD |
879 | /* The next few functions decide how to pack the actions and gotos |
880 | information into yytable. */ | |
c3e23647 | 881 | |
4a120d45 | 882 | static void |
d2729d44 | 883 | sort_actions (void) |
c3e23647 | 884 | { |
6c89f1c1 AD |
885 | int i; |
886 | int j; | |
887 | int k; | |
888 | int t; | |
889 | int w; | |
c3e23647 | 890 | |
d7913476 | 891 | order = XCALLOC (short, nvectors); |
c3e23647 RS |
892 | nentries = 0; |
893 | ||
894 | for (i = 0; i < nvectors; i++) | |
895 | { | |
896 | if (tally[i] > 0) | |
897 | { | |
898 | t = tally[i]; | |
899 | w = width[i]; | |
900 | j = nentries - 1; | |
901 | ||
902 | while (j >= 0 && (width[order[j]] < w)) | |
903 | j--; | |
904 | ||
905 | while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t)) | |
906 | j--; | |
907 | ||
908 | for (k = nentries - 1; k > j; k--) | |
909 | order[k + 1] = order[k]; | |
910 | ||
911 | order[j + 1] = i; | |
912 | nentries++; | |
913 | } | |
914 | } | |
915 | } | |
916 | ||
917 | ||
4a120d45 | 918 | static int |
d2729d44 | 919 | matching_state (int vector) |
c3e23647 | 920 | { |
6c89f1c1 AD |
921 | int i; |
922 | int j; | |
923 | int k; | |
924 | int t; | |
925 | int w; | |
926 | int match; | |
927 | int prev; | |
c3e23647 RS |
928 | |
929 | i = order[vector]; | |
930 | if (i >= nstates) | |
36281465 | 931 | return -1; |
c3e23647 RS |
932 | |
933 | t = tally[i]; | |
934 | w = width[i]; | |
935 | ||
936 | for (prev = vector - 1; prev >= 0; prev--) | |
937 | { | |
938 | j = order[prev]; | |
939 | if (width[j] != w || tally[j] != t) | |
36281465 | 940 | return -1; |
c3e23647 RS |
941 | |
942 | match = 1; | |
943 | for (k = 0; match && k < t; k++) | |
944 | { | |
945 | if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k]) | |
946 | match = 0; | |
947 | } | |
948 | ||
949 | if (match) | |
36281465 | 950 | return j; |
c3e23647 RS |
951 | } |
952 | ||
36281465 | 953 | return -1; |
c3e23647 RS |
954 | } |
955 | ||
956 | ||
4a120d45 | 957 | static int |
d2729d44 | 958 | pack_vector (int vector) |
c3e23647 | 959 | { |
6c89f1c1 AD |
960 | int i; |
961 | int j; | |
962 | int k; | |
963 | int t; | |
964 | int loc = 0; | |
965 | int ok; | |
966 | short *from; | |
967 | short *to; | |
c3e23647 RS |
968 | |
969 | i = order[vector]; | |
970 | t = tally[i]; | |
971 | ||
340ef489 | 972 | assert (t); |
c3e23647 RS |
973 | |
974 | from = froms[i]; | |
975 | to = tos[i]; | |
976 | ||
977 | for (j = lowzero - from[0]; j < MAXTABLE; j++) | |
978 | { | |
979 | ok = 1; | |
980 | ||
981 | for (k = 0; ok && k < t; k++) | |
982 | { | |
983 | loc = j + from[k]; | |
984 | if (loc > MAXTABLE) | |
a0f6b076 | 985 | fatal (_("maximum table size (%d) exceeded"), MAXTABLE); |
c3e23647 RS |
986 | |
987 | if (table[loc] != 0) | |
988 | ok = 0; | |
989 | } | |
990 | ||
991 | for (k = 0; ok && k < vector; k++) | |
992 | { | |
993 | if (pos[k] == j) | |
994 | ok = 0; | |
995 | } | |
996 | ||
997 | if (ok) | |
998 | { | |
999 | for (k = 0; k < t; k++) | |
1000 | { | |
1001 | loc = j + from[k]; | |
1002 | table[loc] = to[k]; | |
1003 | check[loc] = from[k]; | |
1004 | } | |
1005 | ||
1006 | while (table[lowzero] != 0) | |
1007 | lowzero++; | |
1008 | ||
1009 | if (loc > high) | |
1010 | high = loc; | |
1011 | ||
36281465 | 1012 | return j; |
c3e23647 RS |
1013 | } |
1014 | } | |
1015 | ||
ceed8467 AD |
1016 | berror ("pack_vector"); |
1017 | return 0; /* JF keep lint happy */ | |
c3e23647 RS |
1018 | } |
1019 | ||
1020 | ||
6c89f1c1 AD |
1021 | static void |
1022 | pack_table (void) | |
1023 | { | |
1024 | int i; | |
1025 | int place; | |
1026 | int state; | |
1027 | ||
d7913476 AD |
1028 | base = XCALLOC (short, nvectors); |
1029 | pos = XCALLOC (short, nentries); | |
1030 | table = XCALLOC (short, MAXTABLE); | |
1031 | check = XCALLOC (short, MAXTABLE); | |
6c89f1c1 AD |
1032 | |
1033 | lowzero = 0; | |
1034 | high = 0; | |
1035 | ||
1036 | for (i = 0; i < nvectors; i++) | |
1037 | base[i] = MINSHORT; | |
1038 | ||
1039 | for (i = 0; i < MAXTABLE; i++) | |
1040 | check[i] = -1; | |
1041 | ||
1042 | for (i = 0; i < nentries; i++) | |
1043 | { | |
1044 | state = matching_state (i); | |
1045 | ||
1046 | if (state < 0) | |
1047 | place = pack_vector (i); | |
1048 | else | |
1049 | place = base[state]; | |
1050 | ||
1051 | pos[i] = place; | |
1052 | base[order[i]] = place; | |
1053 | } | |
1054 | ||
1055 | for (i = 0; i < nvectors; i++) | |
1056 | { | |
1057 | if (froms[i]) | |
d7913476 | 1058 | XFREE (froms[i]); |
6c89f1c1 | 1059 | if (tos[i]) |
d7913476 | 1060 | XFREE (tos[i]); |
6c89f1c1 AD |
1061 | } |
1062 | ||
d7913476 AD |
1063 | XFREE (froms); |
1064 | XFREE (tos); | |
1065 | XFREE (pos); | |
6c89f1c1 | 1066 | } |
c3e23647 RS |
1067 | |
1068 | /* the following functions output yytable, yycheck | |
1069 | and the vectors whose elements index the portion starts */ | |
1070 | ||
4a120d45 | 1071 | static void |
d2729d44 | 1072 | output_base (void) |
c3e23647 | 1073 | { |
896fe5c1 | 1074 | output_short_table (&table_obstack, NULL, "yypact", base, |
d019d655 | 1075 | base[0], 1, nstates); |
c3e23647 | 1076 | |
896fe5c1 | 1077 | obstack_1grow (&table_obstack, '\n'); |
c3e23647 | 1078 | |
896fe5c1 | 1079 | output_short_table (&table_obstack, NULL, "yypgoto", base, |
d019d655 | 1080 | base[nstates], nstates + 1, nvectors); |
c3e23647 | 1081 | |
d7913476 | 1082 | XFREE (base); |
c3e23647 RS |
1083 | } |
1084 | ||
1085 | ||
4a120d45 | 1086 | static void |
d2729d44 | 1087 | output_table (void) |
c3e23647 | 1088 | { |
896fe5c1 AD |
1089 | obstack_fgrow1 (&table_obstack, "\n\n#define\tYYLAST\t\t%d\n\n\n", high); |
1090 | output_short_table (&table_obstack, NULL, "yytable", table, | |
d019d655 | 1091 | table[0], 1, high + 1); |
d7913476 | 1092 | XFREE (table); |
c3e23647 RS |
1093 | } |
1094 | ||
1095 | ||
4a120d45 | 1096 | static void |
d2729d44 | 1097 | output_check (void) |
c3e23647 | 1098 | { |
896fe5c1 | 1099 | output_short_table (&table_obstack, NULL, "yycheck", check, |
d019d655 | 1100 | check[0], 1, high + 1); |
d7913476 | 1101 | XFREE (check); |
c3e23647 RS |
1102 | } |
1103 | ||
6c89f1c1 AD |
1104 | /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
1105 | and yycheck. */ | |
1106 | ||
1107 | static void | |
1108 | output_actions (void) | |
1109 | { | |
1110 | nvectors = nstates + nvars; | |
c3e23647 | 1111 | |
d7913476 AD |
1112 | froms = XCALLOC (short *, nvectors); |
1113 | tos = XCALLOC (short *, nvectors); | |
1114 | tally = XCALLOC (short, nvectors); | |
1115 | width = XCALLOC (short, nvectors); | |
6c89f1c1 AD |
1116 | |
1117 | token_actions (); | |
1118 | free_shifts (); | |
1119 | free_reductions (); | |
d7913476 AD |
1120 | XFREE (lookaheads); |
1121 | XFREE (LA); | |
1122 | XFREE (LAruleno); | |
1123 | XFREE (accessing_symbol); | |
6c89f1c1 AD |
1124 | |
1125 | goto_actions (); | |
d7913476 AD |
1126 | XFREE (goto_map + ntokens); |
1127 | XFREE (from_state); | |
1128 | XFREE (to_state); | |
6c89f1c1 AD |
1129 | |
1130 | sort_actions (); | |
1131 | pack_table (); | |
896fe5c1 | 1132 | obstack_1grow (&table_obstack, '\n'); |
6c89f1c1 AD |
1133 | output_base (); |
1134 | output_table (); | |
896fe5c1 | 1135 | obstack_1grow (&table_obstack, '\n'); |
6c89f1c1 AD |
1136 | output_check (); |
1137 | } | |
c3e23647 | 1138 | |
ff61dabd AD |
1139 | /*------------------------------------------. |
1140 | | Copy the parser code into TABLE_OBSTACK. | | |
1141 | `------------------------------------------*/ | |
c3e23647 | 1142 | |
4a120d45 | 1143 | static void |
d2729d44 | 1144 | output_parser (void) |
c3e23647 | 1145 | { |
6c89f1c1 | 1146 | int c; |
ff61dabd | 1147 | FILE *fskel; |
ef7ddedd AD |
1148 | size_t line; |
1149 | const char *skeleton = NULL; | |
573c1d9f | 1150 | int actions_dumped = 0; |
c3e23647 RS |
1151 | |
1152 | if (pure_parser) | |
ff4423cc | 1153 | obstack_sgrow (&table_obstack, "#define YYPURE 1\n\n"); |
c3e23647 | 1154 | |
ff61dabd | 1155 | /* Loop over lines in the standard parser file. */ |
c3e23647 | 1156 | if (semantic_parser) |
ef7ddedd | 1157 | skeleton = skeleton_find ("BISON_HAIRY", BISON_HAIRY); |
ceed8467 | 1158 | else |
ef7ddedd AD |
1159 | skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE); |
1160 | fskel = xfopen (skeleton, "r"); | |
1161 | ||
1162 | /* Set LINE to 2, not 1: `#line LINENUM' -- Here LINENUM is a | |
1163 | decimal integer constant. This specifies that the line number of | |
1164 | the *following* line of input, in its original source file, was | |
1165 | LINENUM. */ | |
1166 | line = 2; | |
c3e23647 RS |
1167 | |
1168 | while (1) | |
1169 | { | |
573c1d9f AD |
1170 | enum line_type_e |
1171 | { | |
1172 | regular_line, | |
1173 | sync_line, /* #line. */ | |
1174 | actions_line /* %% actions. */ | |
1175 | }; | |
1176 | enum line_type_e line_type = regular_line; | |
c3e23647 | 1177 | |
ff61dabd | 1178 | c = getc (fskel); |
c3e23647 | 1179 | |
573c1d9f | 1180 | /* Is this line special? */ |
ef7ddedd | 1181 | if (c == '#') |
573c1d9f AD |
1182 | { |
1183 | /* See if it's a `#line' line. */ | |
1184 | if ((c = getc (fskel)) == 'l') | |
1185 | if ((c = getc (fskel)) == 'i') | |
1186 | if ((c = getc (fskel)) == 'n') | |
1187 | if ((c = getc (fskel)) == 'e') | |
1188 | line_type = sync_line; | |
1189 | else | |
ff4423cc | 1190 | obstack_sgrow (&table_obstack, "#lin"); |
ef7ddedd | 1191 | else |
ff4423cc | 1192 | obstack_sgrow (&table_obstack, "#li"); |
c3e23647 | 1193 | else |
ff4423cc | 1194 | obstack_sgrow (&table_obstack, "#l"); |
ef7ddedd | 1195 | else |
ff4423cc | 1196 | obstack_sgrow (&table_obstack, "#"); |
573c1d9f AD |
1197 | } |
1198 | else if (c == '%') | |
1199 | { | |
1200 | /* See if it's a `%% actions' line. */ | |
1201 | if ((c = getc (fskel)) == '%') | |
1202 | if ((c = getc (fskel)) == ' ') | |
1203 | if ((c = getc (fskel)) == 'a') | |
1204 | if ((c = getc (fskel)) == 'c') | |
1205 | if ((c = getc (fskel)) == 't') | |
1206 | if ((c = getc (fskel)) == 'i') | |
1207 | if ((c = getc (fskel)) == 'o') | |
1208 | if ((c = getc (fskel)) == 'n') | |
1209 | if ((c = getc (fskel)) == 's') | |
1210 | line_type = actions_line; | |
1211 | else | |
ff4423cc | 1212 | obstack_sgrow (&table_obstack, "%% action"); |
573c1d9f | 1213 | else |
ff4423cc | 1214 | obstack_sgrow (&table_obstack, "%% actio"); |
573c1d9f | 1215 | else |
ff4423cc | 1216 | obstack_sgrow (&table_obstack, "%% acti"); |
573c1d9f | 1217 | else |
ff4423cc | 1218 | obstack_sgrow (&table_obstack, "%% act"); |
573c1d9f | 1219 | else |
ff4423cc | 1220 | obstack_sgrow (&table_obstack, "%% ac"); |
573c1d9f | 1221 | else |
ff4423cc | 1222 | obstack_sgrow (&table_obstack, "%% a"); |
573c1d9f | 1223 | else |
ff4423cc | 1224 | obstack_sgrow (&table_obstack, "%% "); |
573c1d9f | 1225 | else |
ff4423cc | 1226 | obstack_sgrow (&table_obstack, "%%"); |
573c1d9f | 1227 | else |
ff4423cc | 1228 | obstack_sgrow (&table_obstack, "%"); |
573c1d9f | 1229 | } |
ef7ddedd | 1230 | |
573c1d9f AD |
1231 | switch (line_type) |
1232 | { | |
1233 | case sync_line: | |
1234 | if (!no_lines_flag) | |
1235 | obstack_fgrow2 (&table_obstack, "#line %d %s\n", | |
1236 | line, quotearg_style (c_quoting_style, skeleton)); | |
ef7ddedd | 1237 | |
573c1d9f AD |
1238 | /* Skip the end of line. */ |
1239 | for (; c != '\n' && c != EOF; c = getc (fskel)) | |
1240 | /* nothing */; | |
1241 | break; | |
c3e23647 | 1242 | |
573c1d9f | 1243 | case actions_line: |
ceed8467 | 1244 | { |
573c1d9f AD |
1245 | size_t size = obstack_object_size (&action_obstack); |
1246 | ||
1247 | actions_dumped++; | |
1248 | assert (actions_dumped == 1); | |
1249 | obstack_grow (&table_obstack, | |
1250 | obstack_finish (&action_obstack), | |
1251 | size); | |
ceed8467 | 1252 | } |
573c1d9f AD |
1253 | |
1254 | /* Skip the end of line. */ | |
1255 | for (; c != '\n' && c != EOF; c = getc (fskel)) | |
1256 | /* nothing */; | |
1257 | break; | |
1258 | ||
1259 | case regular_line: | |
1260 | for (; c != '\n' && c != EOF; c = getc (fskel)) | |
1261 | obstack_1grow (&table_obstack, c); | |
1262 | } | |
1263 | ||
c3e23647 RS |
1264 | if (c == EOF) |
1265 | break; | |
896fe5c1 | 1266 | obstack_1grow (&table_obstack, c); |
ef7ddedd | 1267 | line++; |
c3e23647 | 1268 | } |
573c1d9f | 1269 | assert (actions_dumped == 1); |
ff61dabd | 1270 | xfclose (fskel); |
c3e23647 RS |
1271 | } |
1272 | ||
4a120d45 | 1273 | static void |
d2729d44 | 1274 | output_program (void) |
c3e23647 | 1275 | { |
6c89f1c1 | 1276 | int c; |
c3e23647 | 1277 | |
89cab50d | 1278 | if (!no_lines_flag) |
14d3eb9b AD |
1279 | obstack_fgrow2 (&table_obstack, "#line %d %s\n", |
1280 | lineno, quotearg_style (c_quoting_style, infile)); | |
c3e23647 | 1281 | |
14d3eb9b AD |
1282 | while ((c = getc (finput)) != EOF) |
1283 | obstack_1grow (&table_obstack, c); | |
c3e23647 RS |
1284 | } |
1285 | ||
1286 | ||
4a120d45 | 1287 | static void |
d2729d44 | 1288 | free_itemsets (void) |
c3e23647 | 1289 | { |
6c89f1c1 | 1290 | core *cp, *cptmp; |
c3e23647 | 1291 | |
d7913476 | 1292 | XFREE (state_table); |
c3e23647 | 1293 | |
36281465 AD |
1294 | for (cp = first_state; cp; cp = cptmp) |
1295 | { | |
ceed8467 | 1296 | cptmp = cp->next; |
d7913476 | 1297 | XFREE (cp); |
36281465 | 1298 | } |
c3e23647 RS |
1299 | } |
1300 | ||
1301 | ||
6c89f1c1 AD |
1302 | /*----------------------------------------------------------. |
1303 | | Output the parsing tables and the parser code to ftable. | | |
1304 | `----------------------------------------------------------*/ | |
c3e23647 | 1305 | |
6c89f1c1 AD |
1306 | void |
1307 | output (void) | |
1308 | { | |
6c89f1c1 | 1309 | /* output_token_defines(ftable); / * JF put out token defines FIRST */ |
dd60faec AD |
1310 | |
1311 | /* If using a simple parser the definition of YYSTYPE are put into | |
896fe5c1 | 1312 | TABLE_OBSTACK. */ |
dd60faec | 1313 | if (!semantic_parser) |
36281465 | 1314 | { |
dd60faec | 1315 | size_t size = obstack_object_size (&attrs_obstack); |
896fe5c1 | 1316 | obstack_grow (&table_obstack, obstack_finish (&attrs_obstack), size); |
36281465 | 1317 | } |
896fe5c1 | 1318 | reader_output_yylsp (&table_obstack); |
89cab50d | 1319 | if (debug_flag) |
ff4423cc | 1320 | obstack_sgrow (&table_obstack, "\ |
6c89f1c1 | 1321 | #ifndef YYDEBUG\n\ |
71da9eea | 1322 | # define YYDEBUG 1\n\ |
6c89f1c1 | 1323 | #endif\n\ |
896fe5c1 | 1324 | \n"); |
c3e23647 | 1325 | |
6c89f1c1 | 1326 | if (semantic_parser) |
14d3eb9b AD |
1327 | obstack_fgrow1 (&table_obstack, "#include %s\n", |
1328 | quotearg_style (c_quoting_style, attrsfile)); | |
c3e23647 | 1329 | |
89cab50d | 1330 | if (!no_parser_flag) |
ff4423cc | 1331 | obstack_sgrow (&table_obstack, "#include <stdio.h>\n\n"); |
c3e23647 | 1332 | |
6c89f1c1 | 1333 | /* Make "const" do nothing if not in ANSI C. */ |
ff4423cc | 1334 | obstack_sgrow (&table_obstack, "\ |
6c89f1c1 AD |
1335 | #ifndef __cplusplus\n\ |
1336 | # ifndef __STDC__\n\ | |
1337 | # define const\n\ | |
1338 | # endif\n\ | |
1339 | #endif\n\ | |
896fe5c1 | 1340 | \n"); |
c3e23647 | 1341 | |
6c89f1c1 AD |
1342 | free_itemsets (); |
1343 | output_defines (); | |
1344 | output_token_translations (); | |
1345 | /* if (semantic_parser) */ | |
1346 | /* This is now unconditional because debugging printouts can use it. */ | |
1347 | output_gram (); | |
d7913476 | 1348 | XFREE (ritem); |
6c89f1c1 AD |
1349 | if (semantic_parser) |
1350 | output_stos (); | |
1351 | output_rule_data (); | |
1352 | output_actions (); | |
89cab50d | 1353 | if (!no_parser_flag) |
6c89f1c1 AD |
1354 | output_parser (); |
1355 | output_program (); | |
c3e23647 | 1356 | } |