]> git.saurik.com Git - bison.git/blame - src/output.c
* src/gram.h, src/gram.c (error_token_number): Remove, use
[bison.git] / src / output.c
CommitLineData
c3e23647 1/* Output the generated parsing program for bison,
5bb18f9a 2 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002
255ef638 3 Free Software Foundation, Inc.
c3e23647 4
9ee3c97b 5 This file is part of Bison, the GNU Compiler Compiler.
c3e23647 6
9ee3c97b
AD
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
c3e23647 11
9ee3c97b
AD
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
c3e23647 16
9ee3c97b
AD
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
c3e23647
RS
21
22
255ef638
AD
23/* The parser tables consist of these tables. Marked ones needed only
24 for the semantic parser. Double marked are output only if switches
25 are set.
c3e23647 26
255ef638
AD
27 YYTRANSLATE = vector mapping yylex's token numbers into bison's
28 token numbers.
c3e23647 29
255ef638 30 ++ YYTNAME = vector of string-names indexed by bison token number.
c3e23647 31
255ef638
AD
32 ++ YYTOKNUM = vector of yylex token numbers corresponding to
33 entries in YYTNAME.
c3e23647 34
255ef638
AD
35 YYRLINE = vector of line-numbers of all rules. For yydebug
36 printouts.
c3e23647 37
255ef638
AD
38 YYRHS = vector of items of all rules. This is exactly what RITEMS
39 contains. For yydebug and for semantic parser.
c3e23647 40
255ef638 41 YYPRHS[R] = index in YYRHS of first item for rule R.
c3e23647 42
255ef638 43 YYR1[R] = symbol number of symbol that rule R derives.
c3e23647 44
255ef638 45 YYR2[R] = number of symbols composing right hand side of rule R.
c3e23647 46
255ef638
AD
47 + YYSTOS[S] = the symbol number of the symbol that leads to state
48 S.
e372befa 49
255ef638
AD
50 YYDEFACT[S] = default rule to reduce with in state s, when YYTABLE
51 doesn't specify something else to do. Zero means the default is an
52 error.
c3e23647 53
255ef638
AD
54 YYDEFGOTO[I] = default state to go to after a reduction of a rule
55 that generates variable NTOKENS + I, except when YYTABLE specifies
56 something else to do.
c3e23647 57
255ef638
AD
58 YYPACT[S] = index in YYTABLE of the portion describing state S.
59 The lookahead token's type is used to index that portion to find
60 out what to do.
c3e23647 61
255ef638
AD
62 If the value in YYTABLE is positive, we shift the token and go to
63 that state.
c3e23647 64
6c89f1c1 65 If the value is negative, it is minus a rule number to reduce by.
c3e23647 66
255ef638
AD
67 If the value is zero, the default action from YYDEFACT[S] is used.
68
69 YYPGOTO[I] = the index in YYTABLE of the portion describing what to
70 do after reducing a rule that derives variable I + NTOKENS. This
71 portion is indexed by the parser state number, S, as of before the
72 text for this nonterminal was read. The value from YYTABLE is the
73 state to go to if the corresponding value in YYCHECK is S.
74
75 YYTABLE = a vector filled with portions for different uses, found
76 via YYPACT and YYPGOTO.
77
78 YYCHECK = a vector indexed in parallel with YYTABLE. It indicates,
79 in a roundabout way, the bounds of the portion you are trying to
80 examine.
81
82 Suppose that the portion of yytable starts at index P and the index
83 to be examined within the portion is I. Then if YYCHECK[P+I] != I,
84 I is outside the bounds of what is actually allocated, and the
85 default (from YYDEFACT or YYDEFGOTO) should be used. Otherwise,
86 YYTABLE[P+I] should be used.
87
88 YYFINAL = the state number of the termination state. YYFLAG = most
89 negative short int. Used to flag ?? */
c3e23647 90
c3e23647 91#include "system.h"
914feea9 92#include "bitsetv.h"
14d3eb9b 93#include "quotearg.h"
be2a1a68 94#include "error.h"
ceed8467 95#include "getargs.h"
c3e23647
RS
96#include "files.h"
97#include "gram.h"
b2ca4022 98#include "LR0.h"
a0f6b076 99#include "complain.h"
6c89f1c1 100#include "output.h"
720d742f 101#include "lalr.h"
a70083a3 102#include "reader.h"
ad949da9 103#include "symtab.h"
0619caf0 104#include "conflicts.h"
11d82f03 105#include "muscle_tab.h"
be2a1a68
AD
106
107/* From lib/readpipe.h. */
108FILE *readpipe PARAMS ((const char *, ...));
109
110/* From src/scan-skel.l. */
111int skel_lex PARAMS ((void));
112extern FILE *skel_in;
d019d655 113
c3e23647
RS
114static int nvectors;
115static int nentries;
342b8b6e
AD
116static short **froms = NULL;
117static short **tos = NULL;
118static short *tally = NULL;
119static short *width = NULL;
120static short *actrow = NULL;
121static short *state_count = NULL;
122static short *order = NULL;
123static short *base = NULL;
124static short *pos = NULL;
125static short *table = NULL;
126static short *check = NULL;
c3e23647
RS
127static int lowzero;
128static int high;
129
11d82f03 130struct obstack muscle_obstack;
f87685c3 131static struct obstack format_obstack;
c3e23647 132
c7925b99
MA
133int error_verbose = 0;
134
f0440388 135/* Returns the number of lines of S. */
9b3add5b 136size_t
f0440388
MA
137get_lines_number (const char *s)
138{
139 size_t lines = 0;
1fa14068 140
f0440388
MA
141 size_t i;
142 for (i = 0; s[i]; ++i)
d0b0fefa
AD
143 if (s[i] == '\n')
144 ++lines;
1fa14068 145
f0440388
MA
146 return lines;
147}
148
149
680e8701
AD
150/*----------------------------------------------------------------.
151| Format the FIRST and then TABLE_DATA[BEGIN..END[ into OOUT, and |
152| return the number of bits needed for its longuest value. |
153`----------------------------------------------------------------*/
c3e23647 154
680e8701 155static inline long int
62a3e4f0
AD
156output_short_table (struct obstack *oout,
157 short *table_data,
158 short first,
159 int begin,
160 int end)
161{
162 long int max = first;
163 int i;
164 int j = 1;
165
166 obstack_fgrow1 (oout, "%6d", first);
167 for (i = begin; i < end; ++i)
168 {
169 obstack_1grow (oout, ',');
170 if (j >= 10)
171 {
172 obstack_sgrow (oout, "\n ");
173 j = 1;
174 }
175 else
176 ++j;
177 obstack_fgrow1 (oout, "%6d", table_data[i]);
178 if (table_data[i] > max)
179 max = table_data[i];
180 }
181 obstack_1grow (oout, 0);
182
183 return max;
184}
185
186
187/*--------------------.
188| Similar, for ints. |
189`--------------------*/
190
191static inline long int
192output_int_table (struct obstack *oout,
193 int *table_data,
194 int first,
195 int begin,
196 int end)
d019d655 197{
680e8701 198 long int max = first;
26f609ff
RA
199 int i;
200 int j = 1;
342b8b6e 201
26f609ff
RA
202 obstack_fgrow1 (oout, "%6d", first);
203 for (i = begin; i < end; ++i)
d019d655 204 {
896fe5c1 205 obstack_1grow (oout, ',');
d019d655
AD
206 if (j >= 10)
207 {
ff4423cc 208 obstack_sgrow (oout, "\n ");
d019d655
AD
209 j = 1;
210 }
211 else
26f609ff 212 ++j;
8f451ef7 213 obstack_fgrow1 (oout, "%6d", table_data[i]);
680e8701
AD
214 if (table_data[i] > max)
215 max = table_data[i];
c3e23647 216 }
26f609ff 217 obstack_1grow (oout, 0);
680e8701
AD
218
219 return max;
c3e23647
RS
220}
221
222
b0940840
AD
223/*-----------------------------------------------------------------.
224| Prepare the muscles related to the tokens: translate, tname, and |
225| toknum. |
226`-----------------------------------------------------------------*/
227
4a120d45 228static void
b0940840 229prepare_tokens (void)
c3e23647 230{
62a3e4f0 231 long int max = output_short_table (&format_obstack, token_translations,
680e8701 232 0, 1, max_user_token_number + 1);
f87685c3 233 muscle_insert ("translate", obstack_finish (&format_obstack));
680e8701 234 MUSCLE_INSERT_LONG_INT ("token_number_max", max);
342b8b6e 235 XFREE (token_translations);
c3e23647 236
b0940840
AD
237 {
238 int i;
239 int j = 0;
240 for (i = 0; i < nsyms; i++)
241 {
242 /* Be sure not to use twice the same quotearg slot. */
243 const char *cp =
244 quotearg_n_style (1, c_quoting_style,
245 quotearg_style (escape_quoting_style,
246 symbols[i]->tag));
247 /* Width of the next token, including the two quotes, the coma
248 and the space. */
249 int strsize = strlen (cp) + 2;
250
251 if (j + strsize > 75)
252 {
253 obstack_sgrow (&format_obstack, "\n ");
254 j = 2;
255 }
256
257 obstack_sgrow (&format_obstack, cp);
258 obstack_sgrow (&format_obstack, ", ");
259 j += strsize;
260 }
261 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
262 defined). */
263 obstack_sgrow (&format_obstack, "0");
c3e23647 264
b0940840
AD
265 /* Finish table and store. */
266 obstack_1grow (&format_obstack, 0);
267 muscle_insert ("tname", obstack_finish (&format_obstack));
268 }
269
270 /* Output YYTOKNUM. */
b2ed6e58
AD
271 {
272 int i;
b0940840
AD
273 short *values = XCALLOC (short, ntokens + 1);
274 for (i = 0; i < ntokens + 1; ++i)
275 values[i] = symbols[i]->user_token_number;
62a3e4f0 276 output_short_table (&format_obstack, values,
b0940840
AD
277 0, 1, ntokens + 1);
278 muscle_insert ("toknum", obstack_finish (&format_obstack));
279 free (values);
b2ed6e58 280 }
b0940840 281}
b2ed6e58 282
b0940840
AD
283
284/*-------------------------------------------------------------.
285| Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
286| rline. |
287`-------------------------------------------------------------*/
288
289static void
290prepare_rules (void)
291{
62a3e4f0
AD
292 long int max;
293 item_number_t *rhsp;
b0940840
AD
294 int r;
295 int i = 0;
62a3e4f0 296 item_number_t *rhs = XMALLOC (item_number_t, nritems);
b0940840
AD
297 short *prhs = XMALLOC (short, nrules + 1);
298 short *r1 = XMALLOC (short, nrules + 1);
299 short *r2 = XMALLOC (short, nrules + 1);
300 short *rline = XMALLOC (short, nrules + 1);
301
302 for (r = 1; r < nrules + 1; ++r)
303 {
304 /* Index of rule R in RHS. */
305 prhs[r] = i;
306 /* RHS of the rule R. */
307 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
308 rhs[i++] = *rhsp;
309 /* LHS of the rule R. */
310 r1[r] = rules[r].lhs->number;
311 /* Length of rule R's RHS. */
312 r2[r] = i - prhs[r];
313 /* Separator in RHS. */
314 rhs[i++] = -1;
315 /* Line where rule was defined. */
316 rline[r] = rules[r].line;
317 }
318 assert (i == nritems);
319
62a3e4f0 320 max = output_int_table (&format_obstack, rhs, ritem[0], 1, nritems);
b0940840 321 muscle_insert ("rhs", obstack_finish (&format_obstack));
62a3e4f0 322 MUSCLE_INSERT_LONG_INT ("rhs_number_max", max);
b0940840 323
62a3e4f0 324 output_short_table (&format_obstack, prhs, 0, 1, nrules + 1);
f87685c3 325 muscle_insert ("prhs", obstack_finish (&format_obstack));
342b8b6e 326
62a3e4f0 327 output_short_table (&format_obstack, rline, 0, 1, nrules + 1);
b0940840 328 muscle_insert ("rline", obstack_finish (&format_obstack));
b4c4ccc2 329
62a3e4f0 330 output_short_table (&format_obstack, r1, 0, 1, nrules + 1);
b0940840 331 muscle_insert ("r1", obstack_finish (&format_obstack));
26f609ff 332
62a3e4f0 333 output_short_table (&format_obstack, r2, 0, 1, nrules + 1);
b0940840 334 muscle_insert ("r2", obstack_finish (&format_obstack));
796d61fb 335
b0940840
AD
336 free (rhs);
337 free (prhs);
338 free (r2);
c3e23647
RS
339}
340
b0940840
AD
341/*--------------------------------------------.
342| Prepare the muscles related to the states. |
343`--------------------------------------------*/
c3e23647 344
4a120d45 345static void
b0940840 346prepare_states (void)
c3e23647 347{
602bbf31 348 size_t i;
9703cc49
AD
349 short *values = (short *) alloca (sizeof (short) * nstates);
350 for (i = 0; i < nstates; ++i)
29e88316 351 values[i] = states[i]->accessing_symbol;
62a3e4f0 352 output_short_table (&format_obstack, values,
26f609ff 353 0, 1, nstates);
f87685c3 354 muscle_insert ("stos", obstack_finish (&format_obstack));
c3e23647
RS
355}
356
357
6c89f1c1
AD
358/*------------------------------------------------------------------.
359| Decide what to do for each type of token if seen as the lookahead |
360| token in specified state. The value returned is used as the |
361| default action (yydefact) for the state. In addition, actrow is |
362| filled with what to do for each kind of token, index by symbol |
363| number, with zero meaning do the default action. The value |
62a3e4f0 364| SHRT_MIN, a very negative number, means this situation is an |
6c89f1c1
AD
365| error. The parser recognizes this value specially. |
366| |
367| This is where conflicts are resolved. The loop over lookahead |
368| rules considered lower-numbered rules last, and the last rule |
369| considered that likes a token gets to handle it. |
370`------------------------------------------------------------------*/
c3e23647 371
4a120d45 372static int
f9507c28 373action_row (state_t *state)
c3e23647 374{
6c89f1c1 375 int i;
837491d8 376 int default_rule = 0;
f9507c28 377 reductions *redp = state->reductions;
f9507c28
AD
378 shifts *shiftp = state->shifts;
379 errs *errp = state->errs;
837491d8
AD
380 /* set nonzero to inhibit having any default reduction */
381 int nodefault = 0;
c3e23647
RS
382
383 for (i = 0; i < ntokens; i++)
384 actrow[i] = 0;
385
80dac38c 386 if (redp->nreds >= 1)
c3e23647 387 {
837491d8
AD
388 int j;
389 /* loop over all the rules available here which require
390 lookahead */
f9507c28 391 for (i = state->nlookaheads - 1; i >= 0; --i)
837491d8
AD
392 /* and find each token which the rule finds acceptable
393 to come next */
394 for (j = 0; j < ntokens; j++)
395 /* and record this rule as the rule to use if that
396 token follows. */
602bbf31 397 if (bitset_test (LA[state->lookaheadsp + i], j))
b0299a2e 398 actrow[j] = -LArule[state->lookaheadsp + i]->number;
c3e23647
RS
399 }
400
36281465
AD
401 /* Now see which tokens are allowed for shifts in this state. For
402 them, record the shift as the thing to do. So shift is preferred
403 to reduce. */
d954473d 404 for (i = 0; i < shiftp->nshifts; i++)
c3e23647 405 {
837491d8
AD
406 int symbol;
407 int shift_state = shiftp->shifts[i];
d954473d
AD
408 if (!shift_state)
409 continue;
c3e23647 410
29e88316 411 symbol = states[shift_state]->accessing_symbol;
c3e23647 412
d954473d
AD
413 if (ISVAR (symbol))
414 break;
c3e23647 415
d954473d 416 actrow[symbol] = shift_state;
c3e23647 417
d954473d
AD
418 /* Do not use any default reduction if there is a shift for
419 error */
007a50a4 420 if (symbol == errtoken->number)
d954473d 421 nodefault = 1;
c3e23647
RS
422 }
423
36281465 424 /* See which tokens are an explicit error in this state (due to
62a3e4f0 425 %nonassoc). For them, record SHRT_MIN as the action. */
2cec70b9
AD
426 for (i = 0; i < errp->nerrs; i++)
427 {
428 int symbol = errp->errs[i];
62a3e4f0 429 actrow[symbol] = SHRT_MIN;
2cec70b9 430 }
c3e23647 431
36281465
AD
432 /* Now find the most common reduction and make it the default action
433 for this state. */
c3e23647 434
80dac38c 435 if (redp->nreds >= 1 && !nodefault)
c3e23647 436 {
f9507c28 437 if (state->consistent)
c3e23647
RS
438 default_rule = redp->rules[0];
439 else
440 {
7a5350ba 441 int max = 0;
f9507c28 442 for (i = 0; i < state->nlookaheads; i++)
c3e23647 443 {
7a5350ba 444 int count = 0;
b0299a2e 445 int rule = -LArule[state->lookaheadsp + i]->number;
837491d8 446 int j;
9ee3c97b 447
c3e23647 448 for (j = 0; j < ntokens; j++)
837491d8
AD
449 if (actrow[j] == rule)
450 count++;
9ee3c97b 451
c3e23647
RS
452 if (count > max)
453 {
454 max = count;
455 default_rule = rule;
456 }
457 }
9ee3c97b 458
c3e23647
RS
459 /* actions which match the default are replaced with zero,
460 which means "use the default" */
9ee3c97b 461
c3e23647
RS
462 if (max > 0)
463 {
837491d8 464 int j;
c3e23647 465 for (j = 0; j < ntokens; j++)
837491d8
AD
466 if (actrow[j] == default_rule)
467 actrow[j] = 0;
9ee3c97b 468
ceed8467 469 default_rule = -default_rule;
c3e23647
RS
470 }
471 }
472 }
473
474 /* If have no default rule, the default is an error.
475 So replace any action which says "error" with "use default". */
476
477 if (default_rule == 0)
837491d8 478 for (i = 0; i < ntokens; i++)
62a3e4f0 479 if (actrow[i] == SHRT_MIN)
837491d8 480 actrow[i] = 0;
c3e23647 481
36281465 482 return default_rule;
c3e23647
RS
483}
484
485
4a120d45 486static void
d2729d44 487save_row (int state)
c3e23647 488{
6c89f1c1
AD
489 int i;
490 int count;
491 short *sp;
492 short *sp1;
493 short *sp2;
c3e23647
RS
494
495 count = 0;
496 for (i = 0; i < ntokens; i++)
796d61fb
AD
497 if (actrow[i] != 0)
498 count++;
c3e23647
RS
499
500 if (count == 0)
501 return;
502
d7913476
AD
503 froms[state] = sp1 = sp = XCALLOC (short, count);
504 tos[state] = sp2 = XCALLOC (short, count);
c3e23647
RS
505
506 for (i = 0; i < ntokens; i++)
796d61fb
AD
507 if (actrow[i] != 0)
508 {
509 *sp1++ = i;
510 *sp2++ = actrow[i];
511 }
c3e23647
RS
512
513 tally[state] = count;
514 width[state] = sp1[-1] - sp[0] + 1;
515}
516
517
6c89f1c1
AD
518/*------------------------------------------------------------------.
519| Figure out the actions for the specified state, indexed by |
520| lookahead token type. |
521| |
f2acea59
AD
522| The YYDEFACT table is output now. The detailed info is saved for |
523| putting into YYTABLE later. |
6c89f1c1 524`------------------------------------------------------------------*/
c3e23647 525
4a120d45 526static void
6c89f1c1 527token_actions (void)
c3e23647 528{
602bbf31 529 size_t i;
d7913476 530 short *yydefact = XCALLOC (short, nstates);
c3e23647 531
d7913476 532 actrow = XCALLOC (short, ntokens);
f2acea59 533 for (i = 0; i < nstates; ++i)
c3e23647 534 {
29e88316 535 yydefact[i] = action_row (states[i]);
6c89f1c1 536 save_row (i);
c3e23647 537 }
bbb5bcc6 538
62a3e4f0 539 output_short_table (&format_obstack, yydefact,
26f609ff 540 yydefact[0], 1, nstates);
f87685c3 541 muscle_insert ("defact", obstack_finish (&format_obstack));
342b8b6e 542
26f609ff 543 XFREE (actrow);
d7913476 544 XFREE (yydefact);
c3e23647
RS
545}
546
547
3f96f4dc
AD
548/*-----------------------------.
549| Output the actions to OOUT. |
550`-----------------------------*/
551
9b3add5b 552void
be2a1a68 553actions_output (FILE *out)
3f96f4dc
AD
554{
555 int rule;
556 for (rule = 1; rule < nrules + 1; ++rule)
1a2b5d37 557 if (rules[rule].action)
3f96f4dc 558 {
ea52d706 559 fprintf (out, " case %d:\n", rule);
3f96f4dc
AD
560
561 if (!no_lines_flag)
ea52d706 562 fprintf (out, muscle_find ("linef"),
1a2b5d37 563 rules[rule].action_line,
ea52d706
AD
564 quotearg_style (c_quoting_style,
565 muscle_find ("filename")));
3f96f4dc
AD
566 /* As a Bison extension, add the ending semicolon. Since some
567 Yacc don't do that, help people using bison as a Yacc
568 finding their missing semicolons. */
ea52d706 569 fprintf (out, "{ %s%s }\n break;\n\n",
1a2b5d37 570 rules[rule].action,
ea52d706 571 yacc_flag ? ";" : "");
3f96f4dc
AD
572 }
573}
574
575
f499b062
AD
576/*----------------------------.
577| Output the guards to OOUT. |
578`----------------------------*/
579
9b3add5b 580void
be2a1a68 581guards_output (FILE *out)
f499b062
AD
582{
583 int rule;
584 for (rule = 1; rule < nrules + 1; ++rule)
be2a1a68 585 if (rules[rule].guard)
f499b062
AD
586 {
587 fprintf (out, " case %d:\n", rule);
588
589 if (!no_lines_flag)
590 fprintf (out, muscle_find ("linef"),
1a2b5d37 591 rules[rule].guard_line,
f499b062
AD
592 quotearg_style (c_quoting_style,
593 muscle_find ("filename")));
594 fprintf (out, "{ %s; }\n break;\n\n",
1a2b5d37 595 rules[rule].guard);
f499b062
AD
596 }
597}
598
599
091e20bb
AD
600/*---------------------------------------.
601| Output the tokens definition to OOUT. |
602`---------------------------------------*/
603
9b3add5b 604void
be2a1a68 605token_definitions_output (FILE *out)
091e20bb
AD
606{
607 int i;
0d8bed56 608 int first = 1;
091e20bb
AD
609 for (i = 0; i < ntokens; ++i)
610 {
db8837cb 611 symbol_t *symbol = symbols[i];
091e20bb
AD
612 int number = symbol->user_token_number;
613
614 if (number == SALIAS)
615 continue;
616 /* Skip error token. */
007a50a4 617 if (symbol == errtoken)
091e20bb
AD
618 continue;
619 if (symbol->tag[0] == '\'')
620 continue; /* skip literal character */
621 if (symbol->tag[0] == '\"')
622 {
623 /* use literal string only if given a symbol with an alias */
624 if (symbol->alias)
625 symbol = symbol->alias;
626 else
627 continue;
628 }
629
630 /* Don't #define nonliteral tokens whose names contain periods
631 or '$' (as does the default value of the EOF token). */
632 if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$'))
633 continue;
634
0d8bed56
AD
635 fprintf (out, "%s [[[%s]], [%d]]",
636 first ? "" : ",\n", symbol->tag, number);
091e20bb 637 if (semantic_parser)
be2a1a68
AD
638 /* FIXME: This is probably wrong, and should be just as
639 above. --akim. */
d9b739c3 640 fprintf (out, "# define T%s\t%d\n", symbol->tag, symbol->number);
0d8bed56 641 first = 0;
091e20bb
AD
642 }
643}
644
645
4a120d45 646static void
d2729d44 647save_column (int symbol, int default_state)
c3e23647 648{
6c89f1c1 649 int i;
6c89f1c1
AD
650 short *sp;
651 short *sp1;
652 short *sp2;
653 int count;
837491d8 654 int symno = symbol - ntokens + nstates;
c3e23647 655
43591cec
AD
656 short begin = goto_map[symbol];
657 short end = goto_map[symbol + 1];
c3e23647
RS
658
659 count = 0;
43591cec 660 for (i = begin; i < end; i++)
796d61fb
AD
661 if (to_state[i] != default_state)
662 count++;
c3e23647
RS
663
664 if (count == 0)
665 return;
666
d7913476
AD
667 froms[symno] = sp1 = sp = XCALLOC (short, count);
668 tos[symno] = sp2 = XCALLOC (short, count);
c3e23647 669
43591cec 670 for (i = begin; i < end; i++)
796d61fb
AD
671 if (to_state[i] != default_state)
672 {
673 *sp1++ = from_state[i];
674 *sp2++ = to_state[i];
675 }
c3e23647
RS
676
677 tally[symno] = count;
678 width[symno] = sp1[-1] - sp[0] + 1;
679}
680
6c89f1c1
AD
681static int
682default_goto (int symbol)
683{
602bbf31
AD
684 size_t i;
685 size_t m = goto_map[symbol];
686 size_t n = goto_map[symbol + 1];
837491d8
AD
687 int default_state = -1;
688 int max = 0;
6c89f1c1
AD
689
690 if (m == n)
691 return -1;
692
693 for (i = 0; i < nstates; i++)
694 state_count[i] = 0;
695
696 for (i = m; i < n; i++)
697 state_count[to_state[i]]++;
698
6c89f1c1 699 for (i = 0; i < nstates; i++)
796d61fb
AD
700 if (state_count[i] > max)
701 {
702 max = state_count[i];
703 default_state = i;
704 }
6c89f1c1
AD
705
706 return default_state;
707}
708
709
710/*-------------------------------------------------------------------.
711| Figure out what to do after reducing with each rule, depending on |
712| the saved state from before the beginning of parsing the data that |
713| matched this rule. |
714| |
715| The YYDEFGOTO table is output now. The detailed info is saved for |
716| putting into YYTABLE later. |
717`-------------------------------------------------------------------*/
718
719static void
720goto_actions (void)
721{
43591cec 722 int i;
bbb5bcc6 723 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
bbb5bcc6 724
26f609ff 725 state_count = XCALLOC (short, nstates);
43591cec 726 for (i = ntokens; i < nsyms; ++i)
6c89f1c1 727 {
43591cec
AD
728 int default_state = default_goto (i);
729 save_column (i, default_state);
730 yydefgoto[i - ntokens] = default_state;
6c89f1c1
AD
731 }
732
62a3e4f0 733 output_short_table (&format_obstack, yydefgoto,
26f609ff 734 yydefgoto[0], 1, nsyms - ntokens);
f87685c3 735 muscle_insert ("defgoto", obstack_finish (&format_obstack));
43591cec 736
d7913476 737 XFREE (state_count);
43591cec 738 XFREE (yydefgoto);
6c89f1c1 739}
c3e23647
RS
740
741
9ee3c97b
AD
742/* The next few functions decide how to pack the actions and gotos
743 information into yytable. */
c3e23647 744
4a120d45 745static void
d2729d44 746sort_actions (void)
c3e23647 747{
6c89f1c1 748 int i;
c3e23647 749
d7913476 750 order = XCALLOC (short, nvectors);
c3e23647
RS
751 nentries = 0;
752
753 for (i = 0; i < nvectors; i++)
796d61fb
AD
754 if (tally[i] > 0)
755 {
837491d8
AD
756 int k;
757 int t = tally[i];
758 int w = width[i];
759 int j = nentries - 1;
c3e23647 760
796d61fb
AD
761 while (j >= 0 && (width[order[j]] < w))
762 j--;
c3e23647 763
796d61fb
AD
764 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
765 j--;
c3e23647 766
796d61fb
AD
767 for (k = nentries - 1; k > j; k--)
768 order[k + 1] = order[k];
c3e23647 769
796d61fb
AD
770 order[j + 1] = i;
771 nentries++;
772 }
c3e23647
RS
773}
774
775
4a120d45 776static int
d2729d44 777matching_state (int vector)
c3e23647 778{
837491d8 779 int i = order[vector];
6c89f1c1
AD
780 int t;
781 int w;
6c89f1c1 782 int prev;
c3e23647 783
602bbf31 784 if (i >= (int) nstates)
36281465 785 return -1;
c3e23647
RS
786
787 t = tally[i];
788 w = width[i];
789
790 for (prev = vector - 1; prev >= 0; prev--)
791 {
837491d8
AD
792 int j = order[prev];
793 int k;
794 int match = 1;
795
c3e23647 796 if (width[j] != w || tally[j] != t)
36281465 797 return -1;
c3e23647 798
c3e23647 799 for (k = 0; match && k < t; k++)
796d61fb
AD
800 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
801 match = 0;
c3e23647
RS
802
803 if (match)
36281465 804 return j;
c3e23647
RS
805 }
806
36281465 807 return -1;
c3e23647
RS
808}
809
62a3e4f0
AD
810/* FIXME: For the time being, best approximation... */
811#define MAXTABLE SHRT_MAX
c3e23647 812
4a120d45 813static int
d2729d44 814pack_vector (int vector)
c3e23647 815{
837491d8 816 int i = order[vector];
6c89f1c1 817 int j;
837491d8 818 int t = tally[i];
6c89f1c1 819 int loc = 0;
837491d8
AD
820 short *from = froms[i];
821 short *to = tos[i];
c3e23647 822
340ef489 823 assert (t);
c3e23647 824
c3e23647
RS
825 for (j = lowzero - from[0]; j < MAXTABLE; j++)
826 {
837491d8
AD
827 int k;
828 int ok = 1;
c3e23647
RS
829
830 for (k = 0; ok && k < t; k++)
831 {
832 loc = j + from[k];
833 if (loc > MAXTABLE)
a0f6b076 834 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
c3e23647
RS
835
836 if (table[loc] != 0)
837 ok = 0;
838 }
839
840 for (k = 0; ok && k < vector; k++)
796d61fb
AD
841 if (pos[k] == j)
842 ok = 0;
c3e23647
RS
843
844 if (ok)
845 {
846 for (k = 0; k < t; k++)
847 {
848 loc = j + from[k];
849 table[loc] = to[k];
850 check[loc] = from[k];
851 }
852
853 while (table[lowzero] != 0)
854 lowzero++;
855
856 if (loc > high)
857 high = loc;
858
36281465 859 return j;
c3e23647
RS
860 }
861 }
275fc3ad
AD
862#define pack_vector_succeeded 0
863 assert (pack_vector_succeeded);
3843c413 864 return 0;
c3e23647
RS
865}
866
867
6c89f1c1
AD
868static void
869pack_table (void)
870{
871 int i;
872 int place;
873 int state;
874
d7913476
AD
875 base = XCALLOC (short, nvectors);
876 pos = XCALLOC (short, nentries);
877 table = XCALLOC (short, MAXTABLE);
878 check = XCALLOC (short, MAXTABLE);
6c89f1c1
AD
879
880 lowzero = 0;
881 high = 0;
882
883 for (i = 0; i < nvectors; i++)
62a3e4f0 884 base[i] = SHRT_MIN;
6c89f1c1
AD
885
886 for (i = 0; i < MAXTABLE; i++)
887 check[i] = -1;
888
889 for (i = 0; i < nentries; i++)
890 {
891 state = matching_state (i);
892
893 if (state < 0)
894 place = pack_vector (i);
895 else
896 place = base[state];
897
898 pos[i] = place;
899 base[order[i]] = place;
900 }
901
902 for (i = 0; i < nvectors; i++)
903 {
796d61fb
AD
904 XFREE (froms[i]);
905 XFREE (tos[i]);
6c89f1c1
AD
906 }
907
d7913476
AD
908 XFREE (froms);
909 XFREE (tos);
910 XFREE (pos);
6c89f1c1 911}
c3e23647
RS
912
913/* the following functions output yytable, yycheck
914 and the vectors whose elements index the portion starts */
915
4a120d45 916static void
d2729d44 917output_base (void)
c3e23647 918{
26f609ff 919 /* Output pact. */
62a3e4f0 920 output_short_table (&format_obstack, base,
26f609ff 921 base[0], 1, nstates);
f87685c3 922 muscle_insert ("pact", obstack_finish (&format_obstack));
c3e23647 923
26f609ff 924 /* Output pgoto. */
62a3e4f0 925 output_short_table (&format_obstack, base,
26f609ff 926 base[nstates], nstates + 1, nvectors);
f87685c3 927 muscle_insert ("pgoto", obstack_finish (&format_obstack));
c3e23647 928
d7913476 929 XFREE (base);
c3e23647
RS
930}
931
932
4a120d45 933static void
d2729d44 934output_table (void)
c3e23647 935{
62a3e4f0 936 output_short_table (&format_obstack, table,
26f609ff 937 table[0], 1, high + 1);
f87685c3 938 muscle_insert ("table", obstack_finish (&format_obstack));
d7913476 939 XFREE (table);
c3e23647
RS
940}
941
942
4a120d45 943static void
d2729d44 944output_check (void)
c3e23647 945{
62a3e4f0 946 output_short_table (&format_obstack, check,
26f609ff 947 check[0], 1, high + 1);
f87685c3 948 muscle_insert ("check", obstack_finish (&format_obstack));
d7913476 949 XFREE (check);
c3e23647
RS
950}
951
b0940840
AD
952/*-----------------------------------------------------------------.
953| Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
954| and yycheck. |
955`-----------------------------------------------------------------*/
6c89f1c1
AD
956
957static void
958output_actions (void)
959{
602bbf31 960 size_t i;
6c89f1c1 961 nvectors = nstates + nvars;
c3e23647 962
d7913476
AD
963 froms = XCALLOC (short *, nvectors);
964 tos = XCALLOC (short *, nvectors);
965 tally = XCALLOC (short, nvectors);
966 width = XCALLOC (short, nvectors);
6c89f1c1
AD
967
968 token_actions ();
914feea9 969 bitsetv_free (LA);
b0299a2e 970 free (LArule);
6c89f1c1
AD
971
972 goto_actions ();
d7913476
AD
973 XFREE (goto_map + ntokens);
974 XFREE (from_state);
975 XFREE (to_state);
6c89f1c1
AD
976
977 sort_actions ();
978 pack_table ();
4e5caae2 979
6c89f1c1
AD
980 output_base ();
981 output_table ();
4e5caae2 982
6c89f1c1 983 output_check ();
49701457
AD
984
985 for (i = 0; i < nstates; ++i)
986 {
29e88316
AD
987 free (states[i]->shifts);
988 XFREE (states[i]->reductions);
989 free (states[i]->errs);
990 free (states[i]);
49701457 991 }
29e88316 992 XFREE (states);
6c89f1c1 993}
c3e23647 994
652def80 995\f
1239777d
AD
996/*---------------------------.
997| Call the skeleton parser. |
998`---------------------------*/
c3e23647 999
4a120d45 1000static void
1239777d 1001output_skeleton (void)
9b3add5b 1002{
be2a1a68 1003 /* Store the definition of all the muscles. */
d0039cbc 1004 const char *tempdir = getenv ("TMPDIR");
381fb12e
AD
1005 char *tempfile = NULL;
1006 FILE *out = NULL;
381fb12e
AD
1007 int fd;
1008
1009 if (tempdir == NULL)
1010 tempdir = DEFAULT_TMPDIR;
1011 tempfile = xmalloc (strlen (tempdir) + 11);
1012 sprintf (tempfile, "%s/bsnXXXXXX", tempdir);
1013 fd = mkstemp (tempfile);
1014 if (fd == -1)
1015 error (EXIT_FAILURE, errno, "%s", tempfile);
1016
1017 out = fdopen (fd, "w");
1018 if (out == NULL)
1019 error (EXIT_FAILURE, errno, "%s", tempfile);
1020
1021 /* There are no comments, especially not `#': we do want M4 expansion
1022 after `#': think of CPP macros! */
1023 fputs ("m4_changecom()\n", out);
1024 fputs ("m4_init()\n", out);
1025
1026 fputs ("m4_define([b4_actions], \n[[", out);
1027 actions_output (out);
1028 fputs ("]])\n\n", out);
1029
1030 fputs ("m4_define([b4_guards], \n[[", out);
1031 guards_output (out);
1032 fputs ("]])\n\n", out);
1033
0d8bed56 1034 fputs ("m4_define([b4_tokens], \n[", out);
381fb12e 1035 token_definitions_output (out);
0d8bed56 1036 fputs ("])\n\n", out);
be2a1a68 1037
381fb12e 1038 muscles_m4_output (out);
be2a1a68 1039
381fb12e
AD
1040 fputs ("m4_wrap([m4_divert_pop(0)])\n", out);
1041 fputs ("m4_divert_push(0)dnl\n", out);
1042 xfclose (out);
be2a1a68
AD
1043
1044 /* Invoke m4 on the definition of the muscles, and the skeleton. */
1045 {
1046 const char *bison_pkgdatadir = getenv ("BISON_PKGDATADIR");
abe017f6
AD
1047 const char *m4 = getenv ("M4");
1048 if (!m4)
1049 m4 = M4;
be2a1a68
AD
1050 if (!bison_pkgdatadir)
1051 bison_pkgdatadir = PKGDATADIR;
381fb12e
AD
1052 if (trace_flag)
1053 fprintf (stderr,
abe017f6
AD
1054 "running: %s -I %s m4sugar/m4sugar.m4 %s %s\n",
1055 m4, bison_pkgdatadir, tempfile, skeleton);
1056 skel_in = readpipe (m4,
1057 "-I", bison_pkgdatadir,
be2a1a68 1058 "m4sugar/m4sugar.m4",
381fb12e 1059 tempfile,
be2a1a68
AD
1060 skeleton,
1061 NULL);
1062 if (!skel_in)
1063 error (EXIT_FAILURE, errno, "cannot run m4");
1064 skel_lex ();
381fb12e
AD
1065
1066 /* If `debugging', keep this file alive. */
1067 if (!trace_flag)
1068 unlink (tempfile);
be2a1a68 1069 }
26f609ff
RA
1070}
1071
1072static void
1073prepare (void)
1074{
11d82f03 1075 MUSCLE_INSERT_INT ("last", high);
62a3e4f0 1076 MUSCLE_INSERT_INT ("flag", SHRT_MIN);
11d82f03
MA
1077 MUSCLE_INSERT_INT ("pure", pure_parser);
1078 MUSCLE_INSERT_INT ("nsym", nsyms);
1079 MUSCLE_INSERT_INT ("debug", debug_flag);
1080 MUSCLE_INSERT_INT ("final", final_state);
007a50a4
AD
1081 MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number);
1082 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
be2a1a68 1083 MUSCLE_INSERT_INT ("error_verbose", error_verbose);
b5b61c61 1084 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy");
11d82f03 1085
b85810ae
AD
1086 /* FIXME: This is wrong: the muscles should decide whether they hold
1087 a copy or not, but the situation is too obscure currently. */
be2a1a68
AD
1088 MUSCLE_INSERT_STRING ("output_infix", output_infix ? output_infix : "");
1089 MUSCLE_INSERT_STRING ("output_prefix", short_base_name);
1090 MUSCLE_INSERT_STRING ("output_parser_name", parser_file_name);
1091 MUSCLE_INSERT_STRING ("output_header_name", spec_defines_file);
b85810ae 1092
11d82f03
MA
1093 MUSCLE_INSERT_INT ("nnts", nvars);
1094 MUSCLE_INSERT_INT ("nrules", nrules);
1095 MUSCLE_INSERT_INT ("nstates", nstates);
1096 MUSCLE_INSERT_INT ("ntokens", ntokens);
1097
be2a1a68
AD
1098 MUSCLE_INSERT_INT ("locations_flag", locations_flag);
1099 MUSCLE_INSERT_INT ("defines_flag", defines_flag);
381fb12e
AD
1100
1101 /* Copy definitions in directive. */
1102 obstack_1grow (&attrs_obstack, 0);
1103 muscle_insert ("prologue", obstack_finish (&attrs_obstack));
1104
1105 /* Find the right skeleton file. */
1106 if (!skeleton)
1107 {
1108 if (semantic_parser)
1109 skeleton = "bison.hairy";
1110 else
1111 skeleton = "bison.simple";
1112 }
1113
1114 /* Parse the skeleton file and output the needed parsers. */
1115 muscle_insert ("skeleton", skeleton);
26f609ff 1116}
c3e23647 1117
93ede233 1118
6c89f1c1
AD
1119/*----------------------------------------------------------.
1120| Output the parsing tables and the parser code to ftable. |
1121`----------------------------------------------------------*/
c3e23647 1122
6c89f1c1
AD
1123void
1124output (void)
1125{
f87685c3 1126 obstack_init (&format_obstack);
dd60faec 1127
b0940840
AD
1128 prepare_tokens ();
1129 prepare_rules ();
1130 prepare_states ();
6c89f1c1 1131 output_actions ();
342b8b6e 1132
26f609ff 1133 prepare ();
652def80 1134
9b3add5b
RA
1135 /* Process the selected skeleton file. */
1136 output_skeleton ();
1137
f499b062
AD
1138 obstack_free (&muscle_obstack, NULL);
1139 obstack_free (&format_obstack, NULL);
1140 obstack_free (&action_obstack, NULL);
f499b062 1141 obstack_free (&attrs_obstack, NULL);
c3e23647 1142}