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