]> git.saurik.com Git - bison.git/blame - src/output.c
* src/output.c (token_actions): Introduce a temporary array,
[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| |
f2acea59
AD
737| The YYDEFACT table is output now. The detailed info is saved for |
738| putting into YYTABLE later. |
6c89f1c1 739`------------------------------------------------------------------*/
c3e23647 740
4a120d45 741static void
6c89f1c1 742token_actions (void)
c3e23647 743{
6c89f1c1 744 int i;
f2acea59 745 short *yydefact = NEW2 (nstates, short);
c3e23647 746
6c89f1c1 747 actrow = NEW2 (ntokens, short);
f2acea59 748 for (i = 0; i < nstates; ++i)
c3e23647 749 {
f2acea59 750 yydefact[i] = action_row (i);
6c89f1c1 751 save_row (i);
c3e23647 752 }
6c89f1c1 753 FREE (actrow);
f2acea59
AD
754
755 output_short_table (ftable, "yydefact", yydefact,
756 yydefact[0], 1, nstates);
757 FREE (yydefact);
c3e23647
RS
758}
759
760
6c89f1c1
AD
761static void
762free_shifts (void)
c3e23647 763{
6c89f1c1 764 shifts *sp, *sptmp; /* JF derefrenced freed ptr */
c3e23647 765
6c89f1c1 766 FREE (shift_table);
c3e23647 767
6c89f1c1
AD
768 for (sp = first_shift; sp; sp = sptmp)
769 {
770 sptmp = sp->next;
771 FREE (sp);
772 }
773}
c3e23647 774
c3e23647 775
6c89f1c1
AD
776static void
777free_reductions (void)
778{
779 reductions *rp, *rptmp; /* JF fixed freed ptr */
c3e23647 780
6c89f1c1 781 FREE (reduction_table);
c3e23647 782
6c89f1c1 783 for (rp = first_reduction; rp; rp = rptmp)
c3e23647 784 {
6c89f1c1
AD
785 rptmp = rp->next;
786 FREE (rp);
c3e23647 787 }
c3e23647
RS
788}
789
790
6c89f1c1 791
4a120d45 792static void
d2729d44 793save_column (int symbol, int default_state)
c3e23647 794{
6c89f1c1
AD
795 int i;
796 int m;
797 int n;
798 short *sp;
799 short *sp1;
800 short *sp2;
801 int count;
802 int symno;
c3e23647
RS
803
804 m = goto_map[symbol];
805 n = goto_map[symbol + 1];
806
807 count = 0;
808 for (i = m; i < n; i++)
809 {
810 if (to_state[i] != default_state)
811 count++;
812 }
813
814 if (count == 0)
815 return;
816
817 symno = symbol - ntokens + nstates;
818
ceed8467
AD
819 froms[symno] = sp1 = sp = NEW2 (count, short);
820 tos[symno] = sp2 = NEW2 (count, short);
c3e23647
RS
821
822 for (i = m; i < n; i++)
823 {
824 if (to_state[i] != default_state)
825 {
826 *sp1++ = from_state[i];
827 *sp2++ = to_state[i];
828 }
829 }
830
831 tally[symno] = count;
832 width[symno] = sp1[-1] - sp[0] + 1;
833}
834
6c89f1c1
AD
835static int
836default_goto (int symbol)
837{
838 int i;
839 int m;
840 int n;
841 int default_state;
842 int max;
843
844 m = goto_map[symbol];
845 n = goto_map[symbol + 1];
846
847 if (m == n)
848 return -1;
849
850 for (i = 0; i < nstates; i++)
851 state_count[i] = 0;
852
853 for (i = m; i < n; i++)
854 state_count[to_state[i]]++;
855
856 max = 0;
857 default_state = -1;
858
859 for (i = 0; i < nstates; i++)
860 {
861 if (state_count[i] > max)
862 {
863 max = state_count[i];
864 default_state = i;
865 }
866 }
867
868 return default_state;
869}
870
871
872/*-------------------------------------------------------------------.
873| Figure out what to do after reducing with each rule, depending on |
874| the saved state from before the beginning of parsing the data that |
875| matched this rule. |
876| |
877| The YYDEFGOTO table is output now. The detailed info is saved for |
878| putting into YYTABLE later. |
879`-------------------------------------------------------------------*/
880
881static void
882goto_actions (void)
883{
884 int i, j, k;
885
886 state_count = NEW2 (nstates, short);
887
888 k = default_goto (ntokens);
889 fprintf (ftable, "\nstatic const short yydefgoto[] = {%6d", k);
890 save_column (ntokens, k);
891
892 j = 10;
893 for (i = ntokens + 1; i < nsyms; i++)
894 {
895 putc (',', ftable);
896
897 if (j >= 10)
898 {
899 putc ('\n', ftable);
900 j = 1;
901 }
902 else
903 {
904 j++;
905 }
906
907 k = default_goto (i);
908 fprintf (ftable, "%6d", k);
909 save_column (i, k);
910 }
911
912 fprintf (ftable, "\n};\n");
913 FREE (state_count);
914}
c3e23647
RS
915
916
9ee3c97b
AD
917/* The next few functions decide how to pack the actions and gotos
918 information into yytable. */
c3e23647 919
4a120d45 920static void
d2729d44 921sort_actions (void)
c3e23647 922{
6c89f1c1
AD
923 int i;
924 int j;
925 int k;
926 int t;
927 int w;
c3e23647 928
ceed8467 929 order = NEW2 (nvectors, short);
c3e23647
RS
930 nentries = 0;
931
932 for (i = 0; i < nvectors; i++)
933 {
934 if (tally[i] > 0)
935 {
936 t = tally[i];
937 w = width[i];
938 j = nentries - 1;
939
940 while (j >= 0 && (width[order[j]] < w))
941 j--;
942
943 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
944 j--;
945
946 for (k = nentries - 1; k > j; k--)
947 order[k + 1] = order[k];
948
949 order[j + 1] = i;
950 nentries++;
951 }
952 }
953}
954
955
4a120d45 956static int
d2729d44 957matching_state (int vector)
c3e23647 958{
6c89f1c1
AD
959 int i;
960 int j;
961 int k;
962 int t;
963 int w;
964 int match;
965 int prev;
c3e23647
RS
966
967 i = order[vector];
968 if (i >= nstates)
36281465 969 return -1;
c3e23647
RS
970
971 t = tally[i];
972 w = width[i];
973
974 for (prev = vector - 1; prev >= 0; prev--)
975 {
976 j = order[prev];
977 if (width[j] != w || tally[j] != t)
36281465 978 return -1;
c3e23647
RS
979
980 match = 1;
981 for (k = 0; match && k < t; k++)
982 {
983 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
984 match = 0;
985 }
986
987 if (match)
36281465 988 return j;
c3e23647
RS
989 }
990
36281465 991 return -1;
c3e23647
RS
992}
993
994
4a120d45 995static int
d2729d44 996pack_vector (int vector)
c3e23647 997{
6c89f1c1
AD
998 int i;
999 int j;
1000 int k;
1001 int t;
1002 int loc = 0;
1003 int ok;
1004 short *from;
1005 short *to;
c3e23647
RS
1006
1007 i = order[vector];
1008 t = tally[i];
1009
1010 if (t == 0)
ceed8467 1011 berror ("pack_vector");
c3e23647
RS
1012
1013 from = froms[i];
1014 to = tos[i];
1015
1016 for (j = lowzero - from[0]; j < MAXTABLE; j++)
1017 {
1018 ok = 1;
1019
1020 for (k = 0; ok && k < t; k++)
1021 {
1022 loc = j + from[k];
1023 if (loc > MAXTABLE)
a0f6b076 1024 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
c3e23647
RS
1025
1026 if (table[loc] != 0)
1027 ok = 0;
1028 }
1029
1030 for (k = 0; ok && k < vector; k++)
1031 {
1032 if (pos[k] == j)
1033 ok = 0;
1034 }
1035
1036 if (ok)
1037 {
1038 for (k = 0; k < t; k++)
1039 {
1040 loc = j + from[k];
1041 table[loc] = to[k];
1042 check[loc] = from[k];
1043 }
1044
1045 while (table[lowzero] != 0)
1046 lowzero++;
1047
1048 if (loc > high)
1049 high = loc;
1050
36281465 1051 return j;
c3e23647
RS
1052 }
1053 }
1054
ceed8467
AD
1055 berror ("pack_vector");
1056 return 0; /* JF keep lint happy */
c3e23647
RS
1057}
1058
1059
6c89f1c1
AD
1060static void
1061pack_table (void)
1062{
1063 int i;
1064 int place;
1065 int state;
1066
1067 base = NEW2 (nvectors, short);
1068 pos = NEW2 (nentries, short);
1069 table = NEW2 (MAXTABLE, short);
1070 check = NEW2 (MAXTABLE, short);
1071
1072 lowzero = 0;
1073 high = 0;
1074
1075 for (i = 0; i < nvectors; i++)
1076 base[i] = MINSHORT;
1077
1078 for (i = 0; i < MAXTABLE; i++)
1079 check[i] = -1;
1080
1081 for (i = 0; i < nentries; i++)
1082 {
1083 state = matching_state (i);
1084
1085 if (state < 0)
1086 place = pack_vector (i);
1087 else
1088 place = base[state];
1089
1090 pos[i] = place;
1091 base[order[i]] = place;
1092 }
1093
1094 for (i = 0; i < nvectors; i++)
1095 {
1096 if (froms[i])
1097 FREE (froms[i]);
1098 if (tos[i])
1099 FREE (tos[i]);
1100 }
1101
1102 FREE (froms);
1103 FREE (tos);
1104 FREE (pos);
1105}
c3e23647
RS
1106
1107/* the following functions output yytable, yycheck
1108 and the vectors whose elements index the portion starts */
1109
4a120d45 1110static void
d2729d44 1111output_base (void)
c3e23647 1112{
d019d655
AD
1113 output_short_table (ftable, "yypact", base,
1114 base[0], 1, nstates);
c3e23647 1115
d019d655 1116 putc ('\n', ftable);
c3e23647 1117
d019d655
AD
1118 output_short_table (ftable, "yypgoto", base,
1119 base[nstates], nstates + 1, nvectors);
c3e23647 1120
ceed8467 1121 FREE (base);
c3e23647
RS
1122}
1123
1124
4a120d45 1125static void
d2729d44 1126output_table (void)
c3e23647 1127{
d019d655
AD
1128 fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n\n", high);
1129 output_short_table (ftable, "yytable", table,
1130 table[0], 1, high + 1);
ceed8467 1131 FREE (table);
c3e23647
RS
1132}
1133
1134
4a120d45 1135static void
d2729d44 1136output_check (void)
c3e23647 1137{
d019d655
AD
1138 output_short_table (ftable, "yycheck", check,
1139 check[0], 1, high + 1);
ceed8467 1140 FREE (check);
c3e23647
RS
1141}
1142
6c89f1c1
AD
1143/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
1144 and yycheck. */
1145
1146static void
1147output_actions (void)
1148{
1149 nvectors = nstates + nvars;
c3e23647 1150
6c89f1c1
AD
1151 froms = NEW2 (nvectors, short *);
1152 tos = NEW2 (nvectors, short *);
1153 tally = NEW2 (nvectors, short);
1154 width = NEW2 (nvectors, short);
1155
1156 token_actions ();
1157 free_shifts ();
1158 free_reductions ();
1159 FREE (lookaheads);
1160 FREE (LA);
1161 FREE (LAruleno);
1162 FREE (accessing_symbol);
1163
1164 goto_actions ();
1165 FREE (goto_map + ntokens);
1166 FREE (from_state);
1167 FREE (to_state);
1168
1169 sort_actions ();
1170 pack_table ();
d019d655 1171 putc ('\n', ftable);
6c89f1c1
AD
1172 output_base ();
1173 output_table ();
d019d655 1174 putc ('\n', ftable);
6c89f1c1
AD
1175 output_check ();
1176}
c3e23647
RS
1177
1178/* copy the parser code into the ftable file at the end. */
1179
4a120d45 1180static void
d2729d44 1181output_parser (void)
c3e23647 1182{
6c89f1c1 1183 int c;
c3e23647
RS
1184#ifdef DONTDEF
1185 FILE *fpars;
1186#else
1187#define fpars fparser
1188#endif
1189
1190 if (pure_parser)
ceed8467 1191 fprintf (ftable, "#define YYPURE 1\n\n");
c3e23647 1192
ceed8467
AD
1193#ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1194 currently open parser from bison.simple to bison.hairy */
c3e23647
RS
1195 if (semantic_parser)
1196 fpars = fparser;
ceed8467
AD
1197 else
1198 fpars = fparser1;
c3e23647
RS
1199#endif
1200
1201 /* Loop over lines in the standard parser file. */
1202
1203 while (1)
1204 {
1205 int write_line = 1;
1206
ceed8467 1207 c = getc (fpars);
c3e23647
RS
1208
1209 /* See if the line starts with `#line.
ceed8467 1210 If so, set write_line to 0. */
c3e23647 1211 if (nolinesflag)
9ee3c97b 1212 if (c == '#')
c3e23647 1213 {
ceed8467 1214 c = getc (fpars);
c3e23647
RS
1215 if (c == 'l')
1216 {
ceed8467 1217 c = getc (fpars);
c3e23647
RS
1218 if (c == 'i')
1219 {
ceed8467 1220 c = getc (fpars);
c3e23647
RS
1221 if (c == 'n')
1222 {
ceed8467 1223 c = getc (fpars);
c3e23647
RS
1224 if (c == 'e')
1225 write_line = 0;
1226 else
ceed8467 1227 fprintf (ftable, "#lin");
c3e23647
RS
1228 }
1229 else
ceed8467 1230 fprintf (ftable, "#li");
c3e23647
RS
1231 }
1232 else
ceed8467 1233 fprintf (ftable, "#l");
c3e23647
RS
1234 }
1235 else
ceed8467 1236 fprintf (ftable, "#");
c3e23647
RS
1237 }
1238
1239 /* now write out the line... */
ceed8467
AD
1240 for (; c != '\n' && c != EOF; c = getc (fpars))
1241 if (write_line)
1242 {
1243 if (c == '$')
1244 {
1245 /* `$' in the parser file indicates where to put the actions.
1246 Copy them in at this point. */
1247 rewind (faction);
1248 for (c = getc (faction); c != EOF; c = getc (faction))
1249 putc (c, ftable);
1250 }
1251 else
1252 putc (c, ftable);
1253 }
c3e23647
RS
1254 if (c == EOF)
1255 break;
ceed8467 1256 putc (c, ftable);
c3e23647
RS
1257 }
1258}
1259
4a120d45 1260static void
d2729d44 1261output_program (void)
c3e23647 1262{
6c89f1c1 1263 int c;
c3e23647
RS
1264
1265 if (!nolinesflag)
ceed8467 1266 fprintf (ftable, "#line %d \"%s\"\n", lineno, infile);
c3e23647 1267
ceed8467 1268 c = getc (finput);
c3e23647
RS
1269 while (c != EOF)
1270 {
ceed8467
AD
1271 putc (c, ftable);
1272 c = getc (finput);
c3e23647
RS
1273 }
1274}
1275
1276
4a120d45 1277static void
d2729d44 1278free_itemsets (void)
c3e23647 1279{
6c89f1c1 1280 core *cp, *cptmp;
c3e23647 1281
ceed8467 1282 FREE (state_table);
c3e23647 1283
36281465
AD
1284 for (cp = first_state; cp; cp = cptmp)
1285 {
ceed8467
AD
1286 cptmp = cp->next;
1287 FREE (cp);
36281465 1288 }
c3e23647
RS
1289}
1290
1291
6c89f1c1
AD
1292/*----------------------------------------------------------.
1293| Output the parsing tables and the parser code to ftable. |
1294`----------------------------------------------------------*/
c3e23647 1295
6c89f1c1
AD
1296void
1297output (void)
1298{
1299 int c;
c3e23647 1300
6c89f1c1
AD
1301 /* output_token_defines(ftable); / * JF put out token defines FIRST */
1302 if (!semantic_parser) /* JF Put out other stuff */
36281465 1303 {
6c89f1c1
AD
1304 rewind (fattrs);
1305 while ((c = getc (fattrs)) != EOF)
1306 putc (c, ftable);
36281465 1307 }
6c89f1c1
AD
1308 reader_output_yylsp (ftable);
1309 if (debugflag)
1310 fputs ("\
1311#ifndef YYDEBUG\n\
1312#define YYDEBUG 1\n\
1313#endif\n\
1314\n",
1315 ftable);
c3e23647 1316
6c89f1c1
AD
1317 if (semantic_parser)
1318 fprintf (ftable, "#include \"%s\"\n", attrsfile);
c3e23647 1319
6c89f1c1
AD
1320 if (!noparserflag)
1321 fprintf (ftable, "#include <stdio.h>\n\n");
c3e23647 1322
6c89f1c1
AD
1323 /* Make "const" do nothing if not in ANSI C. */
1324 fputs ("\
1325#ifndef __cplusplus\n\
1326# ifndef __STDC__\n\
1327# define const\n\
1328# endif\n\
1329#endif\n\
1330\n",
1331 ftable);
c3e23647 1332
6c89f1c1
AD
1333 free_itemsets ();
1334 output_defines ();
1335 output_token_translations ();
1336/* if (semantic_parser) */
1337 /* This is now unconditional because debugging printouts can use it. */
1338 output_gram ();
1339 FREE (ritem);
1340 if (semantic_parser)
1341 output_stos ();
1342 output_rule_data ();
1343 output_actions ();
1344 if (!noparserflag)
1345 output_parser ();
1346 output_program ();
c3e23647 1347}