1 /* $Id: output.c,v 1.81 2017/04/30 23:23:32 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_stype(FILE * fp)
240 if (!unionized && ntags == 0)
244 "! defined(YYSTYPE) && "
245 "! defined(YYSTYPE_IS_DECLARED)\n");
246 putl_code(fp, "/* Default: YYSTYPE is the semantic value type. */\n");
247 putl_code(fp, "typedef int YYSTYPE;\n");
248 putl_code(fp, "# define YYSTYPE_IS_DECLARED 1\n");
249 putl_code(fp, "#endif\n");
253 #if defined(YYBTYACC)
255 output_ltype(FILE * fp)
258 putl_code(fp, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n");
259 putl_code(fp, "/* Default: YYLTYPE is the text position type. */\n");
260 putl_code(fp, "typedef struct YYLTYPE\n");
261 putl_code(fp, "{\n");
262 putl_code(fp, " int first_line;\n");
263 putl_code(fp, " int first_column;\n");
264 putl_code(fp, " int last_line;\n");
265 putl_code(fp, " int last_column;\n");
266 putl_code(fp, " unsigned source;\n");
267 putl_code(fp, "} YYLTYPE;\n");
268 putl_code(fp, "#define YYLTYPE_IS_DECLARED 1\n");
269 putl_code(fp, "#endif\n");
270 putl_code(fp, "#define YYRHSLOC(rhs, k) ((rhs)[k])\n");
275 output_YYINT_typedef(FILE * fp)
277 /* generate the type used to index the various parser tables */
280 fprintf(fp, "typedef %s YYINT;\n", CONCAT1("", YYINT));
284 output_rule_data(void)
289 output_YYINT_typedef(output_file);
291 start_int_table("lhs", symbol_value[start_symbol]);
294 for (i = 3; i < nrules; i++)
304 output_int(symbol_value[rlhs[i]]);
308 start_int_table("len", 2);
311 for (i = 3; i < nrules; i++)
321 output_int(rrhs[i + 1] - rrhs[i] - 1);
327 output_yydefred(void)
331 start_int_table("defred", (defred[0] ? defred[0] - 2 : 0));
334 for (i = 1; i < nstates; i++)
344 output_int((defred[i] ? defred[i] - 2 : 0));
350 #if defined(YYBTYACC)
352 output_accessing_symbols(void)
359 translate = TMALLOC(int, nstates);
362 for (i = 0; i < nstates; ++i)
364 int gsymb = accessing_symbol[i];
366 translate[i] = symbol_pval[gsymb];
369 putl_code(output_file,
370 "#if defined(YYDESTRUCT_CALL) || defined(YYSTYPE_TOSTRING)\n");
371 /* yystos[] may be unused, depending on compile-time defines */
372 start_int_table("stos", translate[0]);
375 for (i = 1; i < nstates; ++i)
385 output_int(translate[i]);
390 putl_code(output_file,
391 "#endif /* YYDESTRUCT_CALL || YYSTYPE_TOSTRING */\n");
396 find_conflict_base(int cbase)
400 for (i = 0; i < cbase; i++)
402 for (j = 0; j + cbase < nconflicts; j++)
404 if (conflicts[i + j] != conflicts[cbase + j])
407 if (j + cbase >= nconflicts)
418 Value_t shiftcount, reducecount;
419 #if defined(YYBTYACC)
420 Value_t conflictcount = 0;
425 Value_t *actionrow, *r, *s;
428 actionrow = NEW2(PER_STATE * ntokens, Value_t);
429 for (i = 0; i < nstates; ++i)
433 for (j = 0; j < PER_STATE * ntokens; ++j)
438 #if defined(YYBTYACC)
446 for (p = parser[i]; p; p = p->next)
448 #if defined(YYBTYACC)
451 if (csym != -1 && csym != p->symbol)
454 conflicts[nconflicts++] = -1;
455 j = find_conflict_base(cbase);
456 actionrow[csym + 2 * ntokens] = (Value_t)(j + 1);
463 if (conflicts[cbase] == -1)
471 if (p->suppressed == 0)
473 if (p->action_code == SHIFT)
476 actionrow[p->symbol] = p->number;
478 else if (p->action_code == REDUCE && p->number != defred[i])
481 actionrow[p->symbol + ntokens] = p->number;
484 #if defined(YYBTYACC)
485 else if (backtrack && p->suppressed == 1)
488 if (p->action_code == SHIFT)
490 conflicts[nconflicts++] = p->number;
492 else if (p->action_code == REDUCE && p->number != defred[i])
494 if (cbase == nconflicts)
499 conflicts[nconflicts++] = -1;
501 conflicts[nconflicts++] = (Value_t)(p->number - 2);
506 #if defined(YYBTYACC)
507 if (backtrack && csym != -1)
510 conflicts[nconflicts++] = -1;
511 j = find_conflict_base(cbase);
512 actionrow[csym + 2 * ntokens] = (Value_t)(j + 1);
519 if (conflicts[cbase] == -1)
526 tally[i] = shiftcount;
527 tally[nstates + i] = reducecount;
528 #if defined(YYBTYACC)
530 tally[2 * nstates + i] = conflictcount;
533 width[nstates + i] = 0;
534 #if defined(YYBTYACC)
536 width[2 * nstates + i] = 0;
540 froms[i] = r = NEW2(shiftcount, Value_t);
541 tos[i] = s = NEW2(shiftcount, Value_t);
544 for (j = 0; j < 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];
556 width[i] = (Value_t)(max - min + 1);
560 froms[nstates + i] = r = NEW2(reducecount, Value_t);
561 tos[nstates + i] = s = NEW2(reducecount, Value_t);
564 for (j = 0; j < ntokens; ++j)
566 if (actionrow[ntokens + j])
568 if (min > symbol_value[j])
569 min = symbol_value[j];
570 if (max < symbol_value[j])
571 max = symbol_value[j];
572 *r++ = symbol_value[j];
573 *s++ = (Value_t)(actionrow[ntokens + j] - 2);
576 width[nstates + i] = (Value_t)(max - min + 1);
578 #if defined(YYBTYACC)
579 if (backtrack && conflictcount > 0)
581 froms[2 * nstates + i] = r = NEW2(conflictcount, Value_t);
582 tos[2 * nstates + i] = s = NEW2(conflictcount, Value_t);
585 for (j = 0; j < ntokens; ++j)
587 if (actionrow[2 * 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[2 * ntokens + j] - 1);
597 width[2 * nstates + i] = (Value_t)(max - min + 1);
606 default_goto(int symbol)
614 m = goto_map[symbol];
615 n = goto_map[symbol + 1];
620 for (i = 0; i < nstates; i++)
623 for (i = m; i < n; i++)
624 state_count[to_state[i]]++;
628 for (i = 0; i < nstates; i++)
630 if (state_count[i] > max)
632 max = state_count[i];
637 return (default_state);
641 save_column(int symbol, int default_state)
652 m = goto_map[symbol];
653 n = goto_map[symbol + 1];
656 for (i = m; i < n; i++)
658 if (to_state[i] != default_state)
664 symno = symbol_value[symbol] + PER_STATE * nstates;
666 froms[symno] = sp1 = sp = NEW2(count, Value_t);
667 tos[symno] = sp2 = NEW2(count, Value_t);
669 for (i = m; i < n; i++)
671 if (to_state[i] != default_state)
673 *sp1++ = from_state[i];
674 *sp2++ = to_state[i];
678 tally[symno] = count;
679 width[symno] = (Value_t)(sp1[-1] - sp[0] + 1);
687 state_count = NEW2(nstates, Value_t);
689 k = default_goto(start_symbol + 1);
690 start_int_table("dgoto", k);
691 save_column(start_symbol + 1, k);
694 for (i = start_symbol + 2; i < nsyms; i++)
722 order = NEW2(nvectors, Value_t);
725 for (i = 0; i < nvectors; i++)
733 while (j >= 0 && (width[order[j]] < w))
736 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
739 for (k = nentries - 1; k > j; k--)
740 order[k + 1] = order[k];
748 /* The function matching_vector determines if the vector specified by */
749 /* the input parameter matches a previously considered vector. The */
750 /* test at the start of the function checks if the vector represents */
751 /* a row of shifts over terminal symbols or a row of reductions, or a */
752 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
753 /* check if a column of shifts over a nonterminal symbols matches a */
754 /* previously considered vector. Because of the nature of LR parsing */
755 /* tables, no two columns can match. Therefore, the only possible */
756 /* match would be between a row and a column. Such matches are */
757 /* unlikely. Therefore, to save time, no attempt is made to see if a */
758 /* column matches a previously considered vector. */
760 /* Matching_vector is poorly designed. The test could easily be made */
761 /* faster. Also, it depends on the vectors being in a specific */
763 #if defined(YYBTYACC)
765 /* Not really any point in checking for matching conflicts -- it is */
766 /* extremely unlikely to occur, and conflicts are (hopefully) rare. */
770 matching_vector(int vector)
781 if (i >= 2 * nstates)
787 for (prev = vector - 1; prev >= 0; prev--)
790 if (width[j] != w || tally[j] != t)
794 for (k = 0; match && k < t; k++)
796 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
808 pack_vector(int vector)
825 j = lowzero - from[0];
826 for (k = 1; k < t; ++k)
827 if (lowzero - from[k] > j)
828 j = lowzero - from[k];
834 for (k = 0; ok && k < t; k++)
837 if (loc >= maxtable - 1)
839 if (loc >= MAXTABLE - 1)
840 fatal("maximum table size exceeded");
847 while (newmax <= loc);
849 table = TREALLOC(Value_t, table, newmax);
852 check = TREALLOC(Value_t, check, newmax);
855 for (l = maxtable; l < newmax; ++l)
863 if (check[loc] != -1)
866 for (k = 0; ok && k < vector; k++)
873 for (k = 0; k < t; k++)
877 check[loc] = from[k];
882 while (check[lowzero] != -1)
897 base = NEW2(nvectors, Value_t);
898 pos = NEW2(nentries, Value_t);
901 table = NEW2(maxtable, Value_t);
902 check = NEW2(maxtable, Value_t);
907 for (i = 0; i < maxtable; i++)
910 for (i = 0; i < nentries; i++)
912 state = matching_vector(i);
915 place = (Value_t)pack_vector(i);
920 base[order[i]] = place;
923 for (i = 0; i < nvectors; i++)
943 start_int_table("sindex", base[0]);
946 for (i = 1; i < nstates; i++)
961 start_int_table("rindex", base[nstates]);
964 for (i = nstates + 1; i < 2 * nstates; i++)
979 #if defined(YYBTYACC)
980 output_line("#if YYBTYACC");
981 start_int_table("cindex", base[2 * nstates]);
984 for (i = 2 * nstates + 1; i < 3 * nstates; i++)
998 output_line("#endif");
1001 start_int_table("gindex", base[PER_STATE * nstates]);
1004 for (i = PER_STATE * nstates + 1; i < nvectors - 1; i++)
1014 output_int(base[i]);
1027 if (high >= MAXYYINT)
1029 fprintf(stderr, "YYTABLESIZE: %ld\n", high);
1030 fprintf(stderr, "Table is longer than %d elements.\n", MAXYYINT);
1035 fprintf(code_file, "#define YYTABLESIZE %ld\n", high);
1036 start_int_table("table", table[0]);
1039 for (i = 1; i <= high; i++)
1049 output_int(table[i]);
1062 start_int_table("check", check[0]);
1065 for (i = 1; i <= high; i++)
1075 output_int(check[i]);
1082 #if defined(YYBTYACC)
1088 int limit = (conflicts != 0) ? nconflicts : 0;
1093 output_line("#if YYBTYACC");
1094 start_int_table("ctable", conflicts ? conflicts[0] : -1);
1097 for (i = 1; i < limit; i++)
1107 output_int((conflicts != 0 && i < nconflicts) ? conflicts[i] : -1);
1114 output_line("#endif");
1119 output_actions(void)
1121 nvectors = PER_STATE * nstates + nvars;
1123 froms = NEW2(nvectors, Value_t *);
1124 tos = NEW2(nvectors, Value_t *);
1125 tally = NEW2(nvectors, Value_t);
1126 width = NEW2(nvectors, Value_t);
1128 #if defined(YYBTYACC)
1129 if (backtrack && (SRtotal + RRtotal) != 0)
1130 conflicts = NEW2(4 * (SRtotal + RRtotal), Value_t);
1137 FREE(accessing_symbol);
1149 #if defined(YYBTYACC)
1155 is_C_identifier(char *name)
1165 if (!isalpha(c) && c != '_' && c != '$')
1167 while ((c = *++s) != '"')
1169 if (!isalnum(c) && c != '_' && c != '$')
1175 if (!isalpha(c) && c != '_' && c != '$')
1177 while ((c = *++s) != 0)
1179 if (!isalnum(c) && c != '_' && c != '$')
1185 #if USE_HEADER_GUARDS
1187 start_defines_file(void)
1189 fprintf(defines_file, "#ifndef _%s_defines_h_\n", symbol_prefix);
1190 fprintf(defines_file, "#define _%s_defines_h_\n\n", symbol_prefix);
1194 end_defines_file(void)
1196 fprintf(defines_file, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix);
1199 #define start_defines_file() /* nothing */
1200 #define end_defines_file() /* nothing */
1204 output_defines(FILE * fp)
1209 for (i = 2; i < ntokens; ++i)
1212 if (is_C_identifier(s) && (!sflag || *s != '"'))
1214 fprintf(fp, "#define ");
1218 while ((c = *++s) != '"')
1229 while ((c = *++s) != 0);
1231 if (fp == code_file)
1233 fprintf(fp, " %d\n", symbol_value[i]);
1237 if (fp == code_file)
1239 if (fp != defines_file || iflag)
1240 fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]);
1242 if (token_table && rflag && fp != externs_file)
1244 if (fp == code_file)
1246 fputs("#undef yytname\n", fp);
1247 if (fp == code_file)
1249 fputs("#define yytname yyname\n", fp);
1252 if (fp == defines_file || (iflag && !dflag))
1256 if (union_file != 0)
1259 while ((c = getc(union_file)) != EOF)
1262 fprintf(fp, "extern YYSTYPE %slval;\n", symbol_prefix);
1264 #if defined(YYBTYACC)
1272 output_stored_text(FILE * fp)
1278 if (text_file == NULL)
1279 open_error("text_file");
1281 if ((c = getc(in)) == EOF)
1284 while ((c = getc(in)) != EOF)
1288 write_code_lineno(fp);
1294 int i, j, k, max, maxtok;
1295 const char **symnam;
1299 fprintf(code_file, "#define YYFINAL %d\n", final_state);
1301 putl_code(code_file, "#ifndef YYDEBUG\n");
1303 fprintf(code_file, "#define YYDEBUG %d\n", tflag);
1304 putl_code(code_file, "#endif\n");
1308 fprintf(output_file, "#ifndef YYDEBUG\n");
1309 fprintf(output_file, "#define YYDEBUG %d\n", tflag);
1310 fprintf(output_file, "#endif\n");
1314 for (i = 0; i < ntokens; ++i)
1315 if (symbol_value[i] > maxtok)
1316 maxtok = symbol_value[i];
1318 /* symbol_value[$accept] = -1 */
1319 /* symbol_value[<goal>] = 0 */
1320 /* remaining non-terminals start at 1 */
1322 for (i = ntokens; i < nsyms; ++i)
1323 if (((maxtok + 1) + (symbol_value[i] + 1)) > max)
1324 max = (maxtok + 1) + (symbol_value[i] + 1);
1327 fprintf(code_file, "#define YYMAXTOKEN %d\n", maxtok);
1330 fprintf(code_file, "#define YYUNDFTOKEN %d\n", max + 1);
1333 fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? "
1334 "YYUNDFTOKEN : (a))\n");
1336 symnam = TMALLOC(const char *, max + 2);
1339 /* Note that it is not necessary to initialize the element */
1341 #if defined(YYBTYACC)
1342 for (i = 0; i < max; ++i)
1344 for (i = nsyms - 1; i >= 0; --i)
1345 symnam[symbol_pval[i]] = symbol_name[i];
1346 symnam[max + 1] = "illegal-symbol";
1348 for (i = 0; i <= max; ++i)
1350 for (i = ntokens - 1; i >= 2; --i)
1351 symnam[symbol_value[i]] = symbol_name[i];
1352 symnam[0] = "end-of-file";
1353 symnam[max + 1] = "illegal-symbol";
1357 * bison's yytname[] array is roughly the same as byacc's yyname[] array.
1358 * The difference is that byacc does not predefine "$undefined".
1360 * If the grammar declares "%token-table", define symbol "yytname" so
1361 * an application such as ntpd can build.
1367 output_line("#undef yytname");
1368 output_line("#define yytname yyname");
1373 output_line("#if YYDEBUG");
1376 start_str_table("name");
1378 for (i = 0; i <= max + 1; ++i)
1380 if ((s = symnam[i]) != 0)
1401 fprintf(output_file, "\"\\\"");
1407 fprintf(output_file, "\\\\");
1409 fprintf(output_file, "\\\\");
1411 putc(*s, output_file);
1414 putc(*s, output_file);
1416 fprintf(output_file, "\\\"\",");
1418 else if (s[0] == '\'')
1428 fprintf(output_file, "\"'\\\"'\",");
1433 while (*++s != '\'')
1449 fprintf(output_file, "\"'");
1451 while (*++s != '\'')
1455 fprintf(output_file, "\\\\");
1457 fprintf(output_file, "\\\\");
1459 putc(*s, output_file);
1462 putc(*s, output_file);
1464 fprintf(output_file, "'\",");
1469 k = (int)strlen(s) + 3;
1476 putc('"', output_file);
1479 putc(*s, output_file);
1482 fprintf(output_file, "\",");
1493 fprintf(output_file, "0,");
1500 output_line("#if YYDEBUG");
1501 start_str_table("rule");
1502 for (i = 2; i < nrules; ++i)
1504 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1505 for (j = rrhs[i]; ritem[j] > 0; ++j)
1507 s = symbol_name[ritem[j]];
1510 fprintf(output_file, " \\\"");
1516 fprintf(output_file, "\\\\\\\\");
1518 fprintf(output_file, "\\\\%c", s[1]);
1522 putc(*s, output_file);
1524 fprintf(output_file, "\\\"");
1526 else if (s[0] == '\'')
1529 fprintf(output_file, " '\\\"'");
1530 else if (s[1] == '\\')
1533 fprintf(output_file, " '\\\\\\\\");
1535 fprintf(output_file, " '\\\\%c", s[2]);
1537 while (*++s != '\'')
1538 putc(*s, output_file);
1539 putc('\'', output_file);
1542 fprintf(output_file, " '%c'", s[1]);
1545 fprintf(output_file, " %s", s);
1547 fprintf(output_file, "\",");
1552 output_line("#endif");
1555 #if defined(YYBTYACC)
1557 output_backtracking_parser(FILE * fp)
1559 putl_code(fp, "#undef YYBTYACC\n");
1560 #if defined(YYBTYACC)
1563 putl_code(fp, "#define YYBTYACC 1\n");
1565 "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n");
1570 putl_code(fp, "#define YYBTYACC 0\n");
1571 putl_code(fp, "#define YYDEBUGSTR YYPREFIX \"debug\"\n");
1577 output_pure_parser(FILE * fp)
1579 putc_code(fp, '\n');
1581 if (fp == code_file)
1583 fprintf(fp, "#define YYPURE %d\n", pure_parser);
1584 putc_code(fp, '\n');
1587 #if defined(YY_NO_LEAKS)
1589 output_no_leaks(FILE * fp)
1591 putc_code(fp, '\n');
1593 if (fp == code_file)
1595 fputs("#define YY_NO_LEAKS 1\n", fp);
1596 putc_code(fp, '\n');
1601 output_trailing_text(void)
1614 if ((c = getc(in)) == EOF)
1616 write_input_lineno();
1617 putc_code(code_file, c);
1622 write_input_lineno();
1625 putc_code(code_file, c);
1627 while ((c = *++cptr) != '\n');
1628 putc_code(code_file, c);
1632 while ((c = getc(in)) != EOF)
1634 putc_code(code_file, c);
1640 putc_code(code_file, '\n');
1642 write_code_lineno(code_file);
1646 output_semantic_actions(void)
1650 rewind(action_file);
1651 if ((c = getc(action_file)) == EOF)
1655 putc_code(code_file, c);
1656 while ((c = getc(action_file)) != EOF)
1658 putc_code(code_file, c);
1664 putc_code(code_file, '\n');
1667 write_code_lineno(code_file);
1671 output_parse_decl(FILE * fp)
1673 putc_code(fp, '\n');
1674 putl_code(fp, "/* compatibility with bison */\n");
1675 putl_code(fp, "#ifdef YYPARSE_PARAM\n");
1676 putl_code(fp, "/* compatibility with FreeBSD */\n");
1677 putl_code(fp, "# ifdef YYPARSE_PARAM_TYPE\n");
1679 "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
1680 putl_code(fp, "# else\n");
1681 putl_code(fp, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n");
1682 putl_code(fp, "# endif\n");
1683 putl_code(fp, "#else\n");
1685 puts_code(fp, "# define YYPARSE_DECL() yyparse(");
1686 puts_param_types(fp, parse_param, 0);
1687 putl_code(fp, ")\n");
1689 putl_code(fp, "#endif\n");
1693 output_lex_decl(FILE * fp)
1695 putc_code(fp, '\n');
1696 putl_code(fp, "/* Parameters sent to lex. */\n");
1697 putl_code(fp, "#ifdef YYLEX_PARAM\n");
1700 putl_code(fp, "# ifdef YYLEX_PARAM_TYPE\n");
1701 #if defined(YYBTYACC)
1704 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1706 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1711 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1712 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1714 putl_code(fp, "# else\n");
1715 #if defined(YYBTYACC)
1718 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1720 " void * YYLEX_PARAM)\n");
1725 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1726 " void * YYLEX_PARAM)\n");
1728 putl_code(fp, "# endif\n");
1729 #if defined(YYBTYACC)
1732 "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n");
1735 putl_code(fp, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
1739 putl_code(fp, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n");
1740 putl_code(fp, "# define YYLEX yylex(YYLEX_PARAM)\n");
1742 putl_code(fp, "#else\n");
1743 if (pure_parser && lex_param)
1745 #if defined(YYBTYACC)
1748 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, ");
1751 puts_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, ");
1752 puts_param_types(fp, lex_param, 0);
1753 putl_code(fp, ")\n");
1755 #if defined(YYBTYACC)
1757 puts_code(fp, "# define YYLEX yylex(&yylval, &yylloc, ");
1760 puts_code(fp, "# define YYLEX yylex(&yylval, ");
1761 puts_param_names(fp, lex_param, 0);
1762 putl_code(fp, ")\n");
1764 else if (pure_parser)
1766 #if defined(YYBTYACC)
1770 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n");
1771 putl_code(fp, "# define YYLEX yylex(&yylval, &yylloc)\n");
1776 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n");
1777 putl_code(fp, "# define YYLEX yylex(&yylval)\n");
1782 puts_code(fp, "# define YYLEX_DECL() yylex(");
1783 puts_param_types(fp, lex_param, 0);
1784 putl_code(fp, ")\n");
1786 puts_code(fp, "# define YYLEX yylex(");
1787 puts_param_names(fp, lex_param, 0);
1788 putl_code(fp, ")\n");
1792 putl_code(fp, "# define YYLEX_DECL() yylex(void)\n");
1793 putl_code(fp, "# define YYLEX yylex()\n");
1795 putl_code(fp, "#endif\n");
1799 output_error_decl(FILE * fp)
1801 putc_code(fp, '\n');
1802 putl_code(fp, "/* Parameters sent to yyerror. */\n");
1803 putl_code(fp, "#ifndef YYERROR_DECL\n");
1804 puts_code(fp, "#define YYERROR_DECL() yyerror(");
1805 #if defined(YYBTYACC)
1807 puts_code(fp, "YYLTYPE *loc, ");
1809 puts_param_types(fp, parse_param, 1);
1810 putl_code(fp, "const char *s)\n");
1811 putl_code(fp, "#endif\n");
1813 putl_code(fp, "#ifndef YYERROR_CALL\n");
1815 puts_code(fp, "#define YYERROR_CALL(msg) yyerror(");
1816 #if defined(YYBTYACC)
1818 puts_code(fp, "&yylloc, ");
1820 puts_param_names(fp, parse_param, 1);
1821 putl_code(fp, "msg)\n");
1823 putl_code(fp, "#endif\n");
1826 #if defined(YYBTYACC)
1828 output_yydestruct_decl(FILE * fp)
1830 putc_code(fp, '\n');
1831 putl_code(fp, "#ifndef YYDESTRUCT_DECL\n");
1834 "#define YYDESTRUCT_DECL() "
1835 "yydestruct(const char *msg, int psymb, YYSTYPE *val");
1836 #if defined(YYBTYACC)
1838 puts_code(fp, ", YYLTYPE *loc");
1842 puts_code(fp, ", ");
1843 puts_param_types(fp, parse_param, 0);
1845 putl_code(fp, ")\n");
1847 putl_code(fp, "#endif\n");
1849 putl_code(fp, "#ifndef YYDESTRUCT_CALL\n");
1851 puts_code(fp, "#define YYDESTRUCT_CALL(msg, psymb, val");
1852 #if defined(YYBTYACC)
1854 puts_code(fp, ", loc");
1856 puts_code(fp, ") yydestruct(msg, psymb, val");
1857 #if defined(YYBTYACC)
1859 puts_code(fp, ", loc");
1863 puts_code(fp, ", ");
1864 puts_param_names(fp, parse_param, 0);
1866 putl_code(fp, ")\n");
1868 putl_code(fp, "#endif\n");
1872 output_initial_action(void)
1875 fprintf(code_file, "%s\n", initial_action);
1879 output_yydestruct_impl(void)
1882 char *s, *destructor_code;
1884 putc_code(code_file, '\n');
1885 putl_code(code_file, "/* Release memory associated with symbol. */\n");
1886 putl_code(code_file, "#if ! defined YYDESTRUCT_IS_DECLARED\n");
1887 putl_code(code_file, "static void\n");
1888 putl_code(code_file, "YYDESTRUCT_DECL()\n");
1889 putl_code(code_file, "{\n");
1890 putl_code(code_file, " switch (psymb)\n");
1891 putl_code(code_file, " {\n");
1892 for (i = 2; i < nsyms; ++i)
1894 if ((destructor_code = symbol_destructor[i]) != NULL)
1897 fprintf(code_file, "\tcase %d:\n", symbol_pval[i]);
1898 /* comprehend the number of lines in the destructor code */
1899 for (s = destructor_code; (s = strchr(s, '\n')) != NULL; s++)
1901 puts_code(code_file, destructor_code);
1902 putc_code(code_file, '\n');
1903 putl_code(code_file, "\tbreak;\n");
1904 write_code_lineno(code_file);
1905 FREE(destructor_code);
1908 putl_code(code_file, " }\n");
1909 putl_code(code_file, "}\n");
1910 putl_code(code_file, "#define YYDESTRUCT_IS_DECLARED 1\n");
1911 putl_code(code_file, "#endif\n");
1913 DO_FREE(symbol_destructor);
1923 for (cp = first_state; cp; cp = next)
1936 for (sp = first_shift; sp; sp = next)
1944 free_reductions(void)
1946 reductions *rp, *next;
1948 FREE(reduction_table);
1949 for (rp = first_reduction; rp; rp = next)
1957 output_externs(FILE * fp, const char *const section[])
1962 for (i = 0; (s = section[i]) != 0; ++i)
1964 /* prefix non-blank lines that don't start with
1965 C pre-processor directives with 'extern ' */
1966 if (*s && (*s != '#'))
1967 fputs("extern\t", fp);
1968 if (fp == code_file)
1970 fprintf(fp, "%s\n", s);
1983 #if defined(YYBTYACC)
1984 output_backtracking_parser(output_file);
1986 output_backtracking_parser(code_file);
1991 write_code_lineno(code_file);
1993 fprintf(code_file, "#include \"%s\"\n", externs_file_name);
2000 output_pure_parser(fp);
2001 #if defined(YY_NO_LEAKS)
2002 output_no_leaks(fp);
2004 output_stored_text(fp);
2006 #if defined(YYBTYACC)
2010 output_parse_decl(fp);
2011 output_lex_decl(fp);
2012 output_error_decl(fp);
2013 #if defined(YYBTYACC)
2015 output_yydestruct_decl(fp);
2017 if (iflag || !rflag)
2019 write_section(fp, xdecls);
2024 output_externs(externs_file, global_vars);
2026 output_externs(externs_file, impure_vars);
2034 fprintf(code_file, "#include \"%s\"\n", defines_file_name);
2037 output_defines(externs_file);
2041 putc_code(code_file, '\n');
2042 output_defines(code_file);
2047 start_defines_file();
2048 output_defines(defines_file);
2054 #if defined(YYBTYACC)
2055 output_accessing_symbols();
2062 write_section(code_file, xdecls);
2063 output_YYINT_typedef(code_file);
2064 write_section(code_file, tables);
2066 write_section(code_file, global_vars);
2069 write_section(code_file, impure_vars);
2071 write_section(code_file, hdr_defs);
2074 write_section(code_file, hdr_vars);
2076 output_trailing_text();
2077 #if defined(YYBTYACC)
2079 output_yydestruct_impl();
2081 write_section(code_file, body_1);
2084 write_section(code_file, body_vars);
2086 write_section(code_file, body_2);
2089 write_section(code_file, init_vars);
2091 #if defined(YYBTYACC)
2093 output_initial_action();
2095 write_section(code_file, body_3);
2096 output_semantic_actions();
2097 write_section(code_file, trailer);