1 /* $Id: output.c,v 1.76 2016/06/07 00:14:34 tom Exp $ */
5 #define StaticOrR (rflag ? "" : "static ")
6 #define CountLine(fp) (!rflag || ((fp) == code_file))
16 static Value_t **froms;
19 static Value_t *conflicts = NULL;
20 static Value_t nconflicts = 0;
22 static Value_t *tally;
23 static Value_t *width;
24 static Value_t *state_count;
25 static Value_t *order;
29 static Value_t *table;
30 static Value_t *check;
35 putc_code(FILE * fp, int c)
37 if ((c == '\n') && (fp == code_file))
43 putl_code(FILE * fp, const char *s)
51 puts_code(FILE * fp, const char *s)
57 puts_param_types(FILE * fp, param *list, int more)
63 for (p = list; p; p = p->next)
65 size_t len_type = strlen(p->type);
66 fprintf(fp, "%s%s%s%s%s", p->type,
67 (((len_type != 0) && (p->type[len_type - 1] == '*'))
71 ((more || p->next) ? ", " : ""));
82 puts_param_names(FILE * fp, param *list, int more)
86 for (p = list; p; p = p->next)
88 fprintf(fp, "%s%s", p->name,
89 ((more || p->next) ? ", " : ""));
94 write_code_lineno(FILE * fp)
96 if (!lflag && (fp == code_file))
99 fprintf(fp, line_format, outline + 1, code_file_name);
104 write_input_lineno(void)
109 fprintf(code_file, line_format, lineno, input_file_name);
114 define_prefixed(FILE * fp, const char *name)
116 int bump_line = CountLine(fp);
123 fprintf(fp, "#ifndef %s\n", name);
127 fprintf(fp, "#define %-10s %s%s\n", name, symbol_prefix, name + 2);
131 fprintf(fp, "#endif /* %s */\n", name);
135 output_prefix(FILE * fp)
137 if (symbol_prefix == NULL)
139 symbol_prefix = "yy";
143 define_prefixed(fp, "yyparse");
144 define_prefixed(fp, "yylex");
145 define_prefixed(fp, "yyerror");
146 define_prefixed(fp, "yychar");
147 define_prefixed(fp, "yyval");
148 define_prefixed(fp, "yylval");
149 define_prefixed(fp, "yydebug");
150 define_prefixed(fp, "yynerrs");
151 define_prefixed(fp, "yyerrflag");
152 define_prefixed(fp, "yylhs");
153 define_prefixed(fp, "yylen");
154 define_prefixed(fp, "yydefred");
155 #if defined(YYBTYACC)
156 define_prefixed(fp, "yystos");
158 define_prefixed(fp, "yydgoto");
159 define_prefixed(fp, "yysindex");
160 define_prefixed(fp, "yyrindex");
161 define_prefixed(fp, "yygindex");
162 define_prefixed(fp, "yytable");
163 define_prefixed(fp, "yycheck");
164 define_prefixed(fp, "yyname");
165 define_prefixed(fp, "yyrule");
166 #if defined(YYBTYACC)
169 define_prefixed(fp, "yyloc");
170 define_prefixed(fp, "yylloc");
173 putl_code(fp, "#if YYBTYACC\n");
175 define_prefixed(fp, "yycindex");
176 define_prefixed(fp, "yyctable");
179 putl_code(fp, "#endif /* YYBTYACC */\n");
185 fprintf(fp, "#define YYPREFIX \"%s\"\n", symbol_prefix);
193 putc('\n', output_file);
197 output_line(const char *value)
199 fputs(value, output_file);
204 output_int(int value)
206 fprintf(output_file, "%5d,", value);
210 start_int_table(const char *name, int value)
212 int need = 34 - (int)(strlen(symbol_prefix) + strlen(name));
217 "%sconst YYINT %s%s[] = {%*d,",
218 StaticOrR, symbol_prefix, name, need, value);
222 start_str_table(const char *name)
225 "%sconst char *const %s%s[] = {",
226 StaticOrR, symbol_prefix, name);
238 output_YYINT_typedef(FILE * fp)
240 /* generate the type used to index the various parser tables */
243 fprintf(fp, "typedef %s YYINT;\n", CONCAT1("", YYINT));
247 output_rule_data(void)
252 output_YYINT_typedef(output_file);
254 start_int_table("lhs", symbol_value[start_symbol]);
257 for (i = 3; i < nrules; i++)
267 output_int(symbol_value[rlhs[i]]);
271 start_int_table("len", 2);
274 for (i = 3; i < nrules; i++)
284 output_int(rrhs[i + 1] - rrhs[i] - 1);
290 output_yydefred(void)
294 start_int_table("defred", (defred[0] ? defred[0] - 2 : 0));
297 for (i = 1; i < nstates; i++)
307 output_int((defred[i] ? defred[i] - 2 : 0));
313 #if defined(YYBTYACC)
315 output_accessing_symbols(void)
322 translate = TMALLOC(int, nstates);
325 for (i = 0; i < nstates; ++i)
327 int gsymb = accessing_symbol[i];
329 translate[i] = symbol_pval[gsymb];
332 /* yystos[] may be unused, depending on compile-time defines */
333 start_int_table("stos", translate[0]);
336 for (i = 1; i < nstates; ++i)
346 output_int(translate[i]);
355 find_conflict_base(int cbase)
359 for (i = 0; i < cbase; i++)
361 for (j = 0; j + cbase < nconflicts; j++)
363 if (conflicts[i + j] != conflicts[cbase + j])
366 if (j + cbase >= nconflicts)
377 Value_t shiftcount, reducecount;
378 #if defined(YYBTYACC)
379 Value_t conflictcount = 0;
384 Value_t *actionrow, *r, *s;
387 actionrow = NEW2(PER_STATE * ntokens, Value_t);
388 for (i = 0; i < nstates; ++i)
392 for (j = 0; j < PER_STATE * ntokens; ++j)
397 #if defined(YYBTYACC)
405 for (p = parser[i]; p; p = p->next)
407 #if defined(YYBTYACC)
410 if (csym != -1 && csym != p->symbol)
413 conflicts[nconflicts++] = -1;
414 j = find_conflict_base(cbase);
415 actionrow[csym + 2 * ntokens] = (Value_t)(j + 1);
422 if (conflicts[cbase] == -1)
430 if (p->suppressed == 0)
432 if (p->action_code == SHIFT)
435 actionrow[p->symbol] = p->number;
437 else if (p->action_code == REDUCE && p->number != defred[i])
440 actionrow[p->symbol + ntokens] = p->number;
443 #if defined(YYBTYACC)
444 else if (backtrack && p->suppressed == 1)
447 if (p->action_code == SHIFT)
449 conflicts[nconflicts++] = p->number;
451 else if (p->action_code == REDUCE && p->number != defred[i])
453 if (cbase == nconflicts)
458 conflicts[nconflicts++] = -1;
460 conflicts[nconflicts++] = (Value_t)(p->number - 2);
465 #if defined(YYBTYACC)
466 if (backtrack && csym != -1)
469 conflicts[nconflicts++] = -1;
470 j = find_conflict_base(cbase);
471 actionrow[csym + 2 * ntokens] = (Value_t)(j + 1);
478 if (conflicts[cbase] == -1)
485 tally[i] = shiftcount;
486 tally[nstates + i] = reducecount;
487 #if defined(YYBTYACC)
489 tally[2 * nstates + i] = conflictcount;
492 width[nstates + i] = 0;
493 #if defined(YYBTYACC)
495 width[2 * nstates + i] = 0;
499 froms[i] = r = NEW2(shiftcount, Value_t);
500 tos[i] = s = NEW2(shiftcount, Value_t);
503 for (j = 0; j < ntokens; ++j)
507 if (min > symbol_value[j])
508 min = symbol_value[j];
509 if (max < symbol_value[j])
510 max = symbol_value[j];
511 *r++ = symbol_value[j];
515 width[i] = (Value_t)(max - min + 1);
519 froms[nstates + i] = r = NEW2(reducecount, Value_t);
520 tos[nstates + i] = s = NEW2(reducecount, Value_t);
523 for (j = 0; j < ntokens; ++j)
525 if (actionrow[ntokens + j])
527 if (min > symbol_value[j])
528 min = symbol_value[j];
529 if (max < symbol_value[j])
530 max = symbol_value[j];
531 *r++ = symbol_value[j];
532 *s++ = (Value_t)(actionrow[ntokens + j] - 2);
535 width[nstates + i] = (Value_t)(max - min + 1);
537 #if defined(YYBTYACC)
538 if (backtrack && conflictcount > 0)
540 froms[2 * nstates + i] = r = NEW2(conflictcount, Value_t);
541 tos[2 * nstates + i] = s = NEW2(conflictcount, Value_t);
544 for (j = 0; j < ntokens; ++j)
546 if (actionrow[2 * ntokens + j])
548 if (min > symbol_value[j])
549 min = symbol_value[j];
550 if (max < symbol_value[j])
551 max = symbol_value[j];
552 *r++ = symbol_value[j];
553 *s++ = (Value_t)(actionrow[2 * ntokens + j] - 1);
556 width[2 * nstates + i] = (Value_t)(max - min + 1);
565 default_goto(int symbol)
573 m = goto_map[symbol];
574 n = goto_map[symbol + 1];
579 for (i = 0; i < nstates; i++)
582 for (i = m; i < n; i++)
583 state_count[to_state[i]]++;
587 for (i = 0; i < nstates; i++)
589 if (state_count[i] > max)
591 max = state_count[i];
596 return (default_state);
600 save_column(int symbol, int default_state)
611 m = goto_map[symbol];
612 n = goto_map[symbol + 1];
615 for (i = m; i < n; i++)
617 if (to_state[i] != default_state)
623 symno = symbol_value[symbol] + PER_STATE * nstates;
625 froms[symno] = sp1 = sp = NEW2(count, Value_t);
626 tos[symno] = sp2 = NEW2(count, Value_t);
628 for (i = m; i < n; i++)
630 if (to_state[i] != default_state)
632 *sp1++ = from_state[i];
633 *sp2++ = to_state[i];
637 tally[symno] = count;
638 width[symno] = (Value_t)(sp1[-1] - sp[0] + 1);
646 state_count = NEW2(nstates, Value_t);
648 k = default_goto(start_symbol + 1);
649 start_int_table("dgoto", k);
650 save_column(start_symbol + 1, k);
653 for (i = start_symbol + 2; i < nsyms; i++)
681 order = NEW2(nvectors, Value_t);
684 for (i = 0; i < nvectors; i++)
692 while (j >= 0 && (width[order[j]] < w))
695 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
698 for (k = nentries - 1; k > j; k--)
699 order[k + 1] = order[k];
707 /* The function matching_vector determines if the vector specified by */
708 /* the input parameter matches a previously considered vector. The */
709 /* test at the start of the function checks if the vector represents */
710 /* a row of shifts over terminal symbols or a row of reductions, or a */
711 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
712 /* check if a column of shifts over a nonterminal symbols matches a */
713 /* previously considered vector. Because of the nature of LR parsing */
714 /* tables, no two columns can match. Therefore, the only possible */
715 /* match would be between a row and a column. Such matches are */
716 /* unlikely. Therefore, to save time, no attempt is made to see if a */
717 /* column matches a previously considered vector. */
719 /* Matching_vector is poorly designed. The test could easily be made */
720 /* faster. Also, it depends on the vectors being in a specific */
722 #if defined(YYBTYACC)
724 /* Not really any point in checking for matching conflicts -- it is */
725 /* extremely unlikely to occur, and conflicts are (hopefully) rare. */
729 matching_vector(int vector)
740 if (i >= 2 * nstates)
746 for (prev = vector - 1; prev >= 0; prev--)
749 if (width[j] != w || tally[j] != t)
753 for (k = 0; match && k < t; k++)
755 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
767 pack_vector(int vector)
784 j = lowzero - from[0];
785 for (k = 1; k < t; ++k)
786 if (lowzero - from[k] > j)
787 j = lowzero - from[k];
793 for (k = 0; ok && k < t; k++)
796 if (loc >= maxtable - 1)
798 if (loc >= MAXTABLE - 1)
799 fatal("maximum table size exceeded");
806 while (newmax <= loc);
808 table = TREALLOC(Value_t, table, newmax);
811 check = TREALLOC(Value_t, check, newmax);
814 for (l = maxtable; l < newmax; ++l)
822 if (check[loc] != -1)
825 for (k = 0; ok && k < vector; k++)
832 for (k = 0; k < t; k++)
836 check[loc] = from[k];
841 while (check[lowzero] != -1)
856 base = NEW2(nvectors, Value_t);
857 pos = NEW2(nentries, Value_t);
860 table = NEW2(maxtable, Value_t);
861 check = NEW2(maxtable, Value_t);
866 for (i = 0; i < maxtable; i++)
869 for (i = 0; i < nentries; i++)
871 state = matching_vector(i);
874 place = (Value_t)pack_vector(i);
879 base[order[i]] = place;
882 for (i = 0; i < nvectors; i++)
902 start_int_table("sindex", base[0]);
905 for (i = 1; i < nstates; i++)
920 start_int_table("rindex", base[nstates]);
923 for (i = nstates + 1; i < 2 * nstates; i++)
938 #if defined(YYBTYACC)
939 output_line("#if YYBTYACC");
940 start_int_table("cindex", base[2 * nstates]);
943 for (i = 2 * nstates + 1; i < 3 * nstates; i++)
957 output_line("#endif");
960 start_int_table("gindex", base[PER_STATE * nstates]);
963 for (i = PER_STATE * nstates + 1; i < nvectors - 1; i++)
986 if (high >= MAXYYINT)
988 fprintf(stderr, "YYTABLESIZE: %ld\n", high);
989 fprintf(stderr, "Table is longer than %d elements.\n", MAXYYINT);
994 fprintf(code_file, "#define YYTABLESIZE %ld\n", high);
995 start_int_table("table", table[0]);
998 for (i = 1; i <= high; i++)
1008 output_int(table[i]);
1021 start_int_table("check", check[0]);
1024 for (i = 1; i <= high; i++)
1034 output_int(check[i]);
1041 #if defined(YYBTYACC)
1047 int limit = (conflicts != 0) ? nconflicts : 0;
1052 output_line("#if YYBTYACC");
1053 start_int_table("ctable", conflicts ? conflicts[0] : -1);
1056 for (i = 1; i < limit; i++)
1066 output_int((conflicts != 0 && i < nconflicts) ? conflicts[i] : -1);
1073 output_line("#endif");
1078 output_actions(void)
1080 nvectors = PER_STATE * nstates + nvars;
1082 froms = NEW2(nvectors, Value_t *);
1083 tos = NEW2(nvectors, Value_t *);
1084 tally = NEW2(nvectors, Value_t);
1085 width = NEW2(nvectors, Value_t);
1087 #if defined(YYBTYACC)
1088 if (backtrack && (SRtotal + RRtotal) != 0)
1089 conflicts = NEW2(4 * (SRtotal + RRtotal), Value_t);
1096 FREE(accessing_symbol);
1108 #if defined(YYBTYACC)
1114 is_C_identifier(char *name)
1124 if (!isalpha(c) && c != '_' && c != '$')
1126 while ((c = *++s) != '"')
1128 if (!isalnum(c) && c != '_' && c != '$')
1134 if (!isalpha(c) && c != '_' && c != '$')
1136 while ((c = *++s) != 0)
1138 if (!isalnum(c) && c != '_' && c != '$')
1144 #if USE_HEADER_GUARDS
1146 start_defines_file(void)
1148 fprintf(defines_file, "#ifndef _%s_defines_h_\n", symbol_prefix);
1149 fprintf(defines_file, "#define _%s_defines_h_\n\n", symbol_prefix);
1153 end_defines_file(void)
1155 fprintf(defines_file, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix);
1158 #define start_defines_file() /* nothing */
1159 #define end_defines_file() /* nothing */
1163 output_defines(FILE * fp)
1168 for (i = 2; i < ntokens; ++i)
1171 if (is_C_identifier(s) && (!sflag || *s != '"'))
1173 fprintf(fp, "#define ");
1177 while ((c = *++s) != '"')
1188 while ((c = *++s) != 0);
1190 if (fp == code_file)
1192 fprintf(fp, " %d\n", symbol_value[i]);
1196 if (fp == code_file)
1198 if (fp != defines_file || iflag)
1199 fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]);
1201 if (token_table && rflag && fp != externs_file)
1203 if (fp == code_file)
1205 fputs("#undef yytname\n", fp);
1206 if (fp == code_file)
1208 fputs("#define yytname yyname\n", fp);
1211 if (fp == defines_file || (iflag && !dflag))
1215 if (union_file != 0)
1218 while ((c = getc(union_file)) != EOF)
1221 fprintf(fp, "extern YYSTYPE %slval;\n", symbol_prefix);
1227 output_stored_text(FILE * fp)
1233 if (text_file == NULL)
1234 open_error("text_file");
1236 if ((c = getc(in)) == EOF)
1239 while ((c = getc(in)) != EOF)
1243 write_code_lineno(fp);
1249 int i, j, k, max, maxtok;
1250 const char **symnam;
1254 fprintf(code_file, "#define YYFINAL %d\n", final_state);
1256 putl_code(code_file, "#ifndef YYDEBUG\n");
1258 fprintf(code_file, "#define YYDEBUG %d\n", tflag);
1259 putl_code(code_file, "#endif\n");
1263 fprintf(output_file, "#ifndef YYDEBUG\n");
1264 fprintf(output_file, "#define YYDEBUG %d\n", tflag);
1265 fprintf(output_file, "#endif\n");
1269 for (i = 0; i < ntokens; ++i)
1270 if (symbol_value[i] > maxtok)
1271 maxtok = symbol_value[i];
1273 /* symbol_value[$accept] = -1 */
1274 /* symbol_value[<goal>] = 0 */
1275 /* remaining non-terminals start at 1 */
1277 for (i = ntokens; i < nsyms; ++i)
1278 if (((maxtok + 1) + (symbol_value[i] + 1)) > max)
1279 max = (maxtok + 1) + (symbol_value[i] + 1);
1282 fprintf(code_file, "#define YYMAXTOKEN %d\n", maxtok);
1285 fprintf(code_file, "#define YYUNDFTOKEN %d\n", max + 1);
1288 fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? "
1289 "YYUNDFTOKEN : (a))\n");
1291 symnam = TMALLOC(const char *, max + 2);
1294 /* Note that it is not necessary to initialize the element */
1296 #if defined(YYBTYACC)
1297 for (i = 0; i < max; ++i)
1299 for (i = nsyms - 1; i >= 0; --i)
1300 symnam[symbol_pval[i]] = symbol_name[i];
1301 symnam[max + 1] = "illegal-symbol";
1303 for (i = 0; i <= max; ++i)
1305 for (i = ntokens - 1; i >= 2; --i)
1306 symnam[symbol_value[i]] = symbol_name[i];
1307 symnam[0] = "end-of-file";
1308 symnam[max + 1] = "illegal-symbol";
1312 * bison's yytname[] array is roughly the same as byacc's yyname[] array.
1313 * The difference is that byacc does not predefine "$undefined".
1315 * If the grammar declares "%token-table", define symbol "yytname" so
1316 * an application such as ntpd can build.
1322 output_line("#undef yytname");
1323 output_line("#define yytname yyname");
1328 output_line("#if YYDEBUG");
1331 start_str_table("name");
1333 for (i = 0; i <= max + 1; ++i)
1335 if ((s = symnam[i]) != 0)
1356 fprintf(output_file, "\"\\\"");
1362 fprintf(output_file, "\\\\");
1364 fprintf(output_file, "\\\\");
1366 putc(*s, output_file);
1369 putc(*s, output_file);
1371 fprintf(output_file, "\\\"\",");
1373 else if (s[0] == '\'')
1383 fprintf(output_file, "\"'\\\"'\",");
1388 while (*++s != '\'')
1404 fprintf(output_file, "\"'");
1406 while (*++s != '\'')
1410 fprintf(output_file, "\\\\");
1412 fprintf(output_file, "\\\\");
1414 putc(*s, output_file);
1417 putc(*s, output_file);
1419 fprintf(output_file, "'\",");
1424 k = (int)strlen(s) + 3;
1431 putc('"', output_file);
1434 putc(*s, output_file);
1437 fprintf(output_file, "\",");
1448 fprintf(output_file, "0,");
1455 output_line("#if YYDEBUG");
1456 start_str_table("rule");
1457 for (i = 2; i < nrules; ++i)
1459 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1460 for (j = rrhs[i]; ritem[j] > 0; ++j)
1462 s = symbol_name[ritem[j]];
1465 fprintf(output_file, " \\\"");
1471 fprintf(output_file, "\\\\\\\\");
1473 fprintf(output_file, "\\\\%c", s[1]);
1477 putc(*s, output_file);
1479 fprintf(output_file, "\\\"");
1481 else if (s[0] == '\'')
1484 fprintf(output_file, " '\\\"'");
1485 else if (s[1] == '\\')
1488 fprintf(output_file, " '\\\\\\\\");
1490 fprintf(output_file, " '\\\\%c", s[2]);
1492 while (*++s != '\'')
1493 putc(*s, output_file);
1494 putc('\'', output_file);
1497 fprintf(output_file, " '%c'", s[1]);
1500 fprintf(output_file, " %s", s);
1502 fprintf(output_file, "\",");
1507 output_line("#endif");
1510 #if defined(YYBTYACC)
1512 output_backtracking_parser(FILE * fp)
1514 putl_code(fp, "#undef YYBTYACC\n");
1515 #if defined(YYBTYACC)
1518 putl_code(fp, "#define YYBTYACC 1\n");
1520 "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n");
1525 putl_code(fp, "#define YYBTYACC 0\n");
1526 putl_code(fp, "#define YYDEBUGSTR YYPREFIX \"debug\"\n");
1532 output_pure_parser(FILE * fp)
1534 putc_code(fp, '\n');
1536 if (fp == code_file)
1538 fprintf(fp, "#define YYPURE %d\n", pure_parser);
1539 putc_code(fp, '\n');
1543 output_stype(FILE * fp)
1545 if (!unionized && ntags == 0)
1547 putc_code(fp, '\n');
1548 putl_code(fp, "#if "
1549 "! defined(YYSTYPE) && "
1550 "! defined(YYSTYPE_IS_DECLARED)\n");
1551 putl_code(fp, "/* Default: YYSTYPE is the semantic value type. */\n");
1552 putl_code(fp, "typedef int YYSTYPE;\n");
1553 putl_code(fp, "# define YYSTYPE_IS_DECLARED 1\n");
1554 putl_code(fp, "#endif\n");
1558 #if defined(YYBTYACC)
1560 output_ltype(FILE * fp)
1562 putc_code(fp, '\n');
1563 putl_code(fp, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n");
1564 putl_code(fp, "/* Default: YYLTYPE is the text position type. */\n");
1565 putl_code(fp, "typedef struct YYLTYPE\n");
1566 putl_code(fp, "{\n");
1567 putl_code(fp, " int first_line;\n");
1568 putl_code(fp, " int first_column;\n");
1569 putl_code(fp, " int last_line;\n");
1570 putl_code(fp, " int last_column;\n");
1571 putl_code(fp, "} YYLTYPE;\n");
1572 putl_code(fp, "#define YYLTYPE_IS_DECLARED 1\n");
1573 putl_code(fp, "#endif\n");
1578 output_trailing_text(void)
1591 if ((c = getc(in)) == EOF)
1593 write_input_lineno();
1594 putc_code(code_file, c);
1599 write_input_lineno();
1602 putc_code(code_file, c);
1604 while ((c = *++cptr) != '\n');
1605 putc_code(code_file, c);
1609 while ((c = getc(in)) != EOF)
1611 putc_code(code_file, c);
1617 putc_code(code_file, '\n');
1619 write_code_lineno(code_file);
1623 output_semantic_actions(void)
1627 rewind(action_file);
1628 if ((c = getc(action_file)) == EOF)
1632 putc_code(code_file, c);
1633 while ((c = getc(action_file)) != EOF)
1635 putc_code(code_file, c);
1641 putc_code(code_file, '\n');
1644 write_code_lineno(code_file);
1648 output_parse_decl(FILE * fp)
1650 putc_code(fp, '\n');
1651 putl_code(fp, "/* compatibility with bison */\n");
1652 putl_code(fp, "#ifdef YYPARSE_PARAM\n");
1653 putl_code(fp, "/* compatibility with FreeBSD */\n");
1654 putl_code(fp, "# ifdef YYPARSE_PARAM_TYPE\n");
1656 "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
1657 putl_code(fp, "# else\n");
1658 putl_code(fp, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n");
1659 putl_code(fp, "# endif\n");
1660 putl_code(fp, "#else\n");
1662 puts_code(fp, "# define YYPARSE_DECL() yyparse(");
1663 puts_param_types(fp, parse_param, 0);
1664 putl_code(fp, ")\n");
1666 putl_code(fp, "#endif\n");
1670 output_lex_decl(FILE * fp)
1672 putc_code(fp, '\n');
1673 putl_code(fp, "/* Parameters sent to lex. */\n");
1674 putl_code(fp, "#ifdef YYLEX_PARAM\n");
1677 putl_code(fp, "# ifdef YYLEX_PARAM_TYPE\n");
1678 #if defined(YYBTYACC)
1681 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1683 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1688 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1689 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1691 putl_code(fp, "# else\n");
1692 #if defined(YYBTYACC)
1695 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1697 " void * YYLEX_PARAM)\n");
1702 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1703 " void * YYLEX_PARAM)\n");
1705 putl_code(fp, "# endif\n");
1706 #if defined(YYBTYACC)
1709 "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n");
1712 putl_code(fp, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
1716 putl_code(fp, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n");
1717 putl_code(fp, "# define YYLEX yylex(YYLEX_PARAM)\n");
1719 putl_code(fp, "#else\n");
1720 if (pure_parser && lex_param)
1722 #if defined(YYBTYACC)
1725 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, ");
1728 puts_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, ");
1729 puts_param_types(fp, lex_param, 0);
1730 putl_code(fp, ")\n");
1732 #if defined(YYBTYACC)
1734 puts_code(fp, "# define YYLEX yylex(&yylval, &yylloc, ");
1737 puts_code(fp, "# define YYLEX yylex(&yylval, ");
1738 puts_param_names(fp, lex_param, 0);
1739 putl_code(fp, ")\n");
1741 else if (pure_parser)
1743 #if defined(YYBTYACC)
1747 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n");
1748 putl_code(fp, "# define YYLEX yylex(&yylval, &yylloc)\n");
1753 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n");
1754 putl_code(fp, "# define YYLEX yylex(&yylval)\n");
1759 puts_code(fp, "# define YYLEX_DECL() yylex(");
1760 puts_param_types(fp, lex_param, 0);
1761 putl_code(fp, ")\n");
1763 puts_code(fp, "# define YYLEX yylex(");
1764 puts_param_names(fp, lex_param, 0);
1765 putl_code(fp, ")\n");
1769 putl_code(fp, "# define YYLEX_DECL() yylex(void)\n");
1770 putl_code(fp, "# define YYLEX yylex()\n");
1772 putl_code(fp, "#endif\n");
1776 output_error_decl(FILE * fp)
1778 putc_code(fp, '\n');
1779 putl_code(fp, "/* Parameters sent to yyerror. */\n");
1780 putl_code(fp, "#ifndef YYERROR_DECL\n");
1781 puts_code(fp, "#define YYERROR_DECL() yyerror(");
1782 #if defined(YYBTYACC)
1784 puts_code(fp, "YYLTYPE loc, ");
1786 puts_param_types(fp, parse_param, 1);
1787 putl_code(fp, "const char *s)\n");
1788 putl_code(fp, "#endif\n");
1790 putl_code(fp, "#ifndef YYERROR_CALL\n");
1792 puts_code(fp, "#define YYERROR_CALL(msg) yyerror(");
1793 #if defined(YYBTYACC)
1795 puts_code(fp, "yylloc, ");
1797 puts_param_names(fp, parse_param, 1);
1798 putl_code(fp, "msg)\n");
1800 putl_code(fp, "#endif\n");
1803 #if defined(YYBTYACC)
1805 output_yydestruct_decl(FILE * fp)
1807 putc_code(fp, '\n');
1808 putl_code(fp, "#ifndef YYDESTRUCT_DECL\n");
1811 "#define YYDESTRUCT_DECL() "
1812 "yydestruct(const char *msg, int psymb, YYSTYPE *val");
1813 #if defined(YYBTYACC)
1815 puts_code(fp, ", YYLTYPE *loc");
1819 puts_code(fp, ", ");
1820 puts_param_types(fp, parse_param, 0);
1822 putl_code(fp, ")\n");
1824 putl_code(fp, "#endif\n");
1826 putl_code(fp, "#ifndef YYDESTRUCT_CALL\n");
1828 puts_code(fp, "#define YYDESTRUCT_CALL(msg, psymb, val");
1829 #if defined(YYBTYACC)
1831 puts_code(fp, ", loc");
1833 puts_code(fp, ") yydestruct(msg, psymb, val");
1834 #if defined(YYBTYACC)
1836 puts_code(fp, ", loc");
1840 puts_code(fp, ", ");
1841 puts_param_names(fp, parse_param, 0);
1843 putl_code(fp, ")\n");
1845 putl_code(fp, "#endif\n");
1849 output_yydestruct_impl(void)
1852 char *s, *destructor_code;
1854 putc_code(code_file, '\n');
1855 putl_code(code_file, "/* Release memory associated with symbol. */\n");
1856 putl_code(code_file, "#if ! defined YYDESTRUCT_IS_DECLARED\n");
1857 putl_code(code_file, "static void\n");
1858 putl_code(code_file, "YYDESTRUCT_DECL()\n");
1859 putl_code(code_file, "{\n");
1860 putl_code(code_file, " switch (psymb)\n");
1861 putl_code(code_file, " {\n");
1862 for (i = 2; i < nsyms; ++i)
1864 if ((destructor_code = symbol_destructor[i]) != NULL)
1867 fprintf(code_file, "\tcase %d:\n", symbol_pval[i]);
1868 /* comprehend the number of lines in the destructor code */
1869 for (s = destructor_code; (s = strchr(s, '\n')) != NULL; s++)
1871 puts_code(code_file, destructor_code);
1872 putc_code(code_file, '\n');
1873 putl_code(code_file, "\tbreak;\n");
1874 write_code_lineno(code_file);
1875 FREE(destructor_code);
1878 putl_code(code_file, " }\n");
1879 putl_code(code_file, "}\n");
1880 putl_code(code_file, "#define YYDESTRUCT_IS_DECLARED 1\n");
1881 putl_code(code_file, "#endif\n");
1883 DO_FREE(symbol_destructor);
1893 for (cp = first_state; cp; cp = next)
1906 for (sp = first_shift; sp; sp = next)
1914 free_reductions(void)
1916 reductions *rp, *next;
1918 FREE(reduction_table);
1919 for (rp = first_reduction; rp; rp = next)
1927 output_externs(FILE * fp, const char *const section[])
1932 for (i = 0; (s = section[i]) != 0; ++i)
1934 /* prefix non-blank lines that don't start with
1935 C pre-processor directives with 'extern ' */
1936 if (*s && (*s != '#'))
1937 fputs("extern\t", fp);
1938 if (fp == code_file)
1940 fprintf(fp, "%s\n", s);
1953 #if defined(YYBTYACC)
1954 output_backtracking_parser(output_file);
1956 output_backtracking_parser(code_file);
1961 write_code_lineno(code_file);
1963 fprintf(code_file, "#include \"%s\"\n", externs_file_name);
1970 output_pure_parser(fp);
1971 output_stored_text(fp);
1973 #if defined(YYBTYACC)
1977 output_parse_decl(fp);
1978 output_lex_decl(fp);
1979 output_error_decl(fp);
1980 #if defined(YYBTYACC)
1982 output_yydestruct_decl(fp);
1984 if (iflag || !rflag)
1986 write_section(fp, xdecls);
1991 output_externs(externs_file, global_vars);
1993 output_externs(externs_file, impure_vars);
2001 fprintf(code_file, "#include \"%s\"\n", defines_file_name);
2004 output_defines(externs_file);
2008 putc_code(code_file, '\n');
2009 output_defines(code_file);
2014 start_defines_file();
2015 output_defines(defines_file);
2021 #if defined(YYBTYACC)
2022 output_accessing_symbols();
2029 write_section(code_file, xdecls);
2030 output_YYINT_typedef(code_file);
2031 write_section(code_file, tables);
2033 write_section(code_file, global_vars);
2036 write_section(code_file, impure_vars);
2038 write_section(code_file, hdr_defs);
2041 write_section(code_file, hdr_vars);
2043 output_trailing_text();
2044 #if defined(YYBTYACC)
2046 output_yydestruct_impl();
2048 write_section(code_file, body_1);
2051 write_section(code_file, body_vars);
2053 write_section(code_file, body_2);
2054 output_semantic_actions();
2055 write_section(code_file, trailer);