xref: /minix3/external/bsd/byacc/dist/output.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: output.c,v 1.14 2015/01/04 20:32:03 christos Exp $	*/
284d9c625SLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /* Id: output.c,v 1.74 2014/10/05 23:21:09 tom Exp  */
44a17663cSThomas Veerman 
54a17663cSThomas Veerman #include "defs.h"
64a17663cSThomas Veerman 
74a17663cSThomas Veerman #include <sys/cdefs.h>
8*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: output.c,v 1.14 2015/01/04 20:32:03 christos Exp $");
94a17663cSThomas Veerman 
104a17663cSThomas Veerman #define StaticOrR	(rflag ? "" : "static ")
114a17663cSThomas Veerman #define CountLine(fp)   (!rflag || ((fp) == code_file))
124a17663cSThomas Veerman 
13*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
14*0a6a1f1dSLionel Sambuc #define PER_STATE 3
15*0a6a1f1dSLionel Sambuc #else
16*0a6a1f1dSLionel Sambuc #define PER_STATE 2
17*0a6a1f1dSLionel Sambuc #endif
18*0a6a1f1dSLionel Sambuc 
194a17663cSThomas Veerman static int nvectors;
204a17663cSThomas Veerman static int nentries;
214a17663cSThomas Veerman static Value_t **froms;
224a17663cSThomas Veerman static Value_t **tos;
23*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
24*0a6a1f1dSLionel Sambuc static Value_t *conflicts = NULL;
25*0a6a1f1dSLionel Sambuc static Value_t nconflicts = 0;
26*0a6a1f1dSLionel Sambuc #endif
274a17663cSThomas Veerman static Value_t *tally;
284a17663cSThomas Veerman static Value_t *width;
294a17663cSThomas Veerman static Value_t *state_count;
304a17663cSThomas Veerman static Value_t *order;
314a17663cSThomas Veerman static Value_t *base;
324a17663cSThomas Veerman static Value_t *pos;
334a17663cSThomas Veerman static int maxtable;
344a17663cSThomas Veerman static Value_t *table;
354a17663cSThomas Veerman static Value_t *check;
364a17663cSThomas Veerman static int lowzero;
37*0a6a1f1dSLionel Sambuc static long high;
384a17663cSThomas Veerman 
394a17663cSThomas Veerman static void
putc_code(FILE * fp,int c)404a17663cSThomas Veerman putc_code(FILE * fp, int c)
414a17663cSThomas Veerman {
424a17663cSThomas Veerman     if ((c == '\n') && (fp == code_file))
434a17663cSThomas Veerman 	++outline;
444a17663cSThomas Veerman     putc(c, fp);
454a17663cSThomas Veerman }
464a17663cSThomas Veerman 
474a17663cSThomas Veerman static void
putl_code(FILE * fp,const char * s)484a17663cSThomas Veerman putl_code(FILE * fp, const char *s)
494a17663cSThomas Veerman {
504a17663cSThomas Veerman     if (fp == code_file)
514a17663cSThomas Veerman 	++outline;
524a17663cSThomas Veerman     fputs(s, fp);
534a17663cSThomas Veerman }
544a17663cSThomas Veerman 
554a17663cSThomas Veerman static void
puts_code(FILE * fp,const char * s)564a17663cSThomas Veerman puts_code(FILE * fp, const char *s)
574a17663cSThomas Veerman {
584a17663cSThomas Veerman     fputs(s, fp);
594a17663cSThomas Veerman }
604a17663cSThomas Veerman 
614a17663cSThomas Veerman static void
puts_param_types(FILE * fp,param * list,int more)62*0a6a1f1dSLionel Sambuc puts_param_types(FILE * fp, param * list, int more)
63*0a6a1f1dSLionel Sambuc {
64*0a6a1f1dSLionel Sambuc     param *p;
65*0a6a1f1dSLionel Sambuc 
66*0a6a1f1dSLionel Sambuc     if (list != 0)
67*0a6a1f1dSLionel Sambuc     {
68*0a6a1f1dSLionel Sambuc 	for (p = list; p; p = p->next)
69*0a6a1f1dSLionel Sambuc 	{
70*0a6a1f1dSLionel Sambuc 	    size_t len_type = strlen(p->type);
71*0a6a1f1dSLionel Sambuc 	    fprintf(fp, "%s%s%s%s%s", p->type,
72*0a6a1f1dSLionel Sambuc 		    (((len_type != 0) && (p->type[len_type - 1] == '*'))
73*0a6a1f1dSLionel Sambuc 		     ? ""
74*0a6a1f1dSLionel Sambuc 		     : " "),
75*0a6a1f1dSLionel Sambuc 		    p->name, p->type2,
76*0a6a1f1dSLionel Sambuc 		    ((more || p->next) ? ", " : ""));
77*0a6a1f1dSLionel Sambuc 	}
78*0a6a1f1dSLionel Sambuc     }
79*0a6a1f1dSLionel Sambuc     else
80*0a6a1f1dSLionel Sambuc     {
81*0a6a1f1dSLionel Sambuc 	if (!more)
82*0a6a1f1dSLionel Sambuc 	    fprintf(fp, "void");
83*0a6a1f1dSLionel Sambuc     }
84*0a6a1f1dSLionel Sambuc }
85*0a6a1f1dSLionel Sambuc 
86*0a6a1f1dSLionel Sambuc static void
puts_param_names(FILE * fp,param * list,int more)87*0a6a1f1dSLionel Sambuc puts_param_names(FILE * fp, param * list, int more)
88*0a6a1f1dSLionel Sambuc {
89*0a6a1f1dSLionel Sambuc     param *p;
90*0a6a1f1dSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc     for (p = list; p; p = p->next)
92*0a6a1f1dSLionel Sambuc     {
93*0a6a1f1dSLionel Sambuc 	fprintf(fp, "%s%s", p->name,
94*0a6a1f1dSLionel Sambuc 		((more || p->next) ? ", " : ""));
95*0a6a1f1dSLionel Sambuc     }
96*0a6a1f1dSLionel Sambuc }
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc static void
write_code_lineno(FILE * fp)994a17663cSThomas Veerman write_code_lineno(FILE * fp)
1004a17663cSThomas Veerman {
1014a17663cSThomas Veerman     if (!lflag && (fp == code_file))
1024a17663cSThomas Veerman     {
1034a17663cSThomas Veerman 	++outline;
104*0a6a1f1dSLionel Sambuc 	fprintf(fp, line_format, outline + 1, code_file_name);
1054a17663cSThomas Veerman     }
1064a17663cSThomas Veerman }
1074a17663cSThomas Veerman 
1084a17663cSThomas Veerman static void
write_input_lineno(void)1094a17663cSThomas Veerman write_input_lineno(void)
1104a17663cSThomas Veerman {
1114a17663cSThomas Veerman     if (!lflag)
1124a17663cSThomas Veerman     {
1134a17663cSThomas Veerman 	++outline;
1144a17663cSThomas Veerman 	fprintf(code_file, line_format, lineno, input_file_name);
1154a17663cSThomas Veerman     }
1164a17663cSThomas Veerman }
1174a17663cSThomas Veerman 
1184a17663cSThomas Veerman static void
define_prefixed(FILE * fp,const char * name)1194a17663cSThomas Veerman define_prefixed(FILE * fp, const char *name)
1204a17663cSThomas Veerman {
1214a17663cSThomas Veerman     int bump_line = CountLine(fp);
1224a17663cSThomas Veerman     if (bump_line)
1234a17663cSThomas Veerman 	++outline;
1244a17663cSThomas Veerman     fprintf(fp, "\n");
1254a17663cSThomas Veerman 
1264a17663cSThomas Veerman     if (bump_line)
1274a17663cSThomas Veerman 	++outline;
1284a17663cSThomas Veerman     fprintf(fp, "#ifndef %s\n", name);
1294a17663cSThomas Veerman 
1304a17663cSThomas Veerman     if (bump_line)
1314a17663cSThomas Veerman 	++outline;
1324a17663cSThomas Veerman     fprintf(fp, "#define %-10s %s%s\n", name, symbol_prefix, name + 2);
1334a17663cSThomas Veerman 
1344a17663cSThomas Veerman     if (bump_line)
1354a17663cSThomas Veerman 	++outline;
1364a17663cSThomas Veerman     fprintf(fp, "#endif /* %s */\n", name);
1374a17663cSThomas Veerman }
1384a17663cSThomas Veerman 
1394a17663cSThomas Veerman static void
output_prefix(FILE * fp)1404a17663cSThomas Veerman output_prefix(FILE * fp)
1414a17663cSThomas Veerman {
1424a17663cSThomas Veerman     if (symbol_prefix == NULL)
1434a17663cSThomas Veerman     {
1444a17663cSThomas Veerman 	symbol_prefix = "yy";
1454a17663cSThomas Veerman     }
1464a17663cSThomas Veerman     else
1474a17663cSThomas Veerman     {
1484a17663cSThomas Veerman 	define_prefixed(fp, "yyparse");
1494a17663cSThomas Veerman 	define_prefixed(fp, "yylex");
1504a17663cSThomas Veerman 	define_prefixed(fp, "yyerror");
1514a17663cSThomas Veerman 	define_prefixed(fp, "yychar");
1524a17663cSThomas Veerman 	define_prefixed(fp, "yyval");
1534a17663cSThomas Veerman 	define_prefixed(fp, "yylval");
1544a17663cSThomas Veerman 	define_prefixed(fp, "yydebug");
1554a17663cSThomas Veerman 	define_prefixed(fp, "yynerrs");
1564a17663cSThomas Veerman 	define_prefixed(fp, "yyerrflag");
1574a17663cSThomas Veerman 	define_prefixed(fp, "yylhs");
1584a17663cSThomas Veerman 	define_prefixed(fp, "yylen");
1594a17663cSThomas Veerman 	define_prefixed(fp, "yydefred");
160*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
161*0a6a1f1dSLionel Sambuc 	define_prefixed(fp, "yystos");
162*0a6a1f1dSLionel Sambuc #endif
1634a17663cSThomas Veerman 	define_prefixed(fp, "yydgoto");
1644a17663cSThomas Veerman 	define_prefixed(fp, "yysindex");
1654a17663cSThomas Veerman 	define_prefixed(fp, "yyrindex");
1664a17663cSThomas Veerman 	define_prefixed(fp, "yygindex");
1674a17663cSThomas Veerman 	define_prefixed(fp, "yytable");
1684a17663cSThomas Veerman 	define_prefixed(fp, "yycheck");
1694a17663cSThomas Veerman 	define_prefixed(fp, "yyname");
1704a17663cSThomas Veerman 	define_prefixed(fp, "yyrule");
171*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
172*0a6a1f1dSLionel Sambuc 	if (locations)
173*0a6a1f1dSLionel Sambuc 	{
174*0a6a1f1dSLionel Sambuc 	    define_prefixed(fp, "yyloc");
175*0a6a1f1dSLionel Sambuc 	    define_prefixed(fp, "yylloc");
176*0a6a1f1dSLionel Sambuc 	}
177*0a6a1f1dSLionel Sambuc 	putc_code(fp, '\n');
178*0a6a1f1dSLionel Sambuc 	putl_code(fp, "#if YYBTYACC\n");
179*0a6a1f1dSLionel Sambuc 
180*0a6a1f1dSLionel Sambuc 	define_prefixed(fp, "yycindex");
181*0a6a1f1dSLionel Sambuc 	define_prefixed(fp, "yyctable");
182*0a6a1f1dSLionel Sambuc 
183*0a6a1f1dSLionel Sambuc 	putc_code(fp, '\n');
184*0a6a1f1dSLionel Sambuc 	putl_code(fp, "#endif /* YYBTYACC */\n");
185*0a6a1f1dSLionel Sambuc 	putc_code(fp, '\n');
186*0a6a1f1dSLionel Sambuc #endif
1874a17663cSThomas Veerman     }
1884a17663cSThomas Veerman     if (CountLine(fp))
1894a17663cSThomas Veerman 	++outline;
1904a17663cSThomas Veerman     fprintf(fp, "#define YYPREFIX \"%s\"\n", symbol_prefix);
1914a17663cSThomas Veerman }
1924a17663cSThomas Veerman 
1934a17663cSThomas Veerman static void
output_newline(void)1944a17663cSThomas Veerman output_newline(void)
1954a17663cSThomas Veerman {
1964a17663cSThomas Veerman     if (!rflag)
1974a17663cSThomas Veerman 	++outline;
1984a17663cSThomas Veerman     putc('\n', output_file);
1994a17663cSThomas Veerman }
2004a17663cSThomas Veerman 
2014a17663cSThomas Veerman static void
output_line(const char * value)2024a17663cSThomas Veerman output_line(const char *value)
2034a17663cSThomas Veerman {
2044a17663cSThomas Veerman     fputs(value, output_file);
2054a17663cSThomas Veerman     output_newline();
2064a17663cSThomas Veerman }
2074a17663cSThomas Veerman 
2084a17663cSThomas Veerman static void
output_int(int value)2094a17663cSThomas Veerman output_int(int value)
2104a17663cSThomas Veerman {
2114a17663cSThomas Veerman     fprintf(output_file, "%5d,", value);
2124a17663cSThomas Veerman }
2134a17663cSThomas Veerman 
2144a17663cSThomas Veerman static void
start_int_table(const char * name,int value)2154a17663cSThomas Veerman start_int_table(const char *name, int value)
2164a17663cSThomas Veerman {
2174a17663cSThomas Veerman     int need = 34 - (int)(strlen(symbol_prefix) + strlen(name));
2184a17663cSThomas Veerman 
2194a17663cSThomas Veerman     if (need < 6)
2204a17663cSThomas Veerman 	need = 6;
2214a17663cSThomas Veerman     fprintf(output_file,
222*0a6a1f1dSLionel Sambuc 	    "%sconst YYINT %s%s[] = {%*d,",
2234a17663cSThomas Veerman 	    StaticOrR, symbol_prefix, name, need, value);
2244a17663cSThomas Veerman }
2254a17663cSThomas Veerman 
2264a17663cSThomas Veerman static void
start_str_table(const char * name)2274a17663cSThomas Veerman start_str_table(const char *name)
2284a17663cSThomas Veerman {
2294a17663cSThomas Veerman     fprintf(output_file,
230*0a6a1f1dSLionel Sambuc 	    "%sconst char *const %s%s[] = {",
231*0a6a1f1dSLionel Sambuc 	    StaticOrR, symbol_prefix, name);
2324a17663cSThomas Veerman     output_newline();
2334a17663cSThomas Veerman }
2344a17663cSThomas Veerman 
2354a17663cSThomas Veerman static void
end_table(void)2364a17663cSThomas Veerman end_table(void)
2374a17663cSThomas Veerman {
2384a17663cSThomas Veerman     output_newline();
2394a17663cSThomas Veerman     output_line("};");
2404a17663cSThomas Veerman }
2414a17663cSThomas Veerman 
2424a17663cSThomas Veerman static void
output_YYINT_typedef(FILE * fp)243*0a6a1f1dSLionel Sambuc output_YYINT_typedef(FILE * fp)
244*0a6a1f1dSLionel Sambuc {
245*0a6a1f1dSLionel Sambuc     /* generate the type used to index the various parser tables */
246*0a6a1f1dSLionel Sambuc     if (CountLine(fp))
247*0a6a1f1dSLionel Sambuc 	++outline;
248*0a6a1f1dSLionel Sambuc     fprintf(fp, "typedef %s YYINT;\n", CONCAT1("", YYINT));
249*0a6a1f1dSLionel Sambuc }
250*0a6a1f1dSLionel Sambuc 
251*0a6a1f1dSLionel Sambuc static void
output_rule_data(void)2524a17663cSThomas Veerman output_rule_data(void)
2534a17663cSThomas Veerman {
2544a17663cSThomas Veerman     int i;
2554a17663cSThomas Veerman     int j;
2564a17663cSThomas Veerman 
257*0a6a1f1dSLionel Sambuc     output_YYINT_typedef(output_file);
258*0a6a1f1dSLionel Sambuc 
2594a17663cSThomas Veerman     start_int_table("lhs", symbol_value[start_symbol]);
2604a17663cSThomas Veerman 
2614a17663cSThomas Veerman     j = 10;
2624a17663cSThomas Veerman     for (i = 3; i < nrules; i++)
2634a17663cSThomas Veerman     {
2644a17663cSThomas Veerman 	if (j >= 10)
2654a17663cSThomas Veerman 	{
2664a17663cSThomas Veerman 	    output_newline();
2674a17663cSThomas Veerman 	    j = 1;
2684a17663cSThomas Veerman 	}
2694a17663cSThomas Veerman 	else
2704a17663cSThomas Veerman 	    ++j;
2714a17663cSThomas Veerman 
2724a17663cSThomas Veerman 	output_int(symbol_value[rlhs[i]]);
2734a17663cSThomas Veerman     }
2744a17663cSThomas Veerman     end_table();
2754a17663cSThomas Veerman 
2764a17663cSThomas Veerman     start_int_table("len", 2);
2774a17663cSThomas Veerman 
2784a17663cSThomas Veerman     j = 10;
2794a17663cSThomas Veerman     for (i = 3; i < nrules; i++)
2804a17663cSThomas Veerman     {
2814a17663cSThomas Veerman 	if (j >= 10)
2824a17663cSThomas Veerman 	{
2834a17663cSThomas Veerman 	    output_newline();
2844a17663cSThomas Veerman 	    j = 1;
2854a17663cSThomas Veerman 	}
2864a17663cSThomas Veerman 	else
2874a17663cSThomas Veerman 	    j++;
2884a17663cSThomas Veerman 
2894a17663cSThomas Veerman 	output_int(rrhs[i + 1] - rrhs[i] - 1);
2904a17663cSThomas Veerman     }
2914a17663cSThomas Veerman     end_table();
2924a17663cSThomas Veerman }
2934a17663cSThomas Veerman 
2944a17663cSThomas Veerman static void
output_yydefred(void)2954a17663cSThomas Veerman output_yydefred(void)
2964a17663cSThomas Veerman {
2974a17663cSThomas Veerman     int i, j;
2984a17663cSThomas Veerman 
2994a17663cSThomas Veerman     start_int_table("defred", (defred[0] ? defred[0] - 2 : 0));
3004a17663cSThomas Veerman 
3014a17663cSThomas Veerman     j = 10;
3024a17663cSThomas Veerman     for (i = 1; i < nstates; i++)
3034a17663cSThomas Veerman     {
3044a17663cSThomas Veerman 	if (j < 10)
3054a17663cSThomas Veerman 	    ++j;
3064a17663cSThomas Veerman 	else
3074a17663cSThomas Veerman 	{
3084a17663cSThomas Veerman 	    output_newline();
3094a17663cSThomas Veerman 	    j = 1;
3104a17663cSThomas Veerman 	}
3114a17663cSThomas Veerman 
3124a17663cSThomas Veerman 	output_int((defred[i] ? defred[i] - 2 : 0));
3134a17663cSThomas Veerman     }
3144a17663cSThomas Veerman 
3154a17663cSThomas Veerman     end_table();
3164a17663cSThomas Veerman }
3174a17663cSThomas Veerman 
318*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
319*0a6a1f1dSLionel Sambuc static void
output_accessing_symbols(void)320*0a6a1f1dSLionel Sambuc output_accessing_symbols(void)
321*0a6a1f1dSLionel Sambuc {
322*0a6a1f1dSLionel Sambuc     int i, j;
323*0a6a1f1dSLionel Sambuc     int *translate;
324*0a6a1f1dSLionel Sambuc 
325*0a6a1f1dSLionel Sambuc     if (nstates != 0)
326*0a6a1f1dSLionel Sambuc     {
327*0a6a1f1dSLionel Sambuc 	translate = TMALLOC(int, nstates);
328*0a6a1f1dSLionel Sambuc 	NO_SPACE(translate);
329*0a6a1f1dSLionel Sambuc 
330*0a6a1f1dSLionel Sambuc 	for (i = 0; i < nstates; ++i)
331*0a6a1f1dSLionel Sambuc 	{
332*0a6a1f1dSLionel Sambuc 	    int gsymb = accessing_symbol[i];
333*0a6a1f1dSLionel Sambuc 
334*0a6a1f1dSLionel Sambuc 	    translate[i] = symbol_pval[gsymb];
335*0a6a1f1dSLionel Sambuc 	}
336*0a6a1f1dSLionel Sambuc 
337*0a6a1f1dSLionel Sambuc 	putl_code(output_file,
338*0a6a1f1dSLionel Sambuc 	    "#if defined(YYDESTRUCT_CALL) || defined(YYSTYPE_TOSTRING)\n");
339*0a6a1f1dSLionel Sambuc 	/* yystos[] may be unused, depending on compile-time defines */
340*0a6a1f1dSLionel Sambuc 	start_int_table("stos", translate[0]);
341*0a6a1f1dSLionel Sambuc 
342*0a6a1f1dSLionel Sambuc 	j = 10;
343*0a6a1f1dSLionel Sambuc 	for (i = 1; i < nstates; ++i)
344*0a6a1f1dSLionel Sambuc 	{
345*0a6a1f1dSLionel Sambuc 	    if (j < 10)
346*0a6a1f1dSLionel Sambuc 		++j;
347*0a6a1f1dSLionel Sambuc 	    else
348*0a6a1f1dSLionel Sambuc 	    {
349*0a6a1f1dSLionel Sambuc 		output_newline();
350*0a6a1f1dSLionel Sambuc 		j = 1;
351*0a6a1f1dSLionel Sambuc 	    }
352*0a6a1f1dSLionel Sambuc 
353*0a6a1f1dSLionel Sambuc 	    output_int(translate[i]);
354*0a6a1f1dSLionel Sambuc 	}
355*0a6a1f1dSLionel Sambuc 
356*0a6a1f1dSLionel Sambuc 	end_table();
357*0a6a1f1dSLionel Sambuc 	FREE(translate);
358*0a6a1f1dSLionel Sambuc 	putl_code(output_file,
359*0a6a1f1dSLionel Sambuc 	    "#endif /* YYDESTRUCT_CALL || YYSTYPE_TOSTRING */\n");
360*0a6a1f1dSLionel Sambuc     }
361*0a6a1f1dSLionel Sambuc }
362*0a6a1f1dSLionel Sambuc 
363*0a6a1f1dSLionel Sambuc static Value_t
find_conflict_base(int cbase)364*0a6a1f1dSLionel Sambuc find_conflict_base(int cbase)
365*0a6a1f1dSLionel Sambuc {
366*0a6a1f1dSLionel Sambuc     int i, j;
367*0a6a1f1dSLionel Sambuc 
368*0a6a1f1dSLionel Sambuc     for (i = 0; i < cbase; i++)
369*0a6a1f1dSLionel Sambuc     {
370*0a6a1f1dSLionel Sambuc 	for (j = 0; j + cbase < nconflicts; j++)
371*0a6a1f1dSLionel Sambuc 	{
372*0a6a1f1dSLionel Sambuc 	    if (conflicts[i + j] != conflicts[cbase + j])
373*0a6a1f1dSLionel Sambuc 		break;
374*0a6a1f1dSLionel Sambuc 	}
375*0a6a1f1dSLionel Sambuc 	if (j + cbase >= nconflicts)
376*0a6a1f1dSLionel Sambuc 	    break;
377*0a6a1f1dSLionel Sambuc     }
378*0a6a1f1dSLionel Sambuc     return (Value_t) i;
379*0a6a1f1dSLionel Sambuc }
380*0a6a1f1dSLionel Sambuc #endif
381*0a6a1f1dSLionel Sambuc 
3824a17663cSThomas Veerman static void
token_actions(void)3834a17663cSThomas Veerman token_actions(void)
3844a17663cSThomas Veerman {
3854a17663cSThomas Veerman     int i, j;
3864a17663cSThomas Veerman     Value_t shiftcount, reducecount;
387*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
388*0a6a1f1dSLionel Sambuc     Value_t conflictcount = 0;
389*0a6a1f1dSLionel Sambuc     Value_t csym = -1;
390*0a6a1f1dSLionel Sambuc     Value_t cbase = 0;
391*0a6a1f1dSLionel Sambuc #endif
3924a17663cSThomas Veerman     int max, min;
3934a17663cSThomas Veerman     Value_t *actionrow, *r, *s;
3944a17663cSThomas Veerman     action *p;
3954a17663cSThomas Veerman 
396*0a6a1f1dSLionel Sambuc     actionrow = NEW2(PER_STATE * ntokens, Value_t);
3974a17663cSThomas Veerman     for (i = 0; i < nstates; ++i)
3984a17663cSThomas Veerman     {
3994a17663cSThomas Veerman 	if (parser[i])
4004a17663cSThomas Veerman 	{
401*0a6a1f1dSLionel Sambuc 	    for (j = 0; j < PER_STATE * ntokens; ++j)
4024a17663cSThomas Veerman 		actionrow[j] = 0;
4034a17663cSThomas Veerman 
4044a17663cSThomas Veerman 	    shiftcount = 0;
4054a17663cSThomas Veerman 	    reducecount = 0;
406*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
407*0a6a1f1dSLionel Sambuc 	    if (backtrack)
408*0a6a1f1dSLionel Sambuc 	    {
409*0a6a1f1dSLionel Sambuc 		conflictcount = 0;
410*0a6a1f1dSLionel Sambuc 		csym = -1;
411*0a6a1f1dSLionel Sambuc 		cbase = nconflicts;
412*0a6a1f1dSLionel Sambuc 	    }
413*0a6a1f1dSLionel Sambuc #endif
4144a17663cSThomas Veerman 	    for (p = parser[i]; p; p = p->next)
4154a17663cSThomas Veerman 	    {
416*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
417*0a6a1f1dSLionel Sambuc 		if (backtrack)
418*0a6a1f1dSLionel Sambuc 		{
419*0a6a1f1dSLionel Sambuc 		    if (csym != -1 && csym != p->symbol)
420*0a6a1f1dSLionel Sambuc 		    {
421*0a6a1f1dSLionel Sambuc 			conflictcount++;
422*0a6a1f1dSLionel Sambuc 			conflicts[nconflicts++] = -1;
423*0a6a1f1dSLionel Sambuc 			j = find_conflict_base(cbase);
424*0a6a1f1dSLionel Sambuc 			actionrow[csym + 2 * ntokens] = (Value_t) (j + 1);
425*0a6a1f1dSLionel Sambuc 			if (j == cbase)
426*0a6a1f1dSLionel Sambuc 			{
427*0a6a1f1dSLionel Sambuc 			    cbase = nconflicts;
428*0a6a1f1dSLionel Sambuc 			}
429*0a6a1f1dSLionel Sambuc 			else
430*0a6a1f1dSLionel Sambuc 			{
431*0a6a1f1dSLionel Sambuc 			    if (conflicts[cbase] == -1)
432*0a6a1f1dSLionel Sambuc 				cbase++;
433*0a6a1f1dSLionel Sambuc 			    nconflicts = cbase;
434*0a6a1f1dSLionel Sambuc 			}
435*0a6a1f1dSLionel Sambuc 			csym = -1;
436*0a6a1f1dSLionel Sambuc 		    }
437*0a6a1f1dSLionel Sambuc 		}
438*0a6a1f1dSLionel Sambuc #endif
4394a17663cSThomas Veerman 		if (p->suppressed == 0)
4404a17663cSThomas Veerman 		{
4414a17663cSThomas Veerman 		    if (p->action_code == SHIFT)
4424a17663cSThomas Veerman 		    {
4434a17663cSThomas Veerman 			++shiftcount;
4444a17663cSThomas Veerman 			actionrow[p->symbol] = p->number;
4454a17663cSThomas Veerman 		    }
4464a17663cSThomas Veerman 		    else if (p->action_code == REDUCE && p->number != defred[i])
4474a17663cSThomas Veerman 		    {
4484a17663cSThomas Veerman 			++reducecount;
4494a17663cSThomas Veerman 			actionrow[p->symbol + ntokens] = p->number;
4504a17663cSThomas Veerman 		    }
4514a17663cSThomas Veerman 		}
452*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
453*0a6a1f1dSLionel Sambuc 		else if (backtrack && p->suppressed == 1)
454*0a6a1f1dSLionel Sambuc 		{
455*0a6a1f1dSLionel Sambuc 		    csym = p->symbol;
456*0a6a1f1dSLionel Sambuc 		    if (p->action_code == SHIFT)
457*0a6a1f1dSLionel Sambuc 		    {
458*0a6a1f1dSLionel Sambuc 			conflicts[nconflicts++] = p->number;
4594a17663cSThomas Veerman 		    }
460*0a6a1f1dSLionel Sambuc 		    else if (p->action_code == REDUCE && p->number != defred[i])
461*0a6a1f1dSLionel Sambuc 		    {
462*0a6a1f1dSLionel Sambuc 			if (cbase == nconflicts)
463*0a6a1f1dSLionel Sambuc 			{
464*0a6a1f1dSLionel Sambuc 			    if (cbase)
465*0a6a1f1dSLionel Sambuc 				cbase--;
466*0a6a1f1dSLionel Sambuc 			    else
467*0a6a1f1dSLionel Sambuc 				conflicts[nconflicts++] = -1;
468*0a6a1f1dSLionel Sambuc 			}
469*0a6a1f1dSLionel Sambuc 			conflicts[nconflicts++] = (Value_t) (p->number - 2);
470*0a6a1f1dSLionel Sambuc 		    }
471*0a6a1f1dSLionel Sambuc 		}
472*0a6a1f1dSLionel Sambuc #endif
473*0a6a1f1dSLionel Sambuc 	    }
474*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
475*0a6a1f1dSLionel Sambuc 	    if (backtrack && csym != -1)
476*0a6a1f1dSLionel Sambuc 	    {
477*0a6a1f1dSLionel Sambuc 		conflictcount++;
478*0a6a1f1dSLionel Sambuc 		conflicts[nconflicts++] = -1;
479*0a6a1f1dSLionel Sambuc 		j = find_conflict_base(cbase);
480*0a6a1f1dSLionel Sambuc 		actionrow[csym + 2 * ntokens] = (Value_t) (j + 1);
481*0a6a1f1dSLionel Sambuc 		if (j == cbase)
482*0a6a1f1dSLionel Sambuc 		{
483*0a6a1f1dSLionel Sambuc 		    cbase = nconflicts;
484*0a6a1f1dSLionel Sambuc 		}
485*0a6a1f1dSLionel Sambuc 		else
486*0a6a1f1dSLionel Sambuc 		{
487*0a6a1f1dSLionel Sambuc 		    if (conflicts[cbase] == -1)
488*0a6a1f1dSLionel Sambuc 			cbase++;
489*0a6a1f1dSLionel Sambuc 		    nconflicts = cbase;
490*0a6a1f1dSLionel Sambuc 		}
491*0a6a1f1dSLionel Sambuc 	    }
492*0a6a1f1dSLionel Sambuc #endif
4934a17663cSThomas Veerman 
4944a17663cSThomas Veerman 	    tally[i] = shiftcount;
4954a17663cSThomas Veerman 	    tally[nstates + i] = reducecount;
496*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
497*0a6a1f1dSLionel Sambuc 	    if (backtrack)
498*0a6a1f1dSLionel Sambuc 		tally[2 * nstates + i] = conflictcount;
499*0a6a1f1dSLionel Sambuc #endif
5004a17663cSThomas Veerman 	    width[i] = 0;
5014a17663cSThomas Veerman 	    width[nstates + i] = 0;
502*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
503*0a6a1f1dSLionel Sambuc 	    if (backtrack)
504*0a6a1f1dSLionel Sambuc 		width[2 * nstates + i] = 0;
505*0a6a1f1dSLionel Sambuc #endif
5064a17663cSThomas Veerman 	    if (shiftcount > 0)
5074a17663cSThomas Veerman 	    {
5084a17663cSThomas Veerman 		froms[i] = r = NEW2(shiftcount, Value_t);
5094a17663cSThomas Veerman 		tos[i] = s = NEW2(shiftcount, Value_t);
510*0a6a1f1dSLionel Sambuc 		min = MAXYYINT;
5114a17663cSThomas Veerman 		max = 0;
5124a17663cSThomas Veerman 		for (j = 0; j < ntokens; ++j)
5134a17663cSThomas Veerman 		{
5144a17663cSThomas Veerman 		    if (actionrow[j])
5154a17663cSThomas Veerman 		    {
5164a17663cSThomas Veerman 			if (min > symbol_value[j])
5174a17663cSThomas Veerman 			    min = symbol_value[j];
5184a17663cSThomas Veerman 			if (max < symbol_value[j])
5194a17663cSThomas Veerman 			    max = symbol_value[j];
5204a17663cSThomas Veerman 			*r++ = symbol_value[j];
5214a17663cSThomas Veerman 			*s++ = actionrow[j];
5224a17663cSThomas Veerman 		    }
5234a17663cSThomas Veerman 		}
5244a17663cSThomas Veerman 		width[i] = (Value_t) (max - min + 1);
5254a17663cSThomas Veerman 	    }
5264a17663cSThomas Veerman 	    if (reducecount > 0)
5274a17663cSThomas Veerman 	    {
5284a17663cSThomas Veerman 		froms[nstates + i] = r = NEW2(reducecount, Value_t);
5294a17663cSThomas Veerman 		tos[nstates + i] = s = NEW2(reducecount, Value_t);
530*0a6a1f1dSLionel Sambuc 		min = MAXYYINT;
5314a17663cSThomas Veerman 		max = 0;
5324a17663cSThomas Veerman 		for (j = 0; j < ntokens; ++j)
5334a17663cSThomas Veerman 		{
5344a17663cSThomas Veerman 		    if (actionrow[ntokens + j])
5354a17663cSThomas Veerman 		    {
5364a17663cSThomas Veerman 			if (min > symbol_value[j])
5374a17663cSThomas Veerman 			    min = symbol_value[j];
5384a17663cSThomas Veerman 			if (max < symbol_value[j])
5394a17663cSThomas Veerman 			    max = symbol_value[j];
5404a17663cSThomas Veerman 			*r++ = symbol_value[j];
5414a17663cSThomas Veerman 			*s++ = (Value_t) (actionrow[ntokens + j] - 2);
5424a17663cSThomas Veerman 		    }
5434a17663cSThomas Veerman 		}
5444a17663cSThomas Veerman 		width[nstates + i] = (Value_t) (max - min + 1);
5454a17663cSThomas Veerman 	    }
546*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
547*0a6a1f1dSLionel Sambuc 	    if (backtrack && conflictcount > 0)
548*0a6a1f1dSLionel Sambuc 	    {
549*0a6a1f1dSLionel Sambuc 		froms[2 * nstates + i] = r = NEW2(conflictcount, Value_t);
550*0a6a1f1dSLionel Sambuc 		tos[2 * nstates + i] = s = NEW2(conflictcount, Value_t);
551*0a6a1f1dSLionel Sambuc 		min = MAXYYINT;
552*0a6a1f1dSLionel Sambuc 		max = 0;
553*0a6a1f1dSLionel Sambuc 		for (j = 0; j < ntokens; ++j)
554*0a6a1f1dSLionel Sambuc 		{
555*0a6a1f1dSLionel Sambuc 		    if (actionrow[2 * ntokens + j])
556*0a6a1f1dSLionel Sambuc 		    {
557*0a6a1f1dSLionel Sambuc 			if (min > symbol_value[j])
558*0a6a1f1dSLionel Sambuc 			    min = symbol_value[j];
559*0a6a1f1dSLionel Sambuc 			if (max < symbol_value[j])
560*0a6a1f1dSLionel Sambuc 			    max = symbol_value[j];
561*0a6a1f1dSLionel Sambuc 			*r++ = symbol_value[j];
562*0a6a1f1dSLionel Sambuc 			*s++ = (Value_t) (actionrow[2 * ntokens + j] - 1);
563*0a6a1f1dSLionel Sambuc 		    }
564*0a6a1f1dSLionel Sambuc 		}
565*0a6a1f1dSLionel Sambuc 		width[2 * nstates + i] = (Value_t) (max - min + 1);
566*0a6a1f1dSLionel Sambuc 	    }
567*0a6a1f1dSLionel Sambuc #endif
5684a17663cSThomas Veerman 	}
5694a17663cSThomas Veerman     }
5704a17663cSThomas Veerman     FREE(actionrow);
5714a17663cSThomas Veerman }
5724a17663cSThomas Veerman 
5734a17663cSThomas Veerman static int
default_goto(int symbol)5744a17663cSThomas Veerman default_goto(int symbol)
5754a17663cSThomas Veerman {
5764a17663cSThomas Veerman     int i;
5774a17663cSThomas Veerman     int m;
5784a17663cSThomas Veerman     int n;
5794a17663cSThomas Veerman     int default_state;
5804a17663cSThomas Veerman     int max;
5814a17663cSThomas Veerman 
5824a17663cSThomas Veerman     m = goto_map[symbol];
5834a17663cSThomas Veerman     n = goto_map[symbol + 1];
5844a17663cSThomas Veerman 
5854a17663cSThomas Veerman     if (m == n)
5864a17663cSThomas Veerman 	return (0);
5874a17663cSThomas Veerman 
5884a17663cSThomas Veerman     for (i = 0; i < nstates; i++)
5894a17663cSThomas Veerman 	state_count[i] = 0;
5904a17663cSThomas Veerman 
5914a17663cSThomas Veerman     for (i = m; i < n; i++)
5924a17663cSThomas Veerman 	state_count[to_state[i]]++;
5934a17663cSThomas Veerman 
5944a17663cSThomas Veerman     max = 0;
5954a17663cSThomas Veerman     default_state = 0;
5964a17663cSThomas Veerman     for (i = 0; i < nstates; i++)
5974a17663cSThomas Veerman     {
5984a17663cSThomas Veerman 	if (state_count[i] > max)
5994a17663cSThomas Veerman 	{
6004a17663cSThomas Veerman 	    max = state_count[i];
6014a17663cSThomas Veerman 	    default_state = i;
6024a17663cSThomas Veerman 	}
6034a17663cSThomas Veerman     }
6044a17663cSThomas Veerman 
6054a17663cSThomas Veerman     return (default_state);
6064a17663cSThomas Veerman }
6074a17663cSThomas Veerman 
6084a17663cSThomas Veerman static void
save_column(int symbol,int default_state)6094a17663cSThomas Veerman save_column(int symbol, int default_state)
6104a17663cSThomas Veerman {
6114a17663cSThomas Veerman     int i;
6124a17663cSThomas Veerman     int m;
6134a17663cSThomas Veerman     int n;
6144a17663cSThomas Veerman     Value_t *sp;
6154a17663cSThomas Veerman     Value_t *sp1;
6164a17663cSThomas Veerman     Value_t *sp2;
6174a17663cSThomas Veerman     Value_t count;
6184a17663cSThomas Veerman     int symno;
6194a17663cSThomas Veerman 
6204a17663cSThomas Veerman     m = goto_map[symbol];
6214a17663cSThomas Veerman     n = goto_map[symbol + 1];
6224a17663cSThomas Veerman 
6234a17663cSThomas Veerman     count = 0;
6244a17663cSThomas Veerman     for (i = m; i < n; i++)
6254a17663cSThomas Veerman     {
6264a17663cSThomas Veerman 	if (to_state[i] != default_state)
6274a17663cSThomas Veerman 	    ++count;
6284a17663cSThomas Veerman     }
6294a17663cSThomas Veerman     if (count == 0)
6304a17663cSThomas Veerman 	return;
6314a17663cSThomas Veerman 
632*0a6a1f1dSLionel Sambuc     symno = symbol_value[symbol] + PER_STATE * nstates;
6334a17663cSThomas Veerman 
6344a17663cSThomas Veerman     froms[symno] = sp1 = sp = NEW2(count, Value_t);
6354a17663cSThomas Veerman     tos[symno] = sp2 = NEW2(count, Value_t);
6364a17663cSThomas Veerman 
6374a17663cSThomas Veerman     for (i = m; i < n; i++)
6384a17663cSThomas Veerman     {
6394a17663cSThomas Veerman 	if (to_state[i] != default_state)
6404a17663cSThomas Veerman 	{
6414a17663cSThomas Veerman 	    *sp1++ = from_state[i];
6424a17663cSThomas Veerman 	    *sp2++ = to_state[i];
6434a17663cSThomas Veerman 	}
6444a17663cSThomas Veerman     }
6454a17663cSThomas Veerman 
6464a17663cSThomas Veerman     tally[symno] = count;
6474a17663cSThomas Veerman     width[symno] = (Value_t) (sp1[-1] - sp[0] + 1);
6484a17663cSThomas Veerman }
6494a17663cSThomas Veerman 
6504a17663cSThomas Veerman static void
goto_actions(void)6514a17663cSThomas Veerman goto_actions(void)
6524a17663cSThomas Veerman {
6534a17663cSThomas Veerman     int i, j, k;
6544a17663cSThomas Veerman 
6554a17663cSThomas Veerman     state_count = NEW2(nstates, Value_t);
6564a17663cSThomas Veerman 
6574a17663cSThomas Veerman     k = default_goto(start_symbol + 1);
6584a17663cSThomas Veerman     start_int_table("dgoto", k);
6594a17663cSThomas Veerman     save_column(start_symbol + 1, k);
6604a17663cSThomas Veerman 
6614a17663cSThomas Veerman     j = 10;
6624a17663cSThomas Veerman     for (i = start_symbol + 2; i < nsyms; i++)
6634a17663cSThomas Veerman     {
6644a17663cSThomas Veerman 	if (j >= 10)
6654a17663cSThomas Veerman 	{
6664a17663cSThomas Veerman 	    output_newline();
6674a17663cSThomas Veerman 	    j = 1;
6684a17663cSThomas Veerman 	}
6694a17663cSThomas Veerman 	else
6704a17663cSThomas Veerman 	    ++j;
6714a17663cSThomas Veerman 
6724a17663cSThomas Veerman 	k = default_goto(i);
6734a17663cSThomas Veerman 	output_int(k);
6744a17663cSThomas Veerman 	save_column(i, k);
6754a17663cSThomas Veerman     }
6764a17663cSThomas Veerman 
6774a17663cSThomas Veerman     end_table();
6784a17663cSThomas Veerman     FREE(state_count);
6794a17663cSThomas Veerman }
6804a17663cSThomas Veerman 
6814a17663cSThomas Veerman static void
sort_actions(void)6824a17663cSThomas Veerman sort_actions(void)
6834a17663cSThomas Veerman {
6844a17663cSThomas Veerman     Value_t i;
6854a17663cSThomas Veerman     int j;
6864a17663cSThomas Veerman     int k;
6874a17663cSThomas Veerman     int t;
6884a17663cSThomas Veerman     int w;
6894a17663cSThomas Veerman 
6904a17663cSThomas Veerman     order = NEW2(nvectors, Value_t);
6914a17663cSThomas Veerman     nentries = 0;
6924a17663cSThomas Veerman 
6934a17663cSThomas Veerman     for (i = 0; i < nvectors; i++)
6944a17663cSThomas Veerman     {
6954a17663cSThomas Veerman 	if (tally[i] > 0)
6964a17663cSThomas Veerman 	{
6974a17663cSThomas Veerman 	    t = tally[i];
6984a17663cSThomas Veerman 	    w = width[i];
6994a17663cSThomas Veerman 	    j = nentries - 1;
7004a17663cSThomas Veerman 
7014a17663cSThomas Veerman 	    while (j >= 0 && (width[order[j]] < w))
7024a17663cSThomas Veerman 		j--;
7034a17663cSThomas Veerman 
7044a17663cSThomas Veerman 	    while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
7054a17663cSThomas Veerman 		j--;
7064a17663cSThomas Veerman 
7074a17663cSThomas Veerman 	    for (k = nentries - 1; k > j; k--)
7084a17663cSThomas Veerman 		order[k + 1] = order[k];
7094a17663cSThomas Veerman 
7104a17663cSThomas Veerman 	    order[j + 1] = i;
7114a17663cSThomas Veerman 	    nentries++;
7124a17663cSThomas Veerman 	}
7134a17663cSThomas Veerman     }
7144a17663cSThomas Veerman }
7154a17663cSThomas Veerman 
7164a17663cSThomas Veerman /*  The function matching_vector determines if the vector specified by	*/
7174a17663cSThomas Veerman /*  the input parameter matches a previously considered	vector.  The	*/
7184a17663cSThomas Veerman /*  test at the start of the function checks if the vector represents	*/
7194a17663cSThomas Veerman /*  a row of shifts over terminal symbols or a row of reductions, or a	*/
7204a17663cSThomas Veerman /*  column of shifts over a nonterminal symbol.  Berkeley Yacc does not	*/
7214a17663cSThomas Veerman /*  check if a column of shifts over a nonterminal symbols matches a	*/
7224a17663cSThomas Veerman /*  previously considered vector.  Because of the nature of LR parsing	*/
7234a17663cSThomas Veerman /*  tables, no two columns can match.  Therefore, the only possible	*/
7244a17663cSThomas Veerman /*  match would be between a row and a column.  Such matches are	*/
7254a17663cSThomas Veerman /*  unlikely.  Therefore, to save time, no attempt is made to see if a	*/
7264a17663cSThomas Veerman /*  column matches a previously considered vector.			*/
7274a17663cSThomas Veerman /*									*/
7284a17663cSThomas Veerman /*  Matching_vector is poorly designed.  The test could easily be made	*/
7294a17663cSThomas Veerman /*  faster.  Also, it depends on the vectors being in a specific	*/
7304a17663cSThomas Veerman /*  order.								*/
731*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
732*0a6a1f1dSLionel Sambuc /*									*/
733*0a6a1f1dSLionel Sambuc /*  Not really any point in checking for matching conflicts -- it is    */
734*0a6a1f1dSLionel Sambuc /*  extremely unlikely to occur, and conflicts are (hopefully) rare.    */
735*0a6a1f1dSLionel Sambuc #endif
7364a17663cSThomas Veerman 
7374a17663cSThomas Veerman static int
matching_vector(int vector)7384a17663cSThomas Veerman matching_vector(int vector)
7394a17663cSThomas Veerman {
7404a17663cSThomas Veerman     int i;
7414a17663cSThomas Veerman     int j;
7424a17663cSThomas Veerman     int k;
7434a17663cSThomas Veerman     int t;
7444a17663cSThomas Veerman     int w;
7454a17663cSThomas Veerman     int match;
7464a17663cSThomas Veerman     int prev;
7474a17663cSThomas Veerman 
7484a17663cSThomas Veerman     i = order[vector];
7494a17663cSThomas Veerman     if (i >= 2 * nstates)
7504a17663cSThomas Veerman 	return (-1);
7514a17663cSThomas Veerman 
7524a17663cSThomas Veerman     t = tally[i];
7534a17663cSThomas Veerman     w = width[i];
7544a17663cSThomas Veerman 
7554a17663cSThomas Veerman     for (prev = vector - 1; prev >= 0; prev--)
7564a17663cSThomas Veerman     {
7574a17663cSThomas Veerman 	j = order[prev];
7584a17663cSThomas Veerman 	if (width[j] != w || tally[j] != t)
7594a17663cSThomas Veerman 	    return (-1);
7604a17663cSThomas Veerman 
7614a17663cSThomas Veerman 	match = 1;
7624a17663cSThomas Veerman 	for (k = 0; match && k < t; k++)
7634a17663cSThomas Veerman 	{
7644a17663cSThomas Veerman 	    if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
7654a17663cSThomas Veerman 		match = 0;
7664a17663cSThomas Veerman 	}
7674a17663cSThomas Veerman 
7684a17663cSThomas Veerman 	if (match)
7694a17663cSThomas Veerman 	    return (j);
7704a17663cSThomas Veerman     }
7714a17663cSThomas Veerman 
7724a17663cSThomas Veerman     return (-1);
7734a17663cSThomas Veerman }
7744a17663cSThomas Veerman 
7754a17663cSThomas Veerman static int
pack_vector(int vector)7764a17663cSThomas Veerman pack_vector(int vector)
7774a17663cSThomas Veerman {
7784a17663cSThomas Veerman     int i, j, k, l;
7794a17663cSThomas Veerman     int t;
7804a17663cSThomas Veerman     int loc;
7814a17663cSThomas Veerman     int ok;
7824a17663cSThomas Veerman     Value_t *from;
7834a17663cSThomas Veerman     Value_t *to;
7844a17663cSThomas Veerman     int newmax;
7854a17663cSThomas Veerman 
7864a17663cSThomas Veerman     i = order[vector];
7874a17663cSThomas Veerman     t = tally[i];
7884a17663cSThomas Veerman     assert(t);
7894a17663cSThomas Veerman 
7904a17663cSThomas Veerman     from = froms[i];
7914a17663cSThomas Veerman     to = tos[i];
7924a17663cSThomas Veerman 
7934a17663cSThomas Veerman     j = lowzero - from[0];
7944a17663cSThomas Veerman     for (k = 1; k < t; ++k)
7954a17663cSThomas Veerman 	if (lowzero - from[k] > j)
7964a17663cSThomas Veerman 	    j = lowzero - from[k];
7974a17663cSThomas Veerman     for (;; ++j)
7984a17663cSThomas Veerman     {
7994a17663cSThomas Veerman 	if (j == 0)
8004a17663cSThomas Veerman 	    continue;
8014a17663cSThomas Veerman 	ok = 1;
8024a17663cSThomas Veerman 	for (k = 0; ok && k < t; k++)
8034a17663cSThomas Veerman 	{
8044a17663cSThomas Veerman 	    loc = j + from[k];
8054a17663cSThomas Veerman 	    if (loc >= maxtable - 1)
8064a17663cSThomas Veerman 	    {
8074a17663cSThomas Veerman 		if (loc >= MAXTABLE - 1)
8084a17663cSThomas Veerman 		    fatal("maximum table size exceeded");
8094a17663cSThomas Veerman 
8104a17663cSThomas Veerman 		newmax = maxtable;
8114a17663cSThomas Veerman 		do
8124a17663cSThomas Veerman 		{
8134a17663cSThomas Veerman 		    newmax += 200;
8144a17663cSThomas Veerman 		}
8154a17663cSThomas Veerman 		while (newmax <= loc);
8164a17663cSThomas Veerman 
81784d9c625SLionel Sambuc 		table = TREALLOC(Value_t, table, newmax);
8184a17663cSThomas Veerman 		NO_SPACE(table);
8194a17663cSThomas Veerman 
82084d9c625SLionel Sambuc 		check = TREALLOC(Value_t, check, newmax);
8214a17663cSThomas Veerman 		NO_SPACE(check);
8224a17663cSThomas Veerman 
8234a17663cSThomas Veerman 		for (l = maxtable; l < newmax; ++l)
8244a17663cSThomas Veerman 		{
8254a17663cSThomas Veerman 		    table[l] = 0;
8264a17663cSThomas Veerman 		    check[l] = -1;
8274a17663cSThomas Veerman 		}
8284a17663cSThomas Veerman 		maxtable = newmax;
8294a17663cSThomas Veerman 	    }
8304a17663cSThomas Veerman 
8314a17663cSThomas Veerman 	    if (check[loc] != -1)
8324a17663cSThomas Veerman 		ok = 0;
8334a17663cSThomas Veerman 	}
8344a17663cSThomas Veerman 	for (k = 0; ok && k < vector; k++)
8354a17663cSThomas Veerman 	{
8364a17663cSThomas Veerman 	    if (pos[k] == j)
8374a17663cSThomas Veerman 		ok = 0;
8384a17663cSThomas Veerman 	}
8394a17663cSThomas Veerman 	if (ok)
8404a17663cSThomas Veerman 	{
8414a17663cSThomas Veerman 	    for (k = 0; k < t; k++)
8424a17663cSThomas Veerman 	    {
8434a17663cSThomas Veerman 		loc = j + from[k];
8444a17663cSThomas Veerman 		table[loc] = to[k];
8454a17663cSThomas Veerman 		check[loc] = from[k];
8464a17663cSThomas Veerman 		if (loc > high)
8474a17663cSThomas Veerman 		    high = loc;
8484a17663cSThomas Veerman 	    }
8494a17663cSThomas Veerman 
8504a17663cSThomas Veerman 	    while (check[lowzero] != -1)
8514a17663cSThomas Veerman 		++lowzero;
8524a17663cSThomas Veerman 
8534a17663cSThomas Veerman 	    return (j);
8544a17663cSThomas Veerman 	}
8554a17663cSThomas Veerman     }
8564a17663cSThomas Veerman }
8574a17663cSThomas Veerman 
8584a17663cSThomas Veerman static void
pack_table(void)8594a17663cSThomas Veerman pack_table(void)
8604a17663cSThomas Veerman {
8614a17663cSThomas Veerman     int i;
8624a17663cSThomas Veerman     Value_t place;
8634a17663cSThomas Veerman     int state;
8644a17663cSThomas Veerman 
8654a17663cSThomas Veerman     base = NEW2(nvectors, Value_t);
8664a17663cSThomas Veerman     pos = NEW2(nentries, Value_t);
8674a17663cSThomas Veerman 
8684a17663cSThomas Veerman     maxtable = 1000;
8694a17663cSThomas Veerman     table = NEW2(maxtable, Value_t);
8704a17663cSThomas Veerman     check = NEW2(maxtable, Value_t);
8714a17663cSThomas Veerman 
8724a17663cSThomas Veerman     lowzero = 0;
8734a17663cSThomas Veerman     high = 0;
8744a17663cSThomas Veerman 
8754a17663cSThomas Veerman     for (i = 0; i < maxtable; i++)
8764a17663cSThomas Veerman 	check[i] = -1;
8774a17663cSThomas Veerman 
8784a17663cSThomas Veerman     for (i = 0; i < nentries; i++)
8794a17663cSThomas Veerman     {
8804a17663cSThomas Veerman 	state = matching_vector(i);
8814a17663cSThomas Veerman 
8824a17663cSThomas Veerman 	if (state < 0)
8834a17663cSThomas Veerman 	    place = (Value_t) pack_vector(i);
8844a17663cSThomas Veerman 	else
8854a17663cSThomas Veerman 	    place = base[state];
8864a17663cSThomas Veerman 
8874a17663cSThomas Veerman 	pos[i] = place;
8884a17663cSThomas Veerman 	base[order[i]] = place;
8894a17663cSThomas Veerman     }
8904a17663cSThomas Veerman 
8914a17663cSThomas Veerman     for (i = 0; i < nvectors; i++)
8924a17663cSThomas Veerman     {
8934a17663cSThomas Veerman 	if (froms[i])
8944a17663cSThomas Veerman 	    FREE(froms[i]);
8954a17663cSThomas Veerman 	if (tos[i])
8964a17663cSThomas Veerman 	    FREE(tos[i]);
8974a17663cSThomas Veerman     }
8984a17663cSThomas Veerman 
899*0a6a1f1dSLionel Sambuc     DO_FREE(froms);
900*0a6a1f1dSLionel Sambuc     DO_FREE(tos);
901*0a6a1f1dSLionel Sambuc     DO_FREE(tally);
902*0a6a1f1dSLionel Sambuc     DO_FREE(width);
903*0a6a1f1dSLionel Sambuc     DO_FREE(pos);
9044a17663cSThomas Veerman }
9054a17663cSThomas Veerman 
9064a17663cSThomas Veerman static void
output_base(void)9074a17663cSThomas Veerman output_base(void)
9084a17663cSThomas Veerman {
9094a17663cSThomas Veerman     int i, j;
9104a17663cSThomas Veerman 
9114a17663cSThomas Veerman     start_int_table("sindex", base[0]);
9124a17663cSThomas Veerman 
9134a17663cSThomas Veerman     j = 10;
9144a17663cSThomas Veerman     for (i = 1; i < nstates; i++)
9154a17663cSThomas Veerman     {
9164a17663cSThomas Veerman 	if (j >= 10)
9174a17663cSThomas Veerman 	{
9184a17663cSThomas Veerman 	    output_newline();
9194a17663cSThomas Veerman 	    j = 1;
9204a17663cSThomas Veerman 	}
9214a17663cSThomas Veerman 	else
9224a17663cSThomas Veerman 	    ++j;
9234a17663cSThomas Veerman 
9244a17663cSThomas Veerman 	output_int(base[i]);
9254a17663cSThomas Veerman     }
9264a17663cSThomas Veerman 
9274a17663cSThomas Veerman     end_table();
9284a17663cSThomas Veerman 
9294a17663cSThomas Veerman     start_int_table("rindex", base[nstates]);
9304a17663cSThomas Veerman 
9314a17663cSThomas Veerman     j = 10;
9324a17663cSThomas Veerman     for (i = nstates + 1; i < 2 * nstates; i++)
9334a17663cSThomas Veerman     {
9344a17663cSThomas Veerman 	if (j >= 10)
9354a17663cSThomas Veerman 	{
9364a17663cSThomas Veerman 	    output_newline();
9374a17663cSThomas Veerman 	    j = 1;
9384a17663cSThomas Veerman 	}
9394a17663cSThomas Veerman 	else
9404a17663cSThomas Veerman 	    ++j;
9414a17663cSThomas Veerman 
9424a17663cSThomas Veerman 	output_int(base[i]);
9434a17663cSThomas Veerman     }
9444a17663cSThomas Veerman 
9454a17663cSThomas Veerman     end_table();
9464a17663cSThomas Veerman 
947*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
948*0a6a1f1dSLionel Sambuc     output_line("#if YYBTYACC");
949*0a6a1f1dSLionel Sambuc     start_int_table("cindex", base[2 * nstates]);
9504a17663cSThomas Veerman 
9514a17663cSThomas Veerman     j = 10;
952*0a6a1f1dSLionel Sambuc     for (i = 2 * nstates + 1; i < 3 * nstates; i++)
953*0a6a1f1dSLionel Sambuc     {
954*0a6a1f1dSLionel Sambuc 	if (j >= 10)
955*0a6a1f1dSLionel Sambuc 	{
956*0a6a1f1dSLionel Sambuc 	    output_newline();
957*0a6a1f1dSLionel Sambuc 	    j = 1;
958*0a6a1f1dSLionel Sambuc 	}
959*0a6a1f1dSLionel Sambuc 	else
960*0a6a1f1dSLionel Sambuc 	    ++j;
961*0a6a1f1dSLionel Sambuc 
962*0a6a1f1dSLionel Sambuc 	output_int(base[i]);
963*0a6a1f1dSLionel Sambuc     }
964*0a6a1f1dSLionel Sambuc 
965*0a6a1f1dSLionel Sambuc     end_table();
966*0a6a1f1dSLionel Sambuc     output_line("#endif");
967*0a6a1f1dSLionel Sambuc #endif
968*0a6a1f1dSLionel Sambuc 
969*0a6a1f1dSLionel Sambuc     start_int_table("gindex", base[PER_STATE * nstates]);
970*0a6a1f1dSLionel Sambuc 
971*0a6a1f1dSLionel Sambuc     j = 10;
972*0a6a1f1dSLionel Sambuc     for (i = PER_STATE * nstates + 1; i < nvectors - 1; i++)
9734a17663cSThomas Veerman     {
9744a17663cSThomas Veerman 	if (j >= 10)
9754a17663cSThomas Veerman 	{
9764a17663cSThomas Veerman 	    output_newline();
9774a17663cSThomas Veerman 	    j = 1;
9784a17663cSThomas Veerman 	}
9794a17663cSThomas Veerman 	else
9804a17663cSThomas Veerman 	    ++j;
9814a17663cSThomas Veerman 
9824a17663cSThomas Veerman 	output_int(base[i]);
9834a17663cSThomas Veerman     }
9844a17663cSThomas Veerman 
9854a17663cSThomas Veerman     end_table();
9864a17663cSThomas Veerman     FREE(base);
9874a17663cSThomas Veerman }
9884a17663cSThomas Veerman 
9894a17663cSThomas Veerman static void
output_table(void)9904a17663cSThomas Veerman output_table(void)
9914a17663cSThomas Veerman {
9924a17663cSThomas Veerman     int i;
9934a17663cSThomas Veerman     int j;
9944a17663cSThomas Veerman 
995*0a6a1f1dSLionel Sambuc     if (high >= MAXYYINT)
996*0a6a1f1dSLionel Sambuc     {
997*0a6a1f1dSLionel Sambuc 	fprintf(stderr, "YYTABLESIZE: %ld\n", high);
998*0a6a1f1dSLionel Sambuc 	fprintf(stderr, "Table is longer than %d elements.\n", MAXYYINT);
999*0a6a1f1dSLionel Sambuc 	done(1);
1000*0a6a1f1dSLionel Sambuc     }
1001*0a6a1f1dSLionel Sambuc 
10024a17663cSThomas Veerman     ++outline;
1003*0a6a1f1dSLionel Sambuc     fprintf(code_file, "#define YYTABLESIZE %ld\n", high);
10044a17663cSThomas Veerman     start_int_table("table", table[0]);
10054a17663cSThomas Veerman 
10064a17663cSThomas Veerman     j = 10;
10074a17663cSThomas Veerman     for (i = 1; i <= high; i++)
10084a17663cSThomas Veerman     {
10094a17663cSThomas Veerman 	if (j >= 10)
10104a17663cSThomas Veerman 	{
10114a17663cSThomas Veerman 	    output_newline();
10124a17663cSThomas Veerman 	    j = 1;
10134a17663cSThomas Veerman 	}
10144a17663cSThomas Veerman 	else
10154a17663cSThomas Veerman 	    ++j;
10164a17663cSThomas Veerman 
10174a17663cSThomas Veerman 	output_int(table[i]);
10184a17663cSThomas Veerman     }
10194a17663cSThomas Veerman 
10204a17663cSThomas Veerman     end_table();
10214a17663cSThomas Veerman     FREE(table);
10224a17663cSThomas Veerman }
10234a17663cSThomas Veerman 
10244a17663cSThomas Veerman static void
output_check(void)10254a17663cSThomas Veerman output_check(void)
10264a17663cSThomas Veerman {
10274a17663cSThomas Veerman     int i;
10284a17663cSThomas Veerman     int j;
10294a17663cSThomas Veerman 
10304a17663cSThomas Veerman     start_int_table("check", check[0]);
10314a17663cSThomas Veerman 
10324a17663cSThomas Veerman     j = 10;
10334a17663cSThomas Veerman     for (i = 1; i <= high; i++)
10344a17663cSThomas Veerman     {
10354a17663cSThomas Veerman 	if (j >= 10)
10364a17663cSThomas Veerman 	{
10374a17663cSThomas Veerman 	    output_newline();
10384a17663cSThomas Veerman 	    j = 1;
10394a17663cSThomas Veerman 	}
10404a17663cSThomas Veerman 	else
10414a17663cSThomas Veerman 	    ++j;
10424a17663cSThomas Veerman 
10434a17663cSThomas Veerman 	output_int(check[i]);
10444a17663cSThomas Veerman     }
10454a17663cSThomas Veerman 
10464a17663cSThomas Veerman     end_table();
10474a17663cSThomas Veerman     FREE(check);
10484a17663cSThomas Veerman }
10494a17663cSThomas Veerman 
1050*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1051*0a6a1f1dSLionel Sambuc static void
output_ctable(void)1052*0a6a1f1dSLionel Sambuc output_ctable(void)
1053*0a6a1f1dSLionel Sambuc {
1054*0a6a1f1dSLionel Sambuc     int i;
1055*0a6a1f1dSLionel Sambuc     int j;
1056*0a6a1f1dSLionel Sambuc     int limit = (conflicts != 0) ? nconflicts : 0;
1057*0a6a1f1dSLionel Sambuc 
1058*0a6a1f1dSLionel Sambuc     if (limit < high)
1059*0a6a1f1dSLionel Sambuc 	limit = (int)high;
1060*0a6a1f1dSLionel Sambuc 
1061*0a6a1f1dSLionel Sambuc     output_line("#if YYBTYACC");
1062*0a6a1f1dSLionel Sambuc     start_int_table("ctable", conflicts ? conflicts[0] : -1);
1063*0a6a1f1dSLionel Sambuc 
1064*0a6a1f1dSLionel Sambuc     j = 10;
1065*0a6a1f1dSLionel Sambuc     for (i = 1; i < limit; i++)
1066*0a6a1f1dSLionel Sambuc     {
1067*0a6a1f1dSLionel Sambuc 	if (j >= 10)
1068*0a6a1f1dSLionel Sambuc 	{
1069*0a6a1f1dSLionel Sambuc 	    output_newline();
1070*0a6a1f1dSLionel Sambuc 	    j = 1;
1071*0a6a1f1dSLionel Sambuc 	}
1072*0a6a1f1dSLionel Sambuc 	else
1073*0a6a1f1dSLionel Sambuc 	    ++j;
1074*0a6a1f1dSLionel Sambuc 
1075*0a6a1f1dSLionel Sambuc 	output_int((conflicts != 0 && i < nconflicts) ? conflicts[i] : -1);
1076*0a6a1f1dSLionel Sambuc     }
1077*0a6a1f1dSLionel Sambuc 
1078*0a6a1f1dSLionel Sambuc     if (conflicts)
1079*0a6a1f1dSLionel Sambuc 	FREE(conflicts);
1080*0a6a1f1dSLionel Sambuc 
1081*0a6a1f1dSLionel Sambuc     end_table();
1082*0a6a1f1dSLionel Sambuc     output_line("#endif");
1083*0a6a1f1dSLionel Sambuc }
1084*0a6a1f1dSLionel Sambuc #endif
1085*0a6a1f1dSLionel Sambuc 
10864a17663cSThomas Veerman static void
output_actions(void)10874a17663cSThomas Veerman output_actions(void)
10884a17663cSThomas Veerman {
1089*0a6a1f1dSLionel Sambuc     nvectors = PER_STATE * nstates + nvars;
10904a17663cSThomas Veerman 
10914a17663cSThomas Veerman     froms = NEW2(nvectors, Value_t *);
10924a17663cSThomas Veerman     tos = NEW2(nvectors, Value_t *);
10934a17663cSThomas Veerman     tally = NEW2(nvectors, Value_t);
10944a17663cSThomas Veerman     width = NEW2(nvectors, Value_t);
10954a17663cSThomas Veerman 
1096*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1097*0a6a1f1dSLionel Sambuc     if (backtrack && (SRtotal + RRtotal) != 0)
1098*0a6a1f1dSLionel Sambuc 	conflicts = NEW2(4 * (SRtotal + RRtotal), Value_t);
1099*0a6a1f1dSLionel Sambuc #endif
1100*0a6a1f1dSLionel Sambuc 
11014a17663cSThomas Veerman     token_actions();
11024a17663cSThomas Veerman     FREE(lookaheads);
11034a17663cSThomas Veerman     FREE(LA);
11044a17663cSThomas Veerman     FREE(LAruleno);
11054a17663cSThomas Veerman     FREE(accessing_symbol);
11064a17663cSThomas Veerman 
11074a17663cSThomas Veerman     goto_actions();
1108*0a6a1f1dSLionel Sambuc     FREE(goto_base);
11094a17663cSThomas Veerman     FREE(from_state);
11104a17663cSThomas Veerman     FREE(to_state);
11114a17663cSThomas Veerman 
11124a17663cSThomas Veerman     sort_actions();
11134a17663cSThomas Veerman     pack_table();
11144a17663cSThomas Veerman     output_base();
11154a17663cSThomas Veerman     output_table();
11164a17663cSThomas Veerman     output_check();
1117*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1118*0a6a1f1dSLionel Sambuc     output_ctable();
1119*0a6a1f1dSLionel Sambuc #endif
11204a17663cSThomas Veerman }
11214a17663cSThomas Veerman 
11224a17663cSThomas Veerman static int
is_C_identifier(char * name)11234a17663cSThomas Veerman is_C_identifier(char *name)
11244a17663cSThomas Veerman {
11254a17663cSThomas Veerman     char *s;
11264a17663cSThomas Veerman     int c;
11274a17663cSThomas Veerman 
11284a17663cSThomas Veerman     s = name;
11294a17663cSThomas Veerman     c = *s;
11304a17663cSThomas Veerman     if (c == '"')
11314a17663cSThomas Veerman     {
11324a17663cSThomas Veerman 	c = *++s;
11334a17663cSThomas Veerman 	if (!isalpha(c) && c != '_' && c != '$')
11344a17663cSThomas Veerman 	    return (0);
11354a17663cSThomas Veerman 	while ((c = *++s) != '"')
11364a17663cSThomas Veerman 	{
11374a17663cSThomas Veerman 	    if (!isalnum(c) && c != '_' && c != '$')
11384a17663cSThomas Veerman 		return (0);
11394a17663cSThomas Veerman 	}
11404a17663cSThomas Veerman 	return (1);
11414a17663cSThomas Veerman     }
11424a17663cSThomas Veerman 
11434a17663cSThomas Veerman     if (!isalpha(c) && c != '_' && c != '$')
11444a17663cSThomas Veerman 	return (0);
11454a17663cSThomas Veerman     while ((c = *++s) != 0)
11464a17663cSThomas Veerman     {
11474a17663cSThomas Veerman 	if (!isalnum(c) && c != '_' && c != '$')
11484a17663cSThomas Veerman 	    return (0);
11494a17663cSThomas Veerman     }
11504a17663cSThomas Veerman     return (1);
11514a17663cSThomas Veerman }
11524a17663cSThomas Veerman 
1153*0a6a1f1dSLionel Sambuc #if USE_HEADER_GUARDS
1154*0a6a1f1dSLionel Sambuc static void
start_defines_file(void)1155*0a6a1f1dSLionel Sambuc start_defines_file(void)
1156*0a6a1f1dSLionel Sambuc {
1157*0a6a1f1dSLionel Sambuc     fprintf(defines_file, "#ifndef _%s_defines_h_\n", symbol_prefix);
1158*0a6a1f1dSLionel Sambuc     fprintf(defines_file, "#define _%s_defines_h_\n\n", symbol_prefix);
1159*0a6a1f1dSLionel Sambuc }
1160*0a6a1f1dSLionel Sambuc 
1161*0a6a1f1dSLionel Sambuc static void
end_defines_file(void)1162*0a6a1f1dSLionel Sambuc end_defines_file(void)
1163*0a6a1f1dSLionel Sambuc {
1164*0a6a1f1dSLionel Sambuc     fprintf(defines_file, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix);
1165*0a6a1f1dSLionel Sambuc }
1166*0a6a1f1dSLionel Sambuc #else
1167*0a6a1f1dSLionel Sambuc #define start_defines_file()	/* nothing */
1168*0a6a1f1dSLionel Sambuc #define end_defines_file()	/* nothing */
1169*0a6a1f1dSLionel Sambuc #endif
1170*0a6a1f1dSLionel Sambuc 
11714a17663cSThomas Veerman static void
output_defines(FILE * fp)11724a17663cSThomas Veerman output_defines(FILE * fp)
11734a17663cSThomas Veerman {
11744a17663cSThomas Veerman     int c, i;
11754a17663cSThomas Veerman     char *s;
11764a17663cSThomas Veerman 
11774a17663cSThomas Veerman     for (i = 2; i < ntokens; ++i)
11784a17663cSThomas Veerman     {
11794a17663cSThomas Veerman 	s = symbol_name[i];
118084d9c625SLionel Sambuc 	if (is_C_identifier(s) && (!sflag || *s != '"'))
11814a17663cSThomas Veerman 	{
11824a17663cSThomas Veerman 	    fprintf(fp, "#define ");
11834a17663cSThomas Veerman 	    c = *s;
11844a17663cSThomas Veerman 	    if (c == '"')
11854a17663cSThomas Veerman 	    {
11864a17663cSThomas Veerman 		while ((c = *++s) != '"')
11874a17663cSThomas Veerman 		{
11884a17663cSThomas Veerman 		    putc(c, fp);
11894a17663cSThomas Veerman 		}
11904a17663cSThomas Veerman 	    }
11914a17663cSThomas Veerman 	    else
11924a17663cSThomas Veerman 	    {
11934a17663cSThomas Veerman 		do
11944a17663cSThomas Veerman 		{
11954a17663cSThomas Veerman 		    putc(c, fp);
11964a17663cSThomas Veerman 		}
11974a17663cSThomas Veerman 		while ((c = *++s) != 0);
11984a17663cSThomas Veerman 	    }
11994a17663cSThomas Veerman 	    if (fp == code_file)
12004a17663cSThomas Veerman 		++outline;
12014a17663cSThomas Veerman 	    fprintf(fp, " %d\n", symbol_value[i]);
12024a17663cSThomas Veerman 	}
12034a17663cSThomas Veerman     }
12044a17663cSThomas Veerman 
12054a17663cSThomas Veerman     if (fp == code_file)
12064a17663cSThomas Veerman 	++outline;
12074a17663cSThomas Veerman     if (fp != defines_file || iflag)
12084a17663cSThomas Veerman 	fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]);
12094a17663cSThomas Veerman 
12104a17663cSThomas Veerman     if (fp == defines_file || (iflag && !dflag))
12114a17663cSThomas Veerman     {
12124a17663cSThomas Veerman 	if (unionized)
12134a17663cSThomas Veerman 	{
121484d9c625SLionel Sambuc 	    if (union_file != 0)
121584d9c625SLionel Sambuc 	    {
12164a17663cSThomas Veerman 		rewind(union_file);
12174a17663cSThomas Veerman 		while ((c = getc(union_file)) != EOF)
1218*0a6a1f1dSLionel Sambuc 		    putc_code(fp, c);
121984d9c625SLionel Sambuc 	    }
12204a17663cSThomas Veerman 	    fprintf(fp, "extern YYSTYPE %slval;\n", symbol_prefix);
12214a17663cSThomas Veerman 	}
12224a17663cSThomas Veerman     }
12234a17663cSThomas Veerman }
12244a17663cSThomas Veerman 
12254a17663cSThomas Veerman static void
output_stored_text(FILE * fp)12264a17663cSThomas Veerman output_stored_text(FILE * fp)
12274a17663cSThomas Veerman {
12284a17663cSThomas Veerman     int c;
12294a17663cSThomas Veerman     FILE *in;
12304a17663cSThomas Veerman 
12314a17663cSThomas Veerman     rewind(text_file);
12324a17663cSThomas Veerman     if (text_file == NULL)
12334a17663cSThomas Veerman 	open_error("text_file");
12344a17663cSThomas Veerman     in = text_file;
12354a17663cSThomas Veerman     if ((c = getc(in)) == EOF)
12364a17663cSThomas Veerman 	return;
12374a17663cSThomas Veerman     putc_code(fp, c);
12384a17663cSThomas Veerman     while ((c = getc(in)) != EOF)
12394a17663cSThomas Veerman     {
12404a17663cSThomas Veerman 	putc_code(fp, c);
12414a17663cSThomas Veerman     }
12424a17663cSThomas Veerman     write_code_lineno(fp);
12434a17663cSThomas Veerman }
12444a17663cSThomas Veerman 
12454a17663cSThomas Veerman static void
output_debug(void)12464a17663cSThomas Veerman output_debug(void)
12474a17663cSThomas Veerman {
1248*0a6a1f1dSLionel Sambuc     int i, j, k, max, maxtok;
12494a17663cSThomas Veerman     const char **symnam;
12504a17663cSThomas Veerman     const char *s;
12514a17663cSThomas Veerman 
12524a17663cSThomas Veerman     ++outline;
12534a17663cSThomas Veerman     fprintf(code_file, "#define YYFINAL %d\n", final_state);
12544a17663cSThomas Veerman 
12554a17663cSThomas Veerman     putl_code(code_file, "#ifndef YYDEBUG\n");
12564a17663cSThomas Veerman     ++outline;
12574a17663cSThomas Veerman     fprintf(code_file, "#define YYDEBUG %d\n", tflag);
12584a17663cSThomas Veerman     putl_code(code_file, "#endif\n");
12594a17663cSThomas Veerman 
12604a17663cSThomas Veerman     if (rflag)
12614a17663cSThomas Veerman     {
12624a17663cSThomas Veerman 	fprintf(output_file, "#ifndef YYDEBUG\n");
12634a17663cSThomas Veerman 	fprintf(output_file, "#define YYDEBUG %d\n", tflag);
12644a17663cSThomas Veerman 	fprintf(output_file, "#endif\n");
12654a17663cSThomas Veerman     }
12664a17663cSThomas Veerman 
1267*0a6a1f1dSLionel Sambuc     maxtok = 0;
1268*0a6a1f1dSLionel Sambuc     for (i = 0; i < ntokens; ++i)
1269*0a6a1f1dSLionel Sambuc 	if (symbol_value[i] > maxtok)
1270*0a6a1f1dSLionel Sambuc 	    maxtok = symbol_value[i];
1271*0a6a1f1dSLionel Sambuc 
1272*0a6a1f1dSLionel Sambuc     /* symbol_value[$accept] = -1         */
1273*0a6a1f1dSLionel Sambuc     /* symbol_value[<goal>]  = 0          */
1274*0a6a1f1dSLionel Sambuc     /* remaining non-terminals start at 1 */
1275*0a6a1f1dSLionel Sambuc     max = maxtok;
1276*0a6a1f1dSLionel Sambuc     for (i = ntokens; i < nsyms; ++i)
1277*0a6a1f1dSLionel Sambuc 	if (((maxtok + 1) + (symbol_value[i] + 1)) > max)
1278*0a6a1f1dSLionel Sambuc 	    max = (maxtok + 1) + (symbol_value[i] + 1);
12794a17663cSThomas Veerman 
12804a17663cSThomas Veerman     ++outline;
1281*0a6a1f1dSLionel Sambuc     fprintf(code_file, "#define YYMAXTOKEN %d\n", maxtok);
12824a17663cSThomas Veerman 
1283*0a6a1f1dSLionel Sambuc     ++outline;
1284*0a6a1f1dSLionel Sambuc     fprintf(code_file, "#define YYUNDFTOKEN %d\n", max + 1);
1285*0a6a1f1dSLionel Sambuc 
1286*0a6a1f1dSLionel Sambuc     ++outline;
1287*0a6a1f1dSLionel Sambuc     fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? "
1288*0a6a1f1dSLionel Sambuc 	    "YYUNDFTOKEN : (a))\n");
1289*0a6a1f1dSLionel Sambuc 
1290*0a6a1f1dSLionel Sambuc     symnam = TMALLOC(const char *, max + 2);
12914a17663cSThomas Veerman     NO_SPACE(symnam);
12924a17663cSThomas Veerman 
12934a17663cSThomas Veerman     /* Note that it is not necessary to initialize the element          */
12944a17663cSThomas Veerman     /* symnam[max].                                                     */
1295*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
12964a17663cSThomas Veerman     for (i = 0; i < max; ++i)
12974a17663cSThomas Veerman 	symnam[i] = 0;
1298*0a6a1f1dSLionel Sambuc     for (i = nsyms - 1; i >= 0; --i)
1299*0a6a1f1dSLionel Sambuc 	symnam[symbol_pval[i]] = symbol_name[i];
1300*0a6a1f1dSLionel Sambuc     symnam[max + 1] = "illegal-symbol";
1301*0a6a1f1dSLionel Sambuc #else
1302*0a6a1f1dSLionel Sambuc     for (i = 0; i <= max; ++i)
1303*0a6a1f1dSLionel Sambuc 	symnam[i] = 0;
13044a17663cSThomas Veerman     for (i = ntokens - 1; i >= 2; --i)
13054a17663cSThomas Veerman 	symnam[symbol_value[i]] = symbol_name[i];
13064a17663cSThomas Veerman     symnam[0] = "end-of-file";
1307*0a6a1f1dSLionel Sambuc     symnam[max + 1] = "illegal-symbol";
1308*0a6a1f1dSLionel Sambuc #endif
13094a17663cSThomas Veerman 
1310*0a6a1f1dSLionel Sambuc     /*
1311*0a6a1f1dSLionel Sambuc      * bison's yytname[] array is roughly the same as byacc's yyname[] array.
1312*0a6a1f1dSLionel Sambuc      * The difference is that byacc does not predefine "$undefined".
1313*0a6a1f1dSLionel Sambuc      *
1314*0a6a1f1dSLionel Sambuc      * If the grammar declares "%token-table", define symbol "yytname" so
1315*0a6a1f1dSLionel Sambuc      * an application such as ntpd can build.
1316*0a6a1f1dSLionel Sambuc      */
1317*0a6a1f1dSLionel Sambuc     if (token_table)
1318*0a6a1f1dSLionel Sambuc     {
1319*0a6a1f1dSLionel Sambuc 	output_line("#undef yytname");
1320*0a6a1f1dSLionel Sambuc 	output_line("#define yytname yyname");
1321*0a6a1f1dSLionel Sambuc     }
1322*0a6a1f1dSLionel Sambuc     else
1323*0a6a1f1dSLionel Sambuc     {
13244a17663cSThomas Veerman 	output_line("#if YYDEBUG");
1325*0a6a1f1dSLionel Sambuc     }
13264a17663cSThomas Veerman 
13274a17663cSThomas Veerman     start_str_table("name");
13284a17663cSThomas Veerman     j = 80;
1329*0a6a1f1dSLionel Sambuc     for (i = 0; i <= max + 1; ++i)
13304a17663cSThomas Veerman     {
13314a17663cSThomas Veerman 	if ((s = symnam[i]) != 0)
13324a17663cSThomas Veerman 	{
13334a17663cSThomas Veerman 	    if (s[0] == '"')
13344a17663cSThomas Veerman 	    {
13354a17663cSThomas Veerman 		k = 7;
13364a17663cSThomas Veerman 		while (*++s != '"')
13374a17663cSThomas Veerman 		{
13384a17663cSThomas Veerman 		    ++k;
13394a17663cSThomas Veerman 		    if (*s == '\\')
13404a17663cSThomas Veerman 		    {
13414a17663cSThomas Veerman 			k += 2;
13424a17663cSThomas Veerman 			if (*++s == '\\')
13434a17663cSThomas Veerman 			    ++k;
13444a17663cSThomas Veerman 		    }
13454a17663cSThomas Veerman 		}
13464a17663cSThomas Veerman 		j += k;
13474a17663cSThomas Veerman 		if (j > 80)
13484a17663cSThomas Veerman 		{
13494a17663cSThomas Veerman 		    output_newline();
13504a17663cSThomas Veerman 		    j = k;
13514a17663cSThomas Veerman 		}
13524a17663cSThomas Veerman 		fprintf(output_file, "\"\\\"");
13534a17663cSThomas Veerman 		s = symnam[i];
13544a17663cSThomas Veerman 		while (*++s != '"')
13554a17663cSThomas Veerman 		{
13564a17663cSThomas Veerman 		    if (*s == '\\')
13574a17663cSThomas Veerman 		    {
13584a17663cSThomas Veerman 			fprintf(output_file, "\\\\");
13594a17663cSThomas Veerman 			if (*++s == '\\')
13604a17663cSThomas Veerman 			    fprintf(output_file, "\\\\");
13614a17663cSThomas Veerman 			else
13624a17663cSThomas Veerman 			    putc(*s, output_file);
13634a17663cSThomas Veerman 		    }
13644a17663cSThomas Veerman 		    else
13654a17663cSThomas Veerman 			putc(*s, output_file);
13664a17663cSThomas Veerman 		}
13674a17663cSThomas Veerman 		fprintf(output_file, "\\\"\",");
13684a17663cSThomas Veerman 	    }
13694a17663cSThomas Veerman 	    else if (s[0] == '\'')
13704a17663cSThomas Veerman 	    {
13714a17663cSThomas Veerman 		if (s[1] == '"')
13724a17663cSThomas Veerman 		{
13734a17663cSThomas Veerman 		    j += 7;
13744a17663cSThomas Veerman 		    if (j > 80)
13754a17663cSThomas Veerman 		    {
13764a17663cSThomas Veerman 			output_newline();
13774a17663cSThomas Veerman 			j = 7;
13784a17663cSThomas Veerman 		    }
13794a17663cSThomas Veerman 		    fprintf(output_file, "\"'\\\"'\",");
13804a17663cSThomas Veerman 		}
13814a17663cSThomas Veerman 		else
13824a17663cSThomas Veerman 		{
13834a17663cSThomas Veerman 		    k = 5;
13844a17663cSThomas Veerman 		    while (*++s != '\'')
13854a17663cSThomas Veerman 		    {
13864a17663cSThomas Veerman 			++k;
13874a17663cSThomas Veerman 			if (*s == '\\')
13884a17663cSThomas Veerman 			{
13894a17663cSThomas Veerman 			    k += 2;
13904a17663cSThomas Veerman 			    if (*++s == '\\')
13914a17663cSThomas Veerman 				++k;
13924a17663cSThomas Veerman 			}
13934a17663cSThomas Veerman 		    }
13944a17663cSThomas Veerman 		    j += k;
13954a17663cSThomas Veerman 		    if (j > 80)
13964a17663cSThomas Veerman 		    {
13974a17663cSThomas Veerman 			output_newline();
13984a17663cSThomas Veerman 			j = k;
13994a17663cSThomas Veerman 		    }
14004a17663cSThomas Veerman 		    fprintf(output_file, "\"'");
14014a17663cSThomas Veerman 		    s = symnam[i];
14024a17663cSThomas Veerman 		    while (*++s != '\'')
14034a17663cSThomas Veerman 		    {
14044a17663cSThomas Veerman 			if (*s == '\\')
14054a17663cSThomas Veerman 			{
14064a17663cSThomas Veerman 			    fprintf(output_file, "\\\\");
14074a17663cSThomas Veerman 			    if (*++s == '\\')
14084a17663cSThomas Veerman 				fprintf(output_file, "\\\\");
14094a17663cSThomas Veerman 			    else
14104a17663cSThomas Veerman 				putc(*s, output_file);
14114a17663cSThomas Veerman 			}
14124a17663cSThomas Veerman 			else
14134a17663cSThomas Veerman 			    putc(*s, output_file);
14144a17663cSThomas Veerman 		    }
14154a17663cSThomas Veerman 		    fprintf(output_file, "'\",");
14164a17663cSThomas Veerman 		}
14174a17663cSThomas Veerman 	    }
14184a17663cSThomas Veerman 	    else
14194a17663cSThomas Veerman 	    {
14204a17663cSThomas Veerman 		k = (int)strlen(s) + 3;
14214a17663cSThomas Veerman 		j += k;
14224a17663cSThomas Veerman 		if (j > 80)
14234a17663cSThomas Veerman 		{
14244a17663cSThomas Veerman 		    output_newline();
14254a17663cSThomas Veerman 		    j = k;
14264a17663cSThomas Veerman 		}
14274a17663cSThomas Veerman 		putc('"', output_file);
14284a17663cSThomas Veerman 		do
14294a17663cSThomas Veerman 		{
14304a17663cSThomas Veerman 		    putc(*s, output_file);
14314a17663cSThomas Veerman 		}
14324a17663cSThomas Veerman 		while (*++s);
14334a17663cSThomas Veerman 		fprintf(output_file, "\",");
14344a17663cSThomas Veerman 	    }
14354a17663cSThomas Veerman 	}
14364a17663cSThomas Veerman 	else
14374a17663cSThomas Veerman 	{
14384a17663cSThomas Veerman 	    j += 2;
14394a17663cSThomas Veerman 	    if (j > 80)
14404a17663cSThomas Veerman 	    {
14414a17663cSThomas Veerman 		output_newline();
14424a17663cSThomas Veerman 		j = 2;
14434a17663cSThomas Veerman 	    }
14444a17663cSThomas Veerman 	    fprintf(output_file, "0,");
14454a17663cSThomas Veerman 	}
14464a17663cSThomas Veerman     }
14474a17663cSThomas Veerman     end_table();
14484a17663cSThomas Veerman     FREE(symnam);
14494a17663cSThomas Veerman 
1450*0a6a1f1dSLionel Sambuc     if (token_table)
1451*0a6a1f1dSLionel Sambuc 	output_line("#if YYDEBUG");
14524a17663cSThomas Veerman     start_str_table("rule");
14534a17663cSThomas Veerman     for (i = 2; i < nrules; ++i)
14544a17663cSThomas Veerman     {
14554a17663cSThomas Veerman 	fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
14564a17663cSThomas Veerman 	for (j = rrhs[i]; ritem[j] > 0; ++j)
14574a17663cSThomas Veerman 	{
14584a17663cSThomas Veerman 	    s = symbol_name[ritem[j]];
14594a17663cSThomas Veerman 	    if (s[0] == '"')
14604a17663cSThomas Veerman 	    {
14614a17663cSThomas Veerman 		fprintf(output_file, " \\\"");
14624a17663cSThomas Veerman 		while (*++s != '"')
14634a17663cSThomas Veerman 		{
14644a17663cSThomas Veerman 		    if (*s == '\\')
14654a17663cSThomas Veerman 		    {
14664a17663cSThomas Veerman 			if (s[1] == '\\')
14674a17663cSThomas Veerman 			    fprintf(output_file, "\\\\\\\\");
14684a17663cSThomas Veerman 			else
14694a17663cSThomas Veerman 			    fprintf(output_file, "\\\\%c", s[1]);
14704a17663cSThomas Veerman 			++s;
14714a17663cSThomas Veerman 		    }
14724a17663cSThomas Veerman 		    else
14734a17663cSThomas Veerman 			putc(*s, output_file);
14744a17663cSThomas Veerman 		}
14754a17663cSThomas Veerman 		fprintf(output_file, "\\\"");
14764a17663cSThomas Veerman 	    }
14774a17663cSThomas Veerman 	    else if (s[0] == '\'')
14784a17663cSThomas Veerman 	    {
14794a17663cSThomas Veerman 		if (s[1] == '"')
14804a17663cSThomas Veerman 		    fprintf(output_file, " '\\\"'");
14814a17663cSThomas Veerman 		else if (s[1] == '\\')
14824a17663cSThomas Veerman 		{
14834a17663cSThomas Veerman 		    if (s[2] == '\\')
14844a17663cSThomas Veerman 			fprintf(output_file, " '\\\\\\\\");
14854a17663cSThomas Veerman 		    else
14864a17663cSThomas Veerman 			fprintf(output_file, " '\\\\%c", s[2]);
14874a17663cSThomas Veerman 		    s += 2;
14884a17663cSThomas Veerman 		    while (*++s != '\'')
14894a17663cSThomas Veerman 			putc(*s, output_file);
14904a17663cSThomas Veerman 		    putc('\'', output_file);
14914a17663cSThomas Veerman 		}
14924a17663cSThomas Veerman 		else
14934a17663cSThomas Veerman 		    fprintf(output_file, " '%c'", s[1]);
14944a17663cSThomas Veerman 	    }
14954a17663cSThomas Veerman 	    else
14964a17663cSThomas Veerman 		fprintf(output_file, " %s", s);
14974a17663cSThomas Veerman 	}
14984a17663cSThomas Veerman 	fprintf(output_file, "\",");
14994a17663cSThomas Veerman 	output_newline();
15004a17663cSThomas Veerman     }
15014a17663cSThomas Veerman 
15024a17663cSThomas Veerman     end_table();
15034a17663cSThomas Veerman     output_line("#endif");
15044a17663cSThomas Veerman }
15054a17663cSThomas Veerman 
1506*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1507*0a6a1f1dSLionel Sambuc static void
output_backtracking_parser(FILE * fp)1508*0a6a1f1dSLionel Sambuc output_backtracking_parser(FILE * fp)
1509*0a6a1f1dSLionel Sambuc {
1510*0a6a1f1dSLionel Sambuc     putl_code(fp, "#undef YYBTYACC\n");
1511*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1512*0a6a1f1dSLionel Sambuc     if (backtrack)
1513*0a6a1f1dSLionel Sambuc     {
1514*0a6a1f1dSLionel Sambuc 	putl_code(fp, "#define YYBTYACC 1\n");
1515*0a6a1f1dSLionel Sambuc 	putl_code(fp,
1516*0a6a1f1dSLionel Sambuc 		  "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n");
1517*0a6a1f1dSLionel Sambuc     }
1518*0a6a1f1dSLionel Sambuc     else
1519*0a6a1f1dSLionel Sambuc #endif
1520*0a6a1f1dSLionel Sambuc     {
1521*0a6a1f1dSLionel Sambuc 	putl_code(fp, "#define YYBTYACC 0\n");
1522*0a6a1f1dSLionel Sambuc 	putl_code(fp, "#define YYDEBUGSTR YYPREFIX \"debug\"\n");
1523*0a6a1f1dSLionel Sambuc     }
1524*0a6a1f1dSLionel Sambuc }
1525*0a6a1f1dSLionel Sambuc #endif
1526*0a6a1f1dSLionel Sambuc 
15274a17663cSThomas Veerman static void
output_pure_parser(FILE * fp)15284a17663cSThomas Veerman output_pure_parser(FILE * fp)
15294a17663cSThomas Veerman {
15304a17663cSThomas Veerman     putc_code(fp, '\n');
15314a17663cSThomas Veerman 
15324a17663cSThomas Veerman     if (fp == code_file)
1533*0a6a1f1dSLionel Sambuc 	++outline;
15344a17663cSThomas Veerman     fprintf(fp, "#define YYPURE %d\n", pure_parser);
15354a17663cSThomas Veerman     putc_code(fp, '\n');
15364a17663cSThomas Veerman }
15374a17663cSThomas Veerman 
15384a17663cSThomas Veerman static void
output_stype(FILE * fp)15394a17663cSThomas Veerman output_stype(FILE * fp)
15404a17663cSThomas Veerman {
15414a17663cSThomas Veerman     if (!unionized && ntags == 0)
15424a17663cSThomas Veerman     {
15434a17663cSThomas Veerman 	putc_code(fp, '\n');
1544*0a6a1f1dSLionel Sambuc 	putl_code(fp, "#if "
1545*0a6a1f1dSLionel Sambuc 		  "! defined(YYSTYPE) && "
1546*0a6a1f1dSLionel Sambuc 		  "! defined(YYSTYPE_IS_DECLARED)\n");
1547*0a6a1f1dSLionel Sambuc 	putl_code(fp, "/* Default: YYSTYPE is the semantic value type. */\n");
15484a17663cSThomas Veerman 	putl_code(fp, "typedef int YYSTYPE;\n");
1549*0a6a1f1dSLionel Sambuc 	putl_code(fp, "# define YYSTYPE_IS_DECLARED 1\n");
15504a17663cSThomas Veerman 	putl_code(fp, "#endif\n");
15514a17663cSThomas Veerman     }
15524a17663cSThomas Veerman }
15534a17663cSThomas Veerman 
1554*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1555*0a6a1f1dSLionel Sambuc static void
output_ltype(FILE * fp)1556*0a6a1f1dSLionel Sambuc output_ltype(FILE * fp)
1557*0a6a1f1dSLionel Sambuc {
1558*0a6a1f1dSLionel Sambuc     putc_code(fp, '\n');
1559*0a6a1f1dSLionel Sambuc     putl_code(fp, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n");
1560*0a6a1f1dSLionel Sambuc     putl_code(fp, "/* Default: YYLTYPE is the text position type. */\n");
1561*0a6a1f1dSLionel Sambuc     putl_code(fp, "typedef struct YYLTYPE\n");
1562*0a6a1f1dSLionel Sambuc     putl_code(fp, "{\n");
1563*0a6a1f1dSLionel Sambuc     putl_code(fp, "    int first_line;\n");
1564*0a6a1f1dSLionel Sambuc     putl_code(fp, "    int first_column;\n");
1565*0a6a1f1dSLionel Sambuc     putl_code(fp, "    int last_line;\n");
1566*0a6a1f1dSLionel Sambuc     putl_code(fp, "    int last_column;\n");
1567*0a6a1f1dSLionel Sambuc     putl_code(fp, "    unsigned source;\n");
1568*0a6a1f1dSLionel Sambuc     putl_code(fp, "} YYLTYPE;\n");
1569*0a6a1f1dSLionel Sambuc     putl_code(fp, "#define YYLTYPE_IS_DECLARED 1\n");
1570*0a6a1f1dSLionel Sambuc     putl_code(fp, "#endif\n");
1571*0a6a1f1dSLionel Sambuc     putl_code(fp, "#define YYRHSLOC(rhs, k) ((rhs)[k])\n");
1572*0a6a1f1dSLionel Sambuc }
1573*0a6a1f1dSLionel Sambuc #endif
1574*0a6a1f1dSLionel Sambuc 
15754a17663cSThomas Veerman static void
output_trailing_text(void)15764a17663cSThomas Veerman output_trailing_text(void)
15774a17663cSThomas Veerman {
15784a17663cSThomas Veerman     int c, last;
15794a17663cSThomas Veerman     FILE *in;
15804a17663cSThomas Veerman 
15814a17663cSThomas Veerman     if (line == 0)
15824a17663cSThomas Veerman 	return;
15834a17663cSThomas Veerman 
15844a17663cSThomas Veerman     in = input_file;
15854a17663cSThomas Veerman     c = *cptr;
15864a17663cSThomas Veerman     if (c == '\n')
15874a17663cSThomas Veerman     {
15884a17663cSThomas Veerman 	++lineno;
15894a17663cSThomas Veerman 	if ((c = getc(in)) == EOF)
15904a17663cSThomas Veerman 	    return;
15914a17663cSThomas Veerman 	write_input_lineno();
15924a17663cSThomas Veerman 	putc_code(code_file, c);
15934a17663cSThomas Veerman 	last = c;
15944a17663cSThomas Veerman     }
15954a17663cSThomas Veerman     else
15964a17663cSThomas Veerman     {
15974a17663cSThomas Veerman 	write_input_lineno();
15984a17663cSThomas Veerman 	do
15994a17663cSThomas Veerman 	{
16004a17663cSThomas Veerman 	    putc_code(code_file, c);
16014a17663cSThomas Veerman 	}
16024a17663cSThomas Veerman 	while ((c = *++cptr) != '\n');
16034a17663cSThomas Veerman 	putc_code(code_file, c);
16044a17663cSThomas Veerman 	last = '\n';
16054a17663cSThomas Veerman     }
16064a17663cSThomas Veerman 
16074a17663cSThomas Veerman     while ((c = getc(in)) != EOF)
16084a17663cSThomas Veerman     {
16094a17663cSThomas Veerman 	putc_code(code_file, c);
16104a17663cSThomas Veerman 	last = c;
16114a17663cSThomas Veerman     }
16124a17663cSThomas Veerman 
16134a17663cSThomas Veerman     if (last != '\n')
16144a17663cSThomas Veerman     {
16154a17663cSThomas Veerman 	putc_code(code_file, '\n');
16164a17663cSThomas Veerman     }
16174a17663cSThomas Veerman     write_code_lineno(code_file);
16184a17663cSThomas Veerman }
16194a17663cSThomas Veerman 
16204a17663cSThomas Veerman static void
output_semantic_actions(void)16214a17663cSThomas Veerman output_semantic_actions(void)
16224a17663cSThomas Veerman {
16234a17663cSThomas Veerman     int c, last;
16244a17663cSThomas Veerman 
16254a17663cSThomas Veerman     rewind(action_file);
16264a17663cSThomas Veerman     if ((c = getc(action_file)) == EOF)
16274a17663cSThomas Veerman 	return;
16284a17663cSThomas Veerman 
16294a17663cSThomas Veerman     last = c;
16304a17663cSThomas Veerman     putc_code(code_file, c);
16314a17663cSThomas Veerman     while ((c = getc(action_file)) != EOF)
16324a17663cSThomas Veerman     {
16334a17663cSThomas Veerman 	putc_code(code_file, c);
16344a17663cSThomas Veerman 	last = c;
16354a17663cSThomas Veerman     }
16364a17663cSThomas Veerman 
16374a17663cSThomas Veerman     if (last != '\n')
16384a17663cSThomas Veerman     {
16394a17663cSThomas Veerman 	putc_code(code_file, '\n');
16404a17663cSThomas Veerman     }
16414a17663cSThomas Veerman 
16424a17663cSThomas Veerman     write_code_lineno(code_file);
16434a17663cSThomas Veerman }
16444a17663cSThomas Veerman 
16454a17663cSThomas Veerman static void
output_parse_decl(FILE * fp)16464a17663cSThomas Veerman output_parse_decl(FILE * fp)
16474a17663cSThomas Veerman {
1648*0a6a1f1dSLionel Sambuc     putc_code(fp, '\n');
16494a17663cSThomas Veerman     putl_code(fp, "/* compatibility with bison */\n");
16504a17663cSThomas Veerman     putl_code(fp, "#ifdef YYPARSE_PARAM\n");
16514a17663cSThomas Veerman     putl_code(fp, "/* compatibility with FreeBSD */\n");
16524a17663cSThomas Veerman     putl_code(fp, "# ifdef YYPARSE_PARAM_TYPE\n");
16534a17663cSThomas Veerman     putl_code(fp,
16544a17663cSThomas Veerman 	      "#  define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
16554a17663cSThomas Veerman     putl_code(fp, "# else\n");
16564a17663cSThomas Veerman     putl_code(fp, "#  define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n");
16574a17663cSThomas Veerman     putl_code(fp, "# endif\n");
16584a17663cSThomas Veerman     putl_code(fp, "#else\n");
16594a17663cSThomas Veerman 
16604a17663cSThomas Veerman     puts_code(fp, "# define YYPARSE_DECL() yyparse(");
1661*0a6a1f1dSLionel Sambuc     puts_param_types(fp, parse_param, 0);
16624a17663cSThomas Veerman     putl_code(fp, ")\n");
16634a17663cSThomas Veerman 
16644a17663cSThomas Veerman     putl_code(fp, "#endif\n");
16654a17663cSThomas Veerman }
16664a17663cSThomas Veerman 
16674a17663cSThomas Veerman static void
output_lex_decl(FILE * fp)16684a17663cSThomas Veerman output_lex_decl(FILE * fp)
16694a17663cSThomas Veerman {
1670*0a6a1f1dSLionel Sambuc     putc_code(fp, '\n');
16714a17663cSThomas Veerman     putl_code(fp, "/* Parameters sent to lex. */\n");
16724a17663cSThomas Veerman     putl_code(fp, "#ifdef YYLEX_PARAM\n");
16734a17663cSThomas Veerman     if (pure_parser)
16744a17663cSThomas Veerman     {
167584d9c625SLionel Sambuc 	putl_code(fp, "# ifdef YYLEX_PARAM_TYPE\n");
1676*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1677*0a6a1f1dSLionel Sambuc 	if (locations)
1678*0a6a1f1dSLionel Sambuc 	{
1679*0a6a1f1dSLionel Sambuc 	    putl_code(fp, "#  define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1680*0a6a1f1dSLionel Sambuc 		      " YYLTYPE *yylloc,"
1681*0a6a1f1dSLionel Sambuc 		      " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1682*0a6a1f1dSLionel Sambuc 	}
1683*0a6a1f1dSLionel Sambuc 	else
1684*0a6a1f1dSLionel Sambuc #endif
1685*0a6a1f1dSLionel Sambuc 	{
168684d9c625SLionel Sambuc 	    putl_code(fp, "#  define YYLEX_DECL() yylex(YYSTYPE *yylval,"
168784d9c625SLionel Sambuc 		      " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1688*0a6a1f1dSLionel Sambuc 	}
168984d9c625SLionel Sambuc 	putl_code(fp, "# else\n");
1690*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1691*0a6a1f1dSLionel Sambuc 	if (locations)
1692*0a6a1f1dSLionel Sambuc 	{
1693*0a6a1f1dSLionel Sambuc 	    putl_code(fp, "#  define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1694*0a6a1f1dSLionel Sambuc 		      " YYLTYPE *yylloc,"
1695*0a6a1f1dSLionel Sambuc 		      " void * YYLEX_PARAM)\n");
1696*0a6a1f1dSLionel Sambuc 	}
1697*0a6a1f1dSLionel Sambuc 	else
1698*0a6a1f1dSLionel Sambuc #endif
1699*0a6a1f1dSLionel Sambuc 	{
17004a17663cSThomas Veerman 	    putl_code(fp, "#  define YYLEX_DECL() yylex(YYSTYPE *yylval,"
17014a17663cSThomas Veerman 		      " void * YYLEX_PARAM)\n");
1702*0a6a1f1dSLionel Sambuc 	}
170384d9c625SLionel Sambuc 	putl_code(fp, "# endif\n");
1704*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1705*0a6a1f1dSLionel Sambuc 	if (locations)
1706*0a6a1f1dSLionel Sambuc 	    putl_code(fp,
1707*0a6a1f1dSLionel Sambuc 		      "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n");
1708*0a6a1f1dSLionel Sambuc 	else
1709*0a6a1f1dSLionel Sambuc #endif
17104a17663cSThomas Veerman 	    putl_code(fp, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
17114a17663cSThomas Veerman     }
17124a17663cSThomas Veerman     else
17134a17663cSThomas Veerman     {
17144a17663cSThomas Veerman 	putl_code(fp, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n");
17154a17663cSThomas Veerman 	putl_code(fp, "# define YYLEX yylex(YYLEX_PARAM)\n");
17164a17663cSThomas Veerman     }
17174a17663cSThomas Veerman     putl_code(fp, "#else\n");
17184a17663cSThomas Veerman     if (pure_parser && lex_param)
17194a17663cSThomas Veerman     {
1720*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1721*0a6a1f1dSLionel Sambuc 	if (locations)
1722*0a6a1f1dSLionel Sambuc 	    puts_code(fp,
1723*0a6a1f1dSLionel Sambuc 		      "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, ");
1724*0a6a1f1dSLionel Sambuc 	else
1725*0a6a1f1dSLionel Sambuc #endif
17264a17663cSThomas Veerman 	    puts_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, ");
1727*0a6a1f1dSLionel Sambuc 	puts_param_types(fp, lex_param, 0);
17284a17663cSThomas Veerman 	putl_code(fp, ")\n");
17294a17663cSThomas Veerman 
1730*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1731*0a6a1f1dSLionel Sambuc 	if (locations)
1732*0a6a1f1dSLionel Sambuc 	    puts_code(fp, "# define YYLEX yylex(&yylval, &yylloc, ");
1733*0a6a1f1dSLionel Sambuc 	else
1734*0a6a1f1dSLionel Sambuc #endif
17354a17663cSThomas Veerman 	    puts_code(fp, "# define YYLEX yylex(&yylval, ");
1736*0a6a1f1dSLionel Sambuc 	puts_param_names(fp, lex_param, 0);
17374a17663cSThomas Veerman 	putl_code(fp, ")\n");
17384a17663cSThomas Veerman     }
17394a17663cSThomas Veerman     else if (pure_parser)
17404a17663cSThomas Veerman     {
1741*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1742*0a6a1f1dSLionel Sambuc 	if (locations)
1743*0a6a1f1dSLionel Sambuc 	{
1744*0a6a1f1dSLionel Sambuc 	    putl_code(fp,
1745*0a6a1f1dSLionel Sambuc 		      "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n");
1746*0a6a1f1dSLionel Sambuc 	    putl_code(fp, "# define YYLEX yylex(&yylval, &yylloc)\n");
1747*0a6a1f1dSLionel Sambuc 	}
1748*0a6a1f1dSLionel Sambuc 	else
1749*0a6a1f1dSLionel Sambuc #endif
1750*0a6a1f1dSLionel Sambuc 	{
17514a17663cSThomas Veerman 	    putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n");
17524a17663cSThomas Veerman 	    putl_code(fp, "# define YYLEX yylex(&yylval)\n");
17534a17663cSThomas Veerman 	}
1754*0a6a1f1dSLionel Sambuc     }
17554a17663cSThomas Veerman     else if (lex_param)
17564a17663cSThomas Veerman     {
17574a17663cSThomas Veerman 	puts_code(fp, "# define YYLEX_DECL() yylex(");
1758*0a6a1f1dSLionel Sambuc 	puts_param_types(fp, lex_param, 0);
17594a17663cSThomas Veerman 	putl_code(fp, ")\n");
17604a17663cSThomas Veerman 
17614a17663cSThomas Veerman 	puts_code(fp, "# define YYLEX yylex(");
1762*0a6a1f1dSLionel Sambuc 	puts_param_names(fp, lex_param, 0);
17634a17663cSThomas Veerman 	putl_code(fp, ")\n");
17644a17663cSThomas Veerman     }
17654a17663cSThomas Veerman     else
17664a17663cSThomas Veerman     {
17674a17663cSThomas Veerman 	putl_code(fp, "# define YYLEX_DECL() yylex(void)\n");
17684a17663cSThomas Veerman 	putl_code(fp, "# define YYLEX yylex()\n");
17694a17663cSThomas Veerman     }
17704a17663cSThomas Veerman     putl_code(fp, "#endif\n");
17714a17663cSThomas Veerman }
17724a17663cSThomas Veerman 
17734a17663cSThomas Veerman static void
output_error_decl(FILE * fp)17744a17663cSThomas Veerman output_error_decl(FILE * fp)
17754a17663cSThomas Veerman {
1776*0a6a1f1dSLionel Sambuc     putc_code(fp, '\n');
17774a17663cSThomas Veerman     putl_code(fp, "/* Parameters sent to yyerror. */\n");
177884d9c625SLionel Sambuc     putl_code(fp, "#ifndef YYERROR_DECL\n");
1779*0a6a1f1dSLionel Sambuc     puts_code(fp, "#define YYERROR_DECL() yyerror(");
1780*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1781*0a6a1f1dSLionel Sambuc     if (locations)
1782*0a6a1f1dSLionel Sambuc 	puts_code(fp, "YYLTYPE *loc, ");
1783*0a6a1f1dSLionel Sambuc #endif
1784*0a6a1f1dSLionel Sambuc     puts_param_types(fp, parse_param, 1);
17854a17663cSThomas Veerman     putl_code(fp, "const char *s)\n");
178684d9c625SLionel Sambuc     putl_code(fp, "#endif\n");
17874a17663cSThomas Veerman 
178884d9c625SLionel Sambuc     putl_code(fp, "#ifndef YYERROR_CALL\n");
1789*0a6a1f1dSLionel Sambuc 
17904a17663cSThomas Veerman     puts_code(fp, "#define YYERROR_CALL(msg) yyerror(");
1791*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1792*0a6a1f1dSLionel Sambuc     if (locations)
1793*0a6a1f1dSLionel Sambuc 	puts_code(fp, "&yylloc, ");
1794*0a6a1f1dSLionel Sambuc #endif
1795*0a6a1f1dSLionel Sambuc     puts_param_names(fp, parse_param, 1);
17964a17663cSThomas Veerman     putl_code(fp, "msg)\n");
1797*0a6a1f1dSLionel Sambuc 
179884d9c625SLionel Sambuc     putl_code(fp, "#endif\n");
17994a17663cSThomas Veerman }
1800*0a6a1f1dSLionel Sambuc 
1801*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1802*0a6a1f1dSLionel Sambuc static void
output_yydestruct_decl(FILE * fp)1803*0a6a1f1dSLionel Sambuc output_yydestruct_decl(FILE * fp)
18044a17663cSThomas Veerman {
1805*0a6a1f1dSLionel Sambuc     putc_code(fp, '\n');
1806*0a6a1f1dSLionel Sambuc     putl_code(fp, "#ifndef YYDESTRUCT_DECL\n");
1807*0a6a1f1dSLionel Sambuc 
1808*0a6a1f1dSLionel Sambuc     puts_code(fp,
1809*0a6a1f1dSLionel Sambuc 	      "#define YYDESTRUCT_DECL() "
1810*0a6a1f1dSLionel Sambuc 	      "yydestruct(const char *msg, int psymb, YYSTYPE *val");
1811*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1812*0a6a1f1dSLionel Sambuc     if (locations)
1813*0a6a1f1dSLionel Sambuc 	puts_code(fp, ", YYLTYPE *loc");
1814*0a6a1f1dSLionel Sambuc #endif
1815*0a6a1f1dSLionel Sambuc     if (parse_param)
1816*0a6a1f1dSLionel Sambuc     {
1817*0a6a1f1dSLionel Sambuc 	puts_code(fp, ", ");
1818*0a6a1f1dSLionel Sambuc 	puts_param_types(fp, parse_param, 0);
1819*0a6a1f1dSLionel Sambuc     }
1820*0a6a1f1dSLionel Sambuc     putl_code(fp, ")\n");
1821*0a6a1f1dSLionel Sambuc 
182284d9c625SLionel Sambuc     putl_code(fp, "#endif\n");
1823*0a6a1f1dSLionel Sambuc 
1824*0a6a1f1dSLionel Sambuc     putl_code(fp, "#ifndef YYDESTRUCT_CALL\n");
1825*0a6a1f1dSLionel Sambuc 
1826*0a6a1f1dSLionel Sambuc     puts_code(fp, "#define YYDESTRUCT_CALL(msg, psymb, val");
1827*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1828*0a6a1f1dSLionel Sambuc     if (locations)
1829*0a6a1f1dSLionel Sambuc 	puts_code(fp, ", loc");
1830*0a6a1f1dSLionel Sambuc #endif
1831*0a6a1f1dSLionel Sambuc     puts_code(fp, ") yydestruct(msg, psymb, val");
1832*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1833*0a6a1f1dSLionel Sambuc     if (locations)
1834*0a6a1f1dSLionel Sambuc 	puts_code(fp, ", loc");
1835*0a6a1f1dSLionel Sambuc #endif
1836*0a6a1f1dSLionel Sambuc     if (parse_param)
1837*0a6a1f1dSLionel Sambuc     {
1838*0a6a1f1dSLionel Sambuc 	puts_code(fp, ", ");
1839*0a6a1f1dSLionel Sambuc 	puts_param_names(fp, parse_param, 0);
1840*0a6a1f1dSLionel Sambuc     }
1841*0a6a1f1dSLionel Sambuc     putl_code(fp, ")\n");
1842*0a6a1f1dSLionel Sambuc 
184384d9c625SLionel Sambuc     putl_code(fp, "#endif\n");
18444a17663cSThomas Veerman }
1845*0a6a1f1dSLionel Sambuc 
1846*0a6a1f1dSLionel Sambuc static void
output_initial_action(void)1847*0a6a1f1dSLionel Sambuc output_initial_action(void)
1848*0a6a1f1dSLionel Sambuc {
1849*0a6a1f1dSLionel Sambuc     if (initial_action)
1850*0a6a1f1dSLionel Sambuc 	fprintf(code_file, "%s\n", initial_action);
18514a17663cSThomas Veerman }
18524a17663cSThomas Veerman 
18534a17663cSThomas Veerman static void
output_yydestruct_impl(void)1854*0a6a1f1dSLionel Sambuc output_yydestruct_impl(void)
1855*0a6a1f1dSLionel Sambuc {
1856*0a6a1f1dSLionel Sambuc     int i;
1857*0a6a1f1dSLionel Sambuc     char *s, *destructor_code;
1858*0a6a1f1dSLionel Sambuc 
1859*0a6a1f1dSLionel Sambuc     putc_code(code_file, '\n');
1860*0a6a1f1dSLionel Sambuc     putl_code(code_file, "/* Release memory associated with symbol. */\n");
1861*0a6a1f1dSLionel Sambuc     putl_code(code_file, "#if ! defined YYDESTRUCT_IS_DECLARED\n");
1862*0a6a1f1dSLionel Sambuc     putl_code(code_file, "static void\n");
1863*0a6a1f1dSLionel Sambuc     putl_code(code_file, "YYDESTRUCT_DECL()\n");
1864*0a6a1f1dSLionel Sambuc     putl_code(code_file, "{\n");
1865*0a6a1f1dSLionel Sambuc     putl_code(code_file, "    switch (psymb)\n");
1866*0a6a1f1dSLionel Sambuc     putl_code(code_file, "    {\n");
1867*0a6a1f1dSLionel Sambuc     for (i = 2; i < nsyms; ++i)
1868*0a6a1f1dSLionel Sambuc     {
1869*0a6a1f1dSLionel Sambuc 	if ((destructor_code = symbol_destructor[i]) != NULL)
1870*0a6a1f1dSLionel Sambuc 	{
1871*0a6a1f1dSLionel Sambuc 	    ++outline;
1872*0a6a1f1dSLionel Sambuc 	    fprintf(code_file, "\tcase %d:\n", symbol_pval[i]);
1873*0a6a1f1dSLionel Sambuc 	    /* comprehend the number of lines in the destructor code */
1874*0a6a1f1dSLionel Sambuc 	    for (s = destructor_code; (s = strchr(s, '\n')) != NULL; s++)
1875*0a6a1f1dSLionel Sambuc 		++outline;
1876*0a6a1f1dSLionel Sambuc 	    puts_code(code_file, destructor_code);
1877*0a6a1f1dSLionel Sambuc 	    putc_code(code_file, '\n');
1878*0a6a1f1dSLionel Sambuc 	    putl_code(code_file, "\tbreak;\n");
1879*0a6a1f1dSLionel Sambuc 	    write_code_lineno(code_file);
1880*0a6a1f1dSLionel Sambuc 	    FREE(destructor_code);
1881*0a6a1f1dSLionel Sambuc 	}
1882*0a6a1f1dSLionel Sambuc     }
1883*0a6a1f1dSLionel Sambuc     putl_code(code_file, "    }\n");
1884*0a6a1f1dSLionel Sambuc     putl_code(code_file, "}\n");
1885*0a6a1f1dSLionel Sambuc     putl_code(code_file, "#define YYDESTRUCT_IS_DECLARED 1\n");
1886*0a6a1f1dSLionel Sambuc     putl_code(code_file, "#endif\n");
1887*0a6a1f1dSLionel Sambuc 
1888*0a6a1f1dSLionel Sambuc     DO_FREE(symbol_destructor);
1889*0a6a1f1dSLionel Sambuc }
1890*0a6a1f1dSLionel Sambuc #endif
1891*0a6a1f1dSLionel Sambuc 
1892*0a6a1f1dSLionel Sambuc static void
free_itemsets(void)18934a17663cSThomas Veerman free_itemsets(void)
18944a17663cSThomas Veerman {
18954a17663cSThomas Veerman     core *cp, *next;
18964a17663cSThomas Veerman 
18974a17663cSThomas Veerman     FREE(state_table);
18984a17663cSThomas Veerman     for (cp = first_state; cp; cp = next)
18994a17663cSThomas Veerman     {
19004a17663cSThomas Veerman 	next = cp->next;
19014a17663cSThomas Veerman 	FREE(cp);
19024a17663cSThomas Veerman     }
19034a17663cSThomas Veerman }
19044a17663cSThomas Veerman 
19054a17663cSThomas Veerman static void
free_shifts(void)19064a17663cSThomas Veerman free_shifts(void)
19074a17663cSThomas Veerman {
19084a17663cSThomas Veerman     shifts *sp, *next;
19094a17663cSThomas Veerman 
19104a17663cSThomas Veerman     FREE(shift_table);
19114a17663cSThomas Veerman     for (sp = first_shift; sp; sp = next)
19124a17663cSThomas Veerman     {
19134a17663cSThomas Veerman 	next = sp->next;
19144a17663cSThomas Veerman 	FREE(sp);
19154a17663cSThomas Veerman     }
19164a17663cSThomas Veerman }
19174a17663cSThomas Veerman 
19184a17663cSThomas Veerman static void
free_reductions(void)19194a17663cSThomas Veerman free_reductions(void)
19204a17663cSThomas Veerman {
19214a17663cSThomas Veerman     reductions *rp, *next;
19224a17663cSThomas Veerman 
19234a17663cSThomas Veerman     FREE(reduction_table);
19244a17663cSThomas Veerman     for (rp = first_reduction; rp; rp = next)
19254a17663cSThomas Veerman     {
19264a17663cSThomas Veerman 	next = rp->next;
19274a17663cSThomas Veerman 	FREE(rp);
19284a17663cSThomas Veerman     }
19294a17663cSThomas Veerman }
19304a17663cSThomas Veerman 
19314a17663cSThomas Veerman static void
output_externs(FILE * fp,const char * const section[])19324a17663cSThomas Veerman output_externs(FILE * fp, const char *const section[])
19334a17663cSThomas Veerman {
19344a17663cSThomas Veerman     int i;
19354a17663cSThomas Veerman     const char *s;
19364a17663cSThomas Veerman 
19374a17663cSThomas Veerman     for (i = 0; (s = section[i]) != 0; ++i)
19384a17663cSThomas Veerman     {
1939*0a6a1f1dSLionel Sambuc 	/* prefix non-blank lines that don't start with
1940*0a6a1f1dSLionel Sambuc 	   C pre-processor directives with 'extern ' */
1941*0a6a1f1dSLionel Sambuc 	if (*s && (*s != '#'))
19424a17663cSThomas Veerman 	    fputs("extern\t", fp);
19434a17663cSThomas Veerman 	if (fp == code_file)
19444a17663cSThomas Veerman 	    ++outline;
1945*0a6a1f1dSLionel Sambuc 	fprintf(fp, "%s\n", s);
19464a17663cSThomas Veerman     }
19474a17663cSThomas Veerman }
19484a17663cSThomas Veerman 
19494a17663cSThomas Veerman void
output(void)19504a17663cSThomas Veerman output(void)
19514a17663cSThomas Veerman {
19524a17663cSThomas Veerman     FILE *fp;
19534a17663cSThomas Veerman 
19544a17663cSThomas Veerman     free_itemsets();
19554a17663cSThomas Veerman     free_shifts();
19564a17663cSThomas Veerman     free_reductions();
19574a17663cSThomas Veerman 
1958*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1959*0a6a1f1dSLionel Sambuc     output_backtracking_parser(output_file);
1960*0a6a1f1dSLionel Sambuc     if (rflag)
1961*0a6a1f1dSLionel Sambuc 	output_backtracking_parser(code_file);
1962*0a6a1f1dSLionel Sambuc #endif
1963*0a6a1f1dSLionel Sambuc 
19644a17663cSThomas Veerman     if (iflag)
19654a17663cSThomas Veerman     {
1966*0a6a1f1dSLionel Sambuc 	write_code_lineno(code_file);
19674a17663cSThomas Veerman 	++outline;
19684a17663cSThomas Veerman 	fprintf(code_file, "#include \"%s\"\n", externs_file_name);
19694a17663cSThomas Veerman 	fp = externs_file;
19704a17663cSThomas Veerman     }
19714a17663cSThomas Veerman     else
19724a17663cSThomas Veerman 	fp = code_file;
19734a17663cSThomas Veerman 
1974*0a6a1f1dSLionel Sambuc     output_prefix(fp);
19754a17663cSThomas Veerman     output_pure_parser(fp);
19764a17663cSThomas Veerman     output_stored_text(fp);
19774a17663cSThomas Veerman     output_stype(fp);
1978*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1979*0a6a1f1dSLionel Sambuc     if (locations)
1980*0a6a1f1dSLionel Sambuc 	output_ltype(fp);
1981*0a6a1f1dSLionel Sambuc #endif
19824a17663cSThomas Veerman     output_parse_decl(fp);
19834a17663cSThomas Veerman     output_lex_decl(fp);
19844a17663cSThomas Veerman     output_error_decl(fp);
1985*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
1986*0a6a1f1dSLionel Sambuc     if (destructor)
1987*0a6a1f1dSLionel Sambuc 	output_yydestruct_decl(fp);
1988*0a6a1f1dSLionel Sambuc #endif
1989*0a6a1f1dSLionel Sambuc     if (iflag || !rflag)
1990*0a6a1f1dSLionel Sambuc     {
19914a17663cSThomas Veerman 	write_section(fp, xdecls);
1992*0a6a1f1dSLionel Sambuc     }
19934a17663cSThomas Veerman 
19944a17663cSThomas Veerman     if (iflag)
19954a17663cSThomas Veerman     {
19964a17663cSThomas Veerman 	output_externs(externs_file, global_vars);
19974a17663cSThomas Veerman 	if (!pure_parser)
19984a17663cSThomas Veerman 	    output_externs(externs_file, impure_vars);
19994a17663cSThomas Veerman     }
20004a17663cSThomas Veerman 
20014a17663cSThomas Veerman     if (iflag)
20024a17663cSThomas Veerman     {
200384d9c625SLionel Sambuc 	if (dflag)
200484d9c625SLionel Sambuc 	{
20054a17663cSThomas Veerman 	    ++outline;
20064a17663cSThomas Veerman 	    fprintf(code_file, "#include \"%s\"\n", defines_file_name);
200784d9c625SLionel Sambuc 	}
200884d9c625SLionel Sambuc 	else
20094a17663cSThomas Veerman 	    output_defines(externs_file);
20104a17663cSThomas Veerman     }
20114a17663cSThomas Veerman     else
20124a17663cSThomas Veerman     {
20134a17663cSThomas Veerman 	putc_code(code_file, '\n');
20144a17663cSThomas Veerman 	output_defines(code_file);
20154a17663cSThomas Veerman     }
20164a17663cSThomas Veerman 
20174a17663cSThomas Veerman     if (dflag)
2018*0a6a1f1dSLionel Sambuc     {
2019*0a6a1f1dSLionel Sambuc 	start_defines_file();
20204a17663cSThomas Veerman 	output_defines(defines_file);
2021*0a6a1f1dSLionel Sambuc 	end_defines_file();
2022*0a6a1f1dSLionel Sambuc     }
20234a17663cSThomas Veerman 
20244a17663cSThomas Veerman     output_rule_data();
20254a17663cSThomas Veerman     output_yydefred();
2026*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
2027*0a6a1f1dSLionel Sambuc     output_accessing_symbols();
2028*0a6a1f1dSLionel Sambuc #endif
20294a17663cSThomas Veerman     output_actions();
20304a17663cSThomas Veerman     free_parser();
20314a17663cSThomas Veerman     output_debug();
20324a17663cSThomas Veerman     if (rflag)
20334a17663cSThomas Veerman     {
20344a17663cSThomas Veerman 	write_section(code_file, xdecls);
2035*0a6a1f1dSLionel Sambuc 	output_YYINT_typedef(code_file);
20364a17663cSThomas Veerman 	write_section(code_file, tables);
20374a17663cSThomas Veerman     }
20384a17663cSThomas Veerman     write_section(code_file, global_vars);
20394a17663cSThomas Veerman     if (!pure_parser)
20404a17663cSThomas Veerman     {
20414a17663cSThomas Veerman 	write_section(code_file, impure_vars);
20424a17663cSThomas Veerman     }
20434a17663cSThomas Veerman     write_section(code_file, hdr_defs);
20444a17663cSThomas Veerman     if (!pure_parser)
20454a17663cSThomas Veerman     {
20464a17663cSThomas Veerman 	write_section(code_file, hdr_vars);
20474a17663cSThomas Veerman     }
20484a17663cSThomas Veerman     output_trailing_text();
2049*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
2050*0a6a1f1dSLionel Sambuc     if (destructor)
2051*0a6a1f1dSLionel Sambuc 	output_yydestruct_impl();
2052*0a6a1f1dSLionel Sambuc #endif
20534a17663cSThomas Veerman     write_section(code_file, body_1);
20544a17663cSThomas Veerman     if (pure_parser)
20554a17663cSThomas Veerman     {
20564a17663cSThomas Veerman 	write_section(code_file, body_vars);
20574a17663cSThomas Veerman     }
20584a17663cSThomas Veerman     write_section(code_file, body_2);
2059*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
2060*0a6a1f1dSLionel Sambuc     if (initial_action)
2061*0a6a1f1dSLionel Sambuc 	output_initial_action();
2062*0a6a1f1dSLionel Sambuc #endif
20634a17663cSThomas Veerman     write_section(code_file, body_3);
20644a17663cSThomas Veerman     output_semantic_actions();
20654a17663cSThomas Veerman     write_section(code_file, trailer);
20664a17663cSThomas Veerman }
20674a17663cSThomas Veerman 
20684a17663cSThomas Veerman #ifdef NO_LEAKS
20694a17663cSThomas Veerman void
output_leaks(void)20704a17663cSThomas Veerman output_leaks(void)
20714a17663cSThomas Veerman {
20724a17663cSThomas Veerman     DO_FREE(tally);
20734a17663cSThomas Veerman     DO_FREE(width);
20744a17663cSThomas Veerman     DO_FREE(order);
20754a17663cSThomas Veerman }
20764a17663cSThomas Veerman #endif
2077