1 /* $Id: output.c,v 1.92 2019/11/20 00:55:05 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);
189 output_code_lines(FILE * fp, int cl)
191 if (code_lines[cl].lines != NULL)
195 outline += (int)code_lines[cl].num;
199 fprintf(fp, "/* %%code \"%s\" block start */\n", code_lines[cl].name);
200 fputs(code_lines[cl].lines, fp);
201 fprintf(fp, "/* %%code \"%s\" block end */\n", code_lines[cl].name);
204 write_code_lineno(fp);
214 putc('\n', output_file);
218 output_line(const char *value)
220 fputs(value, output_file);
225 output_int(int value)
227 fprintf(output_file, "%5d,", value);
231 start_int_table(const char *name, int value)
233 int need = 34 - (int)(strlen(symbol_prefix) + strlen(name));
238 "%sconst YYINT %s%s[] = {%*d,",
239 StaticOrR, symbol_prefix, name, need, value);
243 start_str_table(const char *name)
246 "%sconst char *const %s%s[] = {",
247 StaticOrR, symbol_prefix, name);
259 output_stype(FILE * fp)
261 if (!unionized && ntags == 0)
265 "! defined(YYSTYPE) && "
266 "! defined(YYSTYPE_IS_DECLARED)\n");
267 putl_code(fp, "/* Default: YYSTYPE is the semantic value type. */\n");
268 putl_code(fp, "typedef int YYSTYPE;\n");
269 putl_code(fp, "# define YYSTYPE_IS_DECLARED 1\n");
270 putl_code(fp, "#endif\n");
274 #if defined(YYBTYACC)
276 output_ltype(FILE * fp)
279 putl_code(fp, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n");
280 putl_code(fp, "/* Default: YYLTYPE is the text position type. */\n");
281 putl_code(fp, "typedef struct YYLTYPE\n");
282 putl_code(fp, "{\n");
283 putl_code(fp, " int first_line;\n");
284 putl_code(fp, " int first_column;\n");
285 putl_code(fp, " int last_line;\n");
286 putl_code(fp, " int last_column;\n");
287 putl_code(fp, " unsigned source;\n");
288 putl_code(fp, "} YYLTYPE;\n");
289 putl_code(fp, "#define YYLTYPE_IS_DECLARED 1\n");
290 putl_code(fp, "#endif\n");
291 putl_code(fp, "#define YYRHSLOC(rhs, k) ((rhs)[k])\n");
296 output_YYINT_typedef(FILE * fp)
298 /* generate the type used to index the various parser tables */
301 fprintf(fp, "typedef %s YYINT;\n", CONCAT1("", YYINT));
305 output_rule_data(void)
310 output_YYINT_typedef(output_file);
312 start_int_table("lhs", symbol_value[start_symbol]);
315 for (i = 3; i < nrules; i++)
325 output_int(symbol_value[rlhs[i]]);
329 start_int_table("len", 2);
332 for (i = 3; i < nrules; i++)
342 output_int(rrhs[i + 1] - rrhs[i] - 1);
348 output_yydefred(void)
352 start_int_table("defred", (defred[0] ? defred[0] - 2 : 0));
355 for (i = 1; i < nstates; i++)
365 output_int((defred[i] ? defred[i] - 2 : 0));
371 #if defined(YYBTYACC)
373 output_accessing_symbols(void)
380 translate = TMALLOC(int, nstates);
383 for (i = 0; i < nstates; ++i)
385 int gsymb = accessing_symbol[i];
387 translate[i] = symbol_pval[gsymb];
390 putl_code(output_file,
391 "#if defined(YYDESTRUCT_CALL) || defined(YYSTYPE_TOSTRING)\n");
392 /* yystos[] may be unused, depending on compile-time defines */
393 start_int_table("stos", translate[0]);
396 for (i = 1; i < nstates; ++i)
406 output_int(translate[i]);
411 putl_code(output_file,
412 "#endif /* YYDESTRUCT_CALL || YYSTYPE_TOSTRING */\n");
417 find_conflict_base(int cbase)
421 for (i = 0; i < cbase; i++)
423 for (j = 0; j + cbase < nconflicts; j++)
425 if (conflicts[i + j] != conflicts[cbase + j])
428 if (j + cbase >= nconflicts)
439 Value_t shiftcount, reducecount;
440 #if defined(YYBTYACC)
441 Value_t conflictcount = 0;
446 Value_t *actionrow, *r, *s;
449 actionrow = NEW2(PER_STATE * ntokens, Value_t);
450 for (i = 0; i < nstates; ++i)
454 for (j = 0; j < PER_STATE * ntokens; ++j)
459 #if defined(YYBTYACC)
467 for (p = parser[i]; p; p = p->next)
469 #if defined(YYBTYACC)
472 if (csym != -1 && csym != p->symbol)
475 conflicts[nconflicts++] = -1;
476 j = find_conflict_base(cbase);
477 actionrow[csym + 2 * ntokens] = (Value_t)(j + 1);
484 if (conflicts[cbase] == -1)
492 if (p->suppressed == 0)
494 if (p->action_code == SHIFT)
497 actionrow[p->symbol] = p->number;
499 else if (p->action_code == REDUCE && p->number != defred[i])
502 actionrow[p->symbol + ntokens] = p->number;
505 #if defined(YYBTYACC)
506 else if (backtrack && p->suppressed == 1)
509 if (p->action_code == SHIFT)
511 conflicts[nconflicts++] = p->number;
513 else if (p->action_code == REDUCE && p->number != defred[i])
515 if (cbase == nconflicts)
520 conflicts[nconflicts++] = -1;
522 conflicts[nconflicts++] = (Value_t)(p->number - 2);
527 #if defined(YYBTYACC)
528 if (backtrack && csym != -1)
531 conflicts[nconflicts++] = -1;
532 j = find_conflict_base(cbase);
533 actionrow[csym + 2 * ntokens] = (Value_t)(j + 1);
540 if (conflicts[cbase] == -1)
547 tally[i] = shiftcount;
548 tally[nstates + i] = reducecount;
549 #if defined(YYBTYACC)
551 tally[2 * nstates + i] = conflictcount;
554 width[nstates + i] = 0;
555 #if defined(YYBTYACC)
557 width[2 * nstates + i] = 0;
561 froms[i] = r = NEW2(shiftcount, Value_t);
562 tos[i] = s = NEW2(shiftcount, Value_t);
565 for (j = 0; j < ntokens; ++j)
569 if (min > symbol_value[j])
570 min = symbol_value[j];
571 if (max < symbol_value[j])
572 max = symbol_value[j];
573 *r++ = symbol_value[j];
577 width[i] = (Value_t)(max - min + 1);
581 froms[nstates + i] = r = NEW2(reducecount, Value_t);
582 tos[nstates + i] = s = NEW2(reducecount, Value_t);
585 for (j = 0; j < ntokens; ++j)
587 if (actionrow[ntokens + j])
589 if (min > symbol_value[j])
590 min = symbol_value[j];
591 if (max < symbol_value[j])
592 max = symbol_value[j];
593 *r++ = symbol_value[j];
594 *s++ = (Value_t)(actionrow[ntokens + j] - 2);
597 width[nstates + i] = (Value_t)(max - min + 1);
599 #if defined(YYBTYACC)
600 if (backtrack && conflictcount > 0)
602 froms[2 * nstates + i] = r = NEW2(conflictcount, Value_t);
603 tos[2 * nstates + i] = s = NEW2(conflictcount, Value_t);
606 for (j = 0; j < ntokens; ++j)
608 if (actionrow[2 * ntokens + j])
610 if (min > symbol_value[j])
611 min = symbol_value[j];
612 if (max < symbol_value[j])
613 max = symbol_value[j];
614 *r++ = symbol_value[j];
615 *s++ = (Value_t)(actionrow[2 * ntokens + j] - 1);
618 width[2 * nstates + i] = (Value_t)(max - min + 1);
627 default_goto(int symbol)
635 m = goto_map[symbol];
636 n = goto_map[symbol + 1];
641 for (i = 0; i < nstates; i++)
644 for (i = m; i < n; i++)
645 state_count[to_state[i]]++;
649 for (i = 0; i < nstates; i++)
651 if (state_count[i] > max)
653 max = state_count[i];
658 return (default_state);
662 save_column(int symbol, int default_state)
673 m = goto_map[symbol];
674 n = goto_map[symbol + 1];
677 for (i = m; i < n; i++)
679 if (to_state[i] != default_state)
685 symno = symbol_value[symbol] + PER_STATE * nstates;
687 froms[symno] = sp1 = sp = NEW2(count, Value_t);
688 tos[symno] = sp2 = NEW2(count, Value_t);
690 for (i = m; i < n; i++)
692 if (to_state[i] != default_state)
694 *sp1++ = from_state[i];
695 *sp2++ = to_state[i];
699 tally[symno] = count;
700 width[symno] = (Value_t)(sp1[-1] - sp[0] + 1);
708 state_count = NEW2(nstates, Value_t);
710 k = default_goto(start_symbol + 1);
711 start_int_table("dgoto", k);
712 save_column(start_symbol + 1, k);
715 for (i = start_symbol + 2; i < nsyms; i++)
743 order = NEW2(nvectors, Value_t);
746 for (i = 0; i < nvectors; i++)
754 while (j >= 0 && (width[order[j]] < w))
757 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
760 for (k = nentries - 1; k > j; k--)
761 order[k + 1] = order[k];
769 /* The function matching_vector determines if the vector specified by */
770 /* the input parameter matches a previously considered vector. The */
771 /* test at the start of the function checks if the vector represents */
772 /* a row of shifts over terminal symbols or a row of reductions, or a */
773 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
774 /* check if a column of shifts over a nonterminal symbols matches a */
775 /* previously considered vector. Because of the nature of LR parsing */
776 /* tables, no two columns can match. Therefore, the only possible */
777 /* match would be between a row and a column. Such matches are */
778 /* unlikely. Therefore, to save time, no attempt is made to see if a */
779 /* column matches a previously considered vector. */
781 /* Matching_vector is poorly designed. The test could easily be made */
782 /* faster. Also, it depends on the vectors being in a specific */
784 #if defined(YYBTYACC)
786 /* Not really any point in checking for matching conflicts -- it is */
787 /* extremely unlikely to occur, and conflicts are (hopefully) rare. */
791 matching_vector(int vector)
802 if (i >= 2 * nstates)
808 for (prev = vector - 1; prev >= 0; prev--)
811 if (width[j] != w || tally[j] != t)
815 for (k = 0; match && k < t; k++)
817 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
829 pack_vector(int vector)
846 j = lowzero - from[0];
847 for (k = 1; k < t; ++k)
848 if (lowzero - from[k] > j)
849 j = lowzero - from[k];
855 for (k = 0; ok && k < t; k++)
858 if (loc >= maxtable - 1)
860 if (loc >= MAXTABLE - 1)
861 fatal("maximum table size exceeded");
868 while (newmax <= loc);
870 table = TREALLOC(Value_t, table, newmax);
873 check = TREALLOC(Value_t, check, newmax);
876 for (l = maxtable; l < newmax; ++l)
884 if (check[loc] != -1)
887 for (k = 0; ok && k < vector; k++)
894 for (k = 0; k < t; k++)
898 check[loc] = from[k];
903 while (check[lowzero] != -1)
918 base = NEW2(nvectors, Value_t);
919 pos = NEW2(nentries, Value_t);
922 table = NEW2(maxtable, Value_t);
923 check = NEW2(maxtable, Value_t);
928 for (i = 0; i < maxtable; i++)
931 for (i = 0; i < nentries; i++)
933 state = matching_vector(i);
936 place = (Value_t)pack_vector(i);
941 base[order[i]] = place;
944 for (i = 0; i < nvectors; i++)
964 start_int_table("sindex", base[0]);
967 for (i = 1; i < nstates; i++)
982 start_int_table("rindex", base[nstates]);
985 for (i = nstates + 1; i < 2 * nstates; i++)
1000 #if defined(YYBTYACC)
1001 output_line("#if YYBTYACC");
1002 start_int_table("cindex", base[2 * nstates]);
1005 for (i = 2 * nstates + 1; i < 3 * nstates; i++)
1015 output_int(base[i]);
1019 output_line("#endif");
1022 start_int_table("gindex", base[PER_STATE * nstates]);
1025 for (i = PER_STATE * nstates + 1; i < nvectors - 1; i++)
1035 output_int(base[i]);
1048 if (high >= MAXYYINT)
1050 fprintf(stderr, "YYTABLESIZE: %ld\n", high);
1051 fprintf(stderr, "Table is longer than %d elements.\n", MAXYYINT);
1056 fprintf(code_file, "#define YYTABLESIZE %ld\n", high);
1057 start_int_table("table", table[0]);
1060 for (i = 1; i <= high; i++)
1070 output_int(table[i]);
1083 start_int_table("check", check[0]);
1086 for (i = 1; i <= high; i++)
1096 output_int(check[i]);
1103 #if defined(YYBTYACC)
1109 int limit = (conflicts != 0) ? nconflicts : 0;
1114 output_line("#if YYBTYACC");
1115 start_int_table("ctable", conflicts ? conflicts[0] : -1);
1118 for (i = 1; i < limit; i++)
1128 output_int((conflicts != 0 && i < nconflicts) ? conflicts[i] : -1);
1135 output_line("#endif");
1140 output_actions(void)
1142 nvectors = PER_STATE * nstates + nvars;
1144 froms = NEW2(nvectors, Value_t *);
1145 tos = NEW2(nvectors, Value_t *);
1146 tally = NEW2(nvectors, Value_t);
1147 width = NEW2(nvectors, Value_t);
1149 #if defined(YYBTYACC)
1150 if (backtrack && (SRtotal + RRtotal) != 0)
1151 conflicts = NEW2(4 * (SRtotal + RRtotal), Value_t);
1158 FREE(accessing_symbol);
1170 #if defined(YYBTYACC)
1176 is_C_identifier(char *name)
1188 while ((c = *++s) != '"')
1198 while ((c = *++s) != 0)
1206 #if USE_HEADER_GUARDS
1208 start_defines_file(void)
1210 fprintf(defines_file, "#ifndef _%s_defines_h_\n", symbol_prefix);
1211 fprintf(defines_file, "#define _%s_defines_h_\n\n", symbol_prefix);
1215 end_defines_file(void)
1217 fprintf(defines_file, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix);
1220 #define start_defines_file() /* nothing */
1221 #define end_defines_file() /* nothing */
1225 output_defines(FILE * fp)
1230 if (fp == defines_file)
1232 output_code_lines(fp, CODE_REQUIRES);
1235 for (i = 2; i < ntokens; ++i)
1238 if (is_C_identifier(s) && (!sflag || *s != '"'))
1240 fprintf(fp, "#define ");
1244 while ((c = *++s) != '"')
1255 while ((c = *++s) != 0);
1257 if (fp == code_file)
1259 fprintf(fp, " %d\n", symbol_value[i]);
1263 if (fp == code_file)
1265 if (fp != defines_file || iflag)
1266 fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]);
1268 if (fp == defines_file)
1270 output_code_lines(fp, CODE_PROVIDES);
1273 if (token_table && rflag && fp != externs_file)
1275 if (fp == code_file)
1277 fputs("#undef yytname\n", fp);
1278 if (fp == code_file)
1280 fputs("#define yytname yyname\n", fp);
1283 if (fp == defines_file || (iflag && !dflag))
1287 if (union_file != 0)
1290 while ((c = getc(union_file)) != EOF)
1294 fprintf(fp, "extern YYSTYPE %slval;\n", symbol_prefix);
1296 #if defined(YYBTYACC)
1300 fprintf(fp, "extern YYLTYPE %slloc;\n", symbol_prefix);
1307 output_stored_text(FILE * fp)
1312 if (text_file == NULL)
1313 open_error("text_file");
1316 if ((c = getc(in)) == EOF)
1319 while ((c = getc(in)) != EOF)
1323 write_code_lineno(fp);
1327 output_yydebug(FILE * fp)
1329 fprintf(fp, "#ifndef YYDEBUG\n");
1330 fprintf(fp, "#define YYDEBUG %d\n", tflag);
1331 fprintf(fp, "#endif\n");
1338 int i, j, k, max, maxtok;
1339 const char **symnam;
1343 fprintf(code_file, "#define YYFINAL %d\n", final_state);
1345 outline += output_yydebug(code_file);
1349 output_yydebug(output_file);
1353 for (i = 0; i < ntokens; ++i)
1354 if (symbol_value[i] > maxtok)
1355 maxtok = symbol_value[i];
1357 /* symbol_value[$accept] = -1 */
1358 /* symbol_value[<goal>] = 0 */
1359 /* remaining non-terminals start at 1 */
1361 for (i = ntokens; i < nsyms; ++i)
1362 if (((maxtok + 1) + (symbol_value[i] + 1)) > max)
1363 max = (maxtok + 1) + (symbol_value[i] + 1);
1366 fprintf(code_file, "#define YYMAXTOKEN %d\n", maxtok);
1369 fprintf(code_file, "#define YYUNDFTOKEN %d\n", max + 1);
1372 fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? "
1373 "YYUNDFTOKEN : (a))\n");
1375 symnam = TMALLOC(const char *, max + 2);
1378 /* Note that it is not necessary to initialize the element */
1380 #if defined(YYBTYACC)
1381 for (i = 0; i < max; ++i)
1383 for (i = nsyms - 1; i >= 0; --i)
1384 symnam[symbol_pval[i]] = symbol_name[i];
1385 symnam[max + 1] = "illegal-symbol";
1387 for (i = 0; i <= max; ++i)
1389 for (i = ntokens - 1; i >= 2; --i)
1390 symnam[symbol_value[i]] = symbol_name[i];
1391 symnam[0] = "end-of-file";
1392 symnam[max + 1] = "illegal-symbol";
1396 * bison's yytname[] array is roughly the same as byacc's yyname[] array.
1397 * The difference is that byacc does not predefine "$undefined".
1399 * If the grammar declares "%token-table", define symbol "yytname" so
1400 * an application such as ntpd can build.
1406 output_line("#undef yytname");
1407 output_line("#define yytname yyname");
1412 output_line("#if YYDEBUG");
1415 start_str_table("name");
1417 for (i = 0; i <= max + 1; ++i)
1419 if ((s = symnam[i]) != 0)
1440 fprintf(output_file, "\"\\\"");
1446 fprintf(output_file, "\\\\");
1448 fprintf(output_file, "\\\\");
1450 putc(*s, output_file);
1453 putc(*s, output_file);
1455 fprintf(output_file, "\\\"\",");
1457 else if (s[0] == '\'')
1467 fprintf(output_file, "\"'\\\"'\",");
1472 while (*++s != '\'')
1488 fprintf(output_file, "\"'");
1490 while (*++s != '\'')
1494 fprintf(output_file, "\\\\");
1496 fprintf(output_file, "\\\\");
1498 putc(*s, output_file);
1501 putc(*s, output_file);
1503 fprintf(output_file, "'\",");
1508 k = (int)strlen(s) + 3;
1515 putc('"', output_file);
1518 putc(*s, output_file);
1521 fprintf(output_file, "\",");
1532 fprintf(output_file, "0,");
1539 output_line("#if YYDEBUG");
1540 start_str_table("rule");
1541 for (i = 2; i < nrules; ++i)
1543 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1544 for (j = rrhs[i]; ritem[j] > 0; ++j)
1546 s = symbol_name[ritem[j]];
1549 fprintf(output_file, " \\\"");
1555 fprintf(output_file, "\\\\\\\\");
1557 fprintf(output_file, "\\\\%c", s[1]);
1561 putc(*s, output_file);
1563 fprintf(output_file, "\\\"");
1565 else if (s[0] == '\'')
1568 fprintf(output_file, " '\\\"'");
1569 else if (s[1] == '\\')
1572 fprintf(output_file, " '\\\\\\\\");
1574 fprintf(output_file, " '\\\\%c", s[2]);
1576 while (*++s != '\'')
1577 putc(*s, output_file);
1578 putc('\'', output_file);
1581 fprintf(output_file, " '%c'", s[1]);
1584 fprintf(output_file, " %s", s);
1586 fprintf(output_file, "\",");
1591 output_line("#endif");
1594 #if defined(YYBTYACC)
1596 output_backtracking_parser(FILE * fp)
1598 putl_code(fp, "#undef YYBTYACC\n");
1599 #if defined(YYBTYACC)
1602 putl_code(fp, "#define YYBTYACC 1\n");
1604 "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n");
1609 putl_code(fp, "#define YYBTYACC 0\n");
1610 putl_code(fp, "#define YYDEBUGSTR YYPREFIX \"debug\"\n");
1616 output_pure_parser(FILE * fp)
1618 putc_code(fp, '\n');
1620 if (fp == code_file)
1622 fprintf(fp, "#define YYPURE %d\n", pure_parser);
1623 putc_code(fp, '\n');
1626 #if defined(YY_NO_LEAKS)
1628 output_no_leaks(FILE * fp)
1630 putc_code(fp, '\n');
1632 if (fp == code_file)
1634 fputs("#define YY_NO_LEAKS 1\n", fp);
1635 putc_code(fp, '\n');
1640 output_trailing_text(void)
1653 if ((c = getc(in)) == EOF)
1655 write_input_lineno();
1656 putc_code(code_file, c);
1661 write_input_lineno();
1664 putc_code(code_file, c);
1666 while ((c = *++cptr) != '\n');
1667 putc_code(code_file, c);
1671 while ((c = getc(in)) != EOF)
1673 putc_code(code_file, c);
1679 putc_code(code_file, '\n');
1681 write_code_lineno(code_file);
1685 output_semantic_actions(void)
1689 rewind(action_file);
1690 if ((c = getc(action_file)) == EOF)
1694 putc_code(code_file, c);
1695 while ((c = getc(action_file)) != EOF)
1697 putc_code(code_file, c);
1703 putc_code(code_file, '\n');
1706 write_code_lineno(code_file);
1710 output_parse_decl(FILE * fp)
1712 putc_code(fp, '\n');
1713 putl_code(fp, "/* compatibility with bison */\n");
1714 putl_code(fp, "#ifdef YYPARSE_PARAM\n");
1715 putl_code(fp, "/* compatibility with FreeBSD */\n");
1716 putl_code(fp, "# ifdef YYPARSE_PARAM_TYPE\n");
1718 "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
1719 putl_code(fp, "# else\n");
1720 putl_code(fp, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n");
1721 putl_code(fp, "# endif\n");
1722 putl_code(fp, "#else\n");
1724 puts_code(fp, "# define YYPARSE_DECL() yyparse(");
1725 puts_param_types(fp, parse_param, 0);
1726 putl_code(fp, ")\n");
1728 putl_code(fp, "#endif\n");
1732 output_lex_decl(FILE * fp)
1734 putc_code(fp, '\n');
1735 putl_code(fp, "/* Parameters sent to lex. */\n");
1736 putl_code(fp, "#ifdef YYLEX_PARAM\n");
1739 putl_code(fp, "# ifdef YYLEX_PARAM_TYPE\n");
1740 #if defined(YYBTYACC)
1743 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1745 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1750 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1751 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1753 putl_code(fp, "# else\n");
1754 #if defined(YYBTYACC)
1757 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1759 " void * YYLEX_PARAM)\n");
1764 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1765 " void * YYLEX_PARAM)\n");
1767 putl_code(fp, "# endif\n");
1768 #if defined(YYBTYACC)
1771 "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n");
1774 putl_code(fp, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
1778 putl_code(fp, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n");
1779 putl_code(fp, "# define YYLEX yylex(YYLEX_PARAM)\n");
1781 putl_code(fp, "#else\n");
1782 if (pure_parser && lex_param)
1784 #if defined(YYBTYACC)
1787 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, ");
1790 puts_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, ");
1791 puts_param_types(fp, lex_param, 0);
1792 putl_code(fp, ")\n");
1794 #if defined(YYBTYACC)
1796 puts_code(fp, "# define YYLEX yylex(&yylval, &yylloc, ");
1799 puts_code(fp, "# define YYLEX yylex(&yylval, ");
1800 puts_param_names(fp, lex_param, 0);
1801 putl_code(fp, ")\n");
1803 else if (pure_parser)
1805 #if defined(YYBTYACC)
1809 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n");
1810 putl_code(fp, "# define YYLEX yylex(&yylval, &yylloc)\n");
1815 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n");
1816 putl_code(fp, "# define YYLEX yylex(&yylval)\n");
1821 puts_code(fp, "# define YYLEX_DECL() yylex(");
1822 puts_param_types(fp, lex_param, 0);
1823 putl_code(fp, ")\n");
1825 puts_code(fp, "# define YYLEX yylex(");
1826 puts_param_names(fp, lex_param, 0);
1827 putl_code(fp, ")\n");
1831 putl_code(fp, "# define YYLEX_DECL() yylex(void)\n");
1832 putl_code(fp, "# define YYLEX yylex()\n");
1834 putl_code(fp, "#endif\n");
1837 * Provide a prototype for yylex for the simplest case. This is done for
1838 * better compatibility with older yacc's, but can be a problem if someone
1839 * uses "static int yylex(void);"
1842 #if defined(YYBTYACC)
1845 && !strcmp(symbol_prefix, "yy"))
1847 putl_code(fp, "\n");
1848 putl_code(fp, "#if !(defined(yylex) || defined(YYSTATE))\n");
1849 putl_code(fp, "int YYLEX_DECL();\n");
1850 putl_code(fp, "#endif\n");
1855 output_error_decl(FILE * fp)
1857 putc_code(fp, '\n');
1858 putl_code(fp, "/* Parameters sent to yyerror. */\n");
1859 putl_code(fp, "#ifndef YYERROR_DECL\n");
1860 puts_code(fp, "#define YYERROR_DECL() yyerror(");
1861 #if defined(YYBTYACC)
1863 puts_code(fp, "YYLTYPE *loc, ");
1865 puts_param_types(fp, parse_param, 1);
1866 putl_code(fp, "const char *s)\n");
1867 putl_code(fp, "#endif\n");
1869 putl_code(fp, "#ifndef YYERROR_CALL\n");
1871 puts_code(fp, "#define YYERROR_CALL(msg) yyerror(");
1872 #if defined(YYBTYACC)
1874 puts_code(fp, "&yylloc, ");
1876 puts_param_names(fp, parse_param, 1);
1877 putl_code(fp, "msg)\n");
1879 putl_code(fp, "#endif\n");
1882 #if defined(YYBTYACC)
1884 output_yydestruct_decl(FILE * fp)
1886 putc_code(fp, '\n');
1887 putl_code(fp, "#ifndef YYDESTRUCT_DECL\n");
1890 "#define YYDESTRUCT_DECL() "
1891 "yydestruct(const char *msg, int psymb, YYSTYPE *val");
1892 #if defined(YYBTYACC)
1894 puts_code(fp, ", YYLTYPE *loc");
1898 puts_code(fp, ", ");
1899 puts_param_types(fp, parse_param, 0);
1901 putl_code(fp, ")\n");
1903 putl_code(fp, "#endif\n");
1905 putl_code(fp, "#ifndef YYDESTRUCT_CALL\n");
1907 puts_code(fp, "#define YYDESTRUCT_CALL(msg, psymb, val");
1908 #if defined(YYBTYACC)
1910 puts_code(fp, ", loc");
1912 puts_code(fp, ") yydestruct(msg, psymb, val");
1913 #if defined(YYBTYACC)
1915 puts_code(fp, ", loc");
1919 puts_code(fp, ", ");
1920 puts_param_names(fp, parse_param, 0);
1922 putl_code(fp, ")\n");
1924 putl_code(fp, "#endif\n");
1928 output_initial_action(void)
1931 fprintf(code_file, "%s\n", initial_action);
1935 output_yydestruct_impl(void)
1938 char *s, *destructor_code;
1940 putc_code(code_file, '\n');
1941 putl_code(code_file, "/* Release memory associated with symbol. */\n");
1942 putl_code(code_file, "#if ! defined YYDESTRUCT_IS_DECLARED\n");
1943 putl_code(code_file, "static void\n");
1944 putl_code(code_file, "YYDESTRUCT_DECL()\n");
1945 putl_code(code_file, "{\n");
1946 putl_code(code_file, " switch (psymb)\n");
1947 putl_code(code_file, " {\n");
1948 for (i = 2; i < nsyms; ++i)
1950 if ((destructor_code = symbol_destructor[i]) != NULL)
1953 fprintf(code_file, "\tcase %d:\n", symbol_pval[i]);
1954 /* comprehend the number of lines in the destructor code */
1955 for (s = destructor_code; (s = strchr(s, '\n')) != NULL; s++)
1957 puts_code(code_file, destructor_code);
1958 putc_code(code_file, '\n');
1959 putl_code(code_file, "\tbreak;\n");
1960 write_code_lineno(code_file);
1961 FREE(destructor_code);
1964 putl_code(code_file, " }\n");
1965 putl_code(code_file, "}\n");
1966 putl_code(code_file, "#define YYDESTRUCT_IS_DECLARED 1\n");
1967 putl_code(code_file, "#endif\n");
1969 DO_FREE(symbol_destructor);
1979 for (cp = first_state; cp; cp = next)
1992 for (sp = first_shift; sp; sp = next)
2000 free_reductions(void)
2002 reductions *rp, *next;
2004 FREE(reduction_table);
2005 for (rp = first_reduction; rp; rp = next)
2013 output_externs(FILE * fp, const char *const section[])
2018 for (i = 0; (s = section[i]) != 0; ++i)
2020 /* prefix non-blank lines that don't start with
2021 C pre-processor directives with 'extern ' */
2022 if (*s && (*s != '#'))
2023 fputs("extern\t", fp);
2024 if (fp == code_file)
2026 fprintf(fp, "%s\n", s);
2039 output_code_lines(code_file, CODE_TOP);
2040 #if defined(YYBTYACC)
2041 output_backtracking_parser(output_file);
2043 output_backtracking_parser(code_file);
2048 write_code_lineno(code_file);
2050 fprintf(code_file, "#include \"%s\"\n", externs_file_name);
2057 output_pure_parser(fp);
2058 #if defined(YY_NO_LEAKS)
2059 output_no_leaks(fp);
2061 output_stored_text(fp);
2063 #if defined(YYBTYACC)
2067 output_parse_decl(fp);
2068 output_lex_decl(fp);
2069 output_error_decl(fp);
2070 #if defined(YYBTYACC)
2072 output_yydestruct_decl(fp);
2074 if (iflag || !rflag)
2076 write_section(fp, xdecls);
2081 fprintf(externs_file, "\n");
2082 output_yydebug(externs_file);
2083 output_externs(externs_file, global_vars);
2085 output_externs(externs_file, impure_vars);
2089 fprintf(code_file, "#include \"%s\"\n", defines_file_name);
2092 output_defines(externs_file);
2096 putc_code(code_file, '\n');
2097 output_defines(code_file);
2102 start_defines_file();
2103 output_defines(defines_file);
2109 #if defined(YYBTYACC)
2110 output_accessing_symbols();
2118 write_section(code_file, xdecls);
2119 output_YYINT_typedef(code_file);
2120 write_section(code_file, tables);
2123 write_section(code_file, global_vars);
2126 write_section(code_file, impure_vars);
2128 output_code_lines(code_file, CODE_REQUIRES);
2130 write_section(code_file, hdr_defs);
2133 write_section(code_file, hdr_vars);
2136 output_code_lines(code_file, CODE_PROVIDES);
2137 output_code_lines(code_file, CODE_HEADER);
2139 output_trailing_text();
2140 #if defined(YYBTYACC)
2142 output_yydestruct_impl();
2144 write_section(code_file, body_1);
2147 write_section(code_file, body_vars);
2149 write_section(code_file, body_2);
2152 write_section(code_file, init_vars);
2154 #if defined(YYBTYACC)
2156 output_initial_action();
2158 write_section(code_file, body_3);
2159 output_semantic_actions();
2160 write_section(code_file, trailer);