diff options
author | Dag-Erling Smørgrav <des@FreeBSD.org> | 2023-02-07 17:25:11 +0000 |
---|---|---|
committer | Dag-Erling Smørgrav <des@FreeBSD.org> | 2023-02-07 17:25:11 +0000 |
commit | cba5e8978cf659b3216c47d38f9917403b4df1c4 (patch) | |
tree | 9de050055829d5d21a3f79f635455864677e3902 /output.c | |
parent | 5dd99b746d26517ef72ee914e30ece2761079321 (diff) | |
download | src-cba5e8978cf659b3216c47d38f9917403b4df1c4.tar.gz src-cba5e8978cf659b3216c47d38f9917403b4df1c4.zip |
Diffstat (limited to 'output.c')
-rw-r--r-- | output.c | 101 |
1 files changed, 69 insertions, 32 deletions
@@ -1,4 +1,4 @@ -/* $Id: output.c,v 1.92 2019/11/20 00:55:05 tom Exp $ */ +/* $Id: output.c,v 1.100 2022/01/09 18:03:56 tom Exp $ */ #include "defs.h" @@ -96,7 +96,7 @@ write_code_lineno(FILE * fp) if (!lflag && (fp == code_file)) { ++outline; - fprintf(fp, line_format, outline + 1, code_file_name); + fprintf_lineno(fp, outline + 1, code_file_name); } } @@ -106,7 +106,7 @@ write_input_lineno(void) if (!lflag) { ++outline; - fprintf(code_file, line_format, lineno, input_file_name); + fprintf_lineno(code_file, lineno, input_file_name); } } @@ -372,12 +372,12 @@ output_yydefred(void) static void output_accessing_symbols(void) { - int i, j; - int *translate; - if (nstates != 0) { - translate = TMALLOC(int, nstates); + int i, j; + int *translate; + + translate = TCMALLOC(int, nstates); NO_SPACE(translate); for (i = 0; i < nstates; ++i) @@ -442,7 +442,7 @@ token_actions(void) Value_t csym = -1; Value_t cbase = 0; #endif - int max, min; + Value_t max, min; Value_t *actionrow, *r, *s; action *p; @@ -791,11 +791,9 @@ static int matching_vector(int vector) { int i; - int j; int k; int t; int w; - int match; int prev; i = order[vector]; @@ -807,19 +805,25 @@ matching_vector(int vector) for (prev = vector - 1; prev >= 0; prev--) { - j = order[prev]; - if (width[j] != w || tally[j] != t) - return (-1); + int j = order[prev]; - match = 1; - for (k = 0; match && k < t; k++) + if (width[j] != w || tally[j] != t) { - if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k]) - match = 0; + return (-1); } + else + { + int match = 1; - if (match) - return (j); + for (k = 0; match && k < t; k++) + { + if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k]) + match = 0; + } + + if (match) + return (j); + } } return (-1); @@ -830,7 +834,7 @@ pack_vector(int vector) { int i, j, k, l; int t; - int loc; + Value_t loc; int ok; Value_t *from; Value_t *to; @@ -854,7 +858,7 @@ pack_vector(int vector) ok = 1; for (k = 0; ok && k < t; k++) { - loc = j + from[k]; + loc = (Value_t)(j + from[k]); if (loc >= maxtable - 1) { if (loc >= MAXTABLE - 1) @@ -893,7 +897,7 @@ pack_vector(int vector) { for (k = 0; k < t; k++) { - loc = j + from[k]; + loc = (Value_t)(j + from[k]); table[loc] = to[k]; check[loc] = from[k]; if (loc > high) @@ -913,7 +917,6 @@ pack_table(void) { int i; Value_t place; - int state; base = NEW2(nvectors, Value_t); pos = NEW2(nentries, Value_t); @@ -930,7 +933,7 @@ pack_table(void) for (i = 0; i < nentries; i++) { - state = matching_vector(i); + int state = matching_vector(i); if (state < 0) place = (Value_t)pack_vector(i); @@ -1048,7 +1051,7 @@ output_table(void) if (high >= MAXYYINT) { fprintf(stderr, "YYTABLESIZE: %ld\n", high); - fprintf(stderr, "Table is longer than %d elements.\n", MAXYYINT); + fprintf(stderr, "Table is longer than %ld elements.\n", (long)MAXYYINT); done(1); } @@ -1225,7 +1228,6 @@ static void output_defines(FILE * fp) { int c, i; - char *s; if (fp == defines_file) { @@ -1234,7 +1236,8 @@ output_defines(FILE * fp) for (i = 2; i < ntokens; ++i) { - s = symbol_name[i]; + char *s = symbol_name[i]; + if (is_C_identifier(s) && (!sflag || *s != '"')) { fprintf(fp, "#define "); @@ -1256,14 +1259,14 @@ output_defines(FILE * fp) } if (fp == code_file) ++outline; - fprintf(fp, " %d\n", symbol_value[i]); + fprintf(fp, " %ld\n", (long)symbol_value[i]); } } if (fp == code_file) ++outline; if (fp != defines_file || iflag) - fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]); + fprintf(fp, "#define YYERRCODE %ld\n", (long)symbol_value[1]); if (fp == defines_file) { @@ -1340,7 +1343,7 @@ output_debug(void) const char *s; ++outline; - fprintf(code_file, "#define YYFINAL %d\n", final_state); + fprintf(code_file, "#define YYFINAL %ld\n", (long)final_state); outline += output_yydebug(code_file); @@ -1372,7 +1375,7 @@ output_debug(void) fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? " "YYUNDFTOKEN : (a))\n"); - symnam = TMALLOC(const char *, max + 2); + symnam = TCMALLOC(const char *, max + 2); NO_SPACE(symnam); /* Note that it is not necessary to initialize the element */ @@ -1685,15 +1688,49 @@ static void output_semantic_actions(void) { int c, last; + int state; + char line_state[20]; rewind(action_file); if ((c = getc(action_file)) == EOF) return; + if (!lflag) + { + state = -1; + sprintf(line_state, line_format, 1, ""); + } + last = c; putc_code(code_file, c); while ((c = getc(action_file)) != EOF) { + /* + * When writing the action file, we did not know the line-numbers in + * the code-file, but wrote empty #line directives. Detect those and + * replace with proper #line directives. + */ + if (!lflag && (last == '\n' || state >= 0)) + { + if (c == line_state[state + 1]) + { + ++state; + if (line_state[state + 1] == '\0') + { + write_code_lineno(code_file); + state = -1; + } + last = c; + continue; + } + else + { + int n; + for (n = 0; n <= state; ++n) + putc_code(code_file, line_state[n]); + state = -1; + } + } putc_code(code_file, c); last = c; } @@ -1956,8 +1993,8 @@ output_yydestruct_impl(void) ++outline; puts_code(code_file, destructor_code); putc_code(code_file, '\n'); - putl_code(code_file, "\tbreak;\n"); write_code_lineno(code_file); + putl_code(code_file, "\tbreak;\n"); FREE(destructor_code); } } |