]> git.saurik.com Git - bison.git/blame - src/output.c
* src/output.c (output_gram): Rely on nritems, not the
[bison.git] / src / output.c
CommitLineData
c3e23647 1/* Output the generated parsing program for bison,
fabd3b43 2 Copyright 1984, 1986, 1989, 1992, 2000, 2001 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"
14d3eb9b 94#include "quotearg.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"
0619caf0 103#include "conflicts.h"
11d82f03 104#include "muscle_tab.h"
c3e23647 105
d019d655 106
c3e23647
RS
107static int nvectors;
108static int nentries;
342b8b6e
AD
109static short **froms = NULL;
110static short **tos = NULL;
111static short *tally = NULL;
112static short *width = NULL;
113static short *actrow = NULL;
114static short *state_count = NULL;
115static short *order = NULL;
116static short *base = NULL;
117static short *pos = NULL;
118static short *table = NULL;
119static short *check = NULL;
c3e23647
RS
120static int lowzero;
121static int high;
122
11d82f03 123struct obstack muscle_obstack;
f87685c3 124static struct obstack format_obstack;
c3e23647 125
c7925b99
MA
126int error_verbose = 0;
127
f0440388
MA
128/* Returns the number of lines of S. */
129static size_t
130get_lines_number (const char *s)
131{
132 size_t lines = 0;
1fa14068 133
f0440388
MA
134 size_t i;
135 for (i = 0; s[i]; ++i)
d0b0fefa
AD
136 if (s[i] == '\n')
137 ++lines;
1fa14068 138
f0440388
MA
139 return lines;
140}
141
142
26f609ff 143/* FIXME. */
c3e23647 144
d019d655 145static inline void
342b8b6e
AD
146output_table_data (struct obstack *oout,
147 short *table_data,
148 short first,
1fa14068
AD
149 int begin,
150 int end)
d019d655 151{
26f609ff
RA
152 int i;
153 int j = 1;
342b8b6e 154
26f609ff
RA
155 obstack_fgrow1 (oout, "%6d", first);
156 for (i = begin; i < end; ++i)
d019d655 157 {
896fe5c1 158 obstack_1grow (oout, ',');
d019d655
AD
159 if (j >= 10)
160 {
ff4423cc 161 obstack_sgrow (oout, "\n ");
d019d655
AD
162 j = 1;
163 }
164 else
26f609ff 165 ++j;
8f451ef7 166 obstack_fgrow1 (oout, "%6d", table_data[i]);
c3e23647 167 }
26f609ff 168 obstack_1grow (oout, 0);
c3e23647
RS
169}
170
171
4a120d45 172static void
d2729d44 173output_token_translations (void)
c3e23647 174{
f87685c3 175 output_table_data (&format_obstack, token_translations,
26f609ff 176 0, 1, max_user_token_number + 1);
f87685c3 177 muscle_insert ("translate", obstack_finish (&format_obstack));
342b8b6e 178 XFREE (token_translations);
c3e23647
RS
179}
180
181
4a120d45 182static void
d2729d44 183output_gram (void)
c3e23647 184{
b2ed6e58
AD
185 {
186 int i;
187 short *values = XCALLOC (short, nrules + 1);
188 for (i = 0; i < nrules + 1; ++i)
189 values[i] = rule_table[i].rhs;
f87685c3 190 output_table_data (&format_obstack, values,
b2ed6e58
AD
191 0, 1, nrules + 1);
192 XFREE (values);
193 }
194
f87685c3 195 muscle_insert ("prhs", obstack_finish (&format_obstack));
342b8b6e 196
1e9798d5 197 {
3db472b9 198 short *yyrhs;
1e9798d5 199 int i;
c3e23647 200
3db472b9 201 yyrhs = XMALLOC (short, nritems);
c3e23647 202
3db472b9
AD
203 for (i = 1; i < nritems; ++i)
204 yyrhs[i] = ritem[i] >= 0 ? ritem[i] : -1;
c3e23647 205
f87685c3 206 output_table_data (&format_obstack, yyrhs,
3db472b9 207 ritem[0], 1, nritems);
f87685c3 208 muscle_insert ("rhs", obstack_finish (&format_obstack));
26f609ff 209
1e9798d5
AD
210 XFREE (yyrhs);
211 }
796d61fb 212
4ec8e00f
MA
213#if 0
214 if (!semantic_parser)
215 obstack_sgrow (&table_obstack, "\n#endif\n");
216#endif
c3e23647
RS
217}
218
219
4a120d45 220static void
d2729d44 221output_stos (void)
c3e23647 222{
9703cc49
AD
223 int i;
224 short *values = (short *) alloca (sizeof (short) * nstates);
225 for (i = 0; i < nstates; ++i)
f693ad14 226 values[i] = state_table[i]->accessing_symbol;
f87685c3 227 output_table_data (&format_obstack, values,
26f609ff 228 0, 1, nstates);
f87685c3 229 muscle_insert ("stos", obstack_finish (&format_obstack));
c3e23647
RS
230}
231
232
4a120d45 233static void
d2729d44 234output_rule_data (void)
c3e23647 235{
6c89f1c1
AD
236 int i;
237 int j;
1e9798d5 238 short *short_tab = NULL;
c3e23647 239
e41dc700
AD
240 {
241 short *values = XCALLOC (short, nrules + 1);
242 for (i = 0; i < nrules + 1; ++i)
243 values[i] = rule_table[i].line;
f87685c3 244 output_table_data (&format_obstack, values,
e41dc700 245 0, 1, nrules + 1);
f87685c3 246 muscle_insert ("rline", obstack_finish (&format_obstack));
e41dc700
AD
247 XFREE (values);
248 }
249
c3e23647 250
1e9798d5
AD
251 j = 0;
252 for (i = 0; i < nsyms; i++)
c3e23647 253 {
92790e5b
AD
254 /* Be sure not to use twice the same quotearg slot. */
255 const char *cp =
256 quotearg_n_style (1, c_quoting_style,
257 quotearg_style (escape_quoting_style, tags[i]));
1e9798d5
AD
258 /* Width of the next token, including the two quotes, the coma
259 and the space. */
92790e5b 260 int strsize = strlen (cp) + 2;
1e9798d5
AD
261
262 if (j + strsize > 75)
c3e23647 263 {
f87685c3 264 obstack_sgrow (&format_obstack, "\n ");
1e9798d5 265 j = 2;
c3e23647
RS
266 }
267
f87685c3
AD
268 obstack_sgrow (&format_obstack, cp);
269 obstack_sgrow (&format_obstack, ", ");
1e9798d5 270 j += strsize;
c3e23647 271 }
9ee3c97b 272 /* add a NULL entry to list of tokens */
f87685c3 273 obstack_sgrow (&format_obstack, "NULL");
c3e23647 274
26f609ff 275 /* Finish table and store. */
f87685c3
AD
276 obstack_1grow (&format_obstack, 0);
277 muscle_insert ("tname", obstack_finish (&format_obstack));
e372befa 278
9ee3c97b 279 /* Output YYTOKNUM. */
f87685c3 280 output_table_data (&format_obstack, user_toknums,
26f609ff 281 0, 1, ntokens + 1);
f87685c3 282 muscle_insert ("toknum", obstack_finish (&format_obstack));
e372befa 283
9ee3c97b 284 /* Output YYR1. */
b2ed6e58
AD
285 {
286 short *values = XCALLOC (short, nrules + 1);
287 for (i = 0; i < nrules + 1; ++i)
288 values[i] = rule_table[i].lhs;
f87685c3 289 output_table_data (&format_obstack, values,
b2ed6e58 290 0, 1, nrules + 1);
f87685c3 291 muscle_insert ("r1", obstack_finish (&format_obstack));
b2ed6e58
AD
292 XFREE (values);
293 }
d019d655 294
9ee3c97b 295 /* Output YYR2. */
1e9798d5 296 short_tab = XMALLOC (short, nrules + 1);
c3e23647 297 for (i = 1; i < nrules; i++)
b2ed6e58
AD
298 short_tab[i] = rule_table[i + 1].rhs - rule_table[i].rhs - 1;
299 short_tab[nrules] = nitems - rule_table[nrules].rhs - 1;
f87685c3 300 output_table_data (&format_obstack, short_tab,
26f609ff 301 0, 1, nrules + 1);
f87685c3 302 muscle_insert ("r2", obstack_finish (&format_obstack));
1e9798d5 303 XFREE (short_tab);
c3e23647
RS
304}
305
6c89f1c1
AD
306/*------------------------------------------------------------------.
307| Decide what to do for each type of token if seen as the lookahead |
308| token in specified state. The value returned is used as the |
309| default action (yydefact) for the state. In addition, actrow is |
310| filled with what to do for each kind of token, index by symbol |
311| number, with zero meaning do the default action. The value |
312| MINSHORT, a very negative number, means this situation is an |
313| error. The parser recognizes this value specially. |
314| |
315| This is where conflicts are resolved. The loop over lookahead |
316| rules considered lower-numbered rules last, and the last rule |
317| considered that likes a token gets to handle it. |
318`------------------------------------------------------------------*/
c3e23647 319
4a120d45 320static int
f9507c28 321action_row (state_t *state)
c3e23647 322{
6c89f1c1 323 int i;
837491d8 324 int default_rule = 0;
f9507c28 325 reductions *redp = state->reductions;
f9507c28
AD
326 shifts *shiftp = state->shifts;
327 errs *errp = state->errs;
837491d8
AD
328 /* set nonzero to inhibit having any default reduction */
329 int nodefault = 0;
c3e23647
RS
330
331 for (i = 0; i < ntokens; i++)
332 actrow[i] = 0;
333
80dac38c 334 if (redp->nreds >= 1)
c3e23647 335 {
837491d8
AD
336 int j;
337 /* loop over all the rules available here which require
338 lookahead */
f9507c28 339 for (i = state->nlookaheads - 1; i >= 0; --i)
837491d8
AD
340 /* and find each token which the rule finds acceptable
341 to come next */
342 for (j = 0; j < ntokens; j++)
343 /* and record this rule as the rule to use if that
344 token follows. */
f9507c28
AD
345 if (BITISSET (LA (state->lookaheadsp + i), j))
346 actrow[j] = -LAruleno[state->lookaheadsp + i];
c3e23647
RS
347 }
348
36281465
AD
349 /* Now see which tokens are allowed for shifts in this state. For
350 them, record the shift as the thing to do. So shift is preferred
351 to reduce. */
d954473d 352 for (i = 0; i < shiftp->nshifts; i++)
c3e23647 353 {
837491d8
AD
354 int symbol;
355 int shift_state = shiftp->shifts[i];
d954473d
AD
356 if (!shift_state)
357 continue;
c3e23647 358
f693ad14 359 symbol = state_table[shift_state]->accessing_symbol;
c3e23647 360
d954473d
AD
361 if (ISVAR (symbol))
362 break;
c3e23647 363
d954473d 364 actrow[symbol] = shift_state;
c3e23647 365
d954473d
AD
366 /* Do not use any default reduction if there is a shift for
367 error */
368 if (symbol == error_token_number)
369 nodefault = 1;
c3e23647
RS
370 }
371
36281465
AD
372 /* See which tokens are an explicit error in this state (due to
373 %nonassoc). For them, record MINSHORT as the action. */
2cec70b9
AD
374 for (i = 0; i < errp->nerrs; i++)
375 {
376 int symbol = errp->errs[i];
377 actrow[symbol] = MINSHORT;
378 }
c3e23647 379
36281465
AD
380 /* Now find the most common reduction and make it the default action
381 for this state. */
c3e23647 382
80dac38c 383 if (redp->nreds >= 1 && !nodefault)
c3e23647 384 {
f9507c28 385 if (state->consistent)
c3e23647
RS
386 default_rule = redp->rules[0];
387 else
388 {
7a5350ba 389 int max = 0;
f9507c28 390 for (i = 0; i < state->nlookaheads; i++)
c3e23647 391 {
7a5350ba 392 int count = 0;
f9507c28 393 int rule = -LAruleno[state->lookaheadsp + i];
837491d8 394 int j;
9ee3c97b 395
c3e23647 396 for (j = 0; j < ntokens; j++)
837491d8
AD
397 if (actrow[j] == rule)
398 count++;
9ee3c97b 399
c3e23647
RS
400 if (count > max)
401 {
402 max = count;
403 default_rule = rule;
404 }
405 }
9ee3c97b 406
c3e23647
RS
407 /* actions which match the default are replaced with zero,
408 which means "use the default" */
9ee3c97b 409
c3e23647
RS
410 if (max > 0)
411 {
837491d8 412 int j;
c3e23647 413 for (j = 0; j < ntokens; j++)
837491d8
AD
414 if (actrow[j] == default_rule)
415 actrow[j] = 0;
9ee3c97b 416
ceed8467 417 default_rule = -default_rule;
c3e23647
RS
418 }
419 }
420 }
421
422 /* If have no default rule, the default is an error.
423 So replace any action which says "error" with "use default". */
424
425 if (default_rule == 0)
837491d8
AD
426 for (i = 0; i < ntokens; i++)
427 if (actrow[i] == MINSHORT)
428 actrow[i] = 0;
c3e23647 429
36281465 430 return default_rule;
c3e23647
RS
431}
432
433
4a120d45 434static void
d2729d44 435save_row (int state)
c3e23647 436{
6c89f1c1
AD
437 int i;
438 int count;
439 short *sp;
440 short *sp1;
441 short *sp2;
c3e23647
RS
442
443 count = 0;
444 for (i = 0; i < ntokens; i++)
796d61fb
AD
445 if (actrow[i] != 0)
446 count++;
c3e23647
RS
447
448 if (count == 0)
449 return;
450
d7913476
AD
451 froms[state] = sp1 = sp = XCALLOC (short, count);
452 tos[state] = sp2 = XCALLOC (short, count);
c3e23647
RS
453
454 for (i = 0; i < ntokens; i++)
796d61fb
AD
455 if (actrow[i] != 0)
456 {
457 *sp1++ = i;
458 *sp2++ = actrow[i];
459 }
c3e23647
RS
460
461 tally[state] = count;
462 width[state] = sp1[-1] - sp[0] + 1;
463}
464
465
6c89f1c1
AD
466/*------------------------------------------------------------------.
467| Figure out the actions for the specified state, indexed by |
468| lookahead token type. |
469| |
f2acea59
AD
470| The YYDEFACT table is output now. The detailed info is saved for |
471| putting into YYTABLE later. |
6c89f1c1 472`------------------------------------------------------------------*/
c3e23647 473
4a120d45 474static void
6c89f1c1 475token_actions (void)
c3e23647 476{
6c89f1c1 477 int i;
d7913476 478 short *yydefact = XCALLOC (short, nstates);
c3e23647 479
d7913476 480 actrow = XCALLOC (short, ntokens);
f2acea59 481 for (i = 0; i < nstates; ++i)
c3e23647 482 {
f9507c28 483 yydefact[i] = action_row (state_table[i]);
6c89f1c1 484 save_row (i);
c3e23647 485 }
bbb5bcc6 486
f87685c3 487 output_table_data (&format_obstack, yydefact,
26f609ff 488 yydefact[0], 1, nstates);
f87685c3 489 muscle_insert ("defact", obstack_finish (&format_obstack));
342b8b6e 490
26f609ff 491 XFREE (actrow);
d7913476 492 XFREE (yydefact);
c3e23647
RS
493}
494
495
3f96f4dc
AD
496/*-----------------------------.
497| Output the actions to OOUT. |
498`-----------------------------*/
499
500static void
f0440388 501actions_output (FILE *out, size_t *line)
3f96f4dc
AD
502{
503 int rule;
504 for (rule = 1; rule < nrules + 1; ++rule)
505 if (rule_table[rule].action)
506 {
ea52d706 507 fprintf (out, " case %d:\n", rule);
3f96f4dc
AD
508
509 if (!no_lines_flag)
ea52d706
AD
510 fprintf (out, muscle_find ("linef"),
511 rule_table[rule].action_line,
512 quotearg_style (c_quoting_style,
513 muscle_find ("filename")));
3f96f4dc
AD
514 /* As a Bison extension, add the ending semicolon. Since some
515 Yacc don't do that, help people using bison as a Yacc
516 finding their missing semicolons. */
ea52d706
AD
517 fprintf (out, "{ %s%s }\n break;\n\n",
518 rule_table[rule].action,
519 yacc_flag ? ";" : "");
f0440388 520
fbc8ecb7
MA
521 /* We always output 4 '\n' per action. */
522 *line += 4;
523 /* Plus one if !no_lines_flag. */
524 if (!no_lines_flag)
525 ++*line;
f0440388
MA
526 /* Get the number of lines written by the user. */
527 *line += get_lines_number (rule_table[rule].action);
3f96f4dc
AD
528 }
529}
530
531
f499b062
AD
532/*----------------------------.
533| Output the guards to OOUT. |
534`----------------------------*/
535
536static void
537guards_output (FILE *out, size_t *line)
538{
539 int rule;
540 for (rule = 1; rule < nrules + 1; ++rule)
541 if (rule_table[rule].action)
542 {
543 fprintf (out, " case %d:\n", rule);
544
545 if (!no_lines_flag)
546 fprintf (out, muscle_find ("linef"),
547 rule_table[rule].guard_line,
548 quotearg_style (c_quoting_style,
549 muscle_find ("filename")));
550 fprintf (out, "{ %s; }\n break;\n\n",
551 rule_table[rule].guard);
552
553 /* We always output 4 '\n' per action. */
554 *line += 4;
555 /* Plus one if !no_lines_flag. */
556 if (!no_lines_flag)
557 ++*line;
558 /* Get the number of lines written by the user. */
559 *line += get_lines_number (rule_table[rule].guard);
560 }
561}
562
563
4a120d45 564static void
d2729d44 565save_column (int symbol, int default_state)
c3e23647 566{
6c89f1c1 567 int i;
6c89f1c1
AD
568 short *sp;
569 short *sp1;
570 short *sp2;
571 int count;
837491d8 572 int symno = symbol - ntokens + nstates;
c3e23647 573
43591cec
AD
574 short begin = goto_map[symbol];
575 short end = goto_map[symbol + 1];
c3e23647
RS
576
577 count = 0;
43591cec 578 for (i = begin; i < end; i++)
796d61fb
AD
579 if (to_state[i] != default_state)
580 count++;
c3e23647
RS
581
582 if (count == 0)
583 return;
584
d7913476
AD
585 froms[symno] = sp1 = sp = XCALLOC (short, count);
586 tos[symno] = sp2 = XCALLOC (short, count);
c3e23647 587
43591cec 588 for (i = begin; i < end; i++)
796d61fb
AD
589 if (to_state[i] != default_state)
590 {
591 *sp1++ = from_state[i];
592 *sp2++ = to_state[i];
593 }
c3e23647
RS
594
595 tally[symno] = count;
596 width[symno] = sp1[-1] - sp[0] + 1;
597}
598
6c89f1c1
AD
599static int
600default_goto (int symbol)
601{
602 int i;
837491d8
AD
603 int m = goto_map[symbol];
604 int n = goto_map[symbol + 1];
605 int default_state = -1;
606 int max = 0;
6c89f1c1
AD
607
608 if (m == n)
609 return -1;
610
611 for (i = 0; i < nstates; i++)
612 state_count[i] = 0;
613
614 for (i = m; i < n; i++)
615 state_count[to_state[i]]++;
616
6c89f1c1 617 for (i = 0; i < nstates; i++)
796d61fb
AD
618 if (state_count[i] > max)
619 {
620 max = state_count[i];
621 default_state = i;
622 }
6c89f1c1
AD
623
624 return default_state;
625}
626
627
628/*-------------------------------------------------------------------.
629| Figure out what to do after reducing with each rule, depending on |
630| the saved state from before the beginning of parsing the data that |
631| matched this rule. |
632| |
633| The YYDEFGOTO table is output now. The detailed info is saved for |
634| putting into YYTABLE later. |
635`-------------------------------------------------------------------*/
636
637static void
638goto_actions (void)
639{
43591cec 640 int i;
bbb5bcc6 641 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
bbb5bcc6 642
26f609ff 643 state_count = XCALLOC (short, nstates);
43591cec 644 for (i = ntokens; i < nsyms; ++i)
6c89f1c1 645 {
43591cec
AD
646 int default_state = default_goto (i);
647 save_column (i, default_state);
648 yydefgoto[i - ntokens] = default_state;
6c89f1c1
AD
649 }
650
f87685c3 651 output_table_data (&format_obstack, yydefgoto,
26f609ff 652 yydefgoto[0], 1, nsyms - ntokens);
f87685c3 653 muscle_insert ("defgoto", obstack_finish (&format_obstack));
43591cec 654
d7913476 655 XFREE (state_count);
43591cec 656 XFREE (yydefgoto);
6c89f1c1 657}
c3e23647
RS
658
659
9ee3c97b
AD
660/* The next few functions decide how to pack the actions and gotos
661 information into yytable. */
c3e23647 662
4a120d45 663static void
d2729d44 664sort_actions (void)
c3e23647 665{
6c89f1c1 666 int i;
c3e23647 667
d7913476 668 order = XCALLOC (short, nvectors);
c3e23647
RS
669 nentries = 0;
670
671 for (i = 0; i < nvectors; i++)
796d61fb
AD
672 if (tally[i] > 0)
673 {
837491d8
AD
674 int k;
675 int t = tally[i];
676 int w = width[i];
677 int j = nentries - 1;
c3e23647 678
796d61fb
AD
679 while (j >= 0 && (width[order[j]] < w))
680 j--;
c3e23647 681
796d61fb
AD
682 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
683 j--;
c3e23647 684
796d61fb
AD
685 for (k = nentries - 1; k > j; k--)
686 order[k + 1] = order[k];
c3e23647 687
796d61fb
AD
688 order[j + 1] = i;
689 nentries++;
690 }
c3e23647
RS
691}
692
693
4a120d45 694static int
d2729d44 695matching_state (int vector)
c3e23647 696{
837491d8 697 int i = order[vector];
6c89f1c1
AD
698 int t;
699 int w;
6c89f1c1 700 int prev;
c3e23647 701
c3e23647 702 if (i >= nstates)
36281465 703 return -1;
c3e23647
RS
704
705 t = tally[i];
706 w = width[i];
707
708 for (prev = vector - 1; prev >= 0; prev--)
709 {
837491d8
AD
710 int j = order[prev];
711 int k;
712 int match = 1;
713
c3e23647 714 if (width[j] != w || tally[j] != t)
36281465 715 return -1;
c3e23647 716
c3e23647 717 for (k = 0; match && k < t; k++)
796d61fb
AD
718 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
719 match = 0;
c3e23647
RS
720
721 if (match)
36281465 722 return j;
c3e23647
RS
723 }
724
36281465 725 return -1;
c3e23647
RS
726}
727
728
4a120d45 729static int
d2729d44 730pack_vector (int vector)
c3e23647 731{
837491d8 732 int i = order[vector];
6c89f1c1 733 int j;
837491d8 734 int t = tally[i];
6c89f1c1 735 int loc = 0;
837491d8
AD
736 short *from = froms[i];
737 short *to = tos[i];
c3e23647 738
340ef489 739 assert (t);
c3e23647 740
c3e23647
RS
741 for (j = lowzero - from[0]; j < MAXTABLE; j++)
742 {
837491d8
AD
743 int k;
744 int ok = 1;
c3e23647
RS
745
746 for (k = 0; ok && k < t; k++)
747 {
748 loc = j + from[k];
749 if (loc > MAXTABLE)
a0f6b076 750 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
c3e23647
RS
751
752 if (table[loc] != 0)
753 ok = 0;
754 }
755
756 for (k = 0; ok && k < vector; k++)
796d61fb
AD
757 if (pos[k] == j)
758 ok = 0;
c3e23647
RS
759
760 if (ok)
761 {
762 for (k = 0; k < t; k++)
763 {
764 loc = j + from[k];
765 table[loc] = to[k];
766 check[loc] = from[k];
767 }
768
769 while (table[lowzero] != 0)
770 lowzero++;
771
772 if (loc > high)
773 high = loc;
774
36281465 775 return j;
c3e23647
RS
776 }
777 }
275fc3ad
AD
778#define pack_vector_succeeded 0
779 assert (pack_vector_succeeded);
3843c413 780 return 0;
c3e23647
RS
781}
782
783
6c89f1c1
AD
784static void
785pack_table (void)
786{
787 int i;
788 int place;
789 int state;
790
d7913476
AD
791 base = XCALLOC (short, nvectors);
792 pos = XCALLOC (short, nentries);
793 table = XCALLOC (short, MAXTABLE);
794 check = XCALLOC (short, MAXTABLE);
6c89f1c1
AD
795
796 lowzero = 0;
797 high = 0;
798
799 for (i = 0; i < nvectors; i++)
800 base[i] = MINSHORT;
801
802 for (i = 0; i < MAXTABLE; i++)
803 check[i] = -1;
804
805 for (i = 0; i < nentries; i++)
806 {
807 state = matching_state (i);
808
809 if (state < 0)
810 place = pack_vector (i);
811 else
812 place = base[state];
813
814 pos[i] = place;
815 base[order[i]] = place;
816 }
817
818 for (i = 0; i < nvectors; i++)
819 {
796d61fb
AD
820 XFREE (froms[i]);
821 XFREE (tos[i]);
6c89f1c1
AD
822 }
823
d7913476
AD
824 XFREE (froms);
825 XFREE (tos);
826 XFREE (pos);
6c89f1c1 827}
c3e23647
RS
828
829/* the following functions output yytable, yycheck
830 and the vectors whose elements index the portion starts */
831
4a120d45 832static void
d2729d44 833output_base (void)
c3e23647 834{
26f609ff 835 /* Output pact. */
f87685c3 836 output_table_data (&format_obstack, base,
26f609ff 837 base[0], 1, nstates);
f87685c3 838 muscle_insert ("pact", obstack_finish (&format_obstack));
c3e23647 839
26f609ff 840 /* Output pgoto. */
f87685c3 841 output_table_data (&format_obstack, base,
26f609ff 842 base[nstates], nstates + 1, nvectors);
f87685c3 843 muscle_insert ("pgoto", obstack_finish (&format_obstack));
c3e23647 844
d7913476 845 XFREE (base);
c3e23647
RS
846}
847
848
4a120d45 849static void
d2729d44 850output_table (void)
c3e23647 851{
f87685c3 852 output_table_data (&format_obstack, table,
26f609ff 853 table[0], 1, high + 1);
f87685c3 854 muscle_insert ("table", obstack_finish (&format_obstack));
d7913476 855 XFREE (table);
c3e23647
RS
856}
857
858
4a120d45 859static void
d2729d44 860output_check (void)
c3e23647 861{
f87685c3 862 output_table_data (&format_obstack, check,
26f609ff 863 check[0], 1, high + 1);
f87685c3 864 muscle_insert ("check", obstack_finish (&format_obstack));
d7913476 865 XFREE (check);
c3e23647
RS
866}
867
6c89f1c1
AD
868/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
869 and yycheck. */
870
871static void
872output_actions (void)
873{
49701457 874 int i;
6c89f1c1 875 nvectors = nstates + nvars;
c3e23647 876
d7913476
AD
877 froms = XCALLOC (short *, nvectors);
878 tos = XCALLOC (short *, nvectors);
879 tally = XCALLOC (short, nvectors);
880 width = XCALLOC (short, nvectors);
6c89f1c1
AD
881
882 token_actions ();
d7913476
AD
883 XFREE (LA);
884 XFREE (LAruleno);
6c89f1c1
AD
885
886 goto_actions ();
d7913476
AD
887 XFREE (goto_map + ntokens);
888 XFREE (from_state);
889 XFREE (to_state);
6c89f1c1
AD
890
891 sort_actions ();
892 pack_table ();
4e5caae2 893
6c89f1c1
AD
894 output_base ();
895 output_table ();
4e5caae2 896
6c89f1c1 897 output_check ();
49701457
AD
898
899 for (i = 0; i < nstates; ++i)
900 {
2cec70b9 901 free (state_table[i]->shifts);
49701457 902 XFREE (state_table[i]->reductions);
2cec70b9 903 free (state_table[i]->errs);
49701457
AD
904 free (state_table[i]);
905 }
9703cc49 906 XFREE (state_table);
6c89f1c1 907}
c3e23647 908
652def80
MA
909\f
910/*------------------------------------------------------------.
911| Copy the parser code from SKEL_FILENAME into OOUT obstack. |
912| and do the muscle substitution. |
913`------------------------------------------------------------*/
c3e23647 914
4a120d45 915static void
ea52d706 916output_parser (const char *skel_filename, FILE *out)
c3e23647 917{
6c89f1c1 918 int c;
ff61dabd 919 FILE *fskel;
897668ee
MA
920 size_t output_line;
921 size_t skeleton_line;
c3e23647 922
652def80 923 fskel = xfopen (skel_filename, "r");
ef7ddedd 924
e8cb70b9 925 /* New output code. */
897668ee
MA
926 output_line = 1;
927 skeleton_line = 1;
26f609ff
RA
928 c = getc (fskel);
929 while (c != EOF)
c3e23647 930 {
26f609ff 931 if (c != '%')
573c1d9f 932 {
26f609ff 933 if (c == '\n')
897668ee
MA
934 {
935 ++output_line;
936 ++skeleton_line;
937 }
ea52d706 938 putc (c, out);
26f609ff 939 c = getc (fskel);
bbb5bcc6 940 }
26f609ff 941 else if ((c = getc (fskel)) == '%')
bbb5bcc6 942 {
11d82f03
MA
943 /* Read the muscle. */
944 const char *muscle_key = 0;
945 const char *muscle_value = 0;
652def80 946
5b5d1929 947 while (isalnum (c = getc (fskel)) || c == '-')
11d82f03
MA
948 obstack_1grow (&muscle_obstack, c);
949 obstack_1grow (&muscle_obstack, 0);
26f609ff 950
e8cb70b9 951 /* Output the right value, or see if it's something special. */
11d82f03
MA
952 muscle_key = obstack_finish (&muscle_obstack);
953 muscle_value = muscle_find (muscle_key);
3f96f4dc 954 if (!strcmp (muscle_key, "actions"))
897668ee 955 actions_output (out, &output_line);
f499b062
AD
956 else if (!strcmp (muscle_key, "guards"))
957 guards_output (out, &output_line);
11d82f03 958 else if (!strcmp (muscle_key, "line"))
897668ee
MA
959 fprintf (out, "%d", output_line);
960 else if (!strcmp (muscle_key, "skeleton-line"))
961 fprintf (out, "%d", skeleton_line);
3f96f4dc 962 else if (muscle_value)
f0440388
MA
963 {
964 fputs (muscle_value, out);
897668ee 965 output_line += get_lines_number (muscle_value);
1fa14068 966 }
bbb5bcc6 967 else
26f609ff 968 {
ea52d706
AD
969 fputs ("%%", out);
970 fputs (muscle_key, out);
26f609ff 971 }
573c1d9f 972 }
b0cfa28a 973 else
ea52d706 974 putc ('%', out);
bbb5bcc6 975 }
26f609ff 976
e8cb70b9 977 /* End. */
ff61dabd 978 xfclose (fskel);
c3e23647
RS
979}
980
652def80
MA
981/*----------------------------------------.
982| Prepare the master parser to be output |
983`----------------------------------------*/
984
985static void
986output_master_parser (void)
987{
ea52d706 988 FILE *parser = xfopen (parser_file_name, "w");
652def80
MA
989 if (!skeleton)
990 {
991 if (semantic_parser)
992 skeleton = skeleton_find ("BISON_HAIRY", BISON_HAIRY);
993 else
994 skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE);
995 }
3843c413 996 muscle_insert ("skeleton", skeleton);
f0440388 997 muscle_insert ("parser-file-name", parser_file_name);
ea52d706
AD
998
999 output_parser (skeleton, parser);
1000 xfclose (parser);
652def80
MA
1001}
1002
1003
26f609ff
RA
1004/* FIXME. */
1005
11d82f03
MA
1006#define MUSCLE_INSERT_INT(Key, Value) \
1007{ \
1008 obstack_fgrow1 (&muscle_obstack, "%d", Value); \
1009 obstack_1grow (&muscle_obstack, 0); \
1010 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
26f609ff
RA
1011}
1012
11d82f03
MA
1013#define MUSCLE_INSERT_STRING(Key, Value) \
1014{ \
1015 obstack_sgrow (&muscle_obstack, Value); \
1016 obstack_1grow (&muscle_obstack, 0); \
1017 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
26f609ff
RA
1018}
1019
11d82f03 1020#define MUSCLE_INSERT_PREFIX(Key, Value) \
26f609ff 1021{ \
11d82f03
MA
1022 obstack_fgrow2 (&muscle_obstack, "%s%s", spec_name_prefix, Value); \
1023 obstack_1grow (&muscle_obstack, 0); \
1024 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
26f609ff
RA
1025}
1026
1027static void
1028prepare (void)
1029{
11d82f03
MA
1030 MUSCLE_INSERT_INT ("last", high);
1031 MUSCLE_INSERT_INT ("flag", MINSHORT);
1032 MUSCLE_INSERT_INT ("pure", pure_parser);
1033 MUSCLE_INSERT_INT ("nsym", nsyms);
1034 MUSCLE_INSERT_INT ("debug", debug_flag);
1035 MUSCLE_INSERT_INT ("final", final_state);
1036 MUSCLE_INSERT_INT ("maxtok", max_user_token_number);
c7925b99 1037 MUSCLE_INSERT_INT ("error-verbose", error_verbose);
78af9bbc 1038 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
11d82f03
MA
1039
1040 MUSCLE_INSERT_INT ("nnts", nvars);
1041 MUSCLE_INSERT_INT ("nrules", nrules);
1042 MUSCLE_INSERT_INT ("nstates", nstates);
1043 MUSCLE_INSERT_INT ("ntokens", ntokens);
1044
5b5d1929 1045 MUSCLE_INSERT_INT ("locations-flag", locations_flag);
26f609ff 1046}
c3e23647 1047
93ede233
AD
1048
1049/*-------------------------.
1050| Output the header file. |
1051`-------------------------*/
1052
1053static void
1054header_output (void)
1055{
1056 FILE *out = xfopen (spec_defines_file, "w");
1057 char *macro_name = compute_header_macro ();
1058
1059 fprintf (out, "#ifndef %s\n", macro_name);
1060 fprintf (out, "# define %s\n\n", macro_name);
1061
1062 fputs (muscle_find ("tokendef"), out);
1063 fprintf (out, "\
1064#ifndef YYSTYPE\n\
1065typedef %s
1066yystype;\n\
1067# define YYSTYPE yystype\n\
1068#endif\n",
1069 muscle_find ("stype"));
1070
1071 if (!pure_parser)
1072 fprintf (out, "\nextern YYSTYPE %slval;\n",
1073 spec_name_prefix);
1074 if (semantic_parser)
1075 {
1076 int i;
1077
1078 for (i = ntokens; i < nsyms; i++)
1079 /* don't make these for dummy nonterminals made by gensym. */
1080 if (*tags[i] != '@')
7742ddeb 1081 fprintf (out, "# define NT%s\t%d\n", tags[i], i);
93ede233
AD
1082 }
1083
1084 fprintf (out, "\n#endif /* not %s */\n", macro_name);
1085 free (macro_name);
1086 xfclose (out);
1087}
1088
1089
6c89f1c1
AD
1090/*----------------------------------------------------------.
1091| Output the parsing tables and the parser code to ftable. |
1092`----------------------------------------------------------*/
c3e23647 1093
6c89f1c1
AD
1094void
1095output (void)
1096{
f87685c3 1097 obstack_init (&format_obstack);
dd60faec 1098
6c89f1c1 1099 output_token_translations ();
6c89f1c1 1100 output_gram ();
26f609ff 1101
d7913476 1102 XFREE (ritem);
6c89f1c1
AD
1103 if (semantic_parser)
1104 output_stos ();
1105 output_rule_data ();
0846f581 1106 XFREE (user_toknums);
6c89f1c1 1107 output_actions ();
342b8b6e 1108
26f609ff 1109 prepare ();
d1a2daf7 1110 /* Copy definitions in directive. */
5449dd0f 1111 obstack_1grow (&attrs_obstack, 0);
11d82f03 1112 muscle_insert ("prologue", obstack_finish (&attrs_obstack));
652def80 1113
93ede233 1114 /* Output the parser. */
652def80 1115 output_master_parser ();
93ede233
AD
1116 /* Output the header if needed. */
1117 if (defines_flag)
1118 header_output ();
26f609ff 1119
3f96f4dc 1120 free (rule_table + 1);
f499b062
AD
1121 obstack_free (&muscle_obstack, NULL);
1122 obstack_free (&format_obstack, NULL);
1123 obstack_free (&action_obstack, NULL);
f499b062 1124 obstack_free (&attrs_obstack, NULL);
c3e23647 1125}