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