1 /* $Id: output.c,v 1.67 2014/04/22 23:16:57 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 write_code_lineno(FILE * fp)
59 if (!lflag && (fp == code_file))
62 fprintf(fp, line_format, outline + 1, code_file_name);
67 write_input_lineno(void)
72 fprintf(code_file, line_format, lineno, input_file_name);
77 define_prefixed(FILE * fp, const char *name)
79 int bump_line = CountLine(fp);
86 fprintf(fp, "#ifndef %s\n", name);
90 fprintf(fp, "#define %-10s %s%s\n", name, symbol_prefix, name + 2);
94 fprintf(fp, "#endif /* %s */\n", name);
98 output_prefix(FILE * fp)
100 if (symbol_prefix == NULL)
102 symbol_prefix = "yy";
106 define_prefixed(fp, "yyparse");
107 define_prefixed(fp, "yylex");
108 define_prefixed(fp, "yyerror");
109 define_prefixed(fp, "yychar");
110 define_prefixed(fp, "yyval");
111 define_prefixed(fp, "yylval");
112 define_prefixed(fp, "yydebug");
113 define_prefixed(fp, "yynerrs");
114 define_prefixed(fp, "yyerrflag");
115 define_prefixed(fp, "yylhs");
116 define_prefixed(fp, "yylen");
117 define_prefixed(fp, "yydefred");
118 #if defined(YYBTYACC)
119 define_prefixed(fp, "yystos");
121 define_prefixed(fp, "yydgoto");
122 define_prefixed(fp, "yysindex");
123 define_prefixed(fp, "yyrindex");
124 define_prefixed(fp, "yygindex");
125 define_prefixed(fp, "yytable");
126 define_prefixed(fp, "yycheck");
127 define_prefixed(fp, "yyname");
128 define_prefixed(fp, "yyrule");
129 #if defined(YYBTYACC)
132 define_prefixed(fp, "yyloc");
133 define_prefixed(fp, "yylloc");
136 putl_code(fp, "#if YYBTYACC\n");
138 define_prefixed(fp, "yycindex");
139 define_prefixed(fp, "yyctable");
142 putl_code(fp, "#endif /* YYBTYACC */\n");
148 fprintf(fp, "#define YYPREFIX \"%s\"\n", symbol_prefix);
156 putc('\n', output_file);
160 output_line(const char *value)
162 fputs(value, output_file);
167 output_int(int value)
169 fprintf(output_file, "%5d,", value);
173 start_int_table(const char *name, int value)
175 int need = 34 - (int)(strlen(symbol_prefix) + strlen(name));
180 "%sconst YYINT %s%s[] = {%*d,",
181 StaticOrR, symbol_prefix, name, need, value);
185 start_str_table(const char *name)
188 "%sconst char *const %s%s[] = {",
189 StaticOrR, symbol_prefix, name);
201 output_YYINT_typedef(FILE * fp)
203 /* generate the type used to index the various parser tables */
206 fprintf(fp, "typedef %s YYINT;\n", CONCAT1("", YYINT));
210 output_rule_data(void)
215 output_YYINT_typedef(output_file);
217 start_int_table("lhs", symbol_value[start_symbol]);
220 for (i = 3; i < nrules; i++)
230 output_int(symbol_value[rlhs[i]]);
234 start_int_table("len", 2);
237 for (i = 3; i < nrules; i++)
247 output_int(rrhs[i + 1] - rrhs[i] - 1);
253 output_yydefred(void)
257 start_int_table("defred", (defred[0] ? defred[0] - 2 : 0));
260 for (i = 1; i < nstates; i++)
270 output_int((defred[i] ? defred[i] - 2 : 0));
276 #if defined(YYBTYACC)
278 output_accessing_symbols(void)
285 translate = TMALLOC(int, nstates);
288 for (i = 0; i < nstates; ++i)
290 int gsymb = accessing_symbol[i];
292 translate[i] = symbol_pval[gsymb];
295 /* yystos[] may be unused, depending on compile-time defines */
296 start_int_table("stos", translate[0]);
299 for (i = 1; i < nstates; ++i)
309 output_int(translate[i]);
318 find_conflict_base(int cbase)
322 for (i = 0; i < cbase; i++)
324 for (j = 0; j + cbase < nconflicts; j++)
326 if (conflicts[i + j] != conflicts[cbase + j])
329 if (j + cbase >= nconflicts)
340 Value_t shiftcount, reducecount;
341 #if defined(YYBTYACC)
342 Value_t conflictcount = 0;
347 Value_t *actionrow, *r, *s;
350 actionrow = NEW2(PER_STATE * ntokens, Value_t);
351 for (i = 0; i < nstates; ++i)
355 for (j = 0; j < PER_STATE * ntokens; ++j)
360 #if defined(YYBTYACC)
368 for (p = parser[i]; p; p = p->next)
370 #if defined(YYBTYACC)
373 if (csym != -1 && csym != p->symbol)
376 conflicts[nconflicts++] = -1;
377 j = find_conflict_base(cbase);
378 actionrow[csym + 2 * ntokens] = (Value_t) (j + 1);
385 if (conflicts[cbase] == -1)
393 if (p->suppressed == 0)
395 if (p->action_code == SHIFT)
398 actionrow[p->symbol] = p->number;
400 else if (p->action_code == REDUCE && p->number != defred[i])
403 actionrow[p->symbol + ntokens] = p->number;
406 #if defined(YYBTYACC)
407 else if (backtrack && p->suppressed == 1)
410 if (p->action_code == SHIFT)
412 conflicts[nconflicts++] = p->number;
414 else if (p->action_code == REDUCE && p->number != defred[i])
416 if (cbase == nconflicts)
421 conflicts[nconflicts++] = -1;
423 conflicts[nconflicts++] = (Value_t) (p->number - 2);
428 #if defined(YYBTYACC)
429 if (backtrack && csym != -1)
432 conflicts[nconflicts++] = -1;
433 j = find_conflict_base(cbase);
434 actionrow[csym + 2 * ntokens] = (Value_t) (j + 1);
441 if (conflicts[cbase] == -1)
448 tally[i] = shiftcount;
449 tally[nstates + i] = reducecount;
450 #if defined(YYBTYACC)
452 tally[2 * nstates + i] = conflictcount;
455 width[nstates + i] = 0;
456 #if defined(YYBTYACC)
458 width[2 * nstates + i] = 0;
462 froms[i] = r = NEW2(shiftcount, Value_t);
463 tos[i] = s = NEW2(shiftcount, Value_t);
466 for (j = 0; j < ntokens; ++j)
470 if (min > symbol_value[j])
471 min = symbol_value[j];
472 if (max < symbol_value[j])
473 max = symbol_value[j];
474 *r++ = symbol_value[j];
478 width[i] = (Value_t) (max - min + 1);
482 froms[nstates + i] = r = NEW2(reducecount, Value_t);
483 tos[nstates + i] = s = NEW2(reducecount, Value_t);
486 for (j = 0; j < ntokens; ++j)
488 if (actionrow[ntokens + j])
490 if (min > symbol_value[j])
491 min = symbol_value[j];
492 if (max < symbol_value[j])
493 max = symbol_value[j];
494 *r++ = symbol_value[j];
495 *s++ = (Value_t) (actionrow[ntokens + j] - 2);
498 width[nstates + i] = (Value_t) (max - min + 1);
500 #if defined(YYBTYACC)
501 if (backtrack && conflictcount > 0)
503 froms[2 * nstates + i] = r = NEW2(conflictcount, Value_t);
504 tos[2 * nstates + i] = s = NEW2(conflictcount, Value_t);
507 for (j = 0; j < ntokens; ++j)
509 if (actionrow[2 * ntokens + j])
511 if (min > symbol_value[j])
512 min = symbol_value[j];
513 if (max < symbol_value[j])
514 max = symbol_value[j];
515 *r++ = symbol_value[j];
516 *s++ = (Value_t) (actionrow[2 * ntokens + j] - 1);
519 width[2 * nstates + i] = (Value_t) (max - min + 1);
528 default_goto(int symbol)
536 m = goto_map[symbol];
537 n = goto_map[symbol + 1];
542 for (i = 0; i < nstates; i++)
545 for (i = m; i < n; i++)
546 state_count[to_state[i]]++;
550 for (i = 0; i < nstates; i++)
552 if (state_count[i] > max)
554 max = state_count[i];
559 return (default_state);
563 save_column(int symbol, int default_state)
574 m = goto_map[symbol];
575 n = goto_map[symbol + 1];
578 for (i = m; i < n; i++)
580 if (to_state[i] != default_state)
586 symno = symbol_value[symbol] + PER_STATE * nstates;
588 froms[symno] = sp1 = sp = NEW2(count, Value_t);
589 tos[symno] = sp2 = NEW2(count, Value_t);
591 for (i = m; i < n; i++)
593 if (to_state[i] != default_state)
595 *sp1++ = from_state[i];
596 *sp2++ = to_state[i];
600 tally[symno] = count;
601 width[symno] = (Value_t) (sp1[-1] - sp[0] + 1);
609 state_count = NEW2(nstates, Value_t);
611 k = default_goto(start_symbol + 1);
612 start_int_table("dgoto", k);
613 save_column(start_symbol + 1, k);
616 for (i = start_symbol + 2; i < nsyms; i++)
644 order = NEW2(nvectors, Value_t);
647 for (i = 0; i < nvectors; i++)
655 while (j >= 0 && (width[order[j]] < w))
658 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
661 for (k = nentries - 1; k > j; k--)
662 order[k + 1] = order[k];
670 /* The function matching_vector determines if the vector specified by */
671 /* the input parameter matches a previously considered vector. The */
672 /* test at the start of the function checks if the vector represents */
673 /* a row of shifts over terminal symbols or a row of reductions, or a */
674 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
675 /* check if a column of shifts over a nonterminal symbols matches a */
676 /* previously considered vector. Because of the nature of LR parsing */
677 /* tables, no two columns can match. Therefore, the only possible */
678 /* match would be between a row and a column. Such matches are */
679 /* unlikely. Therefore, to save time, no attempt is made to see if a */
680 /* column matches a previously considered vector. */
682 /* Matching_vector is poorly designed. The test could easily be made */
683 /* faster. Also, it depends on the vectors being in a specific */
685 #if defined(YYBTYACC)
687 /* Not really any point in checking for matching conflicts -- it is */
688 /* extremely unlikely to occur, and conflicts are (hopefully) rare. */
692 matching_vector(int vector)
703 if (i >= 2 * nstates)
709 for (prev = vector - 1; prev >= 0; prev--)
712 if (width[j] != w || tally[j] != t)
716 for (k = 0; match && k < t; k++)
718 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
730 pack_vector(int vector)
747 j = lowzero - from[0];
748 for (k = 1; k < t; ++k)
749 if (lowzero - from[k] > j)
750 j = lowzero - from[k];
756 for (k = 0; ok && k < t; k++)
759 if (loc >= maxtable - 1)
761 if (loc >= MAXTABLE - 1)
762 fatal("maximum table size exceeded");
769 while (newmax <= loc);
771 table = TREALLOC(Value_t, table, newmax);
774 check = TREALLOC(Value_t, check, newmax);
777 for (l = maxtable; l < newmax; ++l)
785 if (check[loc] != -1)
788 for (k = 0; ok && k < vector; k++)
795 for (k = 0; k < t; k++)
799 check[loc] = from[k];
804 while (check[lowzero] != -1)
819 base = NEW2(nvectors, Value_t);
820 pos = NEW2(nentries, Value_t);
823 table = NEW2(maxtable, Value_t);
824 check = NEW2(maxtable, Value_t);
829 for (i = 0; i < maxtable; i++)
832 for (i = 0; i < nentries; i++)
834 state = matching_vector(i);
837 place = (Value_t) pack_vector(i);
842 base[order[i]] = place;
845 for (i = 0; i < nvectors; i++)
865 start_int_table("sindex", base[0]);
868 for (i = 1; i < nstates; i++)
883 start_int_table("rindex", base[nstates]);
886 for (i = nstates + 1; i < 2 * nstates; i++)
901 #if defined(YYBTYACC)
902 output_line("#if YYBTYACC");
903 start_int_table("cindex", base[2 * nstates]);
906 for (i = 2 * nstates + 1; i < 3 * nstates; i++)
920 output_line("#endif");
923 start_int_table("gindex", base[PER_STATE * nstates]);
926 for (i = PER_STATE * nstates + 1; i < nvectors - 1; i++)
949 if (high >= MAXYYINT)
951 fprintf(stderr, "YYTABLESIZE: %ld\n", high);
952 fprintf(stderr, "Table is longer than %d elements.\n", MAXYYINT);
957 fprintf(code_file, "#define YYTABLESIZE %ld\n", high);
958 start_int_table("table", table[0]);
961 for (i = 1; i <= high; i++)
971 output_int(table[i]);
984 start_int_table("check", check[0]);
987 for (i = 1; i <= high; i++)
997 output_int(check[i]);
1004 #if defined(YYBTYACC)
1013 output_line("#if YYBTYACC");
1014 start_int_table("ctable", conflicts[0]);
1017 for (i = 1; i < nconflicts; i++)
1027 output_int(conflicts[i]);
1031 output_line("#endif");
1038 output_actions(void)
1040 nvectors = PER_STATE * nstates + nvars;
1042 froms = NEW2(nvectors, Value_t *);
1043 tos = NEW2(nvectors, Value_t *);
1044 tally = NEW2(nvectors, Value_t);
1045 width = NEW2(nvectors, Value_t);
1047 #if defined(YYBTYACC)
1048 if (backtrack && (SRtotal + RRtotal) != 0)
1049 conflicts = NEW2(4 * (SRtotal + RRtotal), Value_t);
1056 FREE(accessing_symbol);
1059 FREE(goto_map + ntokens);
1068 #if defined(YYBTYACC)
1074 is_C_identifier(char *name)
1084 if (!isalpha(c) && c != '_' && c != '$')
1086 while ((c = *++s) != '"')
1088 if (!isalnum(c) && c != '_' && c != '$')
1094 if (!isalpha(c) && c != '_' && c != '$')
1096 while ((c = *++s) != 0)
1098 if (!isalnum(c) && c != '_' && c != '$')
1104 #if USE_HEADER_GUARDS
1106 start_defines_file(void)
1108 fprintf(defines_file, "#ifndef _%s_defines_h_\n", symbol_prefix);
1109 fprintf(defines_file, "#define _%s_defines_h_\n\n", symbol_prefix);
1113 end_defines_file(void)
1115 fprintf(defines_file, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix);
1118 #define start_defines_file() /* nothing */
1119 #define end_defines_file() /* nothing */
1123 output_defines(FILE * fp)
1128 for (i = 2; i < ntokens; ++i)
1131 if (is_C_identifier(s) && (!sflag || *s != '"'))
1133 fprintf(fp, "#define ");
1137 while ((c = *++s) != '"')
1148 while ((c = *++s) != 0);
1150 if (fp == code_file)
1152 fprintf(fp, " %d\n", symbol_value[i]);
1156 if (fp == code_file)
1158 if (fp != defines_file || iflag)
1159 fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]);
1161 if (fp == defines_file || (iflag && !dflag))
1165 if (union_file != 0)
1168 while ((c = getc(union_file)) != EOF)
1171 fprintf(fp, "extern YYSTYPE %slval;\n", symbol_prefix);
1177 output_stored_text(FILE * fp)
1183 if (text_file == NULL)
1184 open_error("text_file");
1186 if ((c = getc(in)) == EOF)
1189 while ((c = getc(in)) != EOF)
1193 write_code_lineno(fp);
1199 int i, j, k, max, maxtok;
1200 const char **symnam;
1204 fprintf(code_file, "#define YYFINAL %d\n", final_state);
1206 putl_code(code_file, "#ifndef YYDEBUG\n");
1208 fprintf(code_file, "#define YYDEBUG %d\n", tflag);
1209 putl_code(code_file, "#endif\n");
1213 fprintf(output_file, "#ifndef YYDEBUG\n");
1214 fprintf(output_file, "#define YYDEBUG %d\n", tflag);
1215 fprintf(output_file, "#endif\n");
1219 for (i = 0; i < ntokens; ++i)
1220 if (symbol_value[i] > maxtok)
1221 maxtok = symbol_value[i];
1223 /* symbol_value[$accept] = -1 */
1224 /* symbol_value[<goal>] = 0 */
1225 /* remaining non-terminals start at 1 */
1227 for (i = ntokens; i < nsyms; ++i)
1228 if (((maxtok + 1) + (symbol_value[i] + 1)) > max)
1229 max = (maxtok + 1) + (symbol_value[i] + 1);
1232 fprintf(code_file, "#define YYMAXTOKEN %d\n", maxtok);
1235 fprintf(code_file, "#define YYUNDFTOKEN %d\n", max + 1);
1238 fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? "
1239 "YYUNDFTOKEN : (a))\n");
1241 symnam = TMALLOC(const char *, max + 2);
1244 /* Note that it is not necessary to initialize the element */
1246 #if defined(YYBTYACC)
1247 for (i = 0; i < max; ++i)
1249 for (i = nsyms - 1; i >= 0; --i)
1250 symnam[symbol_pval[i]] = symbol_name[i];
1251 symnam[max + 1] = "illegal-symbol";
1253 for (i = 0; i <= max; ++i)
1255 for (i = ntokens - 1; i >= 2; --i)
1256 symnam[symbol_value[i]] = symbol_name[i];
1257 symnam[0] = "end-of-file";
1258 symnam[max + 1] = "illegal-symbol";
1262 * bison's yytname[] array is roughly the same as byacc's yyname[] array.
1263 * The difference is that byacc does not predefine "$undefined".
1265 * If the grammar declares "%token-table", define symbol "yytname" so
1266 * an application such as ntpd can build.
1270 output_line("#undef yytname");
1271 output_line("#define yytname yyname");
1275 output_line("#if YYDEBUG");
1278 start_str_table("name");
1280 for (i = 0; i <= max + 1; ++i)
1282 if ((s = symnam[i]) != 0)
1303 fprintf(output_file, "\"\\\"");
1309 fprintf(output_file, "\\\\");
1311 fprintf(output_file, "\\\\");
1313 putc(*s, output_file);
1316 putc(*s, output_file);
1318 fprintf(output_file, "\\\"\",");
1320 else if (s[0] == '\'')
1330 fprintf(output_file, "\"'\\\"'\",");
1335 while (*++s != '\'')
1351 fprintf(output_file, "\"'");
1353 while (*++s != '\'')
1357 fprintf(output_file, "\\\\");
1359 fprintf(output_file, "\\\\");
1361 putc(*s, output_file);
1364 putc(*s, output_file);
1366 fprintf(output_file, "'\",");
1371 k = (int)strlen(s) + 3;
1378 putc('"', output_file);
1381 putc(*s, output_file);
1384 fprintf(output_file, "\",");
1395 fprintf(output_file, "0,");
1402 output_line("#if YYDEBUG");
1403 start_str_table("rule");
1404 for (i = 2; i < nrules; ++i)
1406 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1407 for (j = rrhs[i]; ritem[j] > 0; ++j)
1409 s = symbol_name[ritem[j]];
1412 fprintf(output_file, " \\\"");
1418 fprintf(output_file, "\\\\\\\\");
1420 fprintf(output_file, "\\\\%c", s[1]);
1424 putc(*s, output_file);
1426 fprintf(output_file, "\\\"");
1428 else if (s[0] == '\'')
1431 fprintf(output_file, " '\\\"'");
1432 else if (s[1] == '\\')
1435 fprintf(output_file, " '\\\\\\\\");
1437 fprintf(output_file, " '\\\\%c", s[2]);
1439 while (*++s != '\'')
1440 putc(*s, output_file);
1441 putc('\'', output_file);
1444 fprintf(output_file, " '%c'", s[1]);
1447 fprintf(output_file, " %s", s);
1449 fprintf(output_file, "\",");
1454 output_line("#endif");
1457 #if defined(YYBTYACC)
1459 output_backtracking_parser(FILE * fp)
1461 putl_code(fp, "#undef YYBTYACC\n");
1462 #if defined(YYBTYACC)
1465 putl_code(fp, "#define YYBTYACC 1\n");
1467 "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n");
1472 putl_code(fp, "#define YYBTYACC 0\n");
1473 putl_code(fp, "#define YYDEBUGSTR YYPREFIX \"debug\"\n");
1479 output_pure_parser(FILE * fp)
1481 putc_code(fp, '\n');
1483 if (fp == code_file)
1485 fprintf(fp, "#define YYPURE %d\n", pure_parser);
1486 putc_code(fp, '\n');
1490 output_stype(FILE * fp)
1492 if (!unionized && ntags == 0)
1494 putc_code(fp, '\n');
1495 putl_code(fp, "#if "
1496 "! defined(YYSTYPE) && "
1497 "! defined(YYSTYPE_IS_DECLARED)\n");
1498 putl_code(fp, "/* Default: YYSTYPE is the semantic value type. */\n");
1499 putl_code(fp, "typedef int YYSTYPE;\n");
1500 putl_code(fp, "# define YYSTYPE_IS_DECLARED 1\n");
1501 putl_code(fp, "#endif\n");
1505 #if defined(YYBTYACC)
1507 output_ltype(FILE * fp)
1509 putc_code(fp, '\n');
1510 putl_code(fp, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n");
1511 putl_code(fp, "/* Default: YYLTYPE is the text position type. */\n");
1512 putl_code(fp, "typedef struct YYLTYPE\n");
1513 putl_code(fp, "{\n");
1514 putl_code(fp, " int first_line;\n");
1515 putl_code(fp, " int first_column;\n");
1516 putl_code(fp, " int last_line;\n");
1517 putl_code(fp, " int last_column;\n");
1518 putl_code(fp, "} YYLTYPE;\n");
1519 putl_code(fp, "#define YYLTYPE_IS_DECLARED 1\n");
1520 putl_code(fp, "#endif\n");
1525 output_trailing_text(void)
1538 if ((c = getc(in)) == EOF)
1540 write_input_lineno();
1541 putc_code(code_file, c);
1546 write_input_lineno();
1549 putc_code(code_file, c);
1551 while ((c = *++cptr) != '\n');
1552 putc_code(code_file, c);
1556 while ((c = getc(in)) != EOF)
1558 putc_code(code_file, c);
1564 putc_code(code_file, '\n');
1566 write_code_lineno(code_file);
1570 output_semantic_actions(void)
1574 rewind(action_file);
1575 if ((c = getc(action_file)) == EOF)
1579 putc_code(code_file, c);
1580 while ((c = getc(action_file)) != EOF)
1582 putc_code(code_file, c);
1588 putc_code(code_file, '\n');
1591 write_code_lineno(code_file);
1595 output_parse_decl(FILE * fp)
1597 putc_code(fp, '\n');
1598 putl_code(fp, "/* compatibility with bison */\n");
1599 putl_code(fp, "#ifdef YYPARSE_PARAM\n");
1600 putl_code(fp, "/* compatibility with FreeBSD */\n");
1601 putl_code(fp, "# ifdef YYPARSE_PARAM_TYPE\n");
1603 "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
1604 putl_code(fp, "# else\n");
1605 putl_code(fp, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n");
1606 putl_code(fp, "# endif\n");
1607 putl_code(fp, "#else\n");
1609 puts_code(fp, "# define YYPARSE_DECL() yyparse(");
1611 puts_code(fp, "void");
1615 for (p = parse_param; p; p = p->next)
1616 fprintf(fp, "%s %s%s%s", p->type, p->name, p->type2,
1617 p->next ? ", " : "");
1619 putl_code(fp, ")\n");
1621 putl_code(fp, "#endif\n");
1625 output_lex_decl(FILE * fp)
1627 putc_code(fp, '\n');
1628 putl_code(fp, "/* Parameters sent to lex. */\n");
1629 putl_code(fp, "#ifdef YYLEX_PARAM\n");
1632 putl_code(fp, "# ifdef YYLEX_PARAM_TYPE\n");
1633 #if defined(YYBTYACC)
1636 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc,"
1637 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1642 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1643 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1645 putl_code(fp, "# else\n");
1646 #if defined(YYBTYACC)
1649 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc,"
1650 " void * YYLEX_PARAM)\n");
1655 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1656 " void * YYLEX_PARAM)\n");
1658 putl_code(fp, "# endif\n");
1659 #if defined(YYBTYACC)
1662 "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n");
1665 putl_code(fp, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
1669 putl_code(fp, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n");
1670 putl_code(fp, "# define YYLEX yylex(YYLEX_PARAM)\n");
1672 putl_code(fp, "#else\n");
1673 if (pure_parser && lex_param)
1677 #if defined(YYBTYACC)
1680 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, ");
1683 puts_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, ");
1684 for (p = lex_param; p; p = p->next)
1685 fprintf(fp, "%s %s%s%s", p->type, p->name, p->type2,
1686 p->next ? ", " : "");
1687 putl_code(fp, ")\n");
1689 #if defined(YYBTYACC)
1691 puts_code(fp, "# define YYLEX yylex(&yylval, &yylloc, ");
1694 puts_code(fp, "# define YYLEX yylex(&yylval, ");
1695 for (p = lex_param; p; p = p->next)
1696 fprintf(fp, "%s%s", p->name, p->next ? ", " : "");
1697 putl_code(fp, ")\n");
1699 else if (pure_parser)
1701 #if defined(YYBTYACC)
1705 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n");
1706 putl_code(fp, "# define YYLEX yylex(&yylval, &yylloc)\n");
1711 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n");
1712 putl_code(fp, "# define YYLEX yylex(&yylval)\n");
1719 puts_code(fp, "# define YYLEX_DECL() yylex(");
1720 for (p = lex_param; p; p = p->next)
1721 fprintf(fp, "%s %s%s%s", p->type, p->name, p->type2,
1722 p->next ? ", " : "");
1723 putl_code(fp, ")\n");
1725 puts_code(fp, "# define YYLEX yylex(");
1726 for (p = lex_param; p; p = p->next)
1727 fprintf(fp, "%s%s", p->name, p->next ? ", " : "");
1728 putl_code(fp, ")\n");
1732 putl_code(fp, "# define YYLEX_DECL() yylex(void)\n");
1733 putl_code(fp, "# define YYLEX yylex()\n");
1735 putl_code(fp, "#endif\n");
1739 output_error_decl(FILE * fp)
1743 putc_code(fp, '\n');
1744 putl_code(fp, "/* Parameters sent to yyerror. */\n");
1745 putl_code(fp, "#ifndef YYERROR_DECL\n");
1746 puts_code(fp, "#define YYERROR_DECL() yyerror(");
1747 #if defined(YYBTYACC)
1749 puts_code(fp, "YYLTYPE loc, ");
1751 for (p = parse_param; p; p = p->next)
1752 fprintf(fp, "%s %s%s, ", p->type, p->name, p->type2);
1753 putl_code(fp, "const char *s)\n");
1754 putl_code(fp, "#endif\n");
1756 putl_code(fp, "#ifndef YYERROR_CALL\n");
1757 puts_code(fp, "#define YYERROR_CALL(msg) yyerror(");
1759 #if defined(YYBTYACC)
1761 puts_code(fp, "yylloc, ");
1763 for (p = parse_param; p; p = p->next)
1764 fprintf(fp, "%s, ", p->name);
1766 putl_code(fp, "msg)\n");
1767 putl_code(fp, "#endif\n");
1770 #if defined(YYBTYACC)
1772 output_yydestruct_decl(FILE * fp)
1774 putc_code(fp, '\n');
1775 putl_code(fp, "#ifndef YYDESTRUCT_DECL\n");
1776 #if defined(YYBTYACC)
1779 "#define YYDESTRUCT_DECL() yydestruct(const char *msg, int psymb, YYSTYPE *val, YYLTYPE *loc)\n");
1783 "#define YYDESTRUCT_DECL() yydestruct(const char *msg, int psymb, YYSTYPE *val)\n");
1784 putl_code(fp, "#endif\n");
1785 putl_code(fp, "#ifndef YYDESTRUCT_CALL\n");
1786 #if defined(YYBTYACC)
1789 "#define YYDESTRUCT_CALL(msg, psymb, val, loc) yydestruct(msg, psymb, val, loc)\n");
1793 "#define YYDESTRUCT_CALL(msg, psymb, val) yydestruct(msg, psymb, val)\n");
1794 putl_code(fp, "#endif\n");
1798 output_yydestruct_impl(void)
1801 char *s, *destructor_code;
1803 putc_code(code_file, '\n');
1804 putl_code(code_file, "/* Release memory associated with symbol. */\n");
1805 putl_code(code_file, "#if ! defined YYDESTRUCT_IS_DECLARED\n");
1806 putl_code(code_file, "static void\n");
1807 putl_code(code_file, "YYDESTRUCT_DECL()\n");
1808 putl_code(code_file, "{\n");
1809 putl_code(code_file, " switch (psymb)\n");
1810 putl_code(code_file, " {\n");
1811 for (i = 2; i < nsyms; ++i)
1813 if ((destructor_code = symbol_destructor[i]) != NULL)
1816 fprintf(code_file, "\tcase %d:\n", symbol_pval[i]);
1817 /* comprehend the number of lines in the destructor code */
1818 for (s = destructor_code; (s = strchr(s, '\n')) != NULL; s++)
1820 puts_code(code_file, destructor_code);
1821 putc_code(code_file, '\n');
1822 putl_code(code_file, "\tbreak;\n");
1823 write_code_lineno(code_file);
1824 FREE(destructor_code);
1827 putl_code(code_file, " }\n");
1828 putl_code(code_file, "}\n");
1829 putl_code(code_file, "#define YYDESTRUCT_IS_DECLARED 1\n");
1830 putl_code(code_file, "#endif\n");
1832 DO_FREE(symbol_destructor);
1842 for (cp = first_state; cp; cp = next)
1855 for (sp = first_shift; sp; sp = next)
1863 free_reductions(void)
1865 reductions *rp, *next;
1867 FREE(reduction_table);
1868 for (rp = first_reduction; rp; rp = next)
1876 output_externs(FILE * fp, const char *const section[])
1881 for (i = 0; (s = section[i]) != 0; ++i)
1883 /* prefix non-blank lines that don't start with
1884 C pre-processor directives with 'extern ' */
1885 if (*s && (*s != '#'))
1886 fputs("extern\t", fp);
1887 if (fp == code_file)
1889 fprintf(fp, "%s\n", s);
1902 #if defined(YYBTYACC)
1903 output_backtracking_parser(output_file);
1905 output_backtracking_parser(code_file);
1910 write_code_lineno(code_file);
1912 fprintf(code_file, "#include \"%s\"\n", externs_file_name);
1919 output_pure_parser(fp);
1920 output_stored_text(fp);
1922 #if defined(YYBTYACC)
1926 output_parse_decl(fp);
1927 output_lex_decl(fp);
1928 output_error_decl(fp);
1929 #if defined(YYBTYACC)
1931 output_yydestruct_decl(fp);
1933 if (iflag || !rflag)
1935 write_section(fp, xdecls);
1940 output_externs(externs_file, global_vars);
1942 output_externs(externs_file, impure_vars);
1950 fprintf(code_file, "#include \"%s\"\n", defines_file_name);
1953 output_defines(externs_file);
1957 putc_code(code_file, '\n');
1958 output_defines(code_file);
1963 start_defines_file();
1964 output_defines(defines_file);
1970 #if defined(YYBTYACC)
1971 output_accessing_symbols();
1978 write_section(code_file, xdecls);
1979 output_YYINT_typedef(code_file);
1980 write_section(code_file, tables);
1982 write_section(code_file, global_vars);
1985 write_section(code_file, impure_vars);
1987 write_section(code_file, hdr_defs);
1990 write_section(code_file, hdr_vars);
1992 output_trailing_text();
1993 #if defined(YYBTYACC)
1995 output_yydestruct_impl();
1997 write_section(code_file, body_1);
2000 write_section(code_file, body_vars);
2002 write_section(code_file, body_2);
2003 output_semantic_actions();
2004 write_section(code_file, trailer);