1*6166fdceSJung-uk Kim /* $Id: output.c,v 1.81 2017/04/30 23:23:32 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 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 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 5198e903e7SBaptiste Daroussin puts_code(FILE * fp, const char *s) 5298e903e7SBaptiste Daroussin { 5398e903e7SBaptiste Daroussin fputs(s, fp); 5498e903e7SBaptiste Daroussin } 5598e903e7SBaptiste Daroussin 5698e903e7SBaptiste Daroussin static void 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 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 9498e903e7SBaptiste Daroussin write_code_lineno(FILE * fp) 9598e903e7SBaptiste Daroussin { 9698e903e7SBaptiste Daroussin if (!lflag && (fp == code_file)) 9798e903e7SBaptiste Daroussin { 9898e903e7SBaptiste Daroussin ++outline; 990c8de5b0SBaptiste Daroussin fprintf(fp, line_format, outline + 1, code_file_name); 10098e903e7SBaptiste Daroussin } 10198e903e7SBaptiste Daroussin } 10298e903e7SBaptiste Daroussin 10398e903e7SBaptiste Daroussin static void 10498e903e7SBaptiste Daroussin write_input_lineno(void) 10598e903e7SBaptiste Daroussin { 10698e903e7SBaptiste Daroussin if (!lflag) 10798e903e7SBaptiste Daroussin { 10898e903e7SBaptiste Daroussin ++outline; 10998e903e7SBaptiste Daroussin fprintf(code_file, line_format, lineno, input_file_name); 11098e903e7SBaptiste Daroussin } 11198e903e7SBaptiste Daroussin } 11298e903e7SBaptiste Daroussin 11398e903e7SBaptiste Daroussin static void 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 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 18998e903e7SBaptiste Daroussin output_newline(void) 19098e903e7SBaptiste Daroussin { 19198e903e7SBaptiste Daroussin if (!rflag) 19298e903e7SBaptiste Daroussin ++outline; 19398e903e7SBaptiste Daroussin putc('\n', output_file); 19498e903e7SBaptiste Daroussin } 19598e903e7SBaptiste Daroussin 19698e903e7SBaptiste Daroussin static void 19798e903e7SBaptiste Daroussin output_line(const char *value) 19898e903e7SBaptiste Daroussin { 19998e903e7SBaptiste Daroussin fputs(value, output_file); 20098e903e7SBaptiste Daroussin output_newline(); 20198e903e7SBaptiste Daroussin } 20298e903e7SBaptiste Daroussin 20398e903e7SBaptiste Daroussin static void 20498e903e7SBaptiste Daroussin output_int(int value) 20598e903e7SBaptiste Daroussin { 20698e903e7SBaptiste Daroussin fprintf(output_file, "%5d,", value); 20798e903e7SBaptiste Daroussin } 20898e903e7SBaptiste Daroussin 20998e903e7SBaptiste Daroussin static void 21098e903e7SBaptiste Daroussin start_int_table(const char *name, int value) 21198e903e7SBaptiste Daroussin { 21298e903e7SBaptiste Daroussin int need = 34 - (int)(strlen(symbol_prefix) + strlen(name)); 21398e903e7SBaptiste Daroussin 21498e903e7SBaptiste Daroussin if (need < 6) 21598e903e7SBaptiste Daroussin need = 6; 21698e903e7SBaptiste Daroussin fprintf(output_file, 2170c8de5b0SBaptiste Daroussin "%sconst YYINT %s%s[] = {%*d,", 21898e903e7SBaptiste Daroussin StaticOrR, symbol_prefix, name, need, value); 21998e903e7SBaptiste Daroussin } 22098e903e7SBaptiste Daroussin 22198e903e7SBaptiste Daroussin static void 22298e903e7SBaptiste Daroussin start_str_table(const char *name) 22398e903e7SBaptiste Daroussin { 22498e903e7SBaptiste Daroussin fprintf(output_file, 2250c8de5b0SBaptiste Daroussin "%sconst char *const %s%s[] = {", 2260c8de5b0SBaptiste Daroussin StaticOrR, symbol_prefix, name); 22798e903e7SBaptiste Daroussin output_newline(); 22898e903e7SBaptiste Daroussin } 22998e903e7SBaptiste Daroussin 23098e903e7SBaptiste Daroussin static void 23198e903e7SBaptiste Daroussin end_table(void) 23298e903e7SBaptiste Daroussin { 23398e903e7SBaptiste Daroussin output_newline(); 23498e903e7SBaptiste Daroussin output_line("};"); 23598e903e7SBaptiste Daroussin } 23698e903e7SBaptiste Daroussin 23798e903e7SBaptiste Daroussin static void 2382aca18c7SJung-uk Kim output_stype(FILE * fp) 2392aca18c7SJung-uk Kim { 2402aca18c7SJung-uk Kim if (!unionized && ntags == 0) 2412aca18c7SJung-uk Kim { 2422aca18c7SJung-uk Kim putc_code(fp, '\n'); 2432aca18c7SJung-uk Kim putl_code(fp, "#if " 2442aca18c7SJung-uk Kim "! defined(YYSTYPE) && " 2452aca18c7SJung-uk Kim "! defined(YYSTYPE_IS_DECLARED)\n"); 2462aca18c7SJung-uk Kim putl_code(fp, "/* Default: YYSTYPE is the semantic value type. */\n"); 2472aca18c7SJung-uk Kim putl_code(fp, "typedef int YYSTYPE;\n"); 2482aca18c7SJung-uk Kim putl_code(fp, "# define YYSTYPE_IS_DECLARED 1\n"); 2492aca18c7SJung-uk Kim putl_code(fp, "#endif\n"); 2502aca18c7SJung-uk Kim } 2512aca18c7SJung-uk Kim } 2522aca18c7SJung-uk Kim 2532aca18c7SJung-uk Kim #if defined(YYBTYACC) 2542aca18c7SJung-uk Kim static void 2552aca18c7SJung-uk Kim output_ltype(FILE * fp) 2562aca18c7SJung-uk Kim { 2572aca18c7SJung-uk Kim putc_code(fp, '\n'); 2582aca18c7SJung-uk Kim putl_code(fp, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n"); 2592aca18c7SJung-uk Kim putl_code(fp, "/* Default: YYLTYPE is the text position type. */\n"); 2602aca18c7SJung-uk Kim putl_code(fp, "typedef struct YYLTYPE\n"); 2612aca18c7SJung-uk Kim putl_code(fp, "{\n"); 2622aca18c7SJung-uk Kim putl_code(fp, " int first_line;\n"); 2632aca18c7SJung-uk Kim putl_code(fp, " int first_column;\n"); 2642aca18c7SJung-uk Kim putl_code(fp, " int last_line;\n"); 2652aca18c7SJung-uk Kim putl_code(fp, " int last_column;\n"); 2662aca18c7SJung-uk Kim putl_code(fp, " unsigned source;\n"); 2672aca18c7SJung-uk Kim putl_code(fp, "} YYLTYPE;\n"); 2682aca18c7SJung-uk Kim putl_code(fp, "#define YYLTYPE_IS_DECLARED 1\n"); 2692aca18c7SJung-uk Kim putl_code(fp, "#endif\n"); 2702aca18c7SJung-uk Kim putl_code(fp, "#define YYRHSLOC(rhs, k) ((rhs)[k])\n"); 2712aca18c7SJung-uk Kim } 2722aca18c7SJung-uk Kim #endif 2732aca18c7SJung-uk Kim 2742aca18c7SJung-uk Kim static void 2750c8de5b0SBaptiste Daroussin output_YYINT_typedef(FILE * fp) 2760c8de5b0SBaptiste Daroussin { 2770c8de5b0SBaptiste Daroussin /* generate the type used to index the various parser tables */ 2780c8de5b0SBaptiste Daroussin if (CountLine(fp)) 2790c8de5b0SBaptiste Daroussin ++outline; 2800c8de5b0SBaptiste Daroussin fprintf(fp, "typedef %s YYINT;\n", CONCAT1("", YYINT)); 2810c8de5b0SBaptiste Daroussin } 2820c8de5b0SBaptiste Daroussin 2830c8de5b0SBaptiste Daroussin static void 28498e903e7SBaptiste Daroussin output_rule_data(void) 28598e903e7SBaptiste Daroussin { 28698e903e7SBaptiste Daroussin int i; 28798e903e7SBaptiste Daroussin int j; 28898e903e7SBaptiste Daroussin 2890c8de5b0SBaptiste Daroussin output_YYINT_typedef(output_file); 2900c8de5b0SBaptiste Daroussin 29198e903e7SBaptiste Daroussin start_int_table("lhs", symbol_value[start_symbol]); 29298e903e7SBaptiste Daroussin 29398e903e7SBaptiste Daroussin j = 10; 29498e903e7SBaptiste Daroussin for (i = 3; i < nrules; i++) 29598e903e7SBaptiste Daroussin { 29698e903e7SBaptiste Daroussin if (j >= 10) 29798e903e7SBaptiste Daroussin { 29898e903e7SBaptiste Daroussin output_newline(); 29998e903e7SBaptiste Daroussin j = 1; 30098e903e7SBaptiste Daroussin } 30198e903e7SBaptiste Daroussin else 30298e903e7SBaptiste Daroussin ++j; 30398e903e7SBaptiste Daroussin 30498e903e7SBaptiste Daroussin output_int(symbol_value[rlhs[i]]); 30598e903e7SBaptiste Daroussin } 30698e903e7SBaptiste Daroussin end_table(); 30798e903e7SBaptiste Daroussin 30898e903e7SBaptiste Daroussin start_int_table("len", 2); 30998e903e7SBaptiste Daroussin 31098e903e7SBaptiste Daroussin j = 10; 31198e903e7SBaptiste Daroussin for (i = 3; i < nrules; i++) 31298e903e7SBaptiste Daroussin { 31398e903e7SBaptiste Daroussin if (j >= 10) 31498e903e7SBaptiste Daroussin { 31598e903e7SBaptiste Daroussin output_newline(); 31698e903e7SBaptiste Daroussin j = 1; 31798e903e7SBaptiste Daroussin } 31898e903e7SBaptiste Daroussin else 31998e903e7SBaptiste Daroussin j++; 32098e903e7SBaptiste Daroussin 32198e903e7SBaptiste Daroussin output_int(rrhs[i + 1] - rrhs[i] - 1); 32298e903e7SBaptiste Daroussin } 32398e903e7SBaptiste Daroussin end_table(); 32498e903e7SBaptiste Daroussin } 32598e903e7SBaptiste Daroussin 32698e903e7SBaptiste Daroussin static void 32798e903e7SBaptiste Daroussin output_yydefred(void) 32898e903e7SBaptiste Daroussin { 32998e903e7SBaptiste Daroussin int i, j; 33098e903e7SBaptiste Daroussin 33198e903e7SBaptiste Daroussin start_int_table("defred", (defred[0] ? defred[0] - 2 : 0)); 33298e903e7SBaptiste Daroussin 33398e903e7SBaptiste Daroussin j = 10; 33498e903e7SBaptiste Daroussin for (i = 1; i < nstates; i++) 33598e903e7SBaptiste Daroussin { 33698e903e7SBaptiste Daroussin if (j < 10) 33798e903e7SBaptiste Daroussin ++j; 33898e903e7SBaptiste Daroussin else 33998e903e7SBaptiste Daroussin { 34098e903e7SBaptiste Daroussin output_newline(); 34198e903e7SBaptiste Daroussin j = 1; 34298e903e7SBaptiste Daroussin } 34398e903e7SBaptiste Daroussin 34498e903e7SBaptiste Daroussin output_int((defred[i] ? defred[i] - 2 : 0)); 34598e903e7SBaptiste Daroussin } 34698e903e7SBaptiste Daroussin 34798e903e7SBaptiste Daroussin end_table(); 34898e903e7SBaptiste Daroussin } 34998e903e7SBaptiste Daroussin 3500c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 3510c8de5b0SBaptiste Daroussin static void 3520c8de5b0SBaptiste Daroussin output_accessing_symbols(void) 3530c8de5b0SBaptiste Daroussin { 3540c8de5b0SBaptiste Daroussin int i, j; 3550c8de5b0SBaptiste Daroussin int *translate; 3560c8de5b0SBaptiste Daroussin 3570c8de5b0SBaptiste Daroussin if (nstates != 0) 3580c8de5b0SBaptiste Daroussin { 3590c8de5b0SBaptiste Daroussin translate = TMALLOC(int, nstates); 3600c8de5b0SBaptiste Daroussin NO_SPACE(translate); 3610c8de5b0SBaptiste Daroussin 3620c8de5b0SBaptiste Daroussin for (i = 0; i < nstates; ++i) 3630c8de5b0SBaptiste Daroussin { 3640c8de5b0SBaptiste Daroussin int gsymb = accessing_symbol[i]; 3650c8de5b0SBaptiste Daroussin 3660c8de5b0SBaptiste Daroussin translate[i] = symbol_pval[gsymb]; 3670c8de5b0SBaptiste Daroussin } 3680c8de5b0SBaptiste Daroussin 3692aca18c7SJung-uk Kim putl_code(output_file, 3702aca18c7SJung-uk Kim "#if defined(YYDESTRUCT_CALL) || defined(YYSTYPE_TOSTRING)\n"); 3710c8de5b0SBaptiste Daroussin /* yystos[] may be unused, depending on compile-time defines */ 3720c8de5b0SBaptiste Daroussin start_int_table("stos", translate[0]); 3730c8de5b0SBaptiste Daroussin 3740c8de5b0SBaptiste Daroussin j = 10; 3750c8de5b0SBaptiste Daroussin for (i = 1; i < nstates; ++i) 3760c8de5b0SBaptiste Daroussin { 3770c8de5b0SBaptiste Daroussin if (j < 10) 3780c8de5b0SBaptiste Daroussin ++j; 3790c8de5b0SBaptiste Daroussin else 3800c8de5b0SBaptiste Daroussin { 3810c8de5b0SBaptiste Daroussin output_newline(); 3820c8de5b0SBaptiste Daroussin j = 1; 3830c8de5b0SBaptiste Daroussin } 3840c8de5b0SBaptiste Daroussin 3850c8de5b0SBaptiste Daroussin output_int(translate[i]); 3860c8de5b0SBaptiste Daroussin } 3870c8de5b0SBaptiste Daroussin 3880c8de5b0SBaptiste Daroussin end_table(); 3890c8de5b0SBaptiste Daroussin FREE(translate); 3902aca18c7SJung-uk Kim putl_code(output_file, 3912aca18c7SJung-uk Kim "#endif /* YYDESTRUCT_CALL || YYSTYPE_TOSTRING */\n"); 3920c8de5b0SBaptiste Daroussin } 3930c8de5b0SBaptiste Daroussin } 3940c8de5b0SBaptiste Daroussin 3950c8de5b0SBaptiste Daroussin static Value_t 396497dcf4cSBaptiste Daroussin find_conflict_base(int cbase) 3970c8de5b0SBaptiste Daroussin { 398497dcf4cSBaptiste Daroussin int i, j; 3990c8de5b0SBaptiste Daroussin 4000c8de5b0SBaptiste Daroussin for (i = 0; i < cbase; i++) 4010c8de5b0SBaptiste Daroussin { 4020c8de5b0SBaptiste Daroussin for (j = 0; j + cbase < nconflicts; j++) 4030c8de5b0SBaptiste Daroussin { 4040c8de5b0SBaptiste Daroussin if (conflicts[i + j] != conflicts[cbase + j]) 4050c8de5b0SBaptiste Daroussin break; 4060c8de5b0SBaptiste Daroussin } 4070c8de5b0SBaptiste Daroussin if (j + cbase >= nconflicts) 408497dcf4cSBaptiste Daroussin break; 4090c8de5b0SBaptiste Daroussin } 410497dcf4cSBaptiste Daroussin return (Value_t)i; 4110c8de5b0SBaptiste Daroussin } 4120c8de5b0SBaptiste Daroussin #endif 4130c8de5b0SBaptiste Daroussin 41498e903e7SBaptiste Daroussin static void 41598e903e7SBaptiste Daroussin token_actions(void) 41698e903e7SBaptiste Daroussin { 41798e903e7SBaptiste Daroussin int i, j; 41898e903e7SBaptiste Daroussin Value_t shiftcount, reducecount; 4190c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 4200c8de5b0SBaptiste Daroussin Value_t conflictcount = 0; 4210c8de5b0SBaptiste Daroussin Value_t csym = -1; 4220c8de5b0SBaptiste Daroussin Value_t cbase = 0; 4230c8de5b0SBaptiste Daroussin #endif 42498e903e7SBaptiste Daroussin int max, min; 42598e903e7SBaptiste Daroussin Value_t *actionrow, *r, *s; 42698e903e7SBaptiste Daroussin action *p; 42798e903e7SBaptiste Daroussin 4280c8de5b0SBaptiste Daroussin actionrow = NEW2(PER_STATE * ntokens, Value_t); 42998e903e7SBaptiste Daroussin for (i = 0; i < nstates; ++i) 43098e903e7SBaptiste Daroussin { 43198e903e7SBaptiste Daroussin if (parser[i]) 43298e903e7SBaptiste Daroussin { 4330c8de5b0SBaptiste Daroussin for (j = 0; j < PER_STATE * ntokens; ++j) 43498e903e7SBaptiste Daroussin actionrow[j] = 0; 43598e903e7SBaptiste Daroussin 43698e903e7SBaptiste Daroussin shiftcount = 0; 43798e903e7SBaptiste Daroussin reducecount = 0; 4380c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 4390c8de5b0SBaptiste Daroussin if (backtrack) 4400c8de5b0SBaptiste Daroussin { 4410c8de5b0SBaptiste Daroussin conflictcount = 0; 4420c8de5b0SBaptiste Daroussin csym = -1; 4430c8de5b0SBaptiste Daroussin cbase = nconflicts; 4440c8de5b0SBaptiste Daroussin } 4450c8de5b0SBaptiste Daroussin #endif 44698e903e7SBaptiste Daroussin for (p = parser[i]; p; p = p->next) 44798e903e7SBaptiste Daroussin { 4480c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 4490c8de5b0SBaptiste Daroussin if (backtrack) 4500c8de5b0SBaptiste Daroussin { 4510c8de5b0SBaptiste Daroussin if (csym != -1 && csym != p->symbol) 4520c8de5b0SBaptiste Daroussin { 4530c8de5b0SBaptiste Daroussin conflictcount++; 4540c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = -1; 4550c8de5b0SBaptiste Daroussin j = find_conflict_base(cbase); 4560c8de5b0SBaptiste Daroussin actionrow[csym + 2 * ntokens] = (Value_t)(j + 1); 4570c8de5b0SBaptiste Daroussin if (j == cbase) 4580c8de5b0SBaptiste Daroussin { 4590c8de5b0SBaptiste Daroussin cbase = nconflicts; 4600c8de5b0SBaptiste Daroussin } 4610c8de5b0SBaptiste Daroussin else 4620c8de5b0SBaptiste Daroussin { 4630c8de5b0SBaptiste Daroussin if (conflicts[cbase] == -1) 4640c8de5b0SBaptiste Daroussin cbase++; 4650c8de5b0SBaptiste Daroussin nconflicts = cbase; 4660c8de5b0SBaptiste Daroussin } 4670c8de5b0SBaptiste Daroussin csym = -1; 4680c8de5b0SBaptiste Daroussin } 4690c8de5b0SBaptiste Daroussin } 4700c8de5b0SBaptiste Daroussin #endif 47198e903e7SBaptiste Daroussin if (p->suppressed == 0) 47298e903e7SBaptiste Daroussin { 47398e903e7SBaptiste Daroussin if (p->action_code == SHIFT) 47498e903e7SBaptiste Daroussin { 47598e903e7SBaptiste Daroussin ++shiftcount; 47698e903e7SBaptiste Daroussin actionrow[p->symbol] = p->number; 47798e903e7SBaptiste Daroussin } 47898e903e7SBaptiste Daroussin else if (p->action_code == REDUCE && p->number != defred[i]) 47998e903e7SBaptiste Daroussin { 48098e903e7SBaptiste Daroussin ++reducecount; 48198e903e7SBaptiste Daroussin actionrow[p->symbol + ntokens] = p->number; 48298e903e7SBaptiste Daroussin } 48398e903e7SBaptiste Daroussin } 4840c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 4850c8de5b0SBaptiste Daroussin else if (backtrack && p->suppressed == 1) 4860c8de5b0SBaptiste Daroussin { 4870c8de5b0SBaptiste Daroussin csym = p->symbol; 4880c8de5b0SBaptiste Daroussin if (p->action_code == SHIFT) 4890c8de5b0SBaptiste Daroussin { 4900c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = p->number; 49198e903e7SBaptiste Daroussin } 4920c8de5b0SBaptiste Daroussin else if (p->action_code == REDUCE && p->number != defred[i]) 4930c8de5b0SBaptiste Daroussin { 4940c8de5b0SBaptiste Daroussin if (cbase == nconflicts) 4950c8de5b0SBaptiste Daroussin { 4960c8de5b0SBaptiste Daroussin if (cbase) 4970c8de5b0SBaptiste Daroussin cbase--; 4980c8de5b0SBaptiste Daroussin else 4990c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = -1; 5000c8de5b0SBaptiste Daroussin } 5010c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = (Value_t)(p->number - 2); 5020c8de5b0SBaptiste Daroussin } 5030c8de5b0SBaptiste Daroussin } 5040c8de5b0SBaptiste Daroussin #endif 5050c8de5b0SBaptiste Daroussin } 5060c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 5070c8de5b0SBaptiste Daroussin if (backtrack && csym != -1) 5080c8de5b0SBaptiste Daroussin { 5090c8de5b0SBaptiste Daroussin conflictcount++; 5100c8de5b0SBaptiste Daroussin conflicts[nconflicts++] = -1; 5110c8de5b0SBaptiste Daroussin j = find_conflict_base(cbase); 5120c8de5b0SBaptiste Daroussin actionrow[csym + 2 * ntokens] = (Value_t)(j + 1); 5130c8de5b0SBaptiste Daroussin if (j == cbase) 5140c8de5b0SBaptiste Daroussin { 5150c8de5b0SBaptiste Daroussin cbase = nconflicts; 5160c8de5b0SBaptiste Daroussin } 5170c8de5b0SBaptiste Daroussin else 5180c8de5b0SBaptiste Daroussin { 5190c8de5b0SBaptiste Daroussin if (conflicts[cbase] == -1) 5200c8de5b0SBaptiste Daroussin cbase++; 5210c8de5b0SBaptiste Daroussin nconflicts = cbase; 5220c8de5b0SBaptiste Daroussin } 5230c8de5b0SBaptiste Daroussin } 5240c8de5b0SBaptiste Daroussin #endif 52598e903e7SBaptiste Daroussin 52698e903e7SBaptiste Daroussin tally[i] = shiftcount; 52798e903e7SBaptiste Daroussin tally[nstates + i] = reducecount; 5280c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 5290c8de5b0SBaptiste Daroussin if (backtrack) 5300c8de5b0SBaptiste Daroussin tally[2 * nstates + i] = conflictcount; 5310c8de5b0SBaptiste Daroussin #endif 53298e903e7SBaptiste Daroussin width[i] = 0; 53398e903e7SBaptiste Daroussin width[nstates + i] = 0; 5340c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 5350c8de5b0SBaptiste Daroussin if (backtrack) 5360c8de5b0SBaptiste Daroussin width[2 * nstates + i] = 0; 5370c8de5b0SBaptiste Daroussin #endif 53898e903e7SBaptiste Daroussin if (shiftcount > 0) 53998e903e7SBaptiste Daroussin { 54098e903e7SBaptiste Daroussin froms[i] = r = NEW2(shiftcount, Value_t); 54198e903e7SBaptiste Daroussin tos[i] = s = NEW2(shiftcount, Value_t); 5420c8de5b0SBaptiste Daroussin min = MAXYYINT; 54398e903e7SBaptiste Daroussin max = 0; 54498e903e7SBaptiste Daroussin for (j = 0; j < ntokens; ++j) 54598e903e7SBaptiste Daroussin { 54698e903e7SBaptiste Daroussin if (actionrow[j]) 54798e903e7SBaptiste Daroussin { 54898e903e7SBaptiste Daroussin if (min > symbol_value[j]) 54998e903e7SBaptiste Daroussin min = symbol_value[j]; 55098e903e7SBaptiste Daroussin if (max < symbol_value[j]) 55198e903e7SBaptiste Daroussin max = symbol_value[j]; 55298e903e7SBaptiste Daroussin *r++ = symbol_value[j]; 55398e903e7SBaptiste Daroussin *s++ = actionrow[j]; 55498e903e7SBaptiste Daroussin } 55598e903e7SBaptiste Daroussin } 55698e903e7SBaptiste Daroussin width[i] = (Value_t)(max - min + 1); 55798e903e7SBaptiste Daroussin } 55898e903e7SBaptiste Daroussin if (reducecount > 0) 55998e903e7SBaptiste Daroussin { 56098e903e7SBaptiste Daroussin froms[nstates + i] = r = NEW2(reducecount, Value_t); 56198e903e7SBaptiste Daroussin tos[nstates + i] = s = NEW2(reducecount, Value_t); 5620c8de5b0SBaptiste Daroussin min = MAXYYINT; 56398e903e7SBaptiste Daroussin max = 0; 56498e903e7SBaptiste Daroussin for (j = 0; j < ntokens; ++j) 56598e903e7SBaptiste Daroussin { 56698e903e7SBaptiste Daroussin if (actionrow[ntokens + j]) 56798e903e7SBaptiste Daroussin { 56898e903e7SBaptiste Daroussin if (min > symbol_value[j]) 56998e903e7SBaptiste Daroussin min = symbol_value[j]; 57098e903e7SBaptiste Daroussin if (max < symbol_value[j]) 57198e903e7SBaptiste Daroussin max = symbol_value[j]; 57298e903e7SBaptiste Daroussin *r++ = symbol_value[j]; 57398e903e7SBaptiste Daroussin *s++ = (Value_t)(actionrow[ntokens + j] - 2); 57498e903e7SBaptiste Daroussin } 57598e903e7SBaptiste Daroussin } 57698e903e7SBaptiste Daroussin width[nstates + i] = (Value_t)(max - min + 1); 57798e903e7SBaptiste Daroussin } 5780c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 5790c8de5b0SBaptiste Daroussin if (backtrack && conflictcount > 0) 5800c8de5b0SBaptiste Daroussin { 5810c8de5b0SBaptiste Daroussin froms[2 * nstates + i] = r = NEW2(conflictcount, Value_t); 5820c8de5b0SBaptiste Daroussin tos[2 * nstates + i] = s = NEW2(conflictcount, Value_t); 5830c8de5b0SBaptiste Daroussin min = MAXYYINT; 5840c8de5b0SBaptiste Daroussin max = 0; 5850c8de5b0SBaptiste Daroussin for (j = 0; j < ntokens; ++j) 5860c8de5b0SBaptiste Daroussin { 5870c8de5b0SBaptiste Daroussin if (actionrow[2 * ntokens + j]) 5880c8de5b0SBaptiste Daroussin { 5890c8de5b0SBaptiste Daroussin if (min > symbol_value[j]) 5900c8de5b0SBaptiste Daroussin min = symbol_value[j]; 5910c8de5b0SBaptiste Daroussin if (max < symbol_value[j]) 5920c8de5b0SBaptiste Daroussin max = symbol_value[j]; 5930c8de5b0SBaptiste Daroussin *r++ = symbol_value[j]; 5940c8de5b0SBaptiste Daroussin *s++ = (Value_t)(actionrow[2 * ntokens + j] - 1); 5950c8de5b0SBaptiste Daroussin } 5960c8de5b0SBaptiste Daroussin } 5970c8de5b0SBaptiste Daroussin width[2 * nstates + i] = (Value_t)(max - min + 1); 5980c8de5b0SBaptiste Daroussin } 5990c8de5b0SBaptiste Daroussin #endif 60098e903e7SBaptiste Daroussin } 60198e903e7SBaptiste Daroussin } 60298e903e7SBaptiste Daroussin FREE(actionrow); 60398e903e7SBaptiste Daroussin } 60498e903e7SBaptiste Daroussin 60598e903e7SBaptiste Daroussin static int 60698e903e7SBaptiste Daroussin default_goto(int symbol) 60798e903e7SBaptiste Daroussin { 60898e903e7SBaptiste Daroussin int i; 60998e903e7SBaptiste Daroussin int m; 61098e903e7SBaptiste Daroussin int n; 61198e903e7SBaptiste Daroussin int default_state; 61298e903e7SBaptiste Daroussin int max; 61398e903e7SBaptiste Daroussin 61498e903e7SBaptiste Daroussin m = goto_map[symbol]; 61598e903e7SBaptiste Daroussin n = goto_map[symbol + 1]; 61698e903e7SBaptiste Daroussin 61798e903e7SBaptiste Daroussin if (m == n) 61898e903e7SBaptiste Daroussin return (0); 61998e903e7SBaptiste Daroussin 62098e903e7SBaptiste Daroussin for (i = 0; i < nstates; i++) 62198e903e7SBaptiste Daroussin state_count[i] = 0; 62298e903e7SBaptiste Daroussin 62398e903e7SBaptiste Daroussin for (i = m; i < n; i++) 62498e903e7SBaptiste Daroussin state_count[to_state[i]]++; 62598e903e7SBaptiste Daroussin 62698e903e7SBaptiste Daroussin max = 0; 62798e903e7SBaptiste Daroussin default_state = 0; 62898e903e7SBaptiste Daroussin for (i = 0; i < nstates; i++) 62998e903e7SBaptiste Daroussin { 63098e903e7SBaptiste Daroussin if (state_count[i] > max) 63198e903e7SBaptiste Daroussin { 63298e903e7SBaptiste Daroussin max = state_count[i]; 63398e903e7SBaptiste Daroussin default_state = i; 63498e903e7SBaptiste Daroussin } 63598e903e7SBaptiste Daroussin } 63698e903e7SBaptiste Daroussin 63798e903e7SBaptiste Daroussin return (default_state); 63898e903e7SBaptiste Daroussin } 63998e903e7SBaptiste Daroussin 64098e903e7SBaptiste Daroussin static void 64198e903e7SBaptiste Daroussin save_column(int symbol, int default_state) 64298e903e7SBaptiste Daroussin { 64398e903e7SBaptiste Daroussin int i; 64498e903e7SBaptiste Daroussin int m; 64598e903e7SBaptiste Daroussin int n; 64698e903e7SBaptiste Daroussin Value_t *sp; 64798e903e7SBaptiste Daroussin Value_t *sp1; 64898e903e7SBaptiste Daroussin Value_t *sp2; 64998e903e7SBaptiste Daroussin Value_t count; 65098e903e7SBaptiste Daroussin int symno; 65198e903e7SBaptiste Daroussin 65298e903e7SBaptiste Daroussin m = goto_map[symbol]; 65398e903e7SBaptiste Daroussin n = goto_map[symbol + 1]; 65498e903e7SBaptiste Daroussin 65598e903e7SBaptiste Daroussin count = 0; 65698e903e7SBaptiste Daroussin for (i = m; i < n; i++) 65798e903e7SBaptiste Daroussin { 65898e903e7SBaptiste Daroussin if (to_state[i] != default_state) 65998e903e7SBaptiste Daroussin ++count; 66098e903e7SBaptiste Daroussin } 66198e903e7SBaptiste Daroussin if (count == 0) 66298e903e7SBaptiste Daroussin return; 66398e903e7SBaptiste Daroussin 6640c8de5b0SBaptiste Daroussin symno = symbol_value[symbol] + PER_STATE * nstates; 66598e903e7SBaptiste Daroussin 66698e903e7SBaptiste Daroussin froms[symno] = sp1 = sp = NEW2(count, Value_t); 66798e903e7SBaptiste Daroussin tos[symno] = sp2 = NEW2(count, Value_t); 66898e903e7SBaptiste Daroussin 66998e903e7SBaptiste Daroussin for (i = m; i < n; i++) 67098e903e7SBaptiste Daroussin { 67198e903e7SBaptiste Daroussin if (to_state[i] != default_state) 67298e903e7SBaptiste Daroussin { 67398e903e7SBaptiste Daroussin *sp1++ = from_state[i]; 67498e903e7SBaptiste Daroussin *sp2++ = to_state[i]; 67598e903e7SBaptiste Daroussin } 67698e903e7SBaptiste Daroussin } 67798e903e7SBaptiste Daroussin 67898e903e7SBaptiste Daroussin tally[symno] = count; 67998e903e7SBaptiste Daroussin width[symno] = (Value_t)(sp1[-1] - sp[0] + 1); 68098e903e7SBaptiste Daroussin } 68198e903e7SBaptiste Daroussin 68298e903e7SBaptiste Daroussin static void 68398e903e7SBaptiste Daroussin goto_actions(void) 68498e903e7SBaptiste Daroussin { 68598e903e7SBaptiste Daroussin int i, j, k; 68698e903e7SBaptiste Daroussin 68798e903e7SBaptiste Daroussin state_count = NEW2(nstates, Value_t); 68898e903e7SBaptiste Daroussin 68998e903e7SBaptiste Daroussin k = default_goto(start_symbol + 1); 69098e903e7SBaptiste Daroussin start_int_table("dgoto", k); 69198e903e7SBaptiste Daroussin save_column(start_symbol + 1, k); 69298e903e7SBaptiste Daroussin 69398e903e7SBaptiste Daroussin j = 10; 69498e903e7SBaptiste Daroussin for (i = start_symbol + 2; i < nsyms; i++) 69598e903e7SBaptiste Daroussin { 69698e903e7SBaptiste Daroussin if (j >= 10) 69798e903e7SBaptiste Daroussin { 69898e903e7SBaptiste Daroussin output_newline(); 69998e903e7SBaptiste Daroussin j = 1; 70098e903e7SBaptiste Daroussin } 70198e903e7SBaptiste Daroussin else 70298e903e7SBaptiste Daroussin ++j; 70398e903e7SBaptiste Daroussin 70498e903e7SBaptiste Daroussin k = default_goto(i); 70598e903e7SBaptiste Daroussin output_int(k); 70698e903e7SBaptiste Daroussin save_column(i, k); 70798e903e7SBaptiste Daroussin } 70898e903e7SBaptiste Daroussin 70998e903e7SBaptiste Daroussin end_table(); 71098e903e7SBaptiste Daroussin FREE(state_count); 71198e903e7SBaptiste Daroussin } 71298e903e7SBaptiste Daroussin 71398e903e7SBaptiste Daroussin static void 71498e903e7SBaptiste Daroussin sort_actions(void) 71598e903e7SBaptiste Daroussin { 71698e903e7SBaptiste Daroussin Value_t i; 71798e903e7SBaptiste Daroussin int j; 71898e903e7SBaptiste Daroussin int k; 71998e903e7SBaptiste Daroussin int t; 72098e903e7SBaptiste Daroussin int w; 72198e903e7SBaptiste Daroussin 72298e903e7SBaptiste Daroussin order = NEW2(nvectors, Value_t); 72398e903e7SBaptiste Daroussin nentries = 0; 72498e903e7SBaptiste Daroussin 72598e903e7SBaptiste Daroussin for (i = 0; i < nvectors; i++) 72698e903e7SBaptiste Daroussin { 72798e903e7SBaptiste Daroussin if (tally[i] > 0) 72898e903e7SBaptiste Daroussin { 72998e903e7SBaptiste Daroussin t = tally[i]; 73098e903e7SBaptiste Daroussin w = width[i]; 73198e903e7SBaptiste Daroussin j = nentries - 1; 73298e903e7SBaptiste Daroussin 73398e903e7SBaptiste Daroussin while (j >= 0 && (width[order[j]] < w)) 73498e903e7SBaptiste Daroussin j--; 73598e903e7SBaptiste Daroussin 73698e903e7SBaptiste Daroussin while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t)) 73798e903e7SBaptiste Daroussin j--; 73898e903e7SBaptiste Daroussin 73998e903e7SBaptiste Daroussin for (k = nentries - 1; k > j; k--) 74098e903e7SBaptiste Daroussin order[k + 1] = order[k]; 74198e903e7SBaptiste Daroussin 74298e903e7SBaptiste Daroussin order[j + 1] = i; 74398e903e7SBaptiste Daroussin nentries++; 74498e903e7SBaptiste Daroussin } 74598e903e7SBaptiste Daroussin } 74698e903e7SBaptiste Daroussin } 74798e903e7SBaptiste Daroussin 74898e903e7SBaptiste Daroussin /* The function matching_vector determines if the vector specified by */ 74998e903e7SBaptiste Daroussin /* the input parameter matches a previously considered vector. The */ 75098e903e7SBaptiste Daroussin /* test at the start of the function checks if the vector represents */ 75198e903e7SBaptiste Daroussin /* a row of shifts over terminal symbols or a row of reductions, or a */ 75298e903e7SBaptiste Daroussin /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */ 75398e903e7SBaptiste Daroussin /* check if a column of shifts over a nonterminal symbols matches a */ 75498e903e7SBaptiste Daroussin /* previously considered vector. Because of the nature of LR parsing */ 75598e903e7SBaptiste Daroussin /* tables, no two columns can match. Therefore, the only possible */ 75698e903e7SBaptiste Daroussin /* match would be between a row and a column. Such matches are */ 75798e903e7SBaptiste Daroussin /* unlikely. Therefore, to save time, no attempt is made to see if a */ 75898e903e7SBaptiste Daroussin /* column matches a previously considered vector. */ 75998e903e7SBaptiste Daroussin /* */ 76098e903e7SBaptiste Daroussin /* Matching_vector is poorly designed. The test could easily be made */ 76198e903e7SBaptiste Daroussin /* faster. Also, it depends on the vectors being in a specific */ 76298e903e7SBaptiste Daroussin /* order. */ 7630c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 7640c8de5b0SBaptiste Daroussin /* */ 7650c8de5b0SBaptiste Daroussin /* Not really any point in checking for matching conflicts -- it is */ 7660c8de5b0SBaptiste Daroussin /* extremely unlikely to occur, and conflicts are (hopefully) rare. */ 7670c8de5b0SBaptiste Daroussin #endif 76898e903e7SBaptiste Daroussin 76998e903e7SBaptiste Daroussin static int 77098e903e7SBaptiste Daroussin matching_vector(int vector) 77198e903e7SBaptiste Daroussin { 77298e903e7SBaptiste Daroussin int i; 77398e903e7SBaptiste Daroussin int j; 77498e903e7SBaptiste Daroussin int k; 77598e903e7SBaptiste Daroussin int t; 77698e903e7SBaptiste Daroussin int w; 77798e903e7SBaptiste Daroussin int match; 77898e903e7SBaptiste Daroussin int prev; 77998e903e7SBaptiste Daroussin 78098e903e7SBaptiste Daroussin i = order[vector]; 78198e903e7SBaptiste Daroussin if (i >= 2 * nstates) 78298e903e7SBaptiste Daroussin return (-1); 78398e903e7SBaptiste Daroussin 78498e903e7SBaptiste Daroussin t = tally[i]; 78598e903e7SBaptiste Daroussin w = width[i]; 78698e903e7SBaptiste Daroussin 78798e903e7SBaptiste Daroussin for (prev = vector - 1; prev >= 0; prev--) 78898e903e7SBaptiste Daroussin { 78998e903e7SBaptiste Daroussin j = order[prev]; 79098e903e7SBaptiste Daroussin if (width[j] != w || tally[j] != t) 79198e903e7SBaptiste Daroussin return (-1); 79298e903e7SBaptiste Daroussin 79398e903e7SBaptiste Daroussin match = 1; 79498e903e7SBaptiste Daroussin for (k = 0; match && k < t; k++) 79598e903e7SBaptiste Daroussin { 79698e903e7SBaptiste Daroussin if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k]) 79798e903e7SBaptiste Daroussin match = 0; 79898e903e7SBaptiste Daroussin } 79998e903e7SBaptiste Daroussin 80098e903e7SBaptiste Daroussin if (match) 80198e903e7SBaptiste Daroussin return (j); 80298e903e7SBaptiste Daroussin } 80398e903e7SBaptiste Daroussin 80498e903e7SBaptiste Daroussin return (-1); 80598e903e7SBaptiste Daroussin } 80698e903e7SBaptiste Daroussin 80798e903e7SBaptiste Daroussin static int 80898e903e7SBaptiste Daroussin pack_vector(int vector) 80998e903e7SBaptiste Daroussin { 81098e903e7SBaptiste Daroussin int i, j, k, l; 81198e903e7SBaptiste Daroussin int t; 81298e903e7SBaptiste Daroussin int loc; 81398e903e7SBaptiste Daroussin int ok; 81498e903e7SBaptiste Daroussin Value_t *from; 81598e903e7SBaptiste Daroussin Value_t *to; 81698e903e7SBaptiste Daroussin int newmax; 81798e903e7SBaptiste Daroussin 81898e903e7SBaptiste Daroussin i = order[vector]; 81998e903e7SBaptiste Daroussin t = tally[i]; 82098e903e7SBaptiste Daroussin assert(t); 82198e903e7SBaptiste Daroussin 82298e903e7SBaptiste Daroussin from = froms[i]; 82398e903e7SBaptiste Daroussin to = tos[i]; 82498e903e7SBaptiste Daroussin 82598e903e7SBaptiste Daroussin j = lowzero - from[0]; 82698e903e7SBaptiste Daroussin for (k = 1; k < t; ++k) 82798e903e7SBaptiste Daroussin if (lowzero - from[k] > j) 82898e903e7SBaptiste Daroussin j = lowzero - from[k]; 82998e903e7SBaptiste Daroussin for (;; ++j) 83098e903e7SBaptiste Daroussin { 83198e903e7SBaptiste Daroussin if (j == 0) 83298e903e7SBaptiste Daroussin continue; 83398e903e7SBaptiste Daroussin ok = 1; 83498e903e7SBaptiste Daroussin for (k = 0; ok && k < t; k++) 83598e903e7SBaptiste Daroussin { 83698e903e7SBaptiste Daroussin loc = j + from[k]; 83798e903e7SBaptiste Daroussin if (loc >= maxtable - 1) 83898e903e7SBaptiste Daroussin { 83998e903e7SBaptiste Daroussin if (loc >= MAXTABLE - 1) 84098e903e7SBaptiste Daroussin fatal("maximum table size exceeded"); 84198e903e7SBaptiste Daroussin 84298e903e7SBaptiste Daroussin newmax = maxtable; 84398e903e7SBaptiste Daroussin do 84498e903e7SBaptiste Daroussin { 84598e903e7SBaptiste Daroussin newmax += 200; 84698e903e7SBaptiste Daroussin } 84798e903e7SBaptiste Daroussin while (newmax <= loc); 84898e903e7SBaptiste Daroussin 8493e066022SBaptiste Daroussin table = TREALLOC(Value_t, table, newmax); 85098e903e7SBaptiste Daroussin NO_SPACE(table); 85198e903e7SBaptiste Daroussin 8523e066022SBaptiste Daroussin check = TREALLOC(Value_t, check, newmax); 85398e903e7SBaptiste Daroussin NO_SPACE(check); 85498e903e7SBaptiste Daroussin 85598e903e7SBaptiste Daroussin for (l = maxtable; l < newmax; ++l) 85698e903e7SBaptiste Daroussin { 85798e903e7SBaptiste Daroussin table[l] = 0; 85898e903e7SBaptiste Daroussin check[l] = -1; 85998e903e7SBaptiste Daroussin } 86098e903e7SBaptiste Daroussin maxtable = newmax; 86198e903e7SBaptiste Daroussin } 86298e903e7SBaptiste Daroussin 86398e903e7SBaptiste Daroussin if (check[loc] != -1) 86498e903e7SBaptiste Daroussin ok = 0; 86598e903e7SBaptiste Daroussin } 86698e903e7SBaptiste Daroussin for (k = 0; ok && k < vector; k++) 86798e903e7SBaptiste Daroussin { 86898e903e7SBaptiste Daroussin if (pos[k] == j) 86998e903e7SBaptiste Daroussin ok = 0; 87098e903e7SBaptiste Daroussin } 87198e903e7SBaptiste Daroussin if (ok) 87298e903e7SBaptiste Daroussin { 87398e903e7SBaptiste Daroussin for (k = 0; k < t; k++) 87498e903e7SBaptiste Daroussin { 87598e903e7SBaptiste Daroussin loc = j + from[k]; 87698e903e7SBaptiste Daroussin table[loc] = to[k]; 87798e903e7SBaptiste Daroussin check[loc] = from[k]; 87898e903e7SBaptiste Daroussin if (loc > high) 87998e903e7SBaptiste Daroussin high = loc; 88098e903e7SBaptiste Daroussin } 88198e903e7SBaptiste Daroussin 88298e903e7SBaptiste Daroussin while (check[lowzero] != -1) 88398e903e7SBaptiste Daroussin ++lowzero; 88498e903e7SBaptiste Daroussin 88598e903e7SBaptiste Daroussin return (j); 88698e903e7SBaptiste Daroussin } 88798e903e7SBaptiste Daroussin } 88898e903e7SBaptiste Daroussin } 88998e903e7SBaptiste Daroussin 89098e903e7SBaptiste Daroussin static void 89198e903e7SBaptiste Daroussin pack_table(void) 89298e903e7SBaptiste Daroussin { 89398e903e7SBaptiste Daroussin int i; 89498e903e7SBaptiste Daroussin Value_t place; 89598e903e7SBaptiste Daroussin int state; 89698e903e7SBaptiste Daroussin 89798e903e7SBaptiste Daroussin base = NEW2(nvectors, Value_t); 89898e903e7SBaptiste Daroussin pos = NEW2(nentries, Value_t); 89998e903e7SBaptiste Daroussin 90098e903e7SBaptiste Daroussin maxtable = 1000; 90198e903e7SBaptiste Daroussin table = NEW2(maxtable, Value_t); 90298e903e7SBaptiste Daroussin check = NEW2(maxtable, Value_t); 90398e903e7SBaptiste Daroussin 90498e903e7SBaptiste Daroussin lowzero = 0; 90598e903e7SBaptiste Daroussin high = 0; 90698e903e7SBaptiste Daroussin 90798e903e7SBaptiste Daroussin for (i = 0; i < maxtable; i++) 90898e903e7SBaptiste Daroussin check[i] = -1; 90998e903e7SBaptiste Daroussin 91098e903e7SBaptiste Daroussin for (i = 0; i < nentries; i++) 91198e903e7SBaptiste Daroussin { 91298e903e7SBaptiste Daroussin state = matching_vector(i); 91398e903e7SBaptiste Daroussin 91498e903e7SBaptiste Daroussin if (state < 0) 91598e903e7SBaptiste Daroussin place = (Value_t)pack_vector(i); 91698e903e7SBaptiste Daroussin else 91798e903e7SBaptiste Daroussin place = base[state]; 91898e903e7SBaptiste Daroussin 91998e903e7SBaptiste Daroussin pos[i] = place; 92098e903e7SBaptiste Daroussin base[order[i]] = place; 92198e903e7SBaptiste Daroussin } 92298e903e7SBaptiste Daroussin 92398e903e7SBaptiste Daroussin for (i = 0; i < nvectors; i++) 92498e903e7SBaptiste Daroussin { 92598e903e7SBaptiste Daroussin if (froms[i]) 92698e903e7SBaptiste Daroussin FREE(froms[i]); 92798e903e7SBaptiste Daroussin if (tos[i]) 92898e903e7SBaptiste Daroussin FREE(tos[i]); 92998e903e7SBaptiste Daroussin } 93098e903e7SBaptiste Daroussin 9310c8de5b0SBaptiste Daroussin DO_FREE(froms); 9320c8de5b0SBaptiste Daroussin DO_FREE(tos); 9330c8de5b0SBaptiste Daroussin DO_FREE(tally); 9340c8de5b0SBaptiste Daroussin DO_FREE(width); 9350c8de5b0SBaptiste Daroussin DO_FREE(pos); 93698e903e7SBaptiste Daroussin } 93798e903e7SBaptiste Daroussin 93898e903e7SBaptiste Daroussin static void 93998e903e7SBaptiste Daroussin output_base(void) 94098e903e7SBaptiste Daroussin { 94198e903e7SBaptiste Daroussin int i, j; 94298e903e7SBaptiste Daroussin 94398e903e7SBaptiste Daroussin start_int_table("sindex", base[0]); 94498e903e7SBaptiste Daroussin 94598e903e7SBaptiste Daroussin j = 10; 94698e903e7SBaptiste Daroussin for (i = 1; i < nstates; i++) 94798e903e7SBaptiste Daroussin { 94898e903e7SBaptiste Daroussin if (j >= 10) 94998e903e7SBaptiste Daroussin { 95098e903e7SBaptiste Daroussin output_newline(); 95198e903e7SBaptiste Daroussin j = 1; 95298e903e7SBaptiste Daroussin } 95398e903e7SBaptiste Daroussin else 95498e903e7SBaptiste Daroussin ++j; 95598e903e7SBaptiste Daroussin 95698e903e7SBaptiste Daroussin output_int(base[i]); 95798e903e7SBaptiste Daroussin } 95898e903e7SBaptiste Daroussin 95998e903e7SBaptiste Daroussin end_table(); 96098e903e7SBaptiste Daroussin 96198e903e7SBaptiste Daroussin start_int_table("rindex", base[nstates]); 96298e903e7SBaptiste Daroussin 96398e903e7SBaptiste Daroussin j = 10; 96498e903e7SBaptiste Daroussin for (i = nstates + 1; i < 2 * nstates; i++) 96598e903e7SBaptiste Daroussin { 96698e903e7SBaptiste Daroussin if (j >= 10) 96798e903e7SBaptiste Daroussin { 96898e903e7SBaptiste Daroussin output_newline(); 96998e903e7SBaptiste Daroussin j = 1; 97098e903e7SBaptiste Daroussin } 97198e903e7SBaptiste Daroussin else 97298e903e7SBaptiste Daroussin ++j; 97398e903e7SBaptiste Daroussin 97498e903e7SBaptiste Daroussin output_int(base[i]); 97598e903e7SBaptiste Daroussin } 97698e903e7SBaptiste Daroussin 97798e903e7SBaptiste Daroussin end_table(); 97898e903e7SBaptiste Daroussin 9790c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 9800c8de5b0SBaptiste Daroussin output_line("#if YYBTYACC"); 9810c8de5b0SBaptiste Daroussin start_int_table("cindex", base[2 * nstates]); 98298e903e7SBaptiste Daroussin 98398e903e7SBaptiste Daroussin j = 10; 9840c8de5b0SBaptiste Daroussin for (i = 2 * nstates + 1; i < 3 * nstates; i++) 9850c8de5b0SBaptiste Daroussin { 9860c8de5b0SBaptiste Daroussin if (j >= 10) 9870c8de5b0SBaptiste Daroussin { 9880c8de5b0SBaptiste Daroussin output_newline(); 9890c8de5b0SBaptiste Daroussin j = 1; 9900c8de5b0SBaptiste Daroussin } 9910c8de5b0SBaptiste Daroussin else 9920c8de5b0SBaptiste Daroussin ++j; 9930c8de5b0SBaptiste Daroussin 9940c8de5b0SBaptiste Daroussin output_int(base[i]); 9950c8de5b0SBaptiste Daroussin } 9960c8de5b0SBaptiste Daroussin 9970c8de5b0SBaptiste Daroussin end_table(); 9980c8de5b0SBaptiste Daroussin output_line("#endif"); 9990c8de5b0SBaptiste Daroussin #endif 10000c8de5b0SBaptiste Daroussin 10010c8de5b0SBaptiste Daroussin start_int_table("gindex", base[PER_STATE * nstates]); 10020c8de5b0SBaptiste Daroussin 10030c8de5b0SBaptiste Daroussin j = 10; 10040c8de5b0SBaptiste Daroussin for (i = PER_STATE * nstates + 1; i < nvectors - 1; i++) 100598e903e7SBaptiste Daroussin { 100698e903e7SBaptiste Daroussin if (j >= 10) 100798e903e7SBaptiste Daroussin { 100898e903e7SBaptiste Daroussin output_newline(); 100998e903e7SBaptiste Daroussin j = 1; 101098e903e7SBaptiste Daroussin } 101198e903e7SBaptiste Daroussin else 101298e903e7SBaptiste Daroussin ++j; 101398e903e7SBaptiste Daroussin 101498e903e7SBaptiste Daroussin output_int(base[i]); 101598e903e7SBaptiste Daroussin } 101698e903e7SBaptiste Daroussin 101798e903e7SBaptiste Daroussin end_table(); 101898e903e7SBaptiste Daroussin FREE(base); 101998e903e7SBaptiste Daroussin } 102098e903e7SBaptiste Daroussin 102198e903e7SBaptiste Daroussin static void 102298e903e7SBaptiste Daroussin output_table(void) 102398e903e7SBaptiste Daroussin { 102498e903e7SBaptiste Daroussin int i; 102598e903e7SBaptiste Daroussin int j; 102698e903e7SBaptiste Daroussin 10270c8de5b0SBaptiste Daroussin if (high >= MAXYYINT) 10280c8de5b0SBaptiste Daroussin { 10290c8de5b0SBaptiste Daroussin fprintf(stderr, "YYTABLESIZE: %ld\n", high); 10300c8de5b0SBaptiste Daroussin fprintf(stderr, "Table is longer than %d elements.\n", MAXYYINT); 10310c8de5b0SBaptiste Daroussin done(1); 10320c8de5b0SBaptiste Daroussin } 10330c8de5b0SBaptiste Daroussin 103498e903e7SBaptiste Daroussin ++outline; 10350c8de5b0SBaptiste Daroussin fprintf(code_file, "#define YYTABLESIZE %ld\n", high); 103698e903e7SBaptiste Daroussin start_int_table("table", table[0]); 103798e903e7SBaptiste Daroussin 103898e903e7SBaptiste Daroussin j = 10; 103998e903e7SBaptiste Daroussin for (i = 1; i <= high; i++) 104098e903e7SBaptiste Daroussin { 104198e903e7SBaptiste Daroussin if (j >= 10) 104298e903e7SBaptiste Daroussin { 104398e903e7SBaptiste Daroussin output_newline(); 104498e903e7SBaptiste Daroussin j = 1; 104598e903e7SBaptiste Daroussin } 104698e903e7SBaptiste Daroussin else 104798e903e7SBaptiste Daroussin ++j; 104898e903e7SBaptiste Daroussin 104998e903e7SBaptiste Daroussin output_int(table[i]); 105098e903e7SBaptiste Daroussin } 105198e903e7SBaptiste Daroussin 105298e903e7SBaptiste Daroussin end_table(); 105398e903e7SBaptiste Daroussin FREE(table); 105498e903e7SBaptiste Daroussin } 105598e903e7SBaptiste Daroussin 105698e903e7SBaptiste Daroussin static void 105798e903e7SBaptiste Daroussin output_check(void) 105898e903e7SBaptiste Daroussin { 105998e903e7SBaptiste Daroussin int i; 106098e903e7SBaptiste Daroussin int j; 106198e903e7SBaptiste Daroussin 106298e903e7SBaptiste Daroussin start_int_table("check", check[0]); 106398e903e7SBaptiste Daroussin 106498e903e7SBaptiste Daroussin j = 10; 106598e903e7SBaptiste Daroussin for (i = 1; i <= high; i++) 106698e903e7SBaptiste Daroussin { 106798e903e7SBaptiste Daroussin if (j >= 10) 106898e903e7SBaptiste Daroussin { 106998e903e7SBaptiste Daroussin output_newline(); 107098e903e7SBaptiste Daroussin j = 1; 107198e903e7SBaptiste Daroussin } 107298e903e7SBaptiste Daroussin else 107398e903e7SBaptiste Daroussin ++j; 107498e903e7SBaptiste Daroussin 107598e903e7SBaptiste Daroussin output_int(check[i]); 107698e903e7SBaptiste Daroussin } 107798e903e7SBaptiste Daroussin 107898e903e7SBaptiste Daroussin end_table(); 107998e903e7SBaptiste Daroussin FREE(check); 108098e903e7SBaptiste Daroussin } 108198e903e7SBaptiste Daroussin 10820c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 10830c8de5b0SBaptiste Daroussin static void 10840c8de5b0SBaptiste Daroussin output_ctable(void) 10850c8de5b0SBaptiste Daroussin { 10860c8de5b0SBaptiste Daroussin int i; 10870c8de5b0SBaptiste Daroussin int j; 108811fce282SBaptiste Daroussin int limit = (conflicts != 0) ? nconflicts : 0; 10890c8de5b0SBaptiste Daroussin 109011fce282SBaptiste Daroussin if (limit < high) 109111fce282SBaptiste Daroussin limit = (int)high; 109211fce282SBaptiste Daroussin 10930c8de5b0SBaptiste Daroussin output_line("#if YYBTYACC"); 109411fce282SBaptiste Daroussin start_int_table("ctable", conflicts ? conflicts[0] : -1); 10950c8de5b0SBaptiste Daroussin 10960c8de5b0SBaptiste Daroussin j = 10; 109711fce282SBaptiste Daroussin for (i = 1; i < limit; i++) 10980c8de5b0SBaptiste Daroussin { 10990c8de5b0SBaptiste Daroussin if (j >= 10) 11000c8de5b0SBaptiste Daroussin { 11010c8de5b0SBaptiste Daroussin output_newline(); 11020c8de5b0SBaptiste Daroussin j = 1; 11030c8de5b0SBaptiste Daroussin } 11040c8de5b0SBaptiste Daroussin else 11050c8de5b0SBaptiste Daroussin ++j; 11060c8de5b0SBaptiste Daroussin 110711fce282SBaptiste Daroussin output_int((conflicts != 0 && i < nconflicts) ? conflicts[i] : -1); 11080c8de5b0SBaptiste Daroussin } 11090c8de5b0SBaptiste Daroussin 111011fce282SBaptiste Daroussin if (conflicts) 111111fce282SBaptiste Daroussin FREE(conflicts); 111211fce282SBaptiste Daroussin 11130c8de5b0SBaptiste Daroussin end_table(); 11140c8de5b0SBaptiste Daroussin output_line("#endif"); 11150c8de5b0SBaptiste Daroussin } 11160c8de5b0SBaptiste Daroussin #endif 11170c8de5b0SBaptiste Daroussin 111898e903e7SBaptiste Daroussin static void 111998e903e7SBaptiste Daroussin output_actions(void) 112098e903e7SBaptiste Daroussin { 11210c8de5b0SBaptiste Daroussin nvectors = PER_STATE * nstates + nvars; 112298e903e7SBaptiste Daroussin 112398e903e7SBaptiste Daroussin froms = NEW2(nvectors, Value_t *); 112498e903e7SBaptiste Daroussin tos = NEW2(nvectors, Value_t *); 112598e903e7SBaptiste Daroussin tally = NEW2(nvectors, Value_t); 112698e903e7SBaptiste Daroussin width = NEW2(nvectors, Value_t); 112798e903e7SBaptiste Daroussin 11280c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 11290c8de5b0SBaptiste Daroussin if (backtrack && (SRtotal + RRtotal) != 0) 11300c8de5b0SBaptiste Daroussin conflicts = NEW2(4 * (SRtotal + RRtotal), Value_t); 11310c8de5b0SBaptiste Daroussin #endif 11320c8de5b0SBaptiste Daroussin 113398e903e7SBaptiste Daroussin token_actions(); 113498e903e7SBaptiste Daroussin FREE(lookaheads); 113598e903e7SBaptiste Daroussin FREE(LA); 113698e903e7SBaptiste Daroussin FREE(LAruleno); 113798e903e7SBaptiste Daroussin FREE(accessing_symbol); 113898e903e7SBaptiste Daroussin 113998e903e7SBaptiste Daroussin goto_actions(); 114011fce282SBaptiste Daroussin FREE(goto_base); 114198e903e7SBaptiste Daroussin FREE(from_state); 114298e903e7SBaptiste Daroussin FREE(to_state); 114398e903e7SBaptiste Daroussin 114498e903e7SBaptiste Daroussin sort_actions(); 114598e903e7SBaptiste Daroussin pack_table(); 114698e903e7SBaptiste Daroussin output_base(); 114798e903e7SBaptiste Daroussin output_table(); 114898e903e7SBaptiste Daroussin output_check(); 11490c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 11500c8de5b0SBaptiste Daroussin output_ctable(); 11510c8de5b0SBaptiste Daroussin #endif 115298e903e7SBaptiste Daroussin } 115398e903e7SBaptiste Daroussin 115498e903e7SBaptiste Daroussin static int 115598e903e7SBaptiste Daroussin is_C_identifier(char *name) 115698e903e7SBaptiste Daroussin { 115798e903e7SBaptiste Daroussin char *s; 115898e903e7SBaptiste Daroussin int c; 115998e903e7SBaptiste Daroussin 116098e903e7SBaptiste Daroussin s = name; 116198e903e7SBaptiste Daroussin c = *s; 116298e903e7SBaptiste Daroussin if (c == '"') 116398e903e7SBaptiste Daroussin { 116498e903e7SBaptiste Daroussin c = *++s; 116598e903e7SBaptiste Daroussin if (!isalpha(c) && c != '_' && c != '$') 116698e903e7SBaptiste Daroussin return (0); 116798e903e7SBaptiste Daroussin while ((c = *++s) != '"') 116898e903e7SBaptiste Daroussin { 116998e903e7SBaptiste Daroussin if (!isalnum(c) && c != '_' && c != '$') 117098e903e7SBaptiste Daroussin return (0); 117198e903e7SBaptiste Daroussin } 117298e903e7SBaptiste Daroussin return (1); 117398e903e7SBaptiste Daroussin } 117498e903e7SBaptiste Daroussin 117598e903e7SBaptiste Daroussin if (!isalpha(c) && c != '_' && c != '$') 117698e903e7SBaptiste Daroussin return (0); 117798e903e7SBaptiste Daroussin while ((c = *++s) != 0) 117898e903e7SBaptiste Daroussin { 117998e903e7SBaptiste Daroussin if (!isalnum(c) && c != '_' && c != '$') 118098e903e7SBaptiste Daroussin return (0); 118198e903e7SBaptiste Daroussin } 118298e903e7SBaptiste Daroussin return (1); 118398e903e7SBaptiste Daroussin } 118498e903e7SBaptiste Daroussin 11850c8de5b0SBaptiste Daroussin #if USE_HEADER_GUARDS 11860c8de5b0SBaptiste Daroussin static void 11870c8de5b0SBaptiste Daroussin start_defines_file(void) 11880c8de5b0SBaptiste Daroussin { 11890c8de5b0SBaptiste Daroussin fprintf(defines_file, "#ifndef _%s_defines_h_\n", symbol_prefix); 11900c8de5b0SBaptiste Daroussin fprintf(defines_file, "#define _%s_defines_h_\n\n", symbol_prefix); 11910c8de5b0SBaptiste Daroussin } 11920c8de5b0SBaptiste Daroussin 11930c8de5b0SBaptiste Daroussin static void 11940c8de5b0SBaptiste Daroussin end_defines_file(void) 11950c8de5b0SBaptiste Daroussin { 11960c8de5b0SBaptiste Daroussin fprintf(defines_file, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix); 11970c8de5b0SBaptiste Daroussin } 11980c8de5b0SBaptiste Daroussin #else 11990c8de5b0SBaptiste Daroussin #define start_defines_file() /* nothing */ 12000c8de5b0SBaptiste Daroussin #define end_defines_file() /* nothing */ 12010c8de5b0SBaptiste Daroussin #endif 12020c8de5b0SBaptiste Daroussin 120398e903e7SBaptiste Daroussin static void 120498e903e7SBaptiste Daroussin output_defines(FILE * fp) 120598e903e7SBaptiste Daroussin { 120698e903e7SBaptiste Daroussin int c, i; 120798e903e7SBaptiste Daroussin char *s; 120898e903e7SBaptiste Daroussin 120998e903e7SBaptiste Daroussin for (i = 2; i < ntokens; ++i) 121098e903e7SBaptiste Daroussin { 121198e903e7SBaptiste Daroussin s = symbol_name[i]; 121298e903e7SBaptiste Daroussin if (is_C_identifier(s) && (!sflag || *s != '"')) 121398e903e7SBaptiste Daroussin { 121498e903e7SBaptiste Daroussin fprintf(fp, "#define "); 121598e903e7SBaptiste Daroussin c = *s; 121698e903e7SBaptiste Daroussin if (c == '"') 121798e903e7SBaptiste Daroussin { 121898e903e7SBaptiste Daroussin while ((c = *++s) != '"') 121998e903e7SBaptiste Daroussin { 122098e903e7SBaptiste Daroussin putc(c, fp); 122198e903e7SBaptiste Daroussin } 122298e903e7SBaptiste Daroussin } 122398e903e7SBaptiste Daroussin else 122498e903e7SBaptiste Daroussin { 122598e903e7SBaptiste Daroussin do 122698e903e7SBaptiste Daroussin { 122798e903e7SBaptiste Daroussin putc(c, fp); 122898e903e7SBaptiste Daroussin } 122998e903e7SBaptiste Daroussin while ((c = *++s) != 0); 123098e903e7SBaptiste Daroussin } 123198e903e7SBaptiste Daroussin if (fp == code_file) 123298e903e7SBaptiste Daroussin ++outline; 123398e903e7SBaptiste Daroussin fprintf(fp, " %d\n", symbol_value[i]); 123498e903e7SBaptiste Daroussin } 123598e903e7SBaptiste Daroussin } 123698e903e7SBaptiste Daroussin 123798e903e7SBaptiste Daroussin if (fp == code_file) 123898e903e7SBaptiste Daroussin ++outline; 123998e903e7SBaptiste Daroussin if (fp != defines_file || iflag) 124098e903e7SBaptiste Daroussin fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]); 124198e903e7SBaptiste Daroussin 1242c5b5d71aSJung-uk Kim if (token_table && rflag && fp != externs_file) 1243c5b5d71aSJung-uk Kim { 1244c5b5d71aSJung-uk Kim if (fp == code_file) 1245c5b5d71aSJung-uk Kim ++outline; 1246c5b5d71aSJung-uk Kim fputs("#undef yytname\n", fp); 1247c5b5d71aSJung-uk Kim if (fp == code_file) 1248c5b5d71aSJung-uk Kim ++outline; 1249c5b5d71aSJung-uk Kim fputs("#define yytname yyname\n", fp); 1250c5b5d71aSJung-uk Kim } 1251c5b5d71aSJung-uk Kim 125298e903e7SBaptiste Daroussin if (fp == defines_file || (iflag && !dflag)) 125398e903e7SBaptiste Daroussin { 125498e903e7SBaptiste Daroussin if (unionized) 125598e903e7SBaptiste Daroussin { 12565b91e83fSBaptiste Daroussin if (union_file != 0) 12575b91e83fSBaptiste Daroussin { 125898e903e7SBaptiste Daroussin rewind(union_file); 125998e903e7SBaptiste Daroussin while ((c = getc(union_file)) != EOF) 12600c8de5b0SBaptiste Daroussin putc_code(fp, c); 12615b91e83fSBaptiste Daroussin } 126298e903e7SBaptiste Daroussin fprintf(fp, "extern YYSTYPE %slval;\n", symbol_prefix); 126398e903e7SBaptiste Daroussin } 12642aca18c7SJung-uk Kim #if defined(YYBTYACC) 12652aca18c7SJung-uk Kim if (locations) 12662aca18c7SJung-uk Kim output_ltype(fp); 12672aca18c7SJung-uk Kim #endif 126898e903e7SBaptiste Daroussin } 126998e903e7SBaptiste Daroussin } 127098e903e7SBaptiste Daroussin 127198e903e7SBaptiste Daroussin static void 127298e903e7SBaptiste Daroussin output_stored_text(FILE * fp) 127398e903e7SBaptiste Daroussin { 127498e903e7SBaptiste Daroussin int c; 127598e903e7SBaptiste Daroussin FILE *in; 127698e903e7SBaptiste Daroussin 127798e903e7SBaptiste Daroussin rewind(text_file); 127898e903e7SBaptiste Daroussin if (text_file == NULL) 127998e903e7SBaptiste Daroussin open_error("text_file"); 128098e903e7SBaptiste Daroussin in = text_file; 128198e903e7SBaptiste Daroussin if ((c = getc(in)) == EOF) 128298e903e7SBaptiste Daroussin return; 128398e903e7SBaptiste Daroussin putc_code(fp, c); 128498e903e7SBaptiste Daroussin while ((c = getc(in)) != EOF) 128598e903e7SBaptiste Daroussin { 128698e903e7SBaptiste Daroussin putc_code(fp, c); 128798e903e7SBaptiste Daroussin } 128898e903e7SBaptiste Daroussin write_code_lineno(fp); 128998e903e7SBaptiste Daroussin } 129098e903e7SBaptiste Daroussin 129198e903e7SBaptiste Daroussin static void 129298e903e7SBaptiste Daroussin output_debug(void) 129398e903e7SBaptiste Daroussin { 12940c8de5b0SBaptiste Daroussin int i, j, k, max, maxtok; 129598e903e7SBaptiste Daroussin const char **symnam; 129698e903e7SBaptiste Daroussin const char *s; 129798e903e7SBaptiste Daroussin 129898e903e7SBaptiste Daroussin ++outline; 129998e903e7SBaptiste Daroussin fprintf(code_file, "#define YYFINAL %d\n", final_state); 130098e903e7SBaptiste Daroussin 130198e903e7SBaptiste Daroussin putl_code(code_file, "#ifndef YYDEBUG\n"); 130298e903e7SBaptiste Daroussin ++outline; 130398e903e7SBaptiste Daroussin fprintf(code_file, "#define YYDEBUG %d\n", tflag); 130498e903e7SBaptiste Daroussin putl_code(code_file, "#endif\n"); 130598e903e7SBaptiste Daroussin 130698e903e7SBaptiste Daroussin if (rflag) 130798e903e7SBaptiste Daroussin { 130898e903e7SBaptiste Daroussin fprintf(output_file, "#ifndef YYDEBUG\n"); 130998e903e7SBaptiste Daroussin fprintf(output_file, "#define YYDEBUG %d\n", tflag); 131098e903e7SBaptiste Daroussin fprintf(output_file, "#endif\n"); 131198e903e7SBaptiste Daroussin } 131298e903e7SBaptiste Daroussin 13130c8de5b0SBaptiste Daroussin maxtok = 0; 13140c8de5b0SBaptiste Daroussin for (i = 0; i < ntokens; ++i) 13150c8de5b0SBaptiste Daroussin if (symbol_value[i] > maxtok) 13160c8de5b0SBaptiste Daroussin maxtok = symbol_value[i]; 13170c8de5b0SBaptiste Daroussin 13180c8de5b0SBaptiste Daroussin /* symbol_value[$accept] = -1 */ 13190c8de5b0SBaptiste Daroussin /* symbol_value[<goal>] = 0 */ 13200c8de5b0SBaptiste Daroussin /* remaining non-terminals start at 1 */ 13210c8de5b0SBaptiste Daroussin max = maxtok; 13220c8de5b0SBaptiste Daroussin for (i = ntokens; i < nsyms; ++i) 13230c8de5b0SBaptiste Daroussin if (((maxtok + 1) + (symbol_value[i] + 1)) > max) 13240c8de5b0SBaptiste Daroussin max = (maxtok + 1) + (symbol_value[i] + 1); 132598e903e7SBaptiste Daroussin 132698e903e7SBaptiste Daroussin ++outline; 13270c8de5b0SBaptiste Daroussin fprintf(code_file, "#define YYMAXTOKEN %d\n", maxtok); 13280c8de5b0SBaptiste Daroussin 13290c8de5b0SBaptiste Daroussin ++outline; 13300c8de5b0SBaptiste Daroussin fprintf(code_file, "#define YYUNDFTOKEN %d\n", max + 1); 13310c8de5b0SBaptiste Daroussin 13320c8de5b0SBaptiste Daroussin ++outline; 13334b4a8fcaSBaptiste Daroussin fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? " 13340c8de5b0SBaptiste Daroussin "YYUNDFTOKEN : (a))\n"); 133598e903e7SBaptiste Daroussin 13364b4a8fcaSBaptiste Daroussin symnam = TMALLOC(const char *, max + 2); 133798e903e7SBaptiste Daroussin NO_SPACE(symnam); 133898e903e7SBaptiste Daroussin 133998e903e7SBaptiste Daroussin /* Note that it is not necessary to initialize the element */ 134098e903e7SBaptiste Daroussin /* symnam[max]. */ 13410c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 13420c8de5b0SBaptiste Daroussin for (i = 0; i < max; ++i) 13430c8de5b0SBaptiste Daroussin symnam[i] = 0; 13440c8de5b0SBaptiste Daroussin for (i = nsyms - 1; i >= 0; --i) 13450c8de5b0SBaptiste Daroussin symnam[symbol_pval[i]] = symbol_name[i]; 13460c8de5b0SBaptiste Daroussin symnam[max + 1] = "illegal-symbol"; 13470c8de5b0SBaptiste Daroussin #else 13484b4a8fcaSBaptiste Daroussin for (i = 0; i <= max; ++i) 134998e903e7SBaptiste Daroussin symnam[i] = 0; 135098e903e7SBaptiste Daroussin for (i = ntokens - 1; i >= 2; --i) 135198e903e7SBaptiste Daroussin symnam[symbol_value[i]] = symbol_name[i]; 135298e903e7SBaptiste Daroussin symnam[0] = "end-of-file"; 13534b4a8fcaSBaptiste Daroussin symnam[max + 1] = "illegal-symbol"; 13540c8de5b0SBaptiste Daroussin #endif 135598e903e7SBaptiste Daroussin 13564b4a8fcaSBaptiste Daroussin /* 13574b4a8fcaSBaptiste Daroussin * bison's yytname[] array is roughly the same as byacc's yyname[] array. 13580c8de5b0SBaptiste Daroussin * The difference is that byacc does not predefine "$undefined". 13594b4a8fcaSBaptiste Daroussin * 13604b4a8fcaSBaptiste Daroussin * If the grammar declares "%token-table", define symbol "yytname" so 13614b4a8fcaSBaptiste Daroussin * an application such as ntpd can build. 13624b4a8fcaSBaptiste Daroussin */ 13634b4a8fcaSBaptiste Daroussin if (token_table) 13644b4a8fcaSBaptiste Daroussin { 1365c5b5d71aSJung-uk Kim if (!rflag) 1366c5b5d71aSJung-uk Kim { 13674b4a8fcaSBaptiste Daroussin output_line("#undef yytname"); 13684b4a8fcaSBaptiste Daroussin output_line("#define yytname yyname"); 13694b4a8fcaSBaptiste Daroussin } 1370c5b5d71aSJung-uk Kim } 13714b4a8fcaSBaptiste Daroussin else 13724b4a8fcaSBaptiste Daroussin { 137398e903e7SBaptiste Daroussin output_line("#if YYDEBUG"); 13744b4a8fcaSBaptiste Daroussin } 137598e903e7SBaptiste Daroussin 137698e903e7SBaptiste Daroussin start_str_table("name"); 137798e903e7SBaptiste Daroussin j = 80; 13784b4a8fcaSBaptiste Daroussin for (i = 0; i <= max + 1; ++i) 137998e903e7SBaptiste Daroussin { 138098e903e7SBaptiste Daroussin if ((s = symnam[i]) != 0) 138198e903e7SBaptiste Daroussin { 138298e903e7SBaptiste Daroussin if (s[0] == '"') 138398e903e7SBaptiste Daroussin { 138498e903e7SBaptiste Daroussin k = 7; 138598e903e7SBaptiste Daroussin while (*++s != '"') 138698e903e7SBaptiste Daroussin { 138798e903e7SBaptiste Daroussin ++k; 138898e903e7SBaptiste Daroussin if (*s == '\\') 138998e903e7SBaptiste Daroussin { 139098e903e7SBaptiste Daroussin k += 2; 139198e903e7SBaptiste Daroussin if (*++s == '\\') 139298e903e7SBaptiste Daroussin ++k; 139398e903e7SBaptiste Daroussin } 139498e903e7SBaptiste Daroussin } 139598e903e7SBaptiste Daroussin j += k; 139698e903e7SBaptiste Daroussin if (j > 80) 139798e903e7SBaptiste Daroussin { 139898e903e7SBaptiste Daroussin output_newline(); 139998e903e7SBaptiste Daroussin j = k; 140098e903e7SBaptiste Daroussin } 140198e903e7SBaptiste Daroussin fprintf(output_file, "\"\\\""); 140298e903e7SBaptiste Daroussin s = symnam[i]; 140398e903e7SBaptiste Daroussin while (*++s != '"') 140498e903e7SBaptiste Daroussin { 140598e903e7SBaptiste Daroussin if (*s == '\\') 140698e903e7SBaptiste Daroussin { 140798e903e7SBaptiste Daroussin fprintf(output_file, "\\\\"); 140898e903e7SBaptiste Daroussin if (*++s == '\\') 140998e903e7SBaptiste Daroussin fprintf(output_file, "\\\\"); 141098e903e7SBaptiste Daroussin else 141198e903e7SBaptiste Daroussin putc(*s, output_file); 141298e903e7SBaptiste Daroussin } 141398e903e7SBaptiste Daroussin else 141498e903e7SBaptiste Daroussin putc(*s, output_file); 141598e903e7SBaptiste Daroussin } 141698e903e7SBaptiste Daroussin fprintf(output_file, "\\\"\","); 141798e903e7SBaptiste Daroussin } 141898e903e7SBaptiste Daroussin else if (s[0] == '\'') 141998e903e7SBaptiste Daroussin { 142098e903e7SBaptiste Daroussin if (s[1] == '"') 142198e903e7SBaptiste Daroussin { 142298e903e7SBaptiste Daroussin j += 7; 142398e903e7SBaptiste Daroussin if (j > 80) 142498e903e7SBaptiste Daroussin { 142598e903e7SBaptiste Daroussin output_newline(); 142698e903e7SBaptiste Daroussin j = 7; 142798e903e7SBaptiste Daroussin } 142898e903e7SBaptiste Daroussin fprintf(output_file, "\"'\\\"'\","); 142998e903e7SBaptiste Daroussin } 143098e903e7SBaptiste Daroussin else 143198e903e7SBaptiste Daroussin { 143298e903e7SBaptiste Daroussin k = 5; 143398e903e7SBaptiste Daroussin while (*++s != '\'') 143498e903e7SBaptiste Daroussin { 143598e903e7SBaptiste Daroussin ++k; 143698e903e7SBaptiste Daroussin if (*s == '\\') 143798e903e7SBaptiste Daroussin { 143898e903e7SBaptiste Daroussin k += 2; 143998e903e7SBaptiste Daroussin if (*++s == '\\') 144098e903e7SBaptiste Daroussin ++k; 144198e903e7SBaptiste Daroussin } 144298e903e7SBaptiste Daroussin } 144398e903e7SBaptiste Daroussin j += k; 144498e903e7SBaptiste Daroussin if (j > 80) 144598e903e7SBaptiste Daroussin { 144698e903e7SBaptiste Daroussin output_newline(); 144798e903e7SBaptiste Daroussin j = k; 144898e903e7SBaptiste Daroussin } 144998e903e7SBaptiste Daroussin fprintf(output_file, "\"'"); 145098e903e7SBaptiste Daroussin s = symnam[i]; 145198e903e7SBaptiste Daroussin while (*++s != '\'') 145298e903e7SBaptiste Daroussin { 145398e903e7SBaptiste Daroussin if (*s == '\\') 145498e903e7SBaptiste Daroussin { 145598e903e7SBaptiste Daroussin fprintf(output_file, "\\\\"); 145698e903e7SBaptiste Daroussin if (*++s == '\\') 145798e903e7SBaptiste Daroussin fprintf(output_file, "\\\\"); 145898e903e7SBaptiste Daroussin else 145998e903e7SBaptiste Daroussin putc(*s, output_file); 146098e903e7SBaptiste Daroussin } 146198e903e7SBaptiste Daroussin else 146298e903e7SBaptiste Daroussin putc(*s, output_file); 146398e903e7SBaptiste Daroussin } 146498e903e7SBaptiste Daroussin fprintf(output_file, "'\","); 146598e903e7SBaptiste Daroussin } 146698e903e7SBaptiste Daroussin } 146798e903e7SBaptiste Daroussin else 146898e903e7SBaptiste Daroussin { 146998e903e7SBaptiste Daroussin k = (int)strlen(s) + 3; 147098e903e7SBaptiste Daroussin j += k; 147198e903e7SBaptiste Daroussin if (j > 80) 147298e903e7SBaptiste Daroussin { 147398e903e7SBaptiste Daroussin output_newline(); 147498e903e7SBaptiste Daroussin j = k; 147598e903e7SBaptiste Daroussin } 147698e903e7SBaptiste Daroussin putc('"', output_file); 147798e903e7SBaptiste Daroussin do 147898e903e7SBaptiste Daroussin { 147998e903e7SBaptiste Daroussin putc(*s, output_file); 148098e903e7SBaptiste Daroussin } 148198e903e7SBaptiste Daroussin while (*++s); 148298e903e7SBaptiste Daroussin fprintf(output_file, "\","); 148398e903e7SBaptiste Daroussin } 148498e903e7SBaptiste Daroussin } 148598e903e7SBaptiste Daroussin else 148698e903e7SBaptiste Daroussin { 148798e903e7SBaptiste Daroussin j += 2; 148898e903e7SBaptiste Daroussin if (j > 80) 148998e903e7SBaptiste Daroussin { 149098e903e7SBaptiste Daroussin output_newline(); 149198e903e7SBaptiste Daroussin j = 2; 149298e903e7SBaptiste Daroussin } 149398e903e7SBaptiste Daroussin fprintf(output_file, "0,"); 149498e903e7SBaptiste Daroussin } 149598e903e7SBaptiste Daroussin } 149698e903e7SBaptiste Daroussin end_table(); 149798e903e7SBaptiste Daroussin FREE(symnam); 149898e903e7SBaptiste Daroussin 14994b4a8fcaSBaptiste Daroussin if (token_table) 15004b4a8fcaSBaptiste Daroussin output_line("#if YYDEBUG"); 150198e903e7SBaptiste Daroussin start_str_table("rule"); 150298e903e7SBaptiste Daroussin for (i = 2; i < nrules; ++i) 150398e903e7SBaptiste Daroussin { 150498e903e7SBaptiste Daroussin fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]); 150598e903e7SBaptiste Daroussin for (j = rrhs[i]; ritem[j] > 0; ++j) 150698e903e7SBaptiste Daroussin { 150798e903e7SBaptiste Daroussin s = symbol_name[ritem[j]]; 150898e903e7SBaptiste Daroussin if (s[0] == '"') 150998e903e7SBaptiste Daroussin { 151098e903e7SBaptiste Daroussin fprintf(output_file, " \\\""); 151198e903e7SBaptiste Daroussin while (*++s != '"') 151298e903e7SBaptiste Daroussin { 151398e903e7SBaptiste Daroussin if (*s == '\\') 151498e903e7SBaptiste Daroussin { 151598e903e7SBaptiste Daroussin if (s[1] == '\\') 151698e903e7SBaptiste Daroussin fprintf(output_file, "\\\\\\\\"); 151798e903e7SBaptiste Daroussin else 151898e903e7SBaptiste Daroussin fprintf(output_file, "\\\\%c", s[1]); 151998e903e7SBaptiste Daroussin ++s; 152098e903e7SBaptiste Daroussin } 152198e903e7SBaptiste Daroussin else 152298e903e7SBaptiste Daroussin putc(*s, output_file); 152398e903e7SBaptiste Daroussin } 152498e903e7SBaptiste Daroussin fprintf(output_file, "\\\""); 152598e903e7SBaptiste Daroussin } 152698e903e7SBaptiste Daroussin else if (s[0] == '\'') 152798e903e7SBaptiste Daroussin { 152898e903e7SBaptiste Daroussin if (s[1] == '"') 152998e903e7SBaptiste Daroussin fprintf(output_file, " '\\\"'"); 153098e903e7SBaptiste Daroussin else if (s[1] == '\\') 153198e903e7SBaptiste Daroussin { 153298e903e7SBaptiste Daroussin if (s[2] == '\\') 153398e903e7SBaptiste Daroussin fprintf(output_file, " '\\\\\\\\"); 153498e903e7SBaptiste Daroussin else 153598e903e7SBaptiste Daroussin fprintf(output_file, " '\\\\%c", s[2]); 153698e903e7SBaptiste Daroussin s += 2; 153798e903e7SBaptiste Daroussin while (*++s != '\'') 153898e903e7SBaptiste Daroussin putc(*s, output_file); 153998e903e7SBaptiste Daroussin putc('\'', output_file); 154098e903e7SBaptiste Daroussin } 154198e903e7SBaptiste Daroussin else 154298e903e7SBaptiste Daroussin fprintf(output_file, " '%c'", s[1]); 154398e903e7SBaptiste Daroussin } 154498e903e7SBaptiste Daroussin else 154598e903e7SBaptiste Daroussin fprintf(output_file, " %s", s); 154698e903e7SBaptiste Daroussin } 154798e903e7SBaptiste Daroussin fprintf(output_file, "\","); 154898e903e7SBaptiste Daroussin output_newline(); 154998e903e7SBaptiste Daroussin } 155098e903e7SBaptiste Daroussin 155198e903e7SBaptiste Daroussin end_table(); 155298e903e7SBaptiste Daroussin output_line("#endif"); 155398e903e7SBaptiste Daroussin } 155498e903e7SBaptiste Daroussin 15550c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 15560c8de5b0SBaptiste Daroussin static void 15570c8de5b0SBaptiste Daroussin output_backtracking_parser(FILE * fp) 15580c8de5b0SBaptiste Daroussin { 15590c8de5b0SBaptiste Daroussin putl_code(fp, "#undef YYBTYACC\n"); 15600c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 15610c8de5b0SBaptiste Daroussin if (backtrack) 15620c8de5b0SBaptiste Daroussin { 15630c8de5b0SBaptiste Daroussin putl_code(fp, "#define YYBTYACC 1\n"); 15640c8de5b0SBaptiste Daroussin putl_code(fp, 15650c8de5b0SBaptiste Daroussin "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n"); 15660c8de5b0SBaptiste Daroussin } 15670c8de5b0SBaptiste Daroussin else 15680c8de5b0SBaptiste Daroussin #endif 15690c8de5b0SBaptiste Daroussin { 15700c8de5b0SBaptiste Daroussin putl_code(fp, "#define YYBTYACC 0\n"); 15710c8de5b0SBaptiste Daroussin putl_code(fp, "#define YYDEBUGSTR YYPREFIX \"debug\"\n"); 15720c8de5b0SBaptiste Daroussin } 15730c8de5b0SBaptiste Daroussin } 15740c8de5b0SBaptiste Daroussin #endif 15750c8de5b0SBaptiste Daroussin 157698e903e7SBaptiste Daroussin static void 157798e903e7SBaptiste Daroussin output_pure_parser(FILE * fp) 157898e903e7SBaptiste Daroussin { 157998e903e7SBaptiste Daroussin putc_code(fp, '\n'); 158098e903e7SBaptiste Daroussin 158198e903e7SBaptiste Daroussin if (fp == code_file) 15820c8de5b0SBaptiste Daroussin ++outline; 158398e903e7SBaptiste Daroussin fprintf(fp, "#define YYPURE %d\n", pure_parser); 158498e903e7SBaptiste Daroussin putc_code(fp, '\n'); 158598e903e7SBaptiste Daroussin } 158698e903e7SBaptiste Daroussin 1587*6166fdceSJung-uk Kim #if defined(YY_NO_LEAKS) 1588*6166fdceSJung-uk Kim static void 1589*6166fdceSJung-uk Kim output_no_leaks(FILE * fp) 1590*6166fdceSJung-uk Kim { 1591*6166fdceSJung-uk Kim putc_code(fp, '\n'); 1592*6166fdceSJung-uk Kim 1593*6166fdceSJung-uk Kim if (fp == code_file) 1594*6166fdceSJung-uk Kim ++outline; 1595*6166fdceSJung-uk Kim fputs("#define YY_NO_LEAKS 1\n", fp); 1596*6166fdceSJung-uk Kim putc_code(fp, '\n'); 1597*6166fdceSJung-uk Kim } 1598*6166fdceSJung-uk Kim #endif 1599*6166fdceSJung-uk Kim 160098e903e7SBaptiste Daroussin static void 160198e903e7SBaptiste Daroussin output_trailing_text(void) 160298e903e7SBaptiste Daroussin { 160398e903e7SBaptiste Daroussin int c, last; 160498e903e7SBaptiste Daroussin FILE *in; 160598e903e7SBaptiste Daroussin 160698e903e7SBaptiste Daroussin if (line == 0) 160798e903e7SBaptiste Daroussin return; 160898e903e7SBaptiste Daroussin 160998e903e7SBaptiste Daroussin in = input_file; 161098e903e7SBaptiste Daroussin c = *cptr; 161198e903e7SBaptiste Daroussin if (c == '\n') 161298e903e7SBaptiste Daroussin { 161398e903e7SBaptiste Daroussin ++lineno; 161498e903e7SBaptiste Daroussin if ((c = getc(in)) == EOF) 161598e903e7SBaptiste Daroussin return; 161698e903e7SBaptiste Daroussin write_input_lineno(); 161798e903e7SBaptiste Daroussin putc_code(code_file, c); 161898e903e7SBaptiste Daroussin last = c; 161998e903e7SBaptiste Daroussin } 162098e903e7SBaptiste Daroussin else 162198e903e7SBaptiste Daroussin { 162298e903e7SBaptiste Daroussin write_input_lineno(); 162398e903e7SBaptiste Daroussin do 162498e903e7SBaptiste Daroussin { 162598e903e7SBaptiste Daroussin putc_code(code_file, c); 162698e903e7SBaptiste Daroussin } 162798e903e7SBaptiste Daroussin while ((c = *++cptr) != '\n'); 162898e903e7SBaptiste Daroussin putc_code(code_file, c); 162998e903e7SBaptiste Daroussin last = '\n'; 163098e903e7SBaptiste Daroussin } 163198e903e7SBaptiste Daroussin 163298e903e7SBaptiste Daroussin while ((c = getc(in)) != EOF) 163398e903e7SBaptiste Daroussin { 163498e903e7SBaptiste Daroussin putc_code(code_file, c); 163598e903e7SBaptiste Daroussin last = c; 163698e903e7SBaptiste Daroussin } 163798e903e7SBaptiste Daroussin 163898e903e7SBaptiste Daroussin if (last != '\n') 163998e903e7SBaptiste Daroussin { 164098e903e7SBaptiste Daroussin putc_code(code_file, '\n'); 164198e903e7SBaptiste Daroussin } 164298e903e7SBaptiste Daroussin write_code_lineno(code_file); 164398e903e7SBaptiste Daroussin } 164498e903e7SBaptiste Daroussin 164598e903e7SBaptiste Daroussin static void 164698e903e7SBaptiste Daroussin output_semantic_actions(void) 164798e903e7SBaptiste Daroussin { 164898e903e7SBaptiste Daroussin int c, last; 164998e903e7SBaptiste Daroussin 165098e903e7SBaptiste Daroussin rewind(action_file); 165198e903e7SBaptiste Daroussin if ((c = getc(action_file)) == EOF) 165298e903e7SBaptiste Daroussin return; 165398e903e7SBaptiste Daroussin 165498e903e7SBaptiste Daroussin last = c; 165598e903e7SBaptiste Daroussin putc_code(code_file, c); 165698e903e7SBaptiste Daroussin while ((c = getc(action_file)) != EOF) 165798e903e7SBaptiste Daroussin { 165898e903e7SBaptiste Daroussin putc_code(code_file, c); 165998e903e7SBaptiste Daroussin last = c; 166098e903e7SBaptiste Daroussin } 166198e903e7SBaptiste Daroussin 166298e903e7SBaptiste Daroussin if (last != '\n') 166398e903e7SBaptiste Daroussin { 166498e903e7SBaptiste Daroussin putc_code(code_file, '\n'); 166598e903e7SBaptiste Daroussin } 166698e903e7SBaptiste Daroussin 166798e903e7SBaptiste Daroussin write_code_lineno(code_file); 166898e903e7SBaptiste Daroussin } 166998e903e7SBaptiste Daroussin 167098e903e7SBaptiste Daroussin static void 167198e903e7SBaptiste Daroussin output_parse_decl(FILE * fp) 167298e903e7SBaptiste Daroussin { 16730c8de5b0SBaptiste Daroussin putc_code(fp, '\n'); 167498e903e7SBaptiste Daroussin putl_code(fp, "/* compatibility with bison */\n"); 167598e903e7SBaptiste Daroussin putl_code(fp, "#ifdef YYPARSE_PARAM\n"); 167698e903e7SBaptiste Daroussin putl_code(fp, "/* compatibility with FreeBSD */\n"); 167798e903e7SBaptiste Daroussin putl_code(fp, "# ifdef YYPARSE_PARAM_TYPE\n"); 167898e903e7SBaptiste Daroussin putl_code(fp, 167998e903e7SBaptiste Daroussin "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n"); 168098e903e7SBaptiste Daroussin putl_code(fp, "# else\n"); 168198e903e7SBaptiste Daroussin putl_code(fp, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n"); 168298e903e7SBaptiste Daroussin putl_code(fp, "# endif\n"); 168398e903e7SBaptiste Daroussin putl_code(fp, "#else\n"); 168498e903e7SBaptiste Daroussin 168598e903e7SBaptiste Daroussin puts_code(fp, "# define YYPARSE_DECL() yyparse("); 168611fce282SBaptiste Daroussin puts_param_types(fp, parse_param, 0); 168798e903e7SBaptiste Daroussin putl_code(fp, ")\n"); 168898e903e7SBaptiste Daroussin 168998e903e7SBaptiste Daroussin putl_code(fp, "#endif\n"); 169098e903e7SBaptiste Daroussin } 169198e903e7SBaptiste Daroussin 169298e903e7SBaptiste Daroussin static void 169398e903e7SBaptiste Daroussin output_lex_decl(FILE * fp) 169498e903e7SBaptiste Daroussin { 16950c8de5b0SBaptiste Daroussin putc_code(fp, '\n'); 169698e903e7SBaptiste Daroussin putl_code(fp, "/* Parameters sent to lex. */\n"); 169798e903e7SBaptiste Daroussin putl_code(fp, "#ifdef YYLEX_PARAM\n"); 169898e903e7SBaptiste Daroussin if (pure_parser) 169998e903e7SBaptiste Daroussin { 170098e903e7SBaptiste Daroussin putl_code(fp, "# ifdef YYLEX_PARAM_TYPE\n"); 17010c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 17020c8de5b0SBaptiste Daroussin if (locations) 17030c8de5b0SBaptiste Daroussin { 170411fce282SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 170511fce282SBaptiste Daroussin " YYLTYPE *yylloc," 17060c8de5b0SBaptiste Daroussin " YYLEX_PARAM_TYPE YYLEX_PARAM)\n"); 17070c8de5b0SBaptiste Daroussin } 17080c8de5b0SBaptiste Daroussin else 17090c8de5b0SBaptiste Daroussin #endif 17100c8de5b0SBaptiste Daroussin { 171198e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 171298e903e7SBaptiste Daroussin " YYLEX_PARAM_TYPE YYLEX_PARAM)\n"); 17130c8de5b0SBaptiste Daroussin } 171498e903e7SBaptiste Daroussin putl_code(fp, "# else\n"); 17150c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 17160c8de5b0SBaptiste Daroussin if (locations) 17170c8de5b0SBaptiste Daroussin { 171811fce282SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 171911fce282SBaptiste Daroussin " YYLTYPE *yylloc," 17200c8de5b0SBaptiste Daroussin " void * YYLEX_PARAM)\n"); 17210c8de5b0SBaptiste Daroussin } 17220c8de5b0SBaptiste Daroussin else 17230c8de5b0SBaptiste Daroussin #endif 17240c8de5b0SBaptiste Daroussin { 172598e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 172698e903e7SBaptiste Daroussin " void * YYLEX_PARAM)\n"); 17270c8de5b0SBaptiste Daroussin } 172898e903e7SBaptiste Daroussin putl_code(fp, "# endif\n"); 17290c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 17300c8de5b0SBaptiste Daroussin if (locations) 17310c8de5b0SBaptiste Daroussin putl_code(fp, 17320c8de5b0SBaptiste Daroussin "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n"); 17330c8de5b0SBaptiste Daroussin else 17340c8de5b0SBaptiste Daroussin #endif 173598e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n"); 173698e903e7SBaptiste Daroussin } 173798e903e7SBaptiste Daroussin else 173898e903e7SBaptiste Daroussin { 173998e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n"); 174098e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex(YYLEX_PARAM)\n"); 174198e903e7SBaptiste Daroussin } 174298e903e7SBaptiste Daroussin putl_code(fp, "#else\n"); 174398e903e7SBaptiste Daroussin if (pure_parser && lex_param) 174498e903e7SBaptiste Daroussin { 17450c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 17460c8de5b0SBaptiste Daroussin if (locations) 17470c8de5b0SBaptiste Daroussin puts_code(fp, 17480c8de5b0SBaptiste Daroussin "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, "); 17490c8de5b0SBaptiste Daroussin else 17500c8de5b0SBaptiste Daroussin #endif 175198e903e7SBaptiste Daroussin puts_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, "); 175211fce282SBaptiste Daroussin puts_param_types(fp, lex_param, 0); 175398e903e7SBaptiste Daroussin putl_code(fp, ")\n"); 175498e903e7SBaptiste Daroussin 17550c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 17560c8de5b0SBaptiste Daroussin if (locations) 17570c8de5b0SBaptiste Daroussin puts_code(fp, "# define YYLEX yylex(&yylval, &yylloc, "); 17580c8de5b0SBaptiste Daroussin else 17590c8de5b0SBaptiste Daroussin #endif 176098e903e7SBaptiste Daroussin puts_code(fp, "# define YYLEX yylex(&yylval, "); 176111fce282SBaptiste Daroussin puts_param_names(fp, lex_param, 0); 176298e903e7SBaptiste Daroussin putl_code(fp, ")\n"); 176398e903e7SBaptiste Daroussin } 176498e903e7SBaptiste Daroussin else if (pure_parser) 176598e903e7SBaptiste Daroussin { 17660c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 17670c8de5b0SBaptiste Daroussin if (locations) 17680c8de5b0SBaptiste Daroussin { 17690c8de5b0SBaptiste Daroussin putl_code(fp, 17700c8de5b0SBaptiste Daroussin "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n"); 17710c8de5b0SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex(&yylval, &yylloc)\n"); 17720c8de5b0SBaptiste Daroussin } 17730c8de5b0SBaptiste Daroussin else 17740c8de5b0SBaptiste Daroussin #endif 17750c8de5b0SBaptiste Daroussin { 177698e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n"); 177798e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex(&yylval)\n"); 177898e903e7SBaptiste Daroussin } 17790c8de5b0SBaptiste Daroussin } 178098e903e7SBaptiste Daroussin else if (lex_param) 178198e903e7SBaptiste Daroussin { 178298e903e7SBaptiste Daroussin puts_code(fp, "# define YYLEX_DECL() yylex("); 178311fce282SBaptiste Daroussin puts_param_types(fp, lex_param, 0); 178498e903e7SBaptiste Daroussin putl_code(fp, ")\n"); 178598e903e7SBaptiste Daroussin 178698e903e7SBaptiste Daroussin puts_code(fp, "# define YYLEX yylex("); 178711fce282SBaptiste Daroussin puts_param_names(fp, lex_param, 0); 178898e903e7SBaptiste Daroussin putl_code(fp, ")\n"); 178998e903e7SBaptiste Daroussin } 179098e903e7SBaptiste Daroussin else 179198e903e7SBaptiste Daroussin { 179298e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX_DECL() yylex(void)\n"); 179398e903e7SBaptiste Daroussin putl_code(fp, "# define YYLEX yylex()\n"); 179498e903e7SBaptiste Daroussin } 179598e903e7SBaptiste Daroussin putl_code(fp, "#endif\n"); 179698e903e7SBaptiste Daroussin } 179798e903e7SBaptiste Daroussin 179898e903e7SBaptiste Daroussin static void 179998e903e7SBaptiste Daroussin output_error_decl(FILE * fp) 180098e903e7SBaptiste Daroussin { 18010c8de5b0SBaptiste Daroussin putc_code(fp, '\n'); 18020c8de5b0SBaptiste Daroussin putl_code(fp, "/* Parameters sent to yyerror. */\n"); 180398e903e7SBaptiste Daroussin putl_code(fp, "#ifndef YYERROR_DECL\n"); 18040c8de5b0SBaptiste Daroussin puts_code(fp, "#define YYERROR_DECL() yyerror("); 18050c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 18060c8de5b0SBaptiste Daroussin if (locations) 18072aca18c7SJung-uk Kim puts_code(fp, "YYLTYPE *loc, "); 18080c8de5b0SBaptiste Daroussin #endif 180911fce282SBaptiste Daroussin puts_param_types(fp, parse_param, 1); 181098e903e7SBaptiste Daroussin putl_code(fp, "const char *s)\n"); 181198e903e7SBaptiste Daroussin putl_code(fp, "#endif\n"); 181298e903e7SBaptiste Daroussin 181398e903e7SBaptiste Daroussin putl_code(fp, "#ifndef YYERROR_CALL\n"); 181498e903e7SBaptiste Daroussin 181511fce282SBaptiste Daroussin puts_code(fp, "#define YYERROR_CALL(msg) yyerror("); 18160c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 18170c8de5b0SBaptiste Daroussin if (locations) 18182aca18c7SJung-uk Kim puts_code(fp, "&yylloc, "); 18190c8de5b0SBaptiste Daroussin #endif 182011fce282SBaptiste Daroussin puts_param_names(fp, parse_param, 1); 182198e903e7SBaptiste Daroussin putl_code(fp, "msg)\n"); 182211fce282SBaptiste Daroussin 182398e903e7SBaptiste Daroussin putl_code(fp, "#endif\n"); 182498e903e7SBaptiste Daroussin } 18250c8de5b0SBaptiste Daroussin 18260c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 18270c8de5b0SBaptiste Daroussin static void 18280c8de5b0SBaptiste Daroussin output_yydestruct_decl(FILE * fp) 182998e903e7SBaptiste Daroussin { 18300c8de5b0SBaptiste Daroussin putc_code(fp, '\n'); 18310c8de5b0SBaptiste Daroussin putl_code(fp, "#ifndef YYDESTRUCT_DECL\n"); 183211fce282SBaptiste Daroussin 183311fce282SBaptiste Daroussin puts_code(fp, 183411fce282SBaptiste Daroussin "#define YYDESTRUCT_DECL() " 183511fce282SBaptiste Daroussin "yydestruct(const char *msg, int psymb, YYSTYPE *val"); 18360c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 18370c8de5b0SBaptiste Daroussin if (locations) 183811fce282SBaptiste Daroussin puts_code(fp, ", YYLTYPE *loc"); 18390c8de5b0SBaptiste Daroussin #endif 184011fce282SBaptiste Daroussin if (parse_param) 184111fce282SBaptiste Daroussin { 184211fce282SBaptiste Daroussin puts_code(fp, ", "); 184311fce282SBaptiste Daroussin puts_param_types(fp, parse_param, 0); 184411fce282SBaptiste Daroussin } 184511fce282SBaptiste Daroussin putl_code(fp, ")\n"); 184611fce282SBaptiste Daroussin 184798e903e7SBaptiste Daroussin putl_code(fp, "#endif\n"); 184811fce282SBaptiste Daroussin 18490c8de5b0SBaptiste Daroussin putl_code(fp, "#ifndef YYDESTRUCT_CALL\n"); 185011fce282SBaptiste Daroussin 185111fce282SBaptiste Daroussin puts_code(fp, "#define YYDESTRUCT_CALL(msg, psymb, val"); 18520c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 18530c8de5b0SBaptiste Daroussin if (locations) 185411fce282SBaptiste Daroussin puts_code(fp, ", loc"); 18550c8de5b0SBaptiste Daroussin #endif 185611fce282SBaptiste Daroussin puts_code(fp, ") yydestruct(msg, psymb, val"); 185711fce282SBaptiste Daroussin #if defined(YYBTYACC) 185811fce282SBaptiste Daroussin if (locations) 185911fce282SBaptiste Daroussin puts_code(fp, ", loc"); 186011fce282SBaptiste Daroussin #endif 186111fce282SBaptiste Daroussin if (parse_param) 186211fce282SBaptiste Daroussin { 186311fce282SBaptiste Daroussin puts_code(fp, ", "); 186411fce282SBaptiste Daroussin puts_param_names(fp, parse_param, 0); 186511fce282SBaptiste Daroussin } 186611fce282SBaptiste Daroussin putl_code(fp, ")\n"); 186711fce282SBaptiste Daroussin 186898e903e7SBaptiste Daroussin putl_code(fp, "#endif\n"); 186998e903e7SBaptiste Daroussin } 18700c8de5b0SBaptiste Daroussin 18710c8de5b0SBaptiste Daroussin static void 18722aca18c7SJung-uk Kim output_initial_action(void) 18732aca18c7SJung-uk Kim { 18742aca18c7SJung-uk Kim if (initial_action) 18752aca18c7SJung-uk Kim fprintf(code_file, "%s\n", initial_action); 18762aca18c7SJung-uk Kim } 18772aca18c7SJung-uk Kim 18782aca18c7SJung-uk Kim static void 18790c8de5b0SBaptiste Daroussin output_yydestruct_impl(void) 18800c8de5b0SBaptiste Daroussin { 18810c8de5b0SBaptiste Daroussin int i; 18820c8de5b0SBaptiste Daroussin char *s, *destructor_code; 18830c8de5b0SBaptiste Daroussin 18840c8de5b0SBaptiste Daroussin putc_code(code_file, '\n'); 18850c8de5b0SBaptiste Daroussin putl_code(code_file, "/* Release memory associated with symbol. */\n"); 18860c8de5b0SBaptiste Daroussin putl_code(code_file, "#if ! defined YYDESTRUCT_IS_DECLARED\n"); 18870c8de5b0SBaptiste Daroussin putl_code(code_file, "static void\n"); 18880c8de5b0SBaptiste Daroussin putl_code(code_file, "YYDESTRUCT_DECL()\n"); 18890c8de5b0SBaptiste Daroussin putl_code(code_file, "{\n"); 18900c8de5b0SBaptiste Daroussin putl_code(code_file, " switch (psymb)\n"); 18910c8de5b0SBaptiste Daroussin putl_code(code_file, " {\n"); 18920c8de5b0SBaptiste Daroussin for (i = 2; i < nsyms; ++i) 18930c8de5b0SBaptiste Daroussin { 18940c8de5b0SBaptiste Daroussin if ((destructor_code = symbol_destructor[i]) != NULL) 18950c8de5b0SBaptiste Daroussin { 18960c8de5b0SBaptiste Daroussin ++outline; 18970c8de5b0SBaptiste Daroussin fprintf(code_file, "\tcase %d:\n", symbol_pval[i]); 18980c8de5b0SBaptiste Daroussin /* comprehend the number of lines in the destructor code */ 18990c8de5b0SBaptiste Daroussin for (s = destructor_code; (s = strchr(s, '\n')) != NULL; s++) 19000c8de5b0SBaptiste Daroussin ++outline; 19010c8de5b0SBaptiste Daroussin puts_code(code_file, destructor_code); 19020c8de5b0SBaptiste Daroussin putc_code(code_file, '\n'); 19030c8de5b0SBaptiste Daroussin putl_code(code_file, "\tbreak;\n"); 19040c8de5b0SBaptiste Daroussin write_code_lineno(code_file); 19050c8de5b0SBaptiste Daroussin FREE(destructor_code); 190698e903e7SBaptiste Daroussin } 19070c8de5b0SBaptiste Daroussin } 19080c8de5b0SBaptiste Daroussin putl_code(code_file, " }\n"); 19090c8de5b0SBaptiste Daroussin putl_code(code_file, "}\n"); 19100c8de5b0SBaptiste Daroussin putl_code(code_file, "#define YYDESTRUCT_IS_DECLARED 1\n"); 19110c8de5b0SBaptiste Daroussin putl_code(code_file, "#endif\n"); 19120c8de5b0SBaptiste Daroussin 19130c8de5b0SBaptiste Daroussin DO_FREE(symbol_destructor); 19140c8de5b0SBaptiste Daroussin } 19150c8de5b0SBaptiste Daroussin #endif 191698e903e7SBaptiste Daroussin 191798e903e7SBaptiste Daroussin static void 191898e903e7SBaptiste Daroussin free_itemsets(void) 191998e903e7SBaptiste Daroussin { 192098e903e7SBaptiste Daroussin core *cp, *next; 192198e903e7SBaptiste Daroussin 192298e903e7SBaptiste Daroussin FREE(state_table); 192398e903e7SBaptiste Daroussin for (cp = first_state; cp; cp = next) 192498e903e7SBaptiste Daroussin { 192598e903e7SBaptiste Daroussin next = cp->next; 192698e903e7SBaptiste Daroussin FREE(cp); 192798e903e7SBaptiste Daroussin } 192898e903e7SBaptiste Daroussin } 192998e903e7SBaptiste Daroussin 193098e903e7SBaptiste Daroussin static void 193198e903e7SBaptiste Daroussin free_shifts(void) 193298e903e7SBaptiste Daroussin { 193398e903e7SBaptiste Daroussin shifts *sp, *next; 193498e903e7SBaptiste Daroussin 193598e903e7SBaptiste Daroussin FREE(shift_table); 193698e903e7SBaptiste Daroussin for (sp = first_shift; sp; sp = next) 193798e903e7SBaptiste Daroussin { 193898e903e7SBaptiste Daroussin next = sp->next; 193998e903e7SBaptiste Daroussin FREE(sp); 194098e903e7SBaptiste Daroussin } 194198e903e7SBaptiste Daroussin } 194298e903e7SBaptiste Daroussin 194398e903e7SBaptiste Daroussin static void 194498e903e7SBaptiste Daroussin free_reductions(void) 194598e903e7SBaptiste Daroussin { 194698e903e7SBaptiste Daroussin reductions *rp, *next; 194798e903e7SBaptiste Daroussin 194898e903e7SBaptiste Daroussin FREE(reduction_table); 194998e903e7SBaptiste Daroussin for (rp = first_reduction; rp; rp = next) 195098e903e7SBaptiste Daroussin { 195198e903e7SBaptiste Daroussin next = rp->next; 195298e903e7SBaptiste Daroussin FREE(rp); 195398e903e7SBaptiste Daroussin } 195498e903e7SBaptiste Daroussin } 195598e903e7SBaptiste Daroussin 195698e903e7SBaptiste Daroussin static void 195798e903e7SBaptiste Daroussin output_externs(FILE * fp, const char *const section[]) 195898e903e7SBaptiste Daroussin { 195998e903e7SBaptiste Daroussin int i; 196098e903e7SBaptiste Daroussin const char *s; 196198e903e7SBaptiste Daroussin 196298e903e7SBaptiste Daroussin for (i = 0; (s = section[i]) != 0; ++i) 196398e903e7SBaptiste Daroussin { 19640c8de5b0SBaptiste Daroussin /* prefix non-blank lines that don't start with 19650c8de5b0SBaptiste Daroussin C pre-processor directives with 'extern ' */ 19660c8de5b0SBaptiste Daroussin if (*s && (*s != '#')) 196798e903e7SBaptiste Daroussin fputs("extern\t", fp); 196898e903e7SBaptiste Daroussin if (fp == code_file) 196998e903e7SBaptiste Daroussin ++outline; 19700c8de5b0SBaptiste Daroussin fprintf(fp, "%s\n", s); 197198e903e7SBaptiste Daroussin } 197298e903e7SBaptiste Daroussin } 197398e903e7SBaptiste Daroussin 197498e903e7SBaptiste Daroussin void 197598e903e7SBaptiste Daroussin output(void) 197698e903e7SBaptiste Daroussin { 197798e903e7SBaptiste Daroussin FILE *fp; 197898e903e7SBaptiste Daroussin 197998e903e7SBaptiste Daroussin free_itemsets(); 198098e903e7SBaptiste Daroussin free_shifts(); 198198e903e7SBaptiste Daroussin free_reductions(); 198298e903e7SBaptiste Daroussin 19830c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 19840c8de5b0SBaptiste Daroussin output_backtracking_parser(output_file); 19850c8de5b0SBaptiste Daroussin if (rflag) 19860c8de5b0SBaptiste Daroussin output_backtracking_parser(code_file); 19870c8de5b0SBaptiste Daroussin #endif 19880c8de5b0SBaptiste Daroussin 198998e903e7SBaptiste Daroussin if (iflag) 199098e903e7SBaptiste Daroussin { 19910c8de5b0SBaptiste Daroussin write_code_lineno(code_file); 199298e903e7SBaptiste Daroussin ++outline; 199398e903e7SBaptiste Daroussin fprintf(code_file, "#include \"%s\"\n", externs_file_name); 199498e903e7SBaptiste Daroussin fp = externs_file; 199598e903e7SBaptiste Daroussin } 199698e903e7SBaptiste Daroussin else 199798e903e7SBaptiste Daroussin fp = code_file; 199898e903e7SBaptiste Daroussin 19990c8de5b0SBaptiste Daroussin output_prefix(fp); 200098e903e7SBaptiste Daroussin output_pure_parser(fp); 2001*6166fdceSJung-uk Kim #if defined(YY_NO_LEAKS) 2002*6166fdceSJung-uk Kim output_no_leaks(fp); 2003*6166fdceSJung-uk Kim #endif 200498e903e7SBaptiste Daroussin output_stored_text(fp); 200598e903e7SBaptiste Daroussin output_stype(fp); 20060c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 20070c8de5b0SBaptiste Daroussin if (locations) 20080c8de5b0SBaptiste Daroussin output_ltype(fp); 20090c8de5b0SBaptiste Daroussin #endif 201098e903e7SBaptiste Daroussin output_parse_decl(fp); 201198e903e7SBaptiste Daroussin output_lex_decl(fp); 201298e903e7SBaptiste Daroussin output_error_decl(fp); 20130c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 20140c8de5b0SBaptiste Daroussin if (destructor) 20150c8de5b0SBaptiste Daroussin output_yydestruct_decl(fp); 20160c8de5b0SBaptiste Daroussin #endif 20170c8de5b0SBaptiste Daroussin if (iflag || !rflag) 20180c8de5b0SBaptiste Daroussin { 201998e903e7SBaptiste Daroussin write_section(fp, xdecls); 20200c8de5b0SBaptiste Daroussin } 202198e903e7SBaptiste Daroussin 202298e903e7SBaptiste Daroussin if (iflag) 202398e903e7SBaptiste Daroussin { 202498e903e7SBaptiste Daroussin output_externs(externs_file, global_vars); 202598e903e7SBaptiste Daroussin if (!pure_parser) 202698e903e7SBaptiste Daroussin output_externs(externs_file, impure_vars); 202798e903e7SBaptiste Daroussin } 202898e903e7SBaptiste Daroussin 202998e903e7SBaptiste Daroussin if (iflag) 203098e903e7SBaptiste Daroussin { 20315b91e83fSBaptiste Daroussin if (dflag) 20325b91e83fSBaptiste Daroussin { 203398e903e7SBaptiste Daroussin ++outline; 203498e903e7SBaptiste Daroussin fprintf(code_file, "#include \"%s\"\n", defines_file_name); 20355b91e83fSBaptiste Daroussin } 20365b91e83fSBaptiste Daroussin else 203798e903e7SBaptiste Daroussin output_defines(externs_file); 203898e903e7SBaptiste Daroussin } 203998e903e7SBaptiste Daroussin else 204098e903e7SBaptiste Daroussin { 204198e903e7SBaptiste Daroussin putc_code(code_file, '\n'); 204298e903e7SBaptiste Daroussin output_defines(code_file); 204398e903e7SBaptiste Daroussin } 204498e903e7SBaptiste Daroussin 204598e903e7SBaptiste Daroussin if (dflag) 20460c8de5b0SBaptiste Daroussin { 20470c8de5b0SBaptiste Daroussin start_defines_file(); 204898e903e7SBaptiste Daroussin output_defines(defines_file); 20490c8de5b0SBaptiste Daroussin end_defines_file(); 20500c8de5b0SBaptiste Daroussin } 205198e903e7SBaptiste Daroussin 205298e903e7SBaptiste Daroussin output_rule_data(); 205398e903e7SBaptiste Daroussin output_yydefred(); 20540c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 20550c8de5b0SBaptiste Daroussin output_accessing_symbols(); 20560c8de5b0SBaptiste Daroussin #endif 205798e903e7SBaptiste Daroussin output_actions(); 205898e903e7SBaptiste Daroussin free_parser(); 205998e903e7SBaptiste Daroussin output_debug(); 206098e903e7SBaptiste Daroussin if (rflag) 206198e903e7SBaptiste Daroussin { 206298e903e7SBaptiste Daroussin write_section(code_file, xdecls); 20630c8de5b0SBaptiste Daroussin output_YYINT_typedef(code_file); 206498e903e7SBaptiste Daroussin write_section(code_file, tables); 206598e903e7SBaptiste Daroussin } 206698e903e7SBaptiste Daroussin write_section(code_file, global_vars); 206798e903e7SBaptiste Daroussin if (!pure_parser) 206898e903e7SBaptiste Daroussin { 206998e903e7SBaptiste Daroussin write_section(code_file, impure_vars); 207098e903e7SBaptiste Daroussin } 207198e903e7SBaptiste Daroussin write_section(code_file, hdr_defs); 207298e903e7SBaptiste Daroussin if (!pure_parser) 207398e903e7SBaptiste Daroussin { 207498e903e7SBaptiste Daroussin write_section(code_file, hdr_vars); 207598e903e7SBaptiste Daroussin } 207698e903e7SBaptiste Daroussin output_trailing_text(); 20770c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 20780c8de5b0SBaptiste Daroussin if (destructor) 20790c8de5b0SBaptiste Daroussin output_yydestruct_impl(); 20800c8de5b0SBaptiste Daroussin #endif 208198e903e7SBaptiste Daroussin write_section(code_file, body_1); 208298e903e7SBaptiste Daroussin if (pure_parser) 208398e903e7SBaptiste Daroussin { 208498e903e7SBaptiste Daroussin write_section(code_file, body_vars); 208598e903e7SBaptiste Daroussin } 208698e903e7SBaptiste Daroussin write_section(code_file, body_2); 2087*6166fdceSJung-uk Kim if (pure_parser) 2088*6166fdceSJung-uk Kim { 2089*6166fdceSJung-uk Kim write_section(code_file, init_vars); 2090*6166fdceSJung-uk Kim } 20912aca18c7SJung-uk Kim #if defined(YYBTYACC) 20922aca18c7SJung-uk Kim if (initial_action) 20932aca18c7SJung-uk Kim output_initial_action(); 20942aca18c7SJung-uk Kim #endif 20952aca18c7SJung-uk Kim write_section(code_file, body_3); 209698e903e7SBaptiste Daroussin output_semantic_actions(); 209798e903e7SBaptiste Daroussin write_section(code_file, trailer); 209898e903e7SBaptiste Daroussin } 209998e903e7SBaptiste Daroussin 210098e903e7SBaptiste Daroussin #ifdef NO_LEAKS 210198e903e7SBaptiste Daroussin void 210298e903e7SBaptiste Daroussin output_leaks(void) 210398e903e7SBaptiste Daroussin { 210498e903e7SBaptiste Daroussin DO_FREE(tally); 210598e903e7SBaptiste Daroussin DO_FREE(width); 210698e903e7SBaptiste Daroussin DO_FREE(order); 210798e903e7SBaptiste Daroussin } 210898e903e7SBaptiste Daroussin #endif 2109