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