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