1*822ca327SBaptiste Daroussin /* $Id: output.c,v 1.101 2023/05/16 21:19:48 tom Exp $ */
298e903e7SBaptiste Daroussin
398e903e7SBaptiste Daroussin #include "defs.h"
498e903e7SBaptiste Daroussin
598e903e7SBaptiste Daroussin #define StaticOrR (rflag ? "" : "static ")
698e903e7SBaptiste Daroussin #define CountLine(fp) (!rflag || ((fp) == code_file))
798e903e7SBaptiste Daroussin
80c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
90c8de5b0SBaptiste Daroussin #define PER_STATE 3
100c8de5b0SBaptiste Daroussin #else
110c8de5b0SBaptiste Daroussin #define PER_STATE 2
120c8de5b0SBaptiste Daroussin #endif
130c8de5b0SBaptiste Daroussin
1498e903e7SBaptiste Daroussin static int nvectors;
1598e903e7SBaptiste Daroussin static int nentries;
1698e903e7SBaptiste Daroussin static Value_t **froms;
1798e903e7SBaptiste Daroussin static Value_t **tos;
180c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
190c8de5b0SBaptiste Daroussin static Value_t *conflicts = NULL;
200c8de5b0SBaptiste Daroussin static Value_t nconflicts = 0;
210c8de5b0SBaptiste Daroussin #endif
2298e903e7SBaptiste Daroussin static Value_t *tally;
2398e903e7SBaptiste Daroussin static Value_t *width;
2498e903e7SBaptiste Daroussin static Value_t *state_count;
2598e903e7SBaptiste Daroussin static Value_t *order;
2698e903e7SBaptiste Daroussin static Value_t *base;
2798e903e7SBaptiste Daroussin static Value_t *pos;
2898e903e7SBaptiste Daroussin static int maxtable;
2998e903e7SBaptiste Daroussin static Value_t *table;
3098e903e7SBaptiste Daroussin static Value_t *check;
3198e903e7SBaptiste Daroussin static int lowzero;
320c8de5b0SBaptiste Daroussin static long high;
3398e903e7SBaptiste Daroussin
3498e903e7SBaptiste Daroussin static void
putc_code(FILE * fp,int c)3598e903e7SBaptiste Daroussin putc_code(FILE * fp, int c)
3698e903e7SBaptiste Daroussin {
3798e903e7SBaptiste Daroussin if ((c == '\n') && (fp == code_file))
3898e903e7SBaptiste Daroussin ++outline;
3998e903e7SBaptiste Daroussin putc(c, fp);
4098e903e7SBaptiste Daroussin }
4198e903e7SBaptiste Daroussin
4298e903e7SBaptiste Daroussin static void
putl_code(FILE * fp,const char * s)4398e903e7SBaptiste Daroussin putl_code(FILE * fp, const char *s)
4498e903e7SBaptiste Daroussin {
4598e903e7SBaptiste Daroussin if (fp == code_file)
4698e903e7SBaptiste Daroussin ++outline;
4798e903e7SBaptiste Daroussin fputs(s, fp);
4898e903e7SBaptiste Daroussin }
4998e903e7SBaptiste Daroussin
5098e903e7SBaptiste Daroussin static void
puts_code(FILE * fp,const char * s)5198e903e7SBaptiste Daroussin puts_code(FILE * fp, const char *s)
5298e903e7SBaptiste Daroussin {
5398e903e7SBaptiste Daroussin fputs(s, fp);
5498e903e7SBaptiste Daroussin }
5598e903e7SBaptiste Daroussin
5698e903e7SBaptiste Daroussin static void
puts_param_types(FILE * fp,param * list,int more)5711fce282SBaptiste Daroussin puts_param_types(FILE * fp, param *list, int more)
5811fce282SBaptiste Daroussin {
5911fce282SBaptiste Daroussin param *p;
6011fce282SBaptiste Daroussin
6111fce282SBaptiste Daroussin if (list != 0)
6211fce282SBaptiste Daroussin {
6311fce282SBaptiste Daroussin for (p = list; p; p = p->next)
6411fce282SBaptiste Daroussin {
6511fce282SBaptiste Daroussin size_t len_type = strlen(p->type);
6611fce282SBaptiste Daroussin fprintf(fp, "%s%s%s%s%s", p->type,
6711fce282SBaptiste Daroussin (((len_type != 0) && (p->type[len_type - 1] == '*'))
6811fce282SBaptiste Daroussin ? ""
6911fce282SBaptiste Daroussin : " "),
7011fce282SBaptiste Daroussin p->name, p->type2,
7111fce282SBaptiste Daroussin ((more || p->next) ? ", " : ""));
7211fce282SBaptiste Daroussin }
7311fce282SBaptiste Daroussin }
7411fce282SBaptiste Daroussin else
7511fce282SBaptiste Daroussin {
7611fce282SBaptiste Daroussin if (!more)
7711fce282SBaptiste Daroussin fprintf(fp, "void");
7811fce282SBaptiste Daroussin }
7911fce282SBaptiste Daroussin }
8011fce282SBaptiste Daroussin
8111fce282SBaptiste Daroussin static void
puts_param_names(FILE * fp,param * list,int more)8211fce282SBaptiste Daroussin puts_param_names(FILE * fp, param *list, int more)
8311fce282SBaptiste Daroussin {
8411fce282SBaptiste Daroussin param *p;
8511fce282SBaptiste Daroussin
8611fce282SBaptiste Daroussin for (p = list; p; p = p->next)
8711fce282SBaptiste Daroussin {
8811fce282SBaptiste Daroussin fprintf(fp, "%s%s", p->name,
8911fce282SBaptiste Daroussin ((more || p->next) ? ", " : ""));
9011fce282SBaptiste Daroussin }
9111fce282SBaptiste Daroussin }
9211fce282SBaptiste Daroussin
9311fce282SBaptiste Daroussin static void
write_code_lineno(FILE * fp)9498e903e7SBaptiste Daroussin write_code_lineno(FILE * fp)
9598e903e7SBaptiste Daroussin {
9698e903e7SBaptiste Daroussin if (!lflag && (fp == code_file))
9798e903e7SBaptiste Daroussin {
9898e903e7SBaptiste Daroussin ++outline;
998e022d3cSDag-Erling Smørgrav fprintf_lineno(fp, outline + 1, code_file_name);
10098e903e7SBaptiste Daroussin }
10198e903e7SBaptiste Daroussin }
10298e903e7SBaptiste Daroussin
10398e903e7SBaptiste Daroussin static void
write_input_lineno(void)10498e903e7SBaptiste Daroussin write_input_lineno(void)
10598e903e7SBaptiste Daroussin {
10698e903e7SBaptiste Daroussin if (!lflag)
10798e903e7SBaptiste Daroussin {
10898e903e7SBaptiste Daroussin ++outline;
1098e022d3cSDag-Erling Smørgrav fprintf_lineno(code_file, lineno, input_file_name);
11098e903e7SBaptiste Daroussin }
11198e903e7SBaptiste Daroussin }
11298e903e7SBaptiste Daroussin
11398e903e7SBaptiste Daroussin static void
define_prefixed(FILE * fp,const char * name)11498e903e7SBaptiste Daroussin define_prefixed(FILE * fp, const char *name)
11598e903e7SBaptiste Daroussin {
11698e903e7SBaptiste Daroussin int bump_line = CountLine(fp);
11798e903e7SBaptiste Daroussin if (bump_line)
11898e903e7SBaptiste Daroussin ++outline;
11998e903e7SBaptiste Daroussin fprintf(fp, "\n");
12098e903e7SBaptiste Daroussin
12198e903e7SBaptiste Daroussin if (bump_line)
12298e903e7SBaptiste Daroussin ++outline;
12398e903e7SBaptiste Daroussin fprintf(fp, "#ifndef %s\n", name);
12498e903e7SBaptiste Daroussin
12598e903e7SBaptiste Daroussin if (bump_line)
12698e903e7SBaptiste Daroussin ++outline;
12798e903e7SBaptiste Daroussin fprintf(fp, "#define %-10s %s%s\n", name, symbol_prefix, name + 2);
12898e903e7SBaptiste Daroussin
12998e903e7SBaptiste Daroussin if (bump_line)
13098e903e7SBaptiste Daroussin ++outline;
13198e903e7SBaptiste Daroussin fprintf(fp, "#endif /* %s */\n", name);
13298e903e7SBaptiste Daroussin }
13398e903e7SBaptiste Daroussin
13498e903e7SBaptiste Daroussin static void
output_prefix(FILE * fp)13598e903e7SBaptiste Daroussin output_prefix(FILE * fp)
13698e903e7SBaptiste Daroussin {
13798e903e7SBaptiste Daroussin if (symbol_prefix == NULL)
13898e903e7SBaptiste Daroussin {
13998e903e7SBaptiste Daroussin symbol_prefix = "yy";
14098e903e7SBaptiste Daroussin }
14198e903e7SBaptiste Daroussin else
14298e903e7SBaptiste Daroussin {
14398e903e7SBaptiste Daroussin define_prefixed(fp, "yyparse");
14498e903e7SBaptiste Daroussin define_prefixed(fp, "yylex");
14598e903e7SBaptiste Daroussin define_prefixed(fp, "yyerror");
14698e903e7SBaptiste Daroussin define_prefixed(fp, "yychar");
14798e903e7SBaptiste Daroussin define_prefixed(fp, "yyval");
14898e903e7SBaptiste Daroussin define_prefixed(fp, "yylval");
14998e903e7SBaptiste Daroussin define_prefixed(fp, "yydebug");
15098e903e7SBaptiste Daroussin define_prefixed(fp, "yynerrs");
15198e903e7SBaptiste Daroussin define_prefixed(fp, "yyerrflag");
15298e903e7SBaptiste Daroussin define_prefixed(fp, "yylhs");
15398e903e7SBaptiste Daroussin define_prefixed(fp, "yylen");
15498e903e7SBaptiste Daroussin define_prefixed(fp, "yydefred");
1550c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
1560c8de5b0SBaptiste Daroussin define_prefixed(fp, "yystos");
1570c8de5b0SBaptiste Daroussin #endif
15898e903e7SBaptiste Daroussin define_prefixed(fp, "yydgoto");
15998e903e7SBaptiste Daroussin define_prefixed(fp, "yysindex");
16098e903e7SBaptiste Daroussin define_prefixed(fp, "yyrindex");
16198e903e7SBaptiste Daroussin define_prefixed(fp, "yygindex");
16298e903e7SBaptiste Daroussin define_prefixed(fp, "yytable");
16398e903e7SBaptiste Daroussin define_prefixed(fp, "yycheck");
16498e903e7SBaptiste Daroussin define_prefixed(fp, "yyname");
16598e903e7SBaptiste Daroussin define_prefixed(fp, "yyrule");
1660c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
1670c8de5b0SBaptiste Daroussin if (locations)
1680c8de5b0SBaptiste Daroussin {
1690c8de5b0SBaptiste Daroussin define_prefixed(fp, "yyloc");
1700c8de5b0SBaptiste Daroussin define_prefixed(fp, "yylloc");
1710c8de5b0SBaptiste Daroussin }
1720c8de5b0SBaptiste Daroussin putc_code(fp, '\n');
1730c8de5b0SBaptiste Daroussin putl_code(fp, "#if YYBTYACC\n");
1740c8de5b0SBaptiste Daroussin
1750c8de5b0SBaptiste Daroussin define_prefixed(fp, "yycindex");
1760c8de5b0SBaptiste Daroussin define_prefixed(fp, "yyctable");
1770c8de5b0SBaptiste Daroussin
1780c8de5b0SBaptiste Daroussin putc_code(fp, '\n');
1790c8de5b0SBaptiste Daroussin putl_code(fp, "#endif /* YYBTYACC */\n");
1800c8de5b0SBaptiste Daroussin putc_code(fp, '\n');
1810c8de5b0SBaptiste Daroussin #endif
18298e903e7SBaptiste Daroussin }
18398e903e7SBaptiste Daroussin if (CountLine(fp))
18498e903e7SBaptiste Daroussin ++outline;
18598e903e7SBaptiste Daroussin fprintf(fp, "#define YYPREFIX \"%s\"\n", symbol_prefix);
18698e903e7SBaptiste Daroussin }
18798e903e7SBaptiste Daroussin
18898e903e7SBaptiste Daroussin static void
output_code_lines(FILE * fp,int cl)189b53bb29fSJung-uk Kim output_code_lines(FILE * fp, int cl)
190b53bb29fSJung-uk Kim {
191b53bb29fSJung-uk Kim if (code_lines[cl].lines != NULL)
192b53bb29fSJung-uk Kim {
193b53bb29fSJung-uk Kim if (fp == code_file)
194b53bb29fSJung-uk Kim {
195b53bb29fSJung-uk Kim outline += (int)code_lines[cl].num;
196b53bb29fSJung-uk Kim outline += 3;
197b53bb29fSJung-uk Kim fprintf(fp, "\n");
198b53bb29fSJung-uk Kim }
199b53bb29fSJung-uk Kim fprintf(fp, "/* %%code \"%s\" block start */\n", code_lines[cl].name);
200b53bb29fSJung-uk Kim fputs(code_lines[cl].lines, fp);
201b53bb29fSJung-uk Kim fprintf(fp, "/* %%code \"%s\" block end */\n", code_lines[cl].name);
202b53bb29fSJung-uk Kim if (fp == code_file)
203b53bb29fSJung-uk Kim {
204b53bb29fSJung-uk Kim write_code_lineno(fp);
205b53bb29fSJung-uk Kim }
206b53bb29fSJung-uk Kim }
207b53bb29fSJung-uk Kim }
208b53bb29fSJung-uk Kim
209b53bb29fSJung-uk Kim static void
output_newline(void)21098e903e7SBaptiste Daroussin output_newline(void)
21198e903e7SBaptiste Daroussin {
21298e903e7SBaptiste Daroussin if (!rflag)
21398e903e7SBaptiste Daroussin ++outline;
21498e903e7SBaptiste Daroussin putc('\n', output_file);
21598e903e7SBaptiste Daroussin }
21698e903e7SBaptiste Daroussin
21798e903e7SBaptiste Daroussin static void
output_line(const char * value)21898e903e7SBaptiste Daroussin output_line(const char *value)
21998e903e7SBaptiste Daroussin {
22098e903e7SBaptiste Daroussin fputs(value, output_file);
22198e903e7SBaptiste Daroussin output_newline();
22298e903e7SBaptiste Daroussin }
22398e903e7SBaptiste Daroussin
22498e903e7SBaptiste Daroussin static void
output_int(int value)22598e903e7SBaptiste Daroussin output_int(int value)
22698e903e7SBaptiste Daroussin {
22798e903e7SBaptiste Daroussin fprintf(output_file, "%5d,", value);
22898e903e7SBaptiste Daroussin }
22998e903e7SBaptiste Daroussin
23098e903e7SBaptiste Daroussin static void
start_int_table(const char * name,int value)23198e903e7SBaptiste Daroussin start_int_table(const char *name, int value)
23298e903e7SBaptiste Daroussin {
23398e903e7SBaptiste Daroussin int need = 34 - (int)(strlen(symbol_prefix) + strlen(name));
23498e903e7SBaptiste Daroussin
23598e903e7SBaptiste Daroussin if (need < 6)
23698e903e7SBaptiste Daroussin need = 6;
23798e903e7SBaptiste Daroussin fprintf(output_file,
2380c8de5b0SBaptiste Daroussin "%sconst YYINT %s%s[] = {%*d,",
23998e903e7SBaptiste Daroussin StaticOrR, symbol_prefix, name, need, value);
24098e903e7SBaptiste Daroussin }
24198e903e7SBaptiste Daroussin
24298e903e7SBaptiste Daroussin static void
start_str_table(const char * name)24398e903e7SBaptiste Daroussin start_str_table(const char *name)
24498e903e7SBaptiste Daroussin {
24598e903e7SBaptiste Daroussin fprintf(output_file,
2460c8de5b0SBaptiste Daroussin "%sconst char *const %s%s[] = {",
2470c8de5b0SBaptiste Daroussin StaticOrR, symbol_prefix, name);
24898e903e7SBaptiste Daroussin output_newline();
24998e903e7SBaptiste Daroussin }
25098e903e7SBaptiste Daroussin
25198e903e7SBaptiste Daroussin static void
end_table(void)25298e903e7SBaptiste Daroussin end_table(void)
25398e903e7SBaptiste Daroussin {
25498e903e7SBaptiste Daroussin output_newline();
25598e903e7SBaptiste Daroussin output_line("};");
25698e903e7SBaptiste Daroussin }
25798e903e7SBaptiste Daroussin
25898e903e7SBaptiste Daroussin static void
output_stype(FILE * fp)2592aca18c7SJung-uk Kim output_stype(FILE * fp)
2602aca18c7SJung-uk Kim {
2612aca18c7SJung-uk Kim if (!unionized && ntags == 0)
2622aca18c7SJung-uk Kim {
2632aca18c7SJung-uk Kim putc_code(fp, '\n');
2642aca18c7SJung-uk Kim putl_code(fp, "#if "
2652aca18c7SJung-uk Kim "! defined(YYSTYPE) && "
2662aca18c7SJung-uk Kim "! defined(YYSTYPE_IS_DECLARED)\n");
2672aca18c7SJung-uk Kim putl_code(fp, "/* Default: YYSTYPE is the semantic value type. */\n");
2682aca18c7SJung-uk Kim putl_code(fp, "typedef int YYSTYPE;\n");
2692aca18c7SJung-uk Kim putl_code(fp, "# define YYSTYPE_IS_DECLARED 1\n");
2702aca18c7SJung-uk Kim putl_code(fp, "#endif\n");
2712aca18c7SJung-uk Kim }
2722aca18c7SJung-uk Kim }
2732aca18c7SJung-uk Kim
2742aca18c7SJung-uk Kim #if defined(YYBTYACC)
2752aca18c7SJung-uk Kim static void
output_ltype(FILE * fp)2762aca18c7SJung-uk Kim output_ltype(FILE * fp)
2772aca18c7SJung-uk Kim {
2782aca18c7SJung-uk Kim putc_code(fp, '\n');
2792aca18c7SJung-uk Kim putl_code(fp, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n");
2802aca18c7SJung-uk Kim putl_code(fp, "/* Default: YYLTYPE is the text position type. */\n");
2812aca18c7SJung-uk Kim putl_code(fp, "typedef struct YYLTYPE\n");
2822aca18c7SJung-uk Kim putl_code(fp, "{\n");
2832aca18c7SJung-uk Kim putl_code(fp, " int first_line;\n");
2842aca18c7SJung-uk Kim putl_code(fp, " int first_column;\n");
2852aca18c7SJung-uk Kim putl_code(fp, " int last_line;\n");
2862aca18c7SJung-uk Kim putl_code(fp, " int last_column;\n");
2872aca18c7SJung-uk Kim putl_code(fp, " unsigned source;\n");
2882aca18c7SJung-uk Kim putl_code(fp, "} YYLTYPE;\n");
2892aca18c7SJung-uk Kim putl_code(fp, "#define YYLTYPE_IS_DECLARED 1\n");
2902aca18c7SJung-uk Kim putl_code(fp, "#endif\n");
2912aca18c7SJung-uk Kim putl_code(fp, "#define YYRHSLOC(rhs, k) ((rhs)[k])\n");
2922aca18c7SJung-uk Kim }
2932aca18c7SJung-uk Kim #endif
2942aca18c7SJung-uk Kim
2952aca18c7SJung-uk Kim static void
output_YYINT_typedef(FILE * fp)2960c8de5b0SBaptiste Daroussin output_YYINT_typedef(FILE * fp)
2970c8de5b0SBaptiste Daroussin {
2980c8de5b0SBaptiste Daroussin /* generate the type used to index the various parser tables */
2990c8de5b0SBaptiste Daroussin if (CountLine(fp))
3000c8de5b0SBaptiste Daroussin ++outline;
3010c8de5b0SBaptiste Daroussin fprintf(fp, "typedef %s YYINT;\n", CONCAT1("", YYINT));
3020c8de5b0SBaptiste Daroussin }
3030c8de5b0SBaptiste Daroussin
3040c8de5b0SBaptiste Daroussin static void
output_rule_data(void)30598e903e7SBaptiste Daroussin output_rule_data(void)
30698e903e7SBaptiste Daroussin {
30798e903e7SBaptiste Daroussin int i;
30898e903e7SBaptiste Daroussin int j;
30998e903e7SBaptiste Daroussin
3100c8de5b0SBaptiste Daroussin output_YYINT_typedef(output_file);
3110c8de5b0SBaptiste Daroussin
31298e903e7SBaptiste Daroussin start_int_table("lhs", symbol_value[start_symbol]);
31398e903e7SBaptiste Daroussin
31498e903e7SBaptiste Daroussin j = 10;
31598e903e7SBaptiste Daroussin for (i = 3; i < nrules; i++)
31698e903e7SBaptiste Daroussin {
31798e903e7SBaptiste Daroussin if (j >= 10)
31898e903e7SBaptiste Daroussin {
31998e903e7SBaptiste Daroussin output_newline();
32098e903e7SBaptiste Daroussin j = 1;
32198e903e7SBaptiste Daroussin }
32298e903e7SBaptiste Daroussin else
32398e903e7SBaptiste Daroussin ++j;
32498e903e7SBaptiste Daroussin
32598e903e7SBaptiste Daroussin output_int(symbol_value[rlhs[i]]);
32698e903e7SBaptiste Daroussin }
32798e903e7SBaptiste Daroussin end_table();
32898e903e7SBaptiste Daroussin
32998e903e7SBaptiste Daroussin start_int_table("len", 2);
33098e903e7SBaptiste Daroussin
33198e903e7SBaptiste Daroussin j = 10;
33298e903e7SBaptiste Daroussin for (i = 3; i < nrules; i++)
33398e903e7SBaptiste Daroussin {
33498e903e7SBaptiste Daroussin if (j >= 10)
33598e903e7SBaptiste Daroussin {
33698e903e7SBaptiste Daroussin output_newline();
33798e903e7SBaptiste Daroussin j = 1;
33898e903e7SBaptiste Daroussin }
33998e903e7SBaptiste Daroussin else
34098e903e7SBaptiste Daroussin j++;
34198e903e7SBaptiste Daroussin
34298e903e7SBaptiste Daroussin output_int(rrhs[i + 1] - rrhs[i] - 1);
34398e903e7SBaptiste Daroussin }
34498e903e7SBaptiste Daroussin end_table();
34598e903e7SBaptiste Daroussin }
34698e903e7SBaptiste Daroussin
34798e903e7SBaptiste Daroussin static void
output_yydefred(void)34898e903e7SBaptiste Daroussin output_yydefred(void)
34998e903e7SBaptiste Daroussin {
35098e903e7SBaptiste Daroussin int i, j;
35198e903e7SBaptiste Daroussin
35298e903e7SBaptiste Daroussin start_int_table("defred", (defred[0] ? defred[0] - 2 : 0));
35398e903e7SBaptiste Daroussin
35498e903e7SBaptiste Daroussin j = 10;
35598e903e7SBaptiste Daroussin for (i = 1; i < nstates; i++)
35698e903e7SBaptiste Daroussin {
35798e903e7SBaptiste Daroussin if (j < 10)
35898e903e7SBaptiste Daroussin ++j;
35998e903e7SBaptiste Daroussin else
36098e903e7SBaptiste Daroussin {
36198e903e7SBaptiste Daroussin output_newline();
36298e903e7SBaptiste Daroussin j = 1;
36398e903e7SBaptiste Daroussin }
36498e903e7SBaptiste Daroussin
36598e903e7SBaptiste Daroussin output_int((defred[i] ? defred[i] - 2 : 0));
36698e903e7SBaptiste Daroussin }
36798e903e7SBaptiste Daroussin
36898e903e7SBaptiste Daroussin end_table();
36998e903e7SBaptiste Daroussin }
37098e903e7SBaptiste Daroussin
3710c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
3720c8de5b0SBaptiste Daroussin static void
output_accessing_symbols(void)3730c8de5b0SBaptiste Daroussin output_accessing_symbols(void)
3740c8de5b0SBaptiste Daroussin {
3758e022d3cSDag-Erling Smørgrav if (nstates != 0)
3768e022d3cSDag-Erling Smørgrav {
3770c8de5b0SBaptiste Daroussin int i, j;
3780c8de5b0SBaptiste Daroussin int *translate;
3790c8de5b0SBaptiste Daroussin
3808e022d3cSDag-Erling Smørgrav translate = TCMALLOC(int, nstates);
3810c8de5b0SBaptiste Daroussin NO_SPACE(translate);
3820c8de5b0SBaptiste Daroussin
3830c8de5b0SBaptiste Daroussin for (i = 0; i < nstates; ++i)
3840c8de5b0SBaptiste Daroussin {
3850c8de5b0SBaptiste Daroussin int gsymb = accessing_symbol[i];
3860c8de5b0SBaptiste Daroussin
3870c8de5b0SBaptiste Daroussin translate[i] = symbol_pval[gsymb];
3880c8de5b0SBaptiste Daroussin }
3890c8de5b0SBaptiste Daroussin
3902aca18c7SJung-uk Kim putl_code(output_file,
3912aca18c7SJung-uk Kim "#if defined(YYDESTRUCT_CALL) || defined(YYSTYPE_TOSTRING)\n");
3920c8de5b0SBaptiste Daroussin /* yystos[] may be unused, depending on compile-time defines */
3930c8de5b0SBaptiste Daroussin start_int_table("stos", translate[0]);
3940c8de5b0SBaptiste Daroussin
3950c8de5b0SBaptiste Daroussin j = 10;
3960c8de5b0SBaptiste Daroussin for (i = 1; i < nstates; ++i)
3970c8de5b0SBaptiste Daroussin {
3980c8de5b0SBaptiste Daroussin if (j < 10)
3990c8de5b0SBaptiste Daroussin ++j;
4000c8de5b0SBaptiste Daroussin else
4010c8de5b0SBaptiste Daroussin {
4020c8de5b0SBaptiste Daroussin output_newline();
4030c8de5b0SBaptiste Daroussin j = 1;
4040c8de5b0SBaptiste Daroussin }
4050c8de5b0SBaptiste Daroussin
4060c8de5b0SBaptiste Daroussin output_int(translate[i]);
4070c8de5b0SBaptiste Daroussin }
4080c8de5b0SBaptiste Daroussin
4090c8de5b0SBaptiste Daroussin end_table();
4100c8de5b0SBaptiste Daroussin FREE(translate);
4112aca18c7SJung-uk Kim putl_code(output_file,
4122aca18c7SJung-uk Kim "#endif /* YYDESTRUCT_CALL || YYSTYPE_TOSTRING */\n");
4130c8de5b0SBaptiste Daroussin }
4140c8de5b0SBaptiste Daroussin }
4150c8de5b0SBaptiste Daroussin
4160c8de5b0SBaptiste Daroussin static Value_t
find_conflict_base(int cbase)417497dcf4cSBaptiste Daroussin find_conflict_base(int cbase)
4180c8de5b0SBaptiste Daroussin {
419497dcf4cSBaptiste Daroussin int i, j;
4200c8de5b0SBaptiste Daroussin
4210c8de5b0SBaptiste Daroussin for (i = 0; i < cbase; i++)
4220c8de5b0SBaptiste Daroussin {
4230c8de5b0SBaptiste Daroussin for (j = 0; j + cbase < nconflicts; j++)
4240c8de5b0SBaptiste Daroussin {
4250c8de5b0SBaptiste Daroussin if (conflicts[i + j] != conflicts[cbase + j])
4260c8de5b0SBaptiste Daroussin break;
4270c8de5b0SBaptiste Daroussin }
4280c8de5b0SBaptiste Daroussin if (j + cbase >= nconflicts)
429497dcf4cSBaptiste Daroussin break;
4300c8de5b0SBaptiste Daroussin }
431497dcf4cSBaptiste Daroussin return (Value_t)i;
4320c8de5b0SBaptiste Daroussin }
4330c8de5b0SBaptiste Daroussin #endif
4340c8de5b0SBaptiste Daroussin
43598e903e7SBaptiste Daroussin static void
token_actions(void)43698e903e7SBaptiste Daroussin token_actions(void)
43798e903e7SBaptiste Daroussin {
43898e903e7SBaptiste Daroussin int i, j;
43998e903e7SBaptiste Daroussin Value_t shiftcount, reducecount;
4400c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
4410c8de5b0SBaptiste Daroussin Value_t conflictcount = 0;
4420c8de5b0SBaptiste Daroussin Value_t csym = -1;
4430c8de5b0SBaptiste Daroussin Value_t cbase = 0;
4440c8de5b0SBaptiste Daroussin #endif
4458e022d3cSDag-Erling Smørgrav Value_t max, min;
44698e903e7SBaptiste Daroussin Value_t *actionrow, *r, *s;
44798e903e7SBaptiste Daroussin action *p;
44898e903e7SBaptiste Daroussin
4490c8de5b0SBaptiste Daroussin actionrow = NEW2(PER_STATE * ntokens, Value_t);
45098e903e7SBaptiste Daroussin for (i = 0; i < nstates; ++i)
45198e903e7SBaptiste Daroussin {
45298e903e7SBaptiste Daroussin if (parser[i])
45398e903e7SBaptiste Daroussin {
4540c8de5b0SBaptiste Daroussin for (j = 0; j < PER_STATE * ntokens; ++j)
45598e903e7SBaptiste Daroussin actionrow[j] = 0;
45698e903e7SBaptiste Daroussin
45798e903e7SBaptiste Daroussin shiftcount = 0;
45898e903e7SBaptiste Daroussin reducecount = 0;
4590c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
4600c8de5b0SBaptiste Daroussin if (backtrack)
4610c8de5b0SBaptiste Daroussin {
4620c8de5b0SBaptiste Daroussin conflictcount = 0;
4630c8de5b0SBaptiste Daroussin csym = -1;
4640c8de5b0SBaptiste Daroussin cbase = nconflicts;
4650c8de5b0SBaptiste Daroussin }
4660c8de5b0SBaptiste Daroussin #endif
46798e903e7SBaptiste Daroussin for (p = parser[i]; p; p = p->next)
46898e903e7SBaptiste Daroussin {
4690c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
4700c8de5b0SBaptiste Daroussin if (backtrack)
4710c8de5b0SBaptiste Daroussin {
4720c8de5b0SBaptiste Daroussin if (csym != -1 && csym != p->symbol)
4730c8de5b0SBaptiste Daroussin {
4740c8de5b0SBaptiste Daroussin conflictcount++;
4750c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = -1;
4760c8de5b0SBaptiste Daroussin j = find_conflict_base(cbase);
4770c8de5b0SBaptiste Daroussin actionrow[csym + 2 * ntokens] = (Value_t)(j + 1);
4780c8de5b0SBaptiste Daroussin if (j == cbase)
4790c8de5b0SBaptiste Daroussin {
4800c8de5b0SBaptiste Daroussin cbase = nconflicts;
4810c8de5b0SBaptiste Daroussin }
4820c8de5b0SBaptiste Daroussin else
4830c8de5b0SBaptiste Daroussin {
4840c8de5b0SBaptiste Daroussin if (conflicts[cbase] == -1)
4850c8de5b0SBaptiste Daroussin cbase++;
4860c8de5b0SBaptiste Daroussin nconflicts = cbase;
4870c8de5b0SBaptiste Daroussin }
4880c8de5b0SBaptiste Daroussin csym = -1;
4890c8de5b0SBaptiste Daroussin }
4900c8de5b0SBaptiste Daroussin }
4910c8de5b0SBaptiste Daroussin #endif
49298e903e7SBaptiste Daroussin if (p->suppressed == 0)
49398e903e7SBaptiste Daroussin {
49498e903e7SBaptiste Daroussin if (p->action_code == SHIFT)
49598e903e7SBaptiste Daroussin {
49698e903e7SBaptiste Daroussin ++shiftcount;
49798e903e7SBaptiste Daroussin actionrow[p->symbol] = p->number;
49898e903e7SBaptiste Daroussin }
49998e903e7SBaptiste Daroussin else if (p->action_code == REDUCE && p->number != defred[i])
50098e903e7SBaptiste Daroussin {
50198e903e7SBaptiste Daroussin ++reducecount;
50298e903e7SBaptiste Daroussin actionrow[p->symbol + ntokens] = p->number;
50398e903e7SBaptiste Daroussin }
50498e903e7SBaptiste Daroussin }
5050c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
5060c8de5b0SBaptiste Daroussin else if (backtrack && p->suppressed == 1)
5070c8de5b0SBaptiste Daroussin {
5080c8de5b0SBaptiste Daroussin csym = p->symbol;
5090c8de5b0SBaptiste Daroussin if (p->action_code == SHIFT)
5100c8de5b0SBaptiste Daroussin {
5110c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = p->number;
51298e903e7SBaptiste Daroussin }
5130c8de5b0SBaptiste Daroussin else if (p->action_code == REDUCE && p->number != defred[i])
5140c8de5b0SBaptiste Daroussin {
5150c8de5b0SBaptiste Daroussin if (cbase == nconflicts)
5160c8de5b0SBaptiste Daroussin {
5170c8de5b0SBaptiste Daroussin if (cbase)
5180c8de5b0SBaptiste Daroussin cbase--;
5190c8de5b0SBaptiste Daroussin else
5200c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = -1;
5210c8de5b0SBaptiste Daroussin }
5220c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = (Value_t)(p->number - 2);
5230c8de5b0SBaptiste Daroussin }
5240c8de5b0SBaptiste Daroussin }
5250c8de5b0SBaptiste Daroussin #endif
5260c8de5b0SBaptiste Daroussin }
5270c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
5280c8de5b0SBaptiste Daroussin if (backtrack && csym != -1)
5290c8de5b0SBaptiste Daroussin {
5300c8de5b0SBaptiste Daroussin conflictcount++;
5310c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = -1;
5320c8de5b0SBaptiste Daroussin j = find_conflict_base(cbase);
5330c8de5b0SBaptiste Daroussin actionrow[csym + 2 * ntokens] = (Value_t)(j + 1);
5340c8de5b0SBaptiste Daroussin if (j == cbase)
5350c8de5b0SBaptiste Daroussin {
5360c8de5b0SBaptiste Daroussin cbase = nconflicts;
5370c8de5b0SBaptiste Daroussin }
5380c8de5b0SBaptiste Daroussin else
5390c8de5b0SBaptiste Daroussin {
5400c8de5b0SBaptiste Daroussin if (conflicts[cbase] == -1)
5410c8de5b0SBaptiste Daroussin cbase++;
5420c8de5b0SBaptiste Daroussin nconflicts = cbase;
5430c8de5b0SBaptiste Daroussin }
5440c8de5b0SBaptiste Daroussin }
5450c8de5b0SBaptiste Daroussin #endif
54698e903e7SBaptiste Daroussin
54798e903e7SBaptiste Daroussin tally[i] = shiftcount;
54898e903e7SBaptiste Daroussin tally[nstates + i] = reducecount;
5490c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
5500c8de5b0SBaptiste Daroussin if (backtrack)
5510c8de5b0SBaptiste Daroussin tally[2 * nstates + i] = conflictcount;
5520c8de5b0SBaptiste Daroussin #endif
55398e903e7SBaptiste Daroussin width[i] = 0;
55498e903e7SBaptiste Daroussin width[nstates + i] = 0;
5550c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
5560c8de5b0SBaptiste Daroussin if (backtrack)
5570c8de5b0SBaptiste Daroussin width[2 * nstates + i] = 0;
5580c8de5b0SBaptiste Daroussin #endif
55998e903e7SBaptiste Daroussin if (shiftcount > 0)
56098e903e7SBaptiste Daroussin {
56198e903e7SBaptiste Daroussin froms[i] = r = NEW2(shiftcount, Value_t);
56298e903e7SBaptiste Daroussin tos[i] = s = NEW2(shiftcount, Value_t);
5630c8de5b0SBaptiste Daroussin min = MAXYYINT;
56498e903e7SBaptiste Daroussin max = 0;
56598e903e7SBaptiste Daroussin for (j = 0; j < ntokens; ++j)
56698e903e7SBaptiste Daroussin {
56798e903e7SBaptiste Daroussin if (actionrow[j])
56898e903e7SBaptiste Daroussin {
56998e903e7SBaptiste Daroussin if (min > symbol_value[j])
57098e903e7SBaptiste Daroussin min = symbol_value[j];
57198e903e7SBaptiste Daroussin if (max < symbol_value[j])
57298e903e7SBaptiste Daroussin max = symbol_value[j];
57398e903e7SBaptiste Daroussin *r++ = symbol_value[j];
57498e903e7SBaptiste Daroussin *s++ = actionrow[j];
57598e903e7SBaptiste Daroussin }
57698e903e7SBaptiste Daroussin }
57798e903e7SBaptiste Daroussin width[i] = (Value_t)(max - min + 1);
57898e903e7SBaptiste Daroussin }
57998e903e7SBaptiste Daroussin if (reducecount > 0)
58098e903e7SBaptiste Daroussin {
58198e903e7SBaptiste Daroussin froms[nstates + i] = r = NEW2(reducecount, Value_t);
58298e903e7SBaptiste Daroussin tos[nstates + i] = s = NEW2(reducecount, Value_t);
5830c8de5b0SBaptiste Daroussin min = MAXYYINT;
58498e903e7SBaptiste Daroussin max = 0;
58598e903e7SBaptiste Daroussin for (j = 0; j < ntokens; ++j)
58698e903e7SBaptiste Daroussin {
58798e903e7SBaptiste Daroussin if (actionrow[ntokens + j])
58898e903e7SBaptiste Daroussin {
58998e903e7SBaptiste Daroussin if (min > symbol_value[j])
59098e903e7SBaptiste Daroussin min = symbol_value[j];
59198e903e7SBaptiste Daroussin if (max < symbol_value[j])
59298e903e7SBaptiste Daroussin max = symbol_value[j];
59398e903e7SBaptiste Daroussin *r++ = symbol_value[j];
59498e903e7SBaptiste Daroussin *s++ = (Value_t)(actionrow[ntokens + j] - 2);
59598e903e7SBaptiste Daroussin }
59698e903e7SBaptiste Daroussin }
59798e903e7SBaptiste Daroussin width[nstates + i] = (Value_t)(max - min + 1);
59898e903e7SBaptiste Daroussin }
5990c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
6000c8de5b0SBaptiste Daroussin if (backtrack && conflictcount > 0)
6010c8de5b0SBaptiste Daroussin {
6020c8de5b0SBaptiste Daroussin froms[2 * nstates + i] = r = NEW2(conflictcount, Value_t);
6030c8de5b0SBaptiste Daroussin tos[2 * nstates + i] = s = NEW2(conflictcount, Value_t);
6040c8de5b0SBaptiste Daroussin min = MAXYYINT;
6050c8de5b0SBaptiste Daroussin max = 0;
6060c8de5b0SBaptiste Daroussin for (j = 0; j < ntokens; ++j)
6070c8de5b0SBaptiste Daroussin {
6080c8de5b0SBaptiste Daroussin if (actionrow[2 * ntokens + j])
6090c8de5b0SBaptiste Daroussin {
6100c8de5b0SBaptiste Daroussin if (min > symbol_value[j])
6110c8de5b0SBaptiste Daroussin min = symbol_value[j];
6120c8de5b0SBaptiste Daroussin if (max < symbol_value[j])
6130c8de5b0SBaptiste Daroussin max = symbol_value[j];
6140c8de5b0SBaptiste Daroussin *r++ = symbol_value[j];
6150c8de5b0SBaptiste Daroussin *s++ = (Value_t)(actionrow[2 * ntokens + j] - 1);
6160c8de5b0SBaptiste Daroussin }
6170c8de5b0SBaptiste Daroussin }
6180c8de5b0SBaptiste Daroussin width[2 * nstates + i] = (Value_t)(max - min + 1);
6190c8de5b0SBaptiste Daroussin }
6200c8de5b0SBaptiste Daroussin #endif
62198e903e7SBaptiste Daroussin }
62298e903e7SBaptiste Daroussin }
62398e903e7SBaptiste Daroussin FREE(actionrow);
62498e903e7SBaptiste Daroussin }
62598e903e7SBaptiste Daroussin
62698e903e7SBaptiste Daroussin static int
default_goto(int symbol)62798e903e7SBaptiste Daroussin default_goto(int symbol)
62898e903e7SBaptiste Daroussin {
62998e903e7SBaptiste Daroussin int i;
63098e903e7SBaptiste Daroussin int m;
63198e903e7SBaptiste Daroussin int n;
63298e903e7SBaptiste Daroussin int default_state;
63398e903e7SBaptiste Daroussin int max;
63498e903e7SBaptiste Daroussin
63598e903e7SBaptiste Daroussin m = goto_map[symbol];
63698e903e7SBaptiste Daroussin n = goto_map[symbol + 1];
63798e903e7SBaptiste Daroussin
63898e903e7SBaptiste Daroussin if (m == n)
63998e903e7SBaptiste Daroussin return (0);
64098e903e7SBaptiste Daroussin
64198e903e7SBaptiste Daroussin for (i = 0; i < nstates; i++)
64298e903e7SBaptiste Daroussin state_count[i] = 0;
64398e903e7SBaptiste Daroussin
64498e903e7SBaptiste Daroussin for (i = m; i < n; i++)
64598e903e7SBaptiste Daroussin state_count[to_state[i]]++;
64698e903e7SBaptiste Daroussin
64798e903e7SBaptiste Daroussin max = 0;
64898e903e7SBaptiste Daroussin default_state = 0;
64998e903e7SBaptiste Daroussin for (i = 0; i < nstates; i++)
65098e903e7SBaptiste Daroussin {
65198e903e7SBaptiste Daroussin if (state_count[i] > max)
65298e903e7SBaptiste Daroussin {
65398e903e7SBaptiste Daroussin max = state_count[i];
65498e903e7SBaptiste Daroussin default_state = i;
65598e903e7SBaptiste Daroussin }
65698e903e7SBaptiste Daroussin }
65798e903e7SBaptiste Daroussin
65898e903e7SBaptiste Daroussin return (default_state);
65998e903e7SBaptiste Daroussin }
66098e903e7SBaptiste Daroussin
66198e903e7SBaptiste Daroussin static void
save_column(int symbol,int default_state)66298e903e7SBaptiste Daroussin save_column(int symbol, int default_state)
66398e903e7SBaptiste Daroussin {
66498e903e7SBaptiste Daroussin int i;
66598e903e7SBaptiste Daroussin int m;
66698e903e7SBaptiste Daroussin int n;
66798e903e7SBaptiste Daroussin Value_t *sp;
66898e903e7SBaptiste Daroussin Value_t *sp1;
66998e903e7SBaptiste Daroussin Value_t *sp2;
67098e903e7SBaptiste Daroussin Value_t count;
67198e903e7SBaptiste Daroussin int symno;
67298e903e7SBaptiste Daroussin
67398e903e7SBaptiste Daroussin m = goto_map[symbol];
67498e903e7SBaptiste Daroussin n = goto_map[symbol + 1];
67598e903e7SBaptiste Daroussin
67698e903e7SBaptiste Daroussin count = 0;
67798e903e7SBaptiste Daroussin for (i = m; i < n; i++)
67898e903e7SBaptiste Daroussin {
67998e903e7SBaptiste Daroussin if (to_state[i] != default_state)
68098e903e7SBaptiste Daroussin ++count;
68198e903e7SBaptiste Daroussin }
68298e903e7SBaptiste Daroussin if (count == 0)
68398e903e7SBaptiste Daroussin return;
68498e903e7SBaptiste Daroussin
6850c8de5b0SBaptiste Daroussin symno = symbol_value[symbol] + PER_STATE * nstates;
68698e903e7SBaptiste Daroussin
68798e903e7SBaptiste Daroussin froms[symno] = sp1 = sp = NEW2(count, Value_t);
68898e903e7SBaptiste Daroussin tos[symno] = sp2 = NEW2(count, Value_t);
68998e903e7SBaptiste Daroussin
69098e903e7SBaptiste Daroussin for (i = m; i < n; i++)
69198e903e7SBaptiste Daroussin {
69298e903e7SBaptiste Daroussin if (to_state[i] != default_state)
69398e903e7SBaptiste Daroussin {
69498e903e7SBaptiste Daroussin *sp1++ = from_state[i];
69598e903e7SBaptiste Daroussin *sp2++ = to_state[i];
69698e903e7SBaptiste Daroussin }
69798e903e7SBaptiste Daroussin }
69898e903e7SBaptiste Daroussin
69998e903e7SBaptiste Daroussin tally[symno] = count;
70098e903e7SBaptiste Daroussin width[symno] = (Value_t)(sp1[-1] - sp[0] + 1);
70198e903e7SBaptiste Daroussin }
70298e903e7SBaptiste Daroussin
70398e903e7SBaptiste Daroussin static void
goto_actions(void)70498e903e7SBaptiste Daroussin goto_actions(void)
70598e903e7SBaptiste Daroussin {
70698e903e7SBaptiste Daroussin int i, j, k;
70798e903e7SBaptiste Daroussin
70898e903e7SBaptiste Daroussin state_count = NEW2(nstates, Value_t);
70998e903e7SBaptiste Daroussin
71098e903e7SBaptiste Daroussin k = default_goto(start_symbol + 1);
71198e903e7SBaptiste Daroussin start_int_table("dgoto", k);
71298e903e7SBaptiste Daroussin save_column(start_symbol + 1, k);
71398e903e7SBaptiste Daroussin
71498e903e7SBaptiste Daroussin j = 10;
71598e903e7SBaptiste Daroussin for (i = start_symbol + 2; i < nsyms; i++)
71698e903e7SBaptiste Daroussin {
71798e903e7SBaptiste Daroussin if (j >= 10)
71898e903e7SBaptiste Daroussin {
71998e903e7SBaptiste Daroussin output_newline();
72098e903e7SBaptiste Daroussin j = 1;
72198e903e7SBaptiste Daroussin }
72298e903e7SBaptiste Daroussin else
72398e903e7SBaptiste Daroussin ++j;
72498e903e7SBaptiste Daroussin
72598e903e7SBaptiste Daroussin k = default_goto(i);
72698e903e7SBaptiste Daroussin output_int(k);
72798e903e7SBaptiste Daroussin save_column(i, k);
72898e903e7SBaptiste Daroussin }
72998e903e7SBaptiste Daroussin
73098e903e7SBaptiste Daroussin end_table();
73198e903e7SBaptiste Daroussin FREE(state_count);
73298e903e7SBaptiste Daroussin }
73398e903e7SBaptiste Daroussin
73498e903e7SBaptiste Daroussin static void
sort_actions(void)73598e903e7SBaptiste Daroussin sort_actions(void)
73698e903e7SBaptiste Daroussin {
73798e903e7SBaptiste Daroussin Value_t i;
73898e903e7SBaptiste Daroussin int j;
73998e903e7SBaptiste Daroussin int k;
74098e903e7SBaptiste Daroussin int t;
74198e903e7SBaptiste Daroussin int w;
74298e903e7SBaptiste Daroussin
74398e903e7SBaptiste Daroussin order = NEW2(nvectors, Value_t);
74498e903e7SBaptiste Daroussin nentries = 0;
74598e903e7SBaptiste Daroussin
74698e903e7SBaptiste Daroussin for (i = 0; i < nvectors; i++)
74798e903e7SBaptiste Daroussin {
74898e903e7SBaptiste Daroussin if (tally[i] > 0)
74998e903e7SBaptiste Daroussin {
75098e903e7SBaptiste Daroussin t = tally[i];
75198e903e7SBaptiste Daroussin w = width[i];
75298e903e7SBaptiste Daroussin j = nentries - 1;
75398e903e7SBaptiste Daroussin
75498e903e7SBaptiste Daroussin while (j >= 0 && (width[order[j]] < w))
75598e903e7SBaptiste Daroussin j--;
75698e903e7SBaptiste Daroussin
75798e903e7SBaptiste Daroussin while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
75898e903e7SBaptiste Daroussin j--;
75998e903e7SBaptiste Daroussin
76098e903e7SBaptiste Daroussin for (k = nentries - 1; k > j; k--)
76198e903e7SBaptiste Daroussin order[k + 1] = order[k];
76298e903e7SBaptiste Daroussin
76398e903e7SBaptiste Daroussin order[j + 1] = i;
76498e903e7SBaptiste Daroussin nentries++;
76598e903e7SBaptiste Daroussin }
76698e903e7SBaptiste Daroussin }
76798e903e7SBaptiste Daroussin }
76898e903e7SBaptiste Daroussin
76998e903e7SBaptiste Daroussin /* The function matching_vector determines if the vector specified by */
77098e903e7SBaptiste Daroussin /* the input parameter matches a previously considered vector. The */
77198e903e7SBaptiste Daroussin /* test at the start of the function checks if the vector represents */
77298e903e7SBaptiste Daroussin /* a row of shifts over terminal symbols or a row of reductions, or a */
77398e903e7SBaptiste Daroussin /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
77498e903e7SBaptiste Daroussin /* check if a column of shifts over a nonterminal symbols matches a */
77598e903e7SBaptiste Daroussin /* previously considered vector. Because of the nature of LR parsing */
77698e903e7SBaptiste Daroussin /* tables, no two columns can match. Therefore, the only possible */
77798e903e7SBaptiste Daroussin /* match would be between a row and a column. Such matches are */
77898e903e7SBaptiste Daroussin /* unlikely. Therefore, to save time, no attempt is made to see if a */
77998e903e7SBaptiste Daroussin /* column matches a previously considered vector. */
78098e903e7SBaptiste Daroussin /* */
78198e903e7SBaptiste Daroussin /* Matching_vector is poorly designed. The test could easily be made */
78298e903e7SBaptiste Daroussin /* faster. Also, it depends on the vectors being in a specific */
78398e903e7SBaptiste Daroussin /* order. */
7840c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
7850c8de5b0SBaptiste Daroussin /* */
7860c8de5b0SBaptiste Daroussin /* Not really any point in checking for matching conflicts -- it is */
7870c8de5b0SBaptiste Daroussin /* extremely unlikely to occur, and conflicts are (hopefully) rare. */
7880c8de5b0SBaptiste Daroussin #endif
78998e903e7SBaptiste Daroussin
79098e903e7SBaptiste Daroussin static int
matching_vector(int vector)79198e903e7SBaptiste Daroussin matching_vector(int vector)
79298e903e7SBaptiste Daroussin {
79398e903e7SBaptiste Daroussin int i;
79498e903e7SBaptiste Daroussin int k;
79598e903e7SBaptiste Daroussin int t;
79698e903e7SBaptiste Daroussin int w;
79798e903e7SBaptiste Daroussin int prev;
79898e903e7SBaptiste Daroussin
79998e903e7SBaptiste Daroussin i = order[vector];
80098e903e7SBaptiste Daroussin if (i >= 2 * nstates)
80198e903e7SBaptiste Daroussin return (-1);
80298e903e7SBaptiste Daroussin
80398e903e7SBaptiste Daroussin t = tally[i];
80498e903e7SBaptiste Daroussin w = width[i];
80598e903e7SBaptiste Daroussin
80698e903e7SBaptiste Daroussin for (prev = vector - 1; prev >= 0; prev--)
80798e903e7SBaptiste Daroussin {
8088e022d3cSDag-Erling Smørgrav int j = order[prev];
80998e903e7SBaptiste Daroussin
8108e022d3cSDag-Erling Smørgrav if (width[j] != w || tally[j] != t)
8118e022d3cSDag-Erling Smørgrav {
8128e022d3cSDag-Erling Smørgrav return (-1);
8138e022d3cSDag-Erling Smørgrav }
8148e022d3cSDag-Erling Smørgrav else
8158e022d3cSDag-Erling Smørgrav {
8168e022d3cSDag-Erling Smørgrav int match = 1;
8178e022d3cSDag-Erling Smørgrav
81898e903e7SBaptiste Daroussin for (k = 0; match && k < t; k++)
81998e903e7SBaptiste Daroussin {
82098e903e7SBaptiste Daroussin if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
82198e903e7SBaptiste Daroussin match = 0;
82298e903e7SBaptiste Daroussin }
82398e903e7SBaptiste Daroussin
82498e903e7SBaptiste Daroussin if (match)
82598e903e7SBaptiste Daroussin return (j);
82698e903e7SBaptiste Daroussin }
8278e022d3cSDag-Erling Smørgrav }
82898e903e7SBaptiste Daroussin
82998e903e7SBaptiste Daroussin return (-1);
83098e903e7SBaptiste Daroussin }
83198e903e7SBaptiste Daroussin
83298e903e7SBaptiste Daroussin static int
pack_vector(int vector)83398e903e7SBaptiste Daroussin pack_vector(int vector)
83498e903e7SBaptiste Daroussin {
83598e903e7SBaptiste Daroussin int i, j, k, l;
83698e903e7SBaptiste Daroussin int t;
8378e022d3cSDag-Erling Smørgrav Value_t loc;
83898e903e7SBaptiste Daroussin int ok;
83998e903e7SBaptiste Daroussin Value_t *from;
84098e903e7SBaptiste Daroussin Value_t *to;
84198e903e7SBaptiste Daroussin int newmax;
84298e903e7SBaptiste Daroussin
84398e903e7SBaptiste Daroussin i = order[vector];
84498e903e7SBaptiste Daroussin t = tally[i];
84598e903e7SBaptiste Daroussin assert(t);
84698e903e7SBaptiste Daroussin
84798e903e7SBaptiste Daroussin from = froms[i];
84898e903e7SBaptiste Daroussin to = tos[i];
84998e903e7SBaptiste Daroussin
85098e903e7SBaptiste Daroussin j = lowzero - from[0];
85198e903e7SBaptiste Daroussin for (k = 1; k < t; ++k)
85298e903e7SBaptiste Daroussin if (lowzero - from[k] > j)
85398e903e7SBaptiste Daroussin j = lowzero - from[k];
85498e903e7SBaptiste Daroussin for (;; ++j)
85598e903e7SBaptiste Daroussin {
85698e903e7SBaptiste Daroussin if (j == 0)
85798e903e7SBaptiste Daroussin continue;
85898e903e7SBaptiste Daroussin ok = 1;
85998e903e7SBaptiste Daroussin for (k = 0; ok && k < t; k++)
86098e903e7SBaptiste Daroussin {
8618e022d3cSDag-Erling Smørgrav loc = (Value_t)(j + from[k]);
86298e903e7SBaptiste Daroussin if (loc >= maxtable - 1)
86398e903e7SBaptiste Daroussin {
86498e903e7SBaptiste Daroussin if (loc >= MAXTABLE - 1)
86598e903e7SBaptiste Daroussin fatal("maximum table size exceeded");
86698e903e7SBaptiste Daroussin
86798e903e7SBaptiste Daroussin newmax = maxtable;
86898e903e7SBaptiste Daroussin do
86998e903e7SBaptiste Daroussin {
87098e903e7SBaptiste Daroussin newmax += 200;
87198e903e7SBaptiste Daroussin }
87298e903e7SBaptiste Daroussin while (newmax <= loc);
87398e903e7SBaptiste Daroussin
8743e066022SBaptiste Daroussin table = TREALLOC(Value_t, table, newmax);
87598e903e7SBaptiste Daroussin NO_SPACE(table);
87698e903e7SBaptiste Daroussin
8773e066022SBaptiste Daroussin check = TREALLOC(Value_t, check, newmax);
87898e903e7SBaptiste Daroussin NO_SPACE(check);
87998e903e7SBaptiste Daroussin
88098e903e7SBaptiste Daroussin for (l = maxtable; l < newmax; ++l)
88198e903e7SBaptiste Daroussin {
88298e903e7SBaptiste Daroussin table[l] = 0;
88398e903e7SBaptiste Daroussin check[l] = -1;
88498e903e7SBaptiste Daroussin }
88598e903e7SBaptiste Daroussin maxtable = newmax;
88698e903e7SBaptiste Daroussin }
88798e903e7SBaptiste Daroussin
88898e903e7SBaptiste Daroussin if (check[loc] != -1)
88998e903e7SBaptiste Daroussin ok = 0;
89098e903e7SBaptiste Daroussin }
89198e903e7SBaptiste Daroussin for (k = 0; ok && k < vector; k++)
89298e903e7SBaptiste Daroussin {
89398e903e7SBaptiste Daroussin if (pos[k] == j)
89498e903e7SBaptiste Daroussin ok = 0;
89598e903e7SBaptiste Daroussin }
89698e903e7SBaptiste Daroussin if (ok)
89798e903e7SBaptiste Daroussin {
89898e903e7SBaptiste Daroussin for (k = 0; k < t; k++)
89998e903e7SBaptiste Daroussin {
9008e022d3cSDag-Erling Smørgrav loc = (Value_t)(j + from[k]);
90198e903e7SBaptiste Daroussin table[loc] = to[k];
90298e903e7SBaptiste Daroussin check[loc] = from[k];
90398e903e7SBaptiste Daroussin if (loc > high)
90498e903e7SBaptiste Daroussin high = loc;
90598e903e7SBaptiste Daroussin }
90698e903e7SBaptiste Daroussin
90798e903e7SBaptiste Daroussin while (check[lowzero] != -1)
90898e903e7SBaptiste Daroussin ++lowzero;
90998e903e7SBaptiste Daroussin
91098e903e7SBaptiste Daroussin return (j);
91198e903e7SBaptiste Daroussin }
91298e903e7SBaptiste Daroussin }
91398e903e7SBaptiste Daroussin }
91498e903e7SBaptiste Daroussin
91598e903e7SBaptiste Daroussin static void
pack_table(void)91698e903e7SBaptiste Daroussin pack_table(void)
91798e903e7SBaptiste Daroussin {
91898e903e7SBaptiste Daroussin int i;
91998e903e7SBaptiste Daroussin Value_t place;
92098e903e7SBaptiste Daroussin
92198e903e7SBaptiste Daroussin base = NEW2(nvectors, Value_t);
92298e903e7SBaptiste Daroussin pos = NEW2(nentries, Value_t);
92398e903e7SBaptiste Daroussin
92498e903e7SBaptiste Daroussin maxtable = 1000;
92598e903e7SBaptiste Daroussin table = NEW2(maxtable, Value_t);
92698e903e7SBaptiste Daroussin check = NEW2(maxtable, Value_t);
92798e903e7SBaptiste Daroussin
92898e903e7SBaptiste Daroussin lowzero = 0;
92998e903e7SBaptiste Daroussin high = 0;
93098e903e7SBaptiste Daroussin
93198e903e7SBaptiste Daroussin for (i = 0; i < maxtable; i++)
93298e903e7SBaptiste Daroussin check[i] = -1;
93398e903e7SBaptiste Daroussin
93498e903e7SBaptiste Daroussin for (i = 0; i < nentries; i++)
93598e903e7SBaptiste Daroussin {
9368e022d3cSDag-Erling Smørgrav int state = matching_vector(i);
93798e903e7SBaptiste Daroussin
93898e903e7SBaptiste Daroussin if (state < 0)
93998e903e7SBaptiste Daroussin place = (Value_t)pack_vector(i);
94098e903e7SBaptiste Daroussin else
94198e903e7SBaptiste Daroussin place = base[state];
94298e903e7SBaptiste Daroussin
94398e903e7SBaptiste Daroussin pos[i] = place;
94498e903e7SBaptiste Daroussin base[order[i]] = place;
94598e903e7SBaptiste Daroussin }
94698e903e7SBaptiste Daroussin
94798e903e7SBaptiste Daroussin for (i = 0; i < nvectors; i++)
94898e903e7SBaptiste Daroussin {
94998e903e7SBaptiste Daroussin if (froms[i])
95098e903e7SBaptiste Daroussin FREE(froms[i]);
95198e903e7SBaptiste Daroussin if (tos[i])
95298e903e7SBaptiste Daroussin FREE(tos[i]);
95398e903e7SBaptiste Daroussin }
95498e903e7SBaptiste Daroussin
9550c8de5b0SBaptiste Daroussin DO_FREE(froms);
9560c8de5b0SBaptiste Daroussin DO_FREE(tos);
9570c8de5b0SBaptiste Daroussin DO_FREE(tally);
9580c8de5b0SBaptiste Daroussin DO_FREE(width);
9590c8de5b0SBaptiste Daroussin DO_FREE(pos);
96098e903e7SBaptiste Daroussin }
96198e903e7SBaptiste Daroussin
96298e903e7SBaptiste Daroussin static void
output_base(void)96398e903e7SBaptiste Daroussin output_base(void)
96498e903e7SBaptiste Daroussin {
96598e903e7SBaptiste Daroussin int i, j;
96698e903e7SBaptiste Daroussin
96798e903e7SBaptiste Daroussin start_int_table("sindex", base[0]);
96898e903e7SBaptiste Daroussin
96998e903e7SBaptiste Daroussin j = 10;
97098e903e7SBaptiste Daroussin for (i = 1; i < nstates; i++)
97198e903e7SBaptiste Daroussin {
97298e903e7SBaptiste Daroussin if (j >= 10)
97398e903e7SBaptiste Daroussin {
97498e903e7SBaptiste Daroussin output_newline();
97598e903e7SBaptiste Daroussin j = 1;
97698e903e7SBaptiste Daroussin }
97798e903e7SBaptiste Daroussin else
97898e903e7SBaptiste Daroussin ++j;
97998e903e7SBaptiste Daroussin
98098e903e7SBaptiste Daroussin output_int(base[i]);
98198e903e7SBaptiste Daroussin }
98298e903e7SBaptiste Daroussin
98398e903e7SBaptiste Daroussin end_table();
98498e903e7SBaptiste Daroussin
98598e903e7SBaptiste Daroussin start_int_table("rindex", base[nstates]);
98698e903e7SBaptiste Daroussin
98798e903e7SBaptiste Daroussin j = 10;
98898e903e7SBaptiste Daroussin for (i = nstates + 1; i < 2 * nstates; i++)
98998e903e7SBaptiste Daroussin {
99098e903e7SBaptiste Daroussin if (j >= 10)
99198e903e7SBaptiste Daroussin {
99298e903e7SBaptiste Daroussin output_newline();
99398e903e7SBaptiste Daroussin j = 1;
99498e903e7SBaptiste Daroussin }
99598e903e7SBaptiste Daroussin else
99698e903e7SBaptiste Daroussin ++j;
99798e903e7SBaptiste Daroussin
99898e903e7SBaptiste Daroussin output_int(base[i]);
99998e903e7SBaptiste Daroussin }
100098e903e7SBaptiste Daroussin
100198e903e7SBaptiste Daroussin end_table();
100298e903e7SBaptiste Daroussin
10030c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
10040c8de5b0SBaptiste Daroussin output_line("#if YYBTYACC");
10050c8de5b0SBaptiste Daroussin start_int_table("cindex", base[2 * nstates]);
100698e903e7SBaptiste Daroussin
100798e903e7SBaptiste Daroussin j = 10;
10080c8de5b0SBaptiste Daroussin for (i = 2 * nstates + 1; i < 3 * nstates; i++)
10090c8de5b0SBaptiste Daroussin {
10100c8de5b0SBaptiste Daroussin if (j >= 10)
10110c8de5b0SBaptiste Daroussin {
10120c8de5b0SBaptiste Daroussin output_newline();
10130c8de5b0SBaptiste Daroussin j = 1;
10140c8de5b0SBaptiste Daroussin }
10150c8de5b0SBaptiste Daroussin else
10160c8de5b0SBaptiste Daroussin ++j;
10170c8de5b0SBaptiste Daroussin
10180c8de5b0SBaptiste Daroussin output_int(base[i]);
10190c8de5b0SBaptiste Daroussin }
10200c8de5b0SBaptiste Daroussin
10210c8de5b0SBaptiste Daroussin end_table();
10220c8de5b0SBaptiste Daroussin output_line("#endif");
10230c8de5b0SBaptiste Daroussin #endif
10240c8de5b0SBaptiste Daroussin
10250c8de5b0SBaptiste Daroussin start_int_table("gindex", base[PER_STATE * nstates]);
10260c8de5b0SBaptiste Daroussin
10270c8de5b0SBaptiste Daroussin j = 10;
10280c8de5b0SBaptiste Daroussin for (i = PER_STATE * nstates + 1; i < nvectors - 1; i++)
102998e903e7SBaptiste Daroussin {
103098e903e7SBaptiste Daroussin if (j >= 10)
103198e903e7SBaptiste Daroussin {
103298e903e7SBaptiste Daroussin output_newline();
103398e903e7SBaptiste Daroussin j = 1;
103498e903e7SBaptiste Daroussin }
103598e903e7SBaptiste Daroussin else
103698e903e7SBaptiste Daroussin ++j;
103798e903e7SBaptiste Daroussin
103898e903e7SBaptiste Daroussin output_int(base[i]);
103998e903e7SBaptiste Daroussin }
104098e903e7SBaptiste Daroussin
104198e903e7SBaptiste Daroussin end_table();
104298e903e7SBaptiste Daroussin FREE(base);
104398e903e7SBaptiste Daroussin }
104498e903e7SBaptiste Daroussin
104598e903e7SBaptiste Daroussin static void
output_table(void)104698e903e7SBaptiste Daroussin output_table(void)
104798e903e7SBaptiste Daroussin {
104898e903e7SBaptiste Daroussin int i;
104998e903e7SBaptiste Daroussin int j;
105098e903e7SBaptiste Daroussin
10510c8de5b0SBaptiste Daroussin if (high >= MAXYYINT)
10520c8de5b0SBaptiste Daroussin {
10530c8de5b0SBaptiste Daroussin fprintf(stderr, "YYTABLESIZE: %ld\n", high);
10548e022d3cSDag-Erling Smørgrav fprintf(stderr, "Table is longer than %ld elements.\n", (long)MAXYYINT);
10550c8de5b0SBaptiste Daroussin done(1);
10560c8de5b0SBaptiste Daroussin }
10570c8de5b0SBaptiste Daroussin
105898e903e7SBaptiste Daroussin ++outline;
10590c8de5b0SBaptiste Daroussin fprintf(code_file, "#define YYTABLESIZE %ld\n", high);
106098e903e7SBaptiste Daroussin start_int_table("table", table[0]);
106198e903e7SBaptiste Daroussin
106298e903e7SBaptiste Daroussin j = 10;
106398e903e7SBaptiste Daroussin for (i = 1; i <= high; i++)
106498e903e7SBaptiste Daroussin {
106598e903e7SBaptiste Daroussin if (j >= 10)
106698e903e7SBaptiste Daroussin {
106798e903e7SBaptiste Daroussin output_newline();
106898e903e7SBaptiste Daroussin j = 1;
106998e903e7SBaptiste Daroussin }
107098e903e7SBaptiste Daroussin else
107198e903e7SBaptiste Daroussin ++j;
107298e903e7SBaptiste Daroussin
107398e903e7SBaptiste Daroussin output_int(table[i]);
107498e903e7SBaptiste Daroussin }
107598e903e7SBaptiste Daroussin
107698e903e7SBaptiste Daroussin end_table();
107798e903e7SBaptiste Daroussin FREE(table);
107898e903e7SBaptiste Daroussin }
107998e903e7SBaptiste Daroussin
108098e903e7SBaptiste Daroussin static void
output_check(void)108198e903e7SBaptiste Daroussin output_check(void)
108298e903e7SBaptiste Daroussin {
108398e903e7SBaptiste Daroussin int i;
108498e903e7SBaptiste Daroussin int j;
108598e903e7SBaptiste Daroussin
108698e903e7SBaptiste Daroussin start_int_table("check", check[0]);
108798e903e7SBaptiste Daroussin
108898e903e7SBaptiste Daroussin j = 10;
108998e903e7SBaptiste Daroussin for (i = 1; i <= high; i++)
109098e903e7SBaptiste Daroussin {
109198e903e7SBaptiste Daroussin if (j >= 10)
109298e903e7SBaptiste Daroussin {
109398e903e7SBaptiste Daroussin output_newline();
109498e903e7SBaptiste Daroussin j = 1;
109598e903e7SBaptiste Daroussin }
109698e903e7SBaptiste Daroussin else
109798e903e7SBaptiste Daroussin ++j;
109898e903e7SBaptiste Daroussin
109998e903e7SBaptiste Daroussin output_int(check[i]);
110098e903e7SBaptiste Daroussin }
110198e903e7SBaptiste Daroussin
110298e903e7SBaptiste Daroussin end_table();
110398e903e7SBaptiste Daroussin FREE(check);
110498e903e7SBaptiste Daroussin }
110598e903e7SBaptiste Daroussin
11060c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
11070c8de5b0SBaptiste Daroussin static void
output_ctable(void)11080c8de5b0SBaptiste Daroussin output_ctable(void)
11090c8de5b0SBaptiste Daroussin {
11100c8de5b0SBaptiste Daroussin int i;
11110c8de5b0SBaptiste Daroussin int j;
111211fce282SBaptiste Daroussin int limit = (conflicts != 0) ? nconflicts : 0;
11130c8de5b0SBaptiste Daroussin
111411fce282SBaptiste Daroussin if (limit < high)
111511fce282SBaptiste Daroussin limit = (int)high;
111611fce282SBaptiste Daroussin
11170c8de5b0SBaptiste Daroussin output_line("#if YYBTYACC");
111811fce282SBaptiste Daroussin start_int_table("ctable", conflicts ? conflicts[0] : -1);
11190c8de5b0SBaptiste Daroussin
11200c8de5b0SBaptiste Daroussin j = 10;
112111fce282SBaptiste Daroussin for (i = 1; i < limit; i++)
11220c8de5b0SBaptiste Daroussin {
11230c8de5b0SBaptiste Daroussin if (j >= 10)
11240c8de5b0SBaptiste Daroussin {
11250c8de5b0SBaptiste Daroussin output_newline();
11260c8de5b0SBaptiste Daroussin j = 1;
11270c8de5b0SBaptiste Daroussin }
11280c8de5b0SBaptiste Daroussin else
11290c8de5b0SBaptiste Daroussin ++j;
11300c8de5b0SBaptiste Daroussin
113111fce282SBaptiste Daroussin output_int((conflicts != 0 && i < nconflicts) ? conflicts[i] : -1);
11320c8de5b0SBaptiste Daroussin }
11330c8de5b0SBaptiste Daroussin
113411fce282SBaptiste Daroussin if (conflicts)
113511fce282SBaptiste Daroussin FREE(conflicts);
113611fce282SBaptiste Daroussin
11370c8de5b0SBaptiste Daroussin end_table();
11380c8de5b0SBaptiste Daroussin output_line("#endif");
11390c8de5b0SBaptiste Daroussin }
11400c8de5b0SBaptiste Daroussin #endif
11410c8de5b0SBaptiste Daroussin
114298e903e7SBaptiste Daroussin static void
output_actions(void)114398e903e7SBaptiste Daroussin output_actions(void)
114498e903e7SBaptiste Daroussin {
11450c8de5b0SBaptiste Daroussin nvectors = PER_STATE * nstates + nvars;
114698e903e7SBaptiste Daroussin
114798e903e7SBaptiste Daroussin froms = NEW2(nvectors, Value_t *);
114898e903e7SBaptiste Daroussin tos = NEW2(nvectors, Value_t *);
114998e903e7SBaptiste Daroussin tally = NEW2(nvectors, Value_t);
115098e903e7SBaptiste Daroussin width = NEW2(nvectors, Value_t);
115198e903e7SBaptiste Daroussin
11520c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
11530c8de5b0SBaptiste Daroussin if (backtrack && (SRtotal + RRtotal) != 0)
11540c8de5b0SBaptiste Daroussin conflicts = NEW2(4 * (SRtotal + RRtotal), Value_t);
11550c8de5b0SBaptiste Daroussin #endif
11560c8de5b0SBaptiste Daroussin
115798e903e7SBaptiste Daroussin token_actions();
115898e903e7SBaptiste Daroussin FREE(lookaheads);
115998e903e7SBaptiste Daroussin FREE(LA);
116098e903e7SBaptiste Daroussin FREE(LAruleno);
116198e903e7SBaptiste Daroussin FREE(accessing_symbol);
116298e903e7SBaptiste Daroussin
116398e903e7SBaptiste Daroussin goto_actions();
116411fce282SBaptiste Daroussin FREE(goto_base);
116598e903e7SBaptiste Daroussin FREE(from_state);
116698e903e7SBaptiste Daroussin FREE(to_state);
116798e903e7SBaptiste Daroussin
116898e903e7SBaptiste Daroussin sort_actions();
116998e903e7SBaptiste Daroussin pack_table();
117098e903e7SBaptiste Daroussin output_base();
117198e903e7SBaptiste Daroussin output_table();
117298e903e7SBaptiste Daroussin output_check();
11730c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
11740c8de5b0SBaptiste Daroussin output_ctable();
11750c8de5b0SBaptiste Daroussin #endif
117698e903e7SBaptiste Daroussin }
117798e903e7SBaptiste Daroussin
117898e903e7SBaptiste Daroussin static int
is_C_identifier(char * name)117998e903e7SBaptiste Daroussin is_C_identifier(char *name)
118098e903e7SBaptiste Daroussin {
118198e903e7SBaptiste Daroussin char *s;
118298e903e7SBaptiste Daroussin int c;
118398e903e7SBaptiste Daroussin
118498e903e7SBaptiste Daroussin s = name;
118598e903e7SBaptiste Daroussin c = *s;
118698e903e7SBaptiste Daroussin if (c == '"')
118798e903e7SBaptiste Daroussin {
118898e903e7SBaptiste Daroussin c = *++s;
1189b53bb29fSJung-uk Kim if (!IS_NAME1(c))
119098e903e7SBaptiste Daroussin return (0);
119198e903e7SBaptiste Daroussin while ((c = *++s) != '"')
119298e903e7SBaptiste Daroussin {
1193b53bb29fSJung-uk Kim if (!IS_NAME2(c))
119498e903e7SBaptiste Daroussin return (0);
119598e903e7SBaptiste Daroussin }
119698e903e7SBaptiste Daroussin return (1);
119798e903e7SBaptiste Daroussin }
119898e903e7SBaptiste Daroussin
1199b53bb29fSJung-uk Kim if (!IS_NAME1(c))
120098e903e7SBaptiste Daroussin return (0);
120198e903e7SBaptiste Daroussin while ((c = *++s) != 0)
120298e903e7SBaptiste Daroussin {
1203b53bb29fSJung-uk Kim if (!IS_NAME2(c))
120498e903e7SBaptiste Daroussin return (0);
120598e903e7SBaptiste Daroussin }
120698e903e7SBaptiste Daroussin return (1);
120798e903e7SBaptiste Daroussin }
120898e903e7SBaptiste Daroussin
12090c8de5b0SBaptiste Daroussin #if USE_HEADER_GUARDS
12100c8de5b0SBaptiste Daroussin static void
start_defines_file(void)12110c8de5b0SBaptiste Daroussin start_defines_file(void)
12120c8de5b0SBaptiste Daroussin {
12130c8de5b0SBaptiste Daroussin fprintf(defines_file, "#ifndef _%s_defines_h_\n", symbol_prefix);
12140c8de5b0SBaptiste Daroussin fprintf(defines_file, "#define _%s_defines_h_\n\n", symbol_prefix);
12150c8de5b0SBaptiste Daroussin }
12160c8de5b0SBaptiste Daroussin
12170c8de5b0SBaptiste Daroussin static void
end_defines_file(void)12180c8de5b0SBaptiste Daroussin end_defines_file(void)
12190c8de5b0SBaptiste Daroussin {
12200c8de5b0SBaptiste Daroussin fprintf(defines_file, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix);
12210c8de5b0SBaptiste Daroussin }
12220c8de5b0SBaptiste Daroussin #else
12230c8de5b0SBaptiste Daroussin #define start_defines_file() /* nothing */
12240c8de5b0SBaptiste Daroussin #define end_defines_file() /* nothing */
12250c8de5b0SBaptiste Daroussin #endif
12260c8de5b0SBaptiste Daroussin
122798e903e7SBaptiste Daroussin static void
output_defines(FILE * fp)122898e903e7SBaptiste Daroussin output_defines(FILE * fp)
122998e903e7SBaptiste Daroussin {
123098e903e7SBaptiste Daroussin int c, i;
123198e903e7SBaptiste Daroussin
1232b53bb29fSJung-uk Kim if (fp == defines_file)
1233b53bb29fSJung-uk Kim {
1234b53bb29fSJung-uk Kim output_code_lines(fp, CODE_REQUIRES);
1235b53bb29fSJung-uk Kim }
1236b53bb29fSJung-uk Kim
123798e903e7SBaptiste Daroussin for (i = 2; i < ntokens; ++i)
123898e903e7SBaptiste Daroussin {
12398e022d3cSDag-Erling Smørgrav char *s = symbol_name[i];
12408e022d3cSDag-Erling Smørgrav
124198e903e7SBaptiste Daroussin if (is_C_identifier(s) && (!sflag || *s != '"'))
124298e903e7SBaptiste Daroussin {
124398e903e7SBaptiste Daroussin fprintf(fp, "#define ");
124498e903e7SBaptiste Daroussin c = *s;
124598e903e7SBaptiste Daroussin if (c == '"')
124698e903e7SBaptiste Daroussin {
124798e903e7SBaptiste Daroussin while ((c = *++s) != '"')
124898e903e7SBaptiste Daroussin {
124998e903e7SBaptiste Daroussin putc(c, fp);
125098e903e7SBaptiste Daroussin }
125198e903e7SBaptiste Daroussin }
125298e903e7SBaptiste Daroussin else
125398e903e7SBaptiste Daroussin {
125498e903e7SBaptiste Daroussin do
125598e903e7SBaptiste Daroussin {
125698e903e7SBaptiste Daroussin putc(c, fp);
125798e903e7SBaptiste Daroussin }
125898e903e7SBaptiste Daroussin while ((c = *++s) != 0);
125998e903e7SBaptiste Daroussin }
126098e903e7SBaptiste Daroussin if (fp == code_file)
126198e903e7SBaptiste Daroussin ++outline;
12628e022d3cSDag-Erling Smørgrav fprintf(fp, " %ld\n", (long)symbol_value[i]);
126398e903e7SBaptiste Daroussin }
126498e903e7SBaptiste Daroussin }
126598e903e7SBaptiste Daroussin
126698e903e7SBaptiste Daroussin if (fp == code_file)
126798e903e7SBaptiste Daroussin ++outline;
126898e903e7SBaptiste Daroussin if (fp != defines_file || iflag)
12698e022d3cSDag-Erling Smørgrav fprintf(fp, "#define YYERRCODE %ld\n", (long)symbol_value[1]);
127098e903e7SBaptiste Daroussin
1271b53bb29fSJung-uk Kim if (fp == defines_file)
1272b53bb29fSJung-uk Kim {
1273b53bb29fSJung-uk Kim output_code_lines(fp, CODE_PROVIDES);
1274b53bb29fSJung-uk Kim }
1275b53bb29fSJung-uk Kim
1276c5b5d71aSJung-uk Kim if (token_table && rflag && fp != externs_file)
1277c5b5d71aSJung-uk Kim {
1278c5b5d71aSJung-uk Kim if (fp == code_file)
1279c5b5d71aSJung-uk Kim ++outline;
1280c5b5d71aSJung-uk Kim fputs("#undef yytname\n", fp);
1281c5b5d71aSJung-uk Kim if (fp == code_file)
1282c5b5d71aSJung-uk Kim ++outline;
1283c5b5d71aSJung-uk Kim fputs("#define yytname yyname\n", fp);
1284c5b5d71aSJung-uk Kim }
1285c5b5d71aSJung-uk Kim
128698e903e7SBaptiste Daroussin if (fp == defines_file || (iflag && !dflag))
128798e903e7SBaptiste Daroussin {
128898e903e7SBaptiste Daroussin if (unionized)
128998e903e7SBaptiste Daroussin {
12905b91e83fSBaptiste Daroussin if (union_file != 0)
12915b91e83fSBaptiste Daroussin {
129298e903e7SBaptiste Daroussin rewind(union_file);
129398e903e7SBaptiste Daroussin while ((c = getc(union_file)) != EOF)
12940c8de5b0SBaptiste Daroussin putc_code(fp, c);
12955b91e83fSBaptiste Daroussin }
1296b53bb29fSJung-uk Kim if (!pure_parser)
129798e903e7SBaptiste Daroussin fprintf(fp, "extern YYSTYPE %slval;\n", symbol_prefix);
129898e903e7SBaptiste Daroussin }
12992aca18c7SJung-uk Kim #if defined(YYBTYACC)
13002aca18c7SJung-uk Kim if (locations)
1301b53bb29fSJung-uk Kim {
13022aca18c7SJung-uk Kim output_ltype(fp);
1303b53bb29fSJung-uk Kim fprintf(fp, "extern YYLTYPE %slloc;\n", symbol_prefix);
1304b53bb29fSJung-uk Kim }
13052aca18c7SJung-uk Kim #endif
130698e903e7SBaptiste Daroussin }
130798e903e7SBaptiste Daroussin }
130898e903e7SBaptiste Daroussin
130998e903e7SBaptiste Daroussin static void
output_stored_text(FILE * fp)131098e903e7SBaptiste Daroussin output_stored_text(FILE * fp)
131198e903e7SBaptiste Daroussin {
131298e903e7SBaptiste Daroussin int c;
131398e903e7SBaptiste Daroussin FILE *in;
131498e903e7SBaptiste Daroussin
131598e903e7SBaptiste Daroussin if (text_file == NULL)
131698e903e7SBaptiste Daroussin open_error("text_file");
1317b53bb29fSJung-uk Kim rewind(text_file);
131898e903e7SBaptiste Daroussin in = text_file;
131998e903e7SBaptiste Daroussin if ((c = getc(in)) == EOF)
132098e903e7SBaptiste Daroussin return;
132198e903e7SBaptiste Daroussin putc_code(fp, c);
132298e903e7SBaptiste Daroussin while ((c = getc(in)) != EOF)
132398e903e7SBaptiste Daroussin {
132498e903e7SBaptiste Daroussin putc_code(fp, c);
132598e903e7SBaptiste Daroussin }
132698e903e7SBaptiste Daroussin write_code_lineno(fp);
132798e903e7SBaptiste Daroussin }
132898e903e7SBaptiste Daroussin
1329b53bb29fSJung-uk Kim static int
output_yydebug(FILE * fp)1330b53bb29fSJung-uk Kim output_yydebug(FILE * fp)
1331b53bb29fSJung-uk Kim {
1332b53bb29fSJung-uk Kim fprintf(fp, "#ifndef YYDEBUG\n");
1333b53bb29fSJung-uk Kim fprintf(fp, "#define YYDEBUG %d\n", tflag);
1334b53bb29fSJung-uk Kim fprintf(fp, "#endif\n");
1335b53bb29fSJung-uk Kim return 3;
1336b53bb29fSJung-uk Kim }
1337b53bb29fSJung-uk Kim
133898e903e7SBaptiste Daroussin static void
output_debug(void)133998e903e7SBaptiste Daroussin output_debug(void)
134098e903e7SBaptiste Daroussin {
13410c8de5b0SBaptiste Daroussin int i, j, k, max, maxtok;
134298e903e7SBaptiste Daroussin const char **symnam;
134398e903e7SBaptiste Daroussin const char *s;
134498e903e7SBaptiste Daroussin
134598e903e7SBaptiste Daroussin ++outline;
13468e022d3cSDag-Erling Smørgrav fprintf(code_file, "#define YYFINAL %ld\n", (long)final_state);
134798e903e7SBaptiste Daroussin
1348b53bb29fSJung-uk Kim outline += output_yydebug(code_file);
134998e903e7SBaptiste Daroussin
135098e903e7SBaptiste Daroussin if (rflag)
135198e903e7SBaptiste Daroussin {
1352b53bb29fSJung-uk Kim output_yydebug(output_file);
135398e903e7SBaptiste Daroussin }
135498e903e7SBaptiste Daroussin
13550c8de5b0SBaptiste Daroussin maxtok = 0;
13560c8de5b0SBaptiste Daroussin for (i = 0; i < ntokens; ++i)
13570c8de5b0SBaptiste Daroussin if (symbol_value[i] > maxtok)
13580c8de5b0SBaptiste Daroussin maxtok = symbol_value[i];
13590c8de5b0SBaptiste Daroussin
13600c8de5b0SBaptiste Daroussin /* symbol_value[$accept] = -1 */
13610c8de5b0SBaptiste Daroussin /* symbol_value[<goal>] = 0 */
13620c8de5b0SBaptiste Daroussin /* remaining non-terminals start at 1 */
13630c8de5b0SBaptiste Daroussin max = maxtok;
13640c8de5b0SBaptiste Daroussin for (i = ntokens; i < nsyms; ++i)
13650c8de5b0SBaptiste Daroussin if (((maxtok + 1) + (symbol_value[i] + 1)) > max)
13660c8de5b0SBaptiste Daroussin max = (maxtok + 1) + (symbol_value[i] + 1);
136798e903e7SBaptiste Daroussin
136898e903e7SBaptiste Daroussin ++outline;
13690c8de5b0SBaptiste Daroussin fprintf(code_file, "#define YYMAXTOKEN %d\n", maxtok);
13700c8de5b0SBaptiste Daroussin
13710c8de5b0SBaptiste Daroussin ++outline;
13720c8de5b0SBaptiste Daroussin fprintf(code_file, "#define YYUNDFTOKEN %d\n", max + 1);
13730c8de5b0SBaptiste Daroussin
13740c8de5b0SBaptiste Daroussin ++outline;
13754b4a8fcaSBaptiste Daroussin fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? "
13760c8de5b0SBaptiste Daroussin "YYUNDFTOKEN : (a))\n");
137798e903e7SBaptiste Daroussin
13788e022d3cSDag-Erling Smørgrav symnam = TCMALLOC(const char *, max + 2);
137998e903e7SBaptiste Daroussin NO_SPACE(symnam);
138098e903e7SBaptiste Daroussin
138198e903e7SBaptiste Daroussin /* Note that it is not necessary to initialize the element */
138298e903e7SBaptiste Daroussin /* symnam[max]. */
13830c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
13840c8de5b0SBaptiste Daroussin for (i = 0; i < max; ++i)
13850c8de5b0SBaptiste Daroussin symnam[i] = 0;
13860c8de5b0SBaptiste Daroussin for (i = nsyms - 1; i >= 0; --i)
13870c8de5b0SBaptiste Daroussin symnam[symbol_pval[i]] = symbol_name[i];
13880c8de5b0SBaptiste Daroussin symnam[max + 1] = "illegal-symbol";
13890c8de5b0SBaptiste Daroussin #else
13904b4a8fcaSBaptiste Daroussin for (i = 0; i <= max; ++i)
139198e903e7SBaptiste Daroussin symnam[i] = 0;
139298e903e7SBaptiste Daroussin for (i = ntokens - 1; i >= 2; --i)
139398e903e7SBaptiste Daroussin symnam[symbol_value[i]] = symbol_name[i];
139498e903e7SBaptiste Daroussin symnam[0] = "end-of-file";
13954b4a8fcaSBaptiste Daroussin symnam[max + 1] = "illegal-symbol";
13960c8de5b0SBaptiste Daroussin #endif
139798e903e7SBaptiste Daroussin
13984b4a8fcaSBaptiste Daroussin /*
13994b4a8fcaSBaptiste Daroussin * bison's yytname[] array is roughly the same as byacc's yyname[] array.
14000c8de5b0SBaptiste Daroussin * The difference is that byacc does not predefine "$undefined".
14014b4a8fcaSBaptiste Daroussin *
14024b4a8fcaSBaptiste Daroussin * If the grammar declares "%token-table", define symbol "yytname" so
14034b4a8fcaSBaptiste Daroussin * an application such as ntpd can build.
14044b4a8fcaSBaptiste Daroussin */
14054b4a8fcaSBaptiste Daroussin if (token_table)
14064b4a8fcaSBaptiste Daroussin {
1407c5b5d71aSJung-uk Kim if (!rflag)
1408c5b5d71aSJung-uk Kim {
14094b4a8fcaSBaptiste Daroussin output_line("#undef yytname");
14104b4a8fcaSBaptiste Daroussin output_line("#define yytname yyname");
14114b4a8fcaSBaptiste Daroussin }
1412c5b5d71aSJung-uk Kim }
14134b4a8fcaSBaptiste Daroussin else
14144b4a8fcaSBaptiste Daroussin {
141598e903e7SBaptiste Daroussin output_line("#if YYDEBUG");
14164b4a8fcaSBaptiste Daroussin }
141798e903e7SBaptiste Daroussin
141898e903e7SBaptiste Daroussin start_str_table("name");
141998e903e7SBaptiste Daroussin j = 80;
14204b4a8fcaSBaptiste Daroussin for (i = 0; i <= max + 1; ++i)
142198e903e7SBaptiste Daroussin {
142298e903e7SBaptiste Daroussin if ((s = symnam[i]) != 0)
142398e903e7SBaptiste Daroussin {
142498e903e7SBaptiste Daroussin if (s[0] == '"')
142598e903e7SBaptiste Daroussin {
142698e903e7SBaptiste Daroussin k = 7;
142798e903e7SBaptiste Daroussin while (*++s != '"')
142898e903e7SBaptiste Daroussin {
142998e903e7SBaptiste Daroussin ++k;
143098e903e7SBaptiste Daroussin if (*s == '\\')
143198e903e7SBaptiste Daroussin {
143298e903e7SBaptiste Daroussin k += 2;
143398e903e7SBaptiste Daroussin if (*++s == '\\')
143498e903e7SBaptiste Daroussin ++k;
143598e903e7SBaptiste Daroussin }
143698e903e7SBaptiste Daroussin }
143798e903e7SBaptiste Daroussin j += k;
143898e903e7SBaptiste Daroussin if (j > 80)
143998e903e7SBaptiste Daroussin {
144098e903e7SBaptiste Daroussin output_newline();
144198e903e7SBaptiste Daroussin j = k;
144298e903e7SBaptiste Daroussin }
144398e903e7SBaptiste Daroussin fprintf(output_file, "\"\\\"");
144498e903e7SBaptiste Daroussin s = symnam[i];
144598e903e7SBaptiste Daroussin while (*++s != '"')
144698e903e7SBaptiste Daroussin {
144798e903e7SBaptiste Daroussin if (*s == '\\')
144898e903e7SBaptiste Daroussin {
144998e903e7SBaptiste Daroussin fprintf(output_file, "\\\\");
145098e903e7SBaptiste Daroussin if (*++s == '\\')
145198e903e7SBaptiste Daroussin fprintf(output_file, "\\\\");
145298e903e7SBaptiste Daroussin else
145398e903e7SBaptiste Daroussin putc(*s, output_file);
145498e903e7SBaptiste Daroussin }
145598e903e7SBaptiste Daroussin else
145698e903e7SBaptiste Daroussin putc(*s, output_file);
145798e903e7SBaptiste Daroussin }
145898e903e7SBaptiste Daroussin fprintf(output_file, "\\\"\",");
145998e903e7SBaptiste Daroussin }
146098e903e7SBaptiste Daroussin else if (s[0] == '\'')
146198e903e7SBaptiste Daroussin {
146298e903e7SBaptiste Daroussin if (s[1] == '"')
146398e903e7SBaptiste Daroussin {
146498e903e7SBaptiste Daroussin j += 7;
146598e903e7SBaptiste Daroussin if (j > 80)
146698e903e7SBaptiste Daroussin {
146798e903e7SBaptiste Daroussin output_newline();
146898e903e7SBaptiste Daroussin j = 7;
146998e903e7SBaptiste Daroussin }
147098e903e7SBaptiste Daroussin fprintf(output_file, "\"'\\\"'\",");
147198e903e7SBaptiste Daroussin }
147298e903e7SBaptiste Daroussin else
147398e903e7SBaptiste Daroussin {
147498e903e7SBaptiste Daroussin k = 5;
147598e903e7SBaptiste Daroussin while (*++s != '\'')
147698e903e7SBaptiste Daroussin {
147798e903e7SBaptiste Daroussin ++k;
147898e903e7SBaptiste Daroussin if (*s == '\\')
147998e903e7SBaptiste Daroussin {
148098e903e7SBaptiste Daroussin k += 2;
148198e903e7SBaptiste Daroussin if (*++s == '\\')
148298e903e7SBaptiste Daroussin ++k;
148398e903e7SBaptiste Daroussin }
148498e903e7SBaptiste Daroussin }
148598e903e7SBaptiste Daroussin j += k;
148698e903e7SBaptiste Daroussin if (j > 80)
148798e903e7SBaptiste Daroussin {
148898e903e7SBaptiste Daroussin output_newline();
148998e903e7SBaptiste Daroussin j = k;
149098e903e7SBaptiste Daroussin }
149198e903e7SBaptiste Daroussin fprintf(output_file, "\"'");
149298e903e7SBaptiste Daroussin s = symnam[i];
149398e903e7SBaptiste Daroussin while (*++s != '\'')
149498e903e7SBaptiste Daroussin {
149598e903e7SBaptiste Daroussin if (*s == '\\')
149698e903e7SBaptiste Daroussin {
149798e903e7SBaptiste Daroussin fprintf(output_file, "\\\\");
149898e903e7SBaptiste Daroussin if (*++s == '\\')
149998e903e7SBaptiste Daroussin fprintf(output_file, "\\\\");
150098e903e7SBaptiste Daroussin else
150198e903e7SBaptiste Daroussin putc(*s, output_file);
150298e903e7SBaptiste Daroussin }
150398e903e7SBaptiste Daroussin else
150498e903e7SBaptiste Daroussin putc(*s, output_file);
150598e903e7SBaptiste Daroussin }
150698e903e7SBaptiste Daroussin fprintf(output_file, "'\",");
150798e903e7SBaptiste Daroussin }
150898e903e7SBaptiste Daroussin }
150998e903e7SBaptiste Daroussin else
151098e903e7SBaptiste Daroussin {
151198e903e7SBaptiste Daroussin k = (int)strlen(s) + 3;
151298e903e7SBaptiste Daroussin j += k;
151398e903e7SBaptiste Daroussin if (j > 80)
151498e903e7SBaptiste Daroussin {
151598e903e7SBaptiste Daroussin output_newline();
151698e903e7SBaptiste Daroussin j = k;
151798e903e7SBaptiste Daroussin }
151898e903e7SBaptiste Daroussin putc('"', output_file);
151998e903e7SBaptiste Daroussin do
152098e903e7SBaptiste Daroussin {
152198e903e7SBaptiste Daroussin putc(*s, output_file);
152298e903e7SBaptiste Daroussin }
152398e903e7SBaptiste Daroussin while (*++s);
152498e903e7SBaptiste Daroussin fprintf(output_file, "\",");
152598e903e7SBaptiste Daroussin }
152698e903e7SBaptiste Daroussin }
152798e903e7SBaptiste Daroussin else
152898e903e7SBaptiste Daroussin {
152998e903e7SBaptiste Daroussin j += 2;
153098e903e7SBaptiste Daroussin if (j > 80)
153198e903e7SBaptiste Daroussin {
153298e903e7SBaptiste Daroussin output_newline();
153398e903e7SBaptiste Daroussin j = 2;
153498e903e7SBaptiste Daroussin }
153598e903e7SBaptiste Daroussin fprintf(output_file, "0,");
153698e903e7SBaptiste Daroussin }
153798e903e7SBaptiste Daroussin }
153898e903e7SBaptiste Daroussin end_table();
153998e903e7SBaptiste Daroussin FREE(symnam);
154098e903e7SBaptiste Daroussin
15414b4a8fcaSBaptiste Daroussin if (token_table)
15424b4a8fcaSBaptiste Daroussin output_line("#if YYDEBUG");
154398e903e7SBaptiste Daroussin start_str_table("rule");
154498e903e7SBaptiste Daroussin for (i = 2; i < nrules; ++i)
154598e903e7SBaptiste Daroussin {
154698e903e7SBaptiste Daroussin fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
154798e903e7SBaptiste Daroussin for (j = rrhs[i]; ritem[j] > 0; ++j)
154898e903e7SBaptiste Daroussin {
154998e903e7SBaptiste Daroussin s = symbol_name[ritem[j]];
155098e903e7SBaptiste Daroussin if (s[0] == '"')
155198e903e7SBaptiste Daroussin {
155298e903e7SBaptiste Daroussin fprintf(output_file, " \\\"");
155398e903e7SBaptiste Daroussin while (*++s != '"')
155498e903e7SBaptiste Daroussin {
155598e903e7SBaptiste Daroussin if (*s == '\\')
155698e903e7SBaptiste Daroussin {
155798e903e7SBaptiste Daroussin if (s[1] == '\\')
155898e903e7SBaptiste Daroussin fprintf(output_file, "\\\\\\\\");
155998e903e7SBaptiste Daroussin else
156098e903e7SBaptiste Daroussin fprintf(output_file, "\\\\%c", s[1]);
156198e903e7SBaptiste Daroussin ++s;
156298e903e7SBaptiste Daroussin }
156398e903e7SBaptiste Daroussin else
156498e903e7SBaptiste Daroussin putc(*s, output_file);
156598e903e7SBaptiste Daroussin }
156698e903e7SBaptiste Daroussin fprintf(output_file, "\\\"");
156798e903e7SBaptiste Daroussin }
156898e903e7SBaptiste Daroussin else if (s[0] == '\'')
156998e903e7SBaptiste Daroussin {
157098e903e7SBaptiste Daroussin if (s[1] == '"')
157198e903e7SBaptiste Daroussin fprintf(output_file, " '\\\"'");
157298e903e7SBaptiste Daroussin else if (s[1] == '\\')
157398e903e7SBaptiste Daroussin {
157498e903e7SBaptiste Daroussin if (s[2] == '\\')
157598e903e7SBaptiste Daroussin fprintf(output_file, " '\\\\\\\\");
157698e903e7SBaptiste Daroussin else
157798e903e7SBaptiste Daroussin fprintf(output_file, " '\\\\%c", s[2]);
157898e903e7SBaptiste Daroussin s += 2;
157998e903e7SBaptiste Daroussin while (*++s != '\'')
158098e903e7SBaptiste Daroussin putc(*s, output_file);
158198e903e7SBaptiste Daroussin putc('\'', output_file);
158298e903e7SBaptiste Daroussin }
158398e903e7SBaptiste Daroussin else
158498e903e7SBaptiste Daroussin fprintf(output_file, " '%c'", s[1]);
158598e903e7SBaptiste Daroussin }
158698e903e7SBaptiste Daroussin else
158798e903e7SBaptiste Daroussin fprintf(output_file, " %s", s);
158898e903e7SBaptiste Daroussin }
158998e903e7SBaptiste Daroussin fprintf(output_file, "\",");
159098e903e7SBaptiste Daroussin output_newline();
159198e903e7SBaptiste Daroussin }
159298e903e7SBaptiste Daroussin
159398e903e7SBaptiste Daroussin end_table();
159498e903e7SBaptiste Daroussin output_line("#endif");
159598e903e7SBaptiste Daroussin }
159698e903e7SBaptiste Daroussin
15970c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
15980c8de5b0SBaptiste Daroussin static void
output_backtracking_parser(FILE * fp)15990c8de5b0SBaptiste Daroussin output_backtracking_parser(FILE * fp)
16000c8de5b0SBaptiste Daroussin {
16010c8de5b0SBaptiste Daroussin putl_code(fp, "#undef YYBTYACC\n");
16020c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
16030c8de5b0SBaptiste Daroussin if (backtrack)
16040c8de5b0SBaptiste Daroussin {
16050c8de5b0SBaptiste Daroussin putl_code(fp, "#define YYBTYACC 1\n");
16060c8de5b0SBaptiste Daroussin putl_code(fp,
16070c8de5b0SBaptiste Daroussin "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n");
16080c8de5b0SBaptiste Daroussin }
16090c8de5b0SBaptiste Daroussin else
16100c8de5b0SBaptiste Daroussin #endif
16110c8de5b0SBaptiste Daroussin {
16120c8de5b0SBaptiste Daroussin putl_code(fp, "#define YYBTYACC 0\n");
16130c8de5b0SBaptiste Daroussin putl_code(fp, "#define YYDEBUGSTR YYPREFIX \"debug\"\n");
16140c8de5b0SBaptiste Daroussin }
16150c8de5b0SBaptiste Daroussin }
16160c8de5b0SBaptiste Daroussin #endif
16170c8de5b0SBaptiste Daroussin
161898e903e7SBaptiste Daroussin static void
output_pure_parser(FILE * fp)161998e903e7SBaptiste Daroussin output_pure_parser(FILE * fp)
162098e903e7SBaptiste Daroussin {
162198e903e7SBaptiste Daroussin putc_code(fp, '\n');
162298e903e7SBaptiste Daroussin
162398e903e7SBaptiste Daroussin if (fp == code_file)
16240c8de5b0SBaptiste Daroussin ++outline;
162598e903e7SBaptiste Daroussin fprintf(fp, "#define YYPURE %d\n", pure_parser);
16266166fdceSJung-uk Kim #if defined(YY_NO_LEAKS)
16276166fdceSJung-uk Kim if (fp == code_file)
16286166fdceSJung-uk Kim ++outline;
16296166fdceSJung-uk Kim fputs("#define YY_NO_LEAKS 1\n", fp);
1630*822ca327SBaptiste Daroussin #else
16316166fdceSJung-uk Kim putc_code(fp, '\n');
16326166fdceSJung-uk Kim #endif
1633*822ca327SBaptiste Daroussin }
16346166fdceSJung-uk Kim
163598e903e7SBaptiste Daroussin static void
output_trailing_text(void)163698e903e7SBaptiste Daroussin output_trailing_text(void)
163798e903e7SBaptiste Daroussin {
163898e903e7SBaptiste Daroussin int c, last;
163998e903e7SBaptiste Daroussin FILE *in;
164098e903e7SBaptiste Daroussin
164198e903e7SBaptiste Daroussin if (line == 0)
164298e903e7SBaptiste Daroussin return;
164398e903e7SBaptiste Daroussin
164498e903e7SBaptiste Daroussin in = input_file;
164598e903e7SBaptiste Daroussin c = *cptr;
164698e903e7SBaptiste Daroussin if (c == '\n')
164798e903e7SBaptiste Daroussin {
164898e903e7SBaptiste Daroussin ++lineno;
164998e903e7SBaptiste Daroussin if ((c = getc(in)) == EOF)
165098e903e7SBaptiste Daroussin return;
165198e903e7SBaptiste Daroussin write_input_lineno();
165298e903e7SBaptiste Daroussin putc_code(code_file, c);
165398e903e7SBaptiste Daroussin last = c;
165498e903e7SBaptiste Daroussin }
165598e903e7SBaptiste Daroussin else
165698e903e7SBaptiste Daroussin {
165798e903e7SBaptiste Daroussin write_input_lineno();
165898e903e7SBaptiste Daroussin do
165998e903e7SBaptiste Daroussin {
166098e903e7SBaptiste Daroussin putc_code(code_file, c);
166198e903e7SBaptiste Daroussin }
166298e903e7SBaptiste Daroussin while ((c = *++cptr) != '\n');
166398e903e7SBaptiste Daroussin putc_code(code_file, c);
166498e903e7SBaptiste Daroussin last = '\n';
166598e903e7SBaptiste Daroussin }
166698e903e7SBaptiste Daroussin
166798e903e7SBaptiste Daroussin while ((c = getc(in)) != EOF)
166898e903e7SBaptiste Daroussin {
166998e903e7SBaptiste Daroussin putc_code(code_file, c);
167098e903e7SBaptiste Daroussin last = c;
167198e903e7SBaptiste Daroussin }
167298e903e7SBaptiste Daroussin
167398e903e7SBaptiste Daroussin if (last != '\n')
167498e903e7SBaptiste Daroussin {
167598e903e7SBaptiste Daroussin putc_code(code_file, '\n');
167698e903e7SBaptiste Daroussin }
167798e903e7SBaptiste Daroussin write_code_lineno(code_file);
167898e903e7SBaptiste Daroussin }
167998e903e7SBaptiste Daroussin
168098e903e7SBaptiste Daroussin static void
output_semantic_actions(void)168198e903e7SBaptiste Daroussin output_semantic_actions(void)
168298e903e7SBaptiste Daroussin {
168398e903e7SBaptiste Daroussin int c, last;
16848e022d3cSDag-Erling Smørgrav int state;
16858e022d3cSDag-Erling Smørgrav char line_state[20];
168698e903e7SBaptiste Daroussin
168798e903e7SBaptiste Daroussin rewind(action_file);
168898e903e7SBaptiste Daroussin if ((c = getc(action_file)) == EOF)
168998e903e7SBaptiste Daroussin return;
169098e903e7SBaptiste Daroussin
16918e022d3cSDag-Erling Smørgrav if (!lflag)
16928e022d3cSDag-Erling Smørgrav {
16938e022d3cSDag-Erling Smørgrav state = -1;
16948e022d3cSDag-Erling Smørgrav sprintf(line_state, line_format, 1, "");
16958e022d3cSDag-Erling Smørgrav }
16968e022d3cSDag-Erling Smørgrav
169798e903e7SBaptiste Daroussin last = c;
169898e903e7SBaptiste Daroussin putc_code(code_file, c);
169998e903e7SBaptiste Daroussin while ((c = getc(action_file)) != EOF)
170098e903e7SBaptiste Daroussin {
17018e022d3cSDag-Erling Smørgrav /*
17028e022d3cSDag-Erling Smørgrav * When writing the action file, we did not know the line-numbers in
17038e022d3cSDag-Erling Smørgrav * the code-file, but wrote empty #line directives. Detect those and
17048e022d3cSDag-Erling Smørgrav * replace with proper #line directives.
17058e022d3cSDag-Erling Smørgrav */
17068e022d3cSDag-Erling Smørgrav if (!lflag && (last == '\n' || state >= 0))
17078e022d3cSDag-Erling Smørgrav {
17088e022d3cSDag-Erling Smørgrav if (c == line_state[state + 1])
17098e022d3cSDag-Erling Smørgrav {
17108e022d3cSDag-Erling Smørgrav ++state;
17118e022d3cSDag-Erling Smørgrav if (line_state[state + 1] == '\0')
17128e022d3cSDag-Erling Smørgrav {
17138e022d3cSDag-Erling Smørgrav write_code_lineno(code_file);
17148e022d3cSDag-Erling Smørgrav state = -1;
17158e022d3cSDag-Erling Smørgrav }
17168e022d3cSDag-Erling Smørgrav last = c;
17178e022d3cSDag-Erling Smørgrav continue;
17188e022d3cSDag-Erling Smørgrav }
17198e022d3cSDag-Erling Smørgrav else
17208e022d3cSDag-Erling Smørgrav {
17218e022d3cSDag-Erling Smørgrav int n;
17228e022d3cSDag-Erling Smørgrav for (n = 0; n <= state; ++n)
17238e022d3cSDag-Erling Smørgrav putc_code(code_file, line_state[n]);
17248e022d3cSDag-Erling Smørgrav state = -1;
17258e022d3cSDag-Erling Smørgrav }
17268e022d3cSDag-Erling Smørgrav }
172798e903e7SBaptiste Daroussin putc_code(code_file, c);
172898e903e7SBaptiste Daroussin last = c;
172998e903e7SBaptiste Daroussin }
173098e903e7SBaptiste Daroussin
173198e903e7SBaptiste Daroussin if (last != '\n')
173298e903e7SBaptiste Daroussin {
173398e903e7SBaptiste Daroussin putc_code(code_file, '\n');
173498e903e7SBaptiste Daroussin }
173598e903e7SBaptiste Daroussin
173698e903e7SBaptiste Daroussin write_code_lineno(code_file);
173798e903e7SBaptiste Daroussin }
173898e903e7SBaptiste Daroussin
173998e903e7SBaptiste Daroussin static void
output_parse_decl(FILE * fp)174098e903e7SBaptiste Daroussin output_parse_decl(FILE * fp)
174198e903e7SBaptiste Daroussin {
17420c8de5b0SBaptiste Daroussin putc_code(fp, '\n');
174398e903e7SBaptiste Daroussin putl_code(fp, "/* compatibility with bison */\n");
174498e903e7SBaptiste Daroussin putl_code(fp, "#ifdef YYPARSE_PARAM\n");
174598e903e7SBaptiste Daroussin putl_code(fp, "/* compatibility with FreeBSD */\n");
174698e903e7SBaptiste Daroussin putl_code(fp, "# ifdef YYPARSE_PARAM_TYPE\n");
174798e903e7SBaptiste Daroussin putl_code(fp,
174898e903e7SBaptiste Daroussin "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
174998e903e7SBaptiste Daroussin putl_code(fp, "# else\n");
175098e903e7SBaptiste Daroussin putl_code(fp, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n");
175198e903e7SBaptiste Daroussin putl_code(fp, "# endif\n");
175298e903e7SBaptiste Daroussin putl_code(fp, "#else\n");
175398e903e7SBaptiste Daroussin
175498e903e7SBaptiste Daroussin puts_code(fp, "# define YYPARSE_DECL() yyparse(");
175511fce282SBaptiste Daroussin puts_param_types(fp, parse_param, 0);
175698e903e7SBaptiste Daroussin putl_code(fp, ")\n");
175798e903e7SBaptiste Daroussin
175898e903e7SBaptiste Daroussin putl_code(fp, "#endif\n");
175998e903e7SBaptiste Daroussin }
176098e903e7SBaptiste Daroussin
176198e903e7SBaptiste Daroussin static void
output_lex_decl(FILE * fp)176298e903e7SBaptiste Daroussin output_lex_decl(FILE * fp)
176398e903e7SBaptiste Daroussin {
17640c8de5b0SBaptiste Daroussin putc_code(fp, '\n');
176598e903e7SBaptiste Daroussin putl_code(fp, "/* Parameters sent to lex. */\n");
176698e903e7SBaptiste Daroussin putl_code(fp, "#ifdef YYLEX_PARAM\n");
176798e903e7SBaptiste Daroussin if (pure_parser)
176898e903e7SBaptiste Daroussin {
176998e903e7SBaptiste Daroussin putl_code(fp, "# ifdef YYLEX_PARAM_TYPE\n");
17700c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
17710c8de5b0SBaptiste Daroussin if (locations)
17720c8de5b0SBaptiste Daroussin {
177311fce282SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
177411fce282SBaptiste Daroussin " YYLTYPE *yylloc,"
17750c8de5b0SBaptiste Daroussin " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
17760c8de5b0SBaptiste Daroussin }
17770c8de5b0SBaptiste Daroussin else
17780c8de5b0SBaptiste Daroussin #endif
17790c8de5b0SBaptiste Daroussin {
178098e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
178198e903e7SBaptiste Daroussin " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
17820c8de5b0SBaptiste Daroussin }
178398e903e7SBaptiste Daroussin putl_code(fp, "# else\n");
17840c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
17850c8de5b0SBaptiste Daroussin if (locations)
17860c8de5b0SBaptiste Daroussin {
178711fce282SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
178811fce282SBaptiste Daroussin " YYLTYPE *yylloc,"
17890c8de5b0SBaptiste Daroussin " void * YYLEX_PARAM)\n");
17900c8de5b0SBaptiste Daroussin }
17910c8de5b0SBaptiste Daroussin else
17920c8de5b0SBaptiste Daroussin #endif
17930c8de5b0SBaptiste Daroussin {
179498e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
179598e903e7SBaptiste Daroussin " void * YYLEX_PARAM)\n");
17960c8de5b0SBaptiste Daroussin }
179798e903e7SBaptiste Daroussin putl_code(fp, "# endif\n");
17980c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
17990c8de5b0SBaptiste Daroussin if (locations)
18000c8de5b0SBaptiste Daroussin putl_code(fp,
18010c8de5b0SBaptiste Daroussin "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n");
18020c8de5b0SBaptiste Daroussin else
18030c8de5b0SBaptiste Daroussin #endif
180498e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
180598e903e7SBaptiste Daroussin }
180698e903e7SBaptiste Daroussin else
180798e903e7SBaptiste Daroussin {
180898e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n");
180998e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex(YYLEX_PARAM)\n");
181098e903e7SBaptiste Daroussin }
181198e903e7SBaptiste Daroussin putl_code(fp, "#else\n");
181298e903e7SBaptiste Daroussin if (pure_parser && lex_param)
181398e903e7SBaptiste Daroussin {
18140c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
18150c8de5b0SBaptiste Daroussin if (locations)
18160c8de5b0SBaptiste Daroussin puts_code(fp,
18170c8de5b0SBaptiste Daroussin "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, ");
18180c8de5b0SBaptiste Daroussin else
18190c8de5b0SBaptiste Daroussin #endif
182098e903e7SBaptiste Daroussin puts_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, ");
182111fce282SBaptiste Daroussin puts_param_types(fp, lex_param, 0);
182298e903e7SBaptiste Daroussin putl_code(fp, ")\n");
182398e903e7SBaptiste Daroussin
18240c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
18250c8de5b0SBaptiste Daroussin if (locations)
18260c8de5b0SBaptiste Daroussin puts_code(fp, "# define YYLEX yylex(&yylval, &yylloc, ");
18270c8de5b0SBaptiste Daroussin else
18280c8de5b0SBaptiste Daroussin #endif
182998e903e7SBaptiste Daroussin puts_code(fp, "# define YYLEX yylex(&yylval, ");
183011fce282SBaptiste Daroussin puts_param_names(fp, lex_param, 0);
183198e903e7SBaptiste Daroussin putl_code(fp, ")\n");
183298e903e7SBaptiste Daroussin }
183398e903e7SBaptiste Daroussin else if (pure_parser)
183498e903e7SBaptiste Daroussin {
18350c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
18360c8de5b0SBaptiste Daroussin if (locations)
18370c8de5b0SBaptiste Daroussin {
18380c8de5b0SBaptiste Daroussin putl_code(fp,
18390c8de5b0SBaptiste Daroussin "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n");
18400c8de5b0SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex(&yylval, &yylloc)\n");
18410c8de5b0SBaptiste Daroussin }
18420c8de5b0SBaptiste Daroussin else
18430c8de5b0SBaptiste Daroussin #endif
18440c8de5b0SBaptiste Daroussin {
184598e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n");
184698e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex(&yylval)\n");
184798e903e7SBaptiste Daroussin }
18480c8de5b0SBaptiste Daroussin }
184998e903e7SBaptiste Daroussin else if (lex_param)
185098e903e7SBaptiste Daroussin {
185198e903e7SBaptiste Daroussin puts_code(fp, "# define YYLEX_DECL() yylex(");
185211fce282SBaptiste Daroussin puts_param_types(fp, lex_param, 0);
185398e903e7SBaptiste Daroussin putl_code(fp, ")\n");
185498e903e7SBaptiste Daroussin
185598e903e7SBaptiste Daroussin puts_code(fp, "# define YYLEX yylex(");
185611fce282SBaptiste Daroussin puts_param_names(fp, lex_param, 0);
185798e903e7SBaptiste Daroussin putl_code(fp, ")\n");
185898e903e7SBaptiste Daroussin }
185998e903e7SBaptiste Daroussin else
186098e903e7SBaptiste Daroussin {
186198e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(void)\n");
186298e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex()\n");
186398e903e7SBaptiste Daroussin }
186498e903e7SBaptiste Daroussin putl_code(fp, "#endif\n");
1865b53bb29fSJung-uk Kim
1866b53bb29fSJung-uk Kim /*
1867b53bb29fSJung-uk Kim * Provide a prototype for yylex for the simplest case. This is done for
1868b53bb29fSJung-uk Kim * better compatibility with older yacc's, but can be a problem if someone
1869b53bb29fSJung-uk Kim * uses "static int yylex(void);"
1870b53bb29fSJung-uk Kim */
1871b53bb29fSJung-uk Kim if (!pure_parser
1872b53bb29fSJung-uk Kim #if defined(YYBTYACC)
1873b53bb29fSJung-uk Kim && !backtrack
1874b53bb29fSJung-uk Kim #endif
1875b53bb29fSJung-uk Kim && !strcmp(symbol_prefix, "yy"))
1876b53bb29fSJung-uk Kim {
1877b53bb29fSJung-uk Kim putl_code(fp, "\n");
1878b53bb29fSJung-uk Kim putl_code(fp, "#if !(defined(yylex) || defined(YYSTATE))\n");
1879b53bb29fSJung-uk Kim putl_code(fp, "int YYLEX_DECL();\n");
1880b53bb29fSJung-uk Kim putl_code(fp, "#endif\n");
1881b53bb29fSJung-uk Kim }
188298e903e7SBaptiste Daroussin }
188398e903e7SBaptiste Daroussin
188498e903e7SBaptiste Daroussin static void
output_error_decl(FILE * fp)188598e903e7SBaptiste Daroussin output_error_decl(FILE * fp)
188698e903e7SBaptiste Daroussin {
18870c8de5b0SBaptiste Daroussin putc_code(fp, '\n');
18880c8de5b0SBaptiste Daroussin putl_code(fp, "/* Parameters sent to yyerror. */\n");
188998e903e7SBaptiste Daroussin putl_code(fp, "#ifndef YYERROR_DECL\n");
18900c8de5b0SBaptiste Daroussin puts_code(fp, "#define YYERROR_DECL() yyerror(");
18910c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
18920c8de5b0SBaptiste Daroussin if (locations)
18932aca18c7SJung-uk Kim puts_code(fp, "YYLTYPE *loc, ");
18940c8de5b0SBaptiste Daroussin #endif
189511fce282SBaptiste Daroussin puts_param_types(fp, parse_param, 1);
189698e903e7SBaptiste Daroussin putl_code(fp, "const char *s)\n");
189798e903e7SBaptiste Daroussin putl_code(fp, "#endif\n");
189898e903e7SBaptiste Daroussin
189998e903e7SBaptiste Daroussin putl_code(fp, "#ifndef YYERROR_CALL\n");
190098e903e7SBaptiste Daroussin
190111fce282SBaptiste Daroussin puts_code(fp, "#define YYERROR_CALL(msg) yyerror(");
19020c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
19030c8de5b0SBaptiste Daroussin if (locations)
19042aca18c7SJung-uk Kim puts_code(fp, "&yylloc, ");
19050c8de5b0SBaptiste Daroussin #endif
190611fce282SBaptiste Daroussin puts_param_names(fp, parse_param, 1);
190798e903e7SBaptiste Daroussin putl_code(fp, "msg)\n");
190811fce282SBaptiste Daroussin
190998e903e7SBaptiste Daroussin putl_code(fp, "#endif\n");
191098e903e7SBaptiste Daroussin }
19110c8de5b0SBaptiste Daroussin
19120c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
19130c8de5b0SBaptiste Daroussin static void
output_yydestruct_decl(FILE * fp)19140c8de5b0SBaptiste Daroussin output_yydestruct_decl(FILE * fp)
191598e903e7SBaptiste Daroussin {
19160c8de5b0SBaptiste Daroussin putc_code(fp, '\n');
19170c8de5b0SBaptiste Daroussin putl_code(fp, "#ifndef YYDESTRUCT_DECL\n");
191811fce282SBaptiste Daroussin
191911fce282SBaptiste Daroussin puts_code(fp,
192011fce282SBaptiste Daroussin "#define YYDESTRUCT_DECL() "
192111fce282SBaptiste Daroussin "yydestruct(const char *msg, int psymb, YYSTYPE *val");
19220c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
19230c8de5b0SBaptiste Daroussin if (locations)
192411fce282SBaptiste Daroussin puts_code(fp, ", YYLTYPE *loc");
19250c8de5b0SBaptiste Daroussin #endif
192611fce282SBaptiste Daroussin if (parse_param)
192711fce282SBaptiste Daroussin {
192811fce282SBaptiste Daroussin puts_code(fp, ", ");
192911fce282SBaptiste Daroussin puts_param_types(fp, parse_param, 0);
193011fce282SBaptiste Daroussin }
193111fce282SBaptiste Daroussin putl_code(fp, ")\n");
193211fce282SBaptiste Daroussin
193398e903e7SBaptiste Daroussin putl_code(fp, "#endif\n");
193411fce282SBaptiste Daroussin
19350c8de5b0SBaptiste Daroussin putl_code(fp, "#ifndef YYDESTRUCT_CALL\n");
193611fce282SBaptiste Daroussin
193711fce282SBaptiste Daroussin puts_code(fp, "#define YYDESTRUCT_CALL(msg, psymb, val");
19380c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
19390c8de5b0SBaptiste Daroussin if (locations)
194011fce282SBaptiste Daroussin puts_code(fp, ", loc");
19410c8de5b0SBaptiste Daroussin #endif
194211fce282SBaptiste Daroussin puts_code(fp, ") yydestruct(msg, psymb, val");
194311fce282SBaptiste Daroussin #if defined(YYBTYACC)
194411fce282SBaptiste Daroussin if (locations)
194511fce282SBaptiste Daroussin puts_code(fp, ", loc");
194611fce282SBaptiste Daroussin #endif
194711fce282SBaptiste Daroussin if (parse_param)
194811fce282SBaptiste Daroussin {
194911fce282SBaptiste Daroussin puts_code(fp, ", ");
195011fce282SBaptiste Daroussin puts_param_names(fp, parse_param, 0);
195111fce282SBaptiste Daroussin }
195211fce282SBaptiste Daroussin putl_code(fp, ")\n");
195311fce282SBaptiste Daroussin
195498e903e7SBaptiste Daroussin putl_code(fp, "#endif\n");
195598e903e7SBaptiste Daroussin }
19560c8de5b0SBaptiste Daroussin
19570c8de5b0SBaptiste Daroussin static void
output_initial_action(void)19582aca18c7SJung-uk Kim output_initial_action(void)
19592aca18c7SJung-uk Kim {
19602aca18c7SJung-uk Kim if (initial_action)
19612aca18c7SJung-uk Kim fprintf(code_file, "%s\n", initial_action);
19622aca18c7SJung-uk Kim }
19632aca18c7SJung-uk Kim
19642aca18c7SJung-uk Kim static void
output_yydestruct_impl(void)19650c8de5b0SBaptiste Daroussin output_yydestruct_impl(void)
19660c8de5b0SBaptiste Daroussin {
19670c8de5b0SBaptiste Daroussin int i;
19680c8de5b0SBaptiste Daroussin char *s, *destructor_code;
19690c8de5b0SBaptiste Daroussin
19700c8de5b0SBaptiste Daroussin putc_code(code_file, '\n');
19710c8de5b0SBaptiste Daroussin putl_code(code_file, "/* Release memory associated with symbol. */\n");
19720c8de5b0SBaptiste Daroussin putl_code(code_file, "#if ! defined YYDESTRUCT_IS_DECLARED\n");
19730c8de5b0SBaptiste Daroussin putl_code(code_file, "static void\n");
19740c8de5b0SBaptiste Daroussin putl_code(code_file, "YYDESTRUCT_DECL()\n");
19750c8de5b0SBaptiste Daroussin putl_code(code_file, "{\n");
19760c8de5b0SBaptiste Daroussin putl_code(code_file, " switch (psymb)\n");
19770c8de5b0SBaptiste Daroussin putl_code(code_file, " {\n");
19780c8de5b0SBaptiste Daroussin for (i = 2; i < nsyms; ++i)
19790c8de5b0SBaptiste Daroussin {
19800c8de5b0SBaptiste Daroussin if ((destructor_code = symbol_destructor[i]) != NULL)
19810c8de5b0SBaptiste Daroussin {
19820c8de5b0SBaptiste Daroussin ++outline;
19830c8de5b0SBaptiste Daroussin fprintf(code_file, "\tcase %d:\n", symbol_pval[i]);
19840c8de5b0SBaptiste Daroussin /* comprehend the number of lines in the destructor code */
19850c8de5b0SBaptiste Daroussin for (s = destructor_code; (s = strchr(s, '\n')) != NULL; s++)
19860c8de5b0SBaptiste Daroussin ++outline;
19870c8de5b0SBaptiste Daroussin puts_code(code_file, destructor_code);
19880c8de5b0SBaptiste Daroussin putc_code(code_file, '\n');
19890c8de5b0SBaptiste Daroussin write_code_lineno(code_file);
19908e022d3cSDag-Erling Smørgrav putl_code(code_file, "\tbreak;\n");
19910c8de5b0SBaptiste Daroussin FREE(destructor_code);
199298e903e7SBaptiste Daroussin }
19930c8de5b0SBaptiste Daroussin }
19940c8de5b0SBaptiste Daroussin putl_code(code_file, " }\n");
19950c8de5b0SBaptiste Daroussin putl_code(code_file, "}\n");
19960c8de5b0SBaptiste Daroussin putl_code(code_file, "#define YYDESTRUCT_IS_DECLARED 1\n");
19970c8de5b0SBaptiste Daroussin putl_code(code_file, "#endif\n");
19980c8de5b0SBaptiste Daroussin
19990c8de5b0SBaptiste Daroussin DO_FREE(symbol_destructor);
20000c8de5b0SBaptiste Daroussin }
20010c8de5b0SBaptiste Daroussin #endif
200298e903e7SBaptiste Daroussin
200398e903e7SBaptiste Daroussin static void
free_itemsets(void)200498e903e7SBaptiste Daroussin free_itemsets(void)
200598e903e7SBaptiste Daroussin {
200698e903e7SBaptiste Daroussin core *cp, *next;
200798e903e7SBaptiste Daroussin
200898e903e7SBaptiste Daroussin FREE(state_table);
200998e903e7SBaptiste Daroussin for (cp = first_state; cp; cp = next)
201098e903e7SBaptiste Daroussin {
201198e903e7SBaptiste Daroussin next = cp->next;
201298e903e7SBaptiste Daroussin FREE(cp);
201398e903e7SBaptiste Daroussin }
201498e903e7SBaptiste Daroussin }
201598e903e7SBaptiste Daroussin
201698e903e7SBaptiste Daroussin static void
free_shifts(void)201798e903e7SBaptiste Daroussin free_shifts(void)
201898e903e7SBaptiste Daroussin {
201998e903e7SBaptiste Daroussin shifts *sp, *next;
202098e903e7SBaptiste Daroussin
202198e903e7SBaptiste Daroussin FREE(shift_table);
202298e903e7SBaptiste Daroussin for (sp = first_shift; sp; sp = next)
202398e903e7SBaptiste Daroussin {
202498e903e7SBaptiste Daroussin next = sp->next;
202598e903e7SBaptiste Daroussin FREE(sp);
202698e903e7SBaptiste Daroussin }
202798e903e7SBaptiste Daroussin }
202898e903e7SBaptiste Daroussin
202998e903e7SBaptiste Daroussin static void
free_reductions(void)203098e903e7SBaptiste Daroussin free_reductions(void)
203198e903e7SBaptiste Daroussin {
203298e903e7SBaptiste Daroussin reductions *rp, *next;
203398e903e7SBaptiste Daroussin
203498e903e7SBaptiste Daroussin FREE(reduction_table);
203598e903e7SBaptiste Daroussin for (rp = first_reduction; rp; rp = next)
203698e903e7SBaptiste Daroussin {
203798e903e7SBaptiste Daroussin next = rp->next;
203898e903e7SBaptiste Daroussin FREE(rp);
203998e903e7SBaptiste Daroussin }
204098e903e7SBaptiste Daroussin }
204198e903e7SBaptiste Daroussin
204298e903e7SBaptiste Daroussin static void
output_externs(FILE * fp,const char * const section[])204398e903e7SBaptiste Daroussin output_externs(FILE * fp, const char *const section[])
204498e903e7SBaptiste Daroussin {
204598e903e7SBaptiste Daroussin int i;
204698e903e7SBaptiste Daroussin const char *s;
204798e903e7SBaptiste Daroussin
204898e903e7SBaptiste Daroussin for (i = 0; (s = section[i]) != 0; ++i)
204998e903e7SBaptiste Daroussin {
20500c8de5b0SBaptiste Daroussin /* prefix non-blank lines that don't start with
20510c8de5b0SBaptiste Daroussin C pre-processor directives with 'extern ' */
20520c8de5b0SBaptiste Daroussin if (*s && (*s != '#'))
205398e903e7SBaptiste Daroussin fputs("extern\t", fp);
205498e903e7SBaptiste Daroussin if (fp == code_file)
205598e903e7SBaptiste Daroussin ++outline;
20560c8de5b0SBaptiste Daroussin fprintf(fp, "%s\n", s);
205798e903e7SBaptiste Daroussin }
205898e903e7SBaptiste Daroussin }
205998e903e7SBaptiste Daroussin
206098e903e7SBaptiste Daroussin void
output(void)206198e903e7SBaptiste Daroussin output(void)
206298e903e7SBaptiste Daroussin {
206398e903e7SBaptiste Daroussin FILE *fp;
206498e903e7SBaptiste Daroussin
206598e903e7SBaptiste Daroussin free_itemsets();
206698e903e7SBaptiste Daroussin free_shifts();
206798e903e7SBaptiste Daroussin free_reductions();
206898e903e7SBaptiste Daroussin
2069b53bb29fSJung-uk Kim output_code_lines(code_file, CODE_TOP);
20700c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
20710c8de5b0SBaptiste Daroussin output_backtracking_parser(output_file);
20720c8de5b0SBaptiste Daroussin if (rflag)
20730c8de5b0SBaptiste Daroussin output_backtracking_parser(code_file);
20740c8de5b0SBaptiste Daroussin #endif
20750c8de5b0SBaptiste Daroussin
207698e903e7SBaptiste Daroussin if (iflag)
207798e903e7SBaptiste Daroussin {
20780c8de5b0SBaptiste Daroussin write_code_lineno(code_file);
207998e903e7SBaptiste Daroussin ++outline;
208098e903e7SBaptiste Daroussin fprintf(code_file, "#include \"%s\"\n", externs_file_name);
208198e903e7SBaptiste Daroussin fp = externs_file;
208298e903e7SBaptiste Daroussin }
208398e903e7SBaptiste Daroussin else
208498e903e7SBaptiste Daroussin fp = code_file;
208598e903e7SBaptiste Daroussin
20860c8de5b0SBaptiste Daroussin output_prefix(fp);
208798e903e7SBaptiste Daroussin output_pure_parser(fp);
208898e903e7SBaptiste Daroussin output_stored_text(fp);
208998e903e7SBaptiste Daroussin output_stype(fp);
20900c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
20910c8de5b0SBaptiste Daroussin if (locations)
20920c8de5b0SBaptiste Daroussin output_ltype(fp);
20930c8de5b0SBaptiste Daroussin #endif
209498e903e7SBaptiste Daroussin output_parse_decl(fp);
209598e903e7SBaptiste Daroussin output_lex_decl(fp);
209698e903e7SBaptiste Daroussin output_error_decl(fp);
20970c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
20980c8de5b0SBaptiste Daroussin if (destructor)
20990c8de5b0SBaptiste Daroussin output_yydestruct_decl(fp);
21000c8de5b0SBaptiste Daroussin #endif
21010c8de5b0SBaptiste Daroussin if (iflag || !rflag)
21020c8de5b0SBaptiste Daroussin {
210398e903e7SBaptiste Daroussin write_section(fp, xdecls);
21040c8de5b0SBaptiste Daroussin }
210598e903e7SBaptiste Daroussin
210698e903e7SBaptiste Daroussin if (iflag)
210798e903e7SBaptiste Daroussin {
2108b53bb29fSJung-uk Kim fprintf(externs_file, "\n");
2109b53bb29fSJung-uk Kim output_yydebug(externs_file);
211098e903e7SBaptiste Daroussin output_externs(externs_file, global_vars);
211198e903e7SBaptiste Daroussin if (!pure_parser)
211298e903e7SBaptiste Daroussin output_externs(externs_file, impure_vars);
21135b91e83fSBaptiste Daroussin if (dflag)
21145b91e83fSBaptiste Daroussin {
211598e903e7SBaptiste Daroussin ++outline;
211698e903e7SBaptiste Daroussin fprintf(code_file, "#include \"%s\"\n", defines_file_name);
21175b91e83fSBaptiste Daroussin }
21185b91e83fSBaptiste Daroussin else
211998e903e7SBaptiste Daroussin output_defines(externs_file);
212098e903e7SBaptiste Daroussin }
212198e903e7SBaptiste Daroussin else
212298e903e7SBaptiste Daroussin {
212398e903e7SBaptiste Daroussin putc_code(code_file, '\n');
212498e903e7SBaptiste Daroussin output_defines(code_file);
212598e903e7SBaptiste Daroussin }
212698e903e7SBaptiste Daroussin
212798e903e7SBaptiste Daroussin if (dflag)
21280c8de5b0SBaptiste Daroussin {
21290c8de5b0SBaptiste Daroussin start_defines_file();
213098e903e7SBaptiste Daroussin output_defines(defines_file);
21310c8de5b0SBaptiste Daroussin end_defines_file();
21320c8de5b0SBaptiste Daroussin }
213398e903e7SBaptiste Daroussin
213498e903e7SBaptiste Daroussin output_rule_data();
213598e903e7SBaptiste Daroussin output_yydefred();
21360c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
21370c8de5b0SBaptiste Daroussin output_accessing_symbols();
21380c8de5b0SBaptiste Daroussin #endif
213998e903e7SBaptiste Daroussin output_actions();
214098e903e7SBaptiste Daroussin free_parser();
214198e903e7SBaptiste Daroussin output_debug();
2142b53bb29fSJung-uk Kim
214398e903e7SBaptiste Daroussin if (rflag)
214498e903e7SBaptiste Daroussin {
214598e903e7SBaptiste Daroussin write_section(code_file, xdecls);
21460c8de5b0SBaptiste Daroussin output_YYINT_typedef(code_file);
214798e903e7SBaptiste Daroussin write_section(code_file, tables);
214898e903e7SBaptiste Daroussin }
2149b53bb29fSJung-uk Kim
215098e903e7SBaptiste Daroussin write_section(code_file, global_vars);
215198e903e7SBaptiste Daroussin if (!pure_parser)
215298e903e7SBaptiste Daroussin {
215398e903e7SBaptiste Daroussin write_section(code_file, impure_vars);
215498e903e7SBaptiste Daroussin }
2155b53bb29fSJung-uk Kim output_code_lines(code_file, CODE_REQUIRES);
2156b53bb29fSJung-uk Kim
215798e903e7SBaptiste Daroussin write_section(code_file, hdr_defs);
215898e903e7SBaptiste Daroussin if (!pure_parser)
215998e903e7SBaptiste Daroussin {
216098e903e7SBaptiste Daroussin write_section(code_file, hdr_vars);
216198e903e7SBaptiste Daroussin }
2162b53bb29fSJung-uk Kim
2163b53bb29fSJung-uk Kim output_code_lines(code_file, CODE_PROVIDES);
2164b53bb29fSJung-uk Kim output_code_lines(code_file, CODE_HEADER);
2165b53bb29fSJung-uk Kim
216698e903e7SBaptiste Daroussin output_trailing_text();
21670c8de5b0SBaptiste Daroussin #if defined(YYBTYACC)
21680c8de5b0SBaptiste Daroussin if (destructor)
21690c8de5b0SBaptiste Daroussin output_yydestruct_impl();
21700c8de5b0SBaptiste Daroussin #endif
217198e903e7SBaptiste Daroussin write_section(code_file, body_1);
217298e903e7SBaptiste Daroussin if (pure_parser)
217398e903e7SBaptiste Daroussin {
217498e903e7SBaptiste Daroussin write_section(code_file, body_vars);
217598e903e7SBaptiste Daroussin }
217698e903e7SBaptiste Daroussin write_section(code_file, body_2);
21776166fdceSJung-uk Kim if (pure_parser)
21786166fdceSJung-uk Kim {
21796166fdceSJung-uk Kim write_section(code_file, init_vars);
21806166fdceSJung-uk Kim }
21812aca18c7SJung-uk Kim #if defined(YYBTYACC)
21822aca18c7SJung-uk Kim if (initial_action)
21832aca18c7SJung-uk Kim output_initial_action();
21842aca18c7SJung-uk Kim #endif
21852aca18c7SJung-uk Kim write_section(code_file, body_3);
218698e903e7SBaptiste Daroussin output_semantic_actions();
218798e903e7SBaptiste Daroussin write_section(code_file, trailer);
218898e903e7SBaptiste Daroussin }
218998e903e7SBaptiste Daroussin
219098e903e7SBaptiste Daroussin #ifdef NO_LEAKS
219198e903e7SBaptiste Daroussin void
output_leaks(void)219298e903e7SBaptiste Daroussin output_leaks(void)
219398e903e7SBaptiste Daroussin {
219498e903e7SBaptiste Daroussin DO_FREE(tally);
219598e903e7SBaptiste Daroussin DO_FREE(width);
219698e903e7SBaptiste Daroussin DO_FREE(order);
219798e903e7SBaptiste Daroussin }
219898e903e7SBaptiste Daroussin #endif
2199