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