3 # Copyright © 2023-2024 Nick Bowler
5 # Hackjob to improve the horrible yytname array generated by Bison.
7 # Bison generates a list of symbol names as a static array of char pointers
8 # initialized with string literals, which is simply horrible. These pointers
9 # are two to four times larger than necessary, but with position-independent
10 # code this also forces them into an unshareable, writeable segment since
11 # they are not compile-time constants and must be initialized by the dynamic
14 # Furthermore, the names of nonterminal symbols are always included but they
15 # are not always needed; they should only be output by tracing code which
16 # is disabled by default at compile time.
18 # This script replaces the definition of the yytname array with a function
19 # that implements the same lookup using truly constant tables, and makes the
20 # inclusion of nonterminal symbols in these tables conditional on YYDEBUG.
22 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
23 # This is free software: you are free to do what the fuck you want to.
24 # There is NO WARRANTY, to the extent permitted by law.
32 # Locate YYNTOKENS definition, needed to distinguish terminal symbols
34 $1 == "#define" && $2 == "YYNTOKENS" { yyntokens = $3; }
36 # Locate the yytname array definition to replace it.
37 $0 ~ /static.*yytname *\[/ { in_table = 1; }
39 # If we are not in the yytname definition, look for direct references to the
40 # array and replace them with a function call.
45 while ((m = match(right, /yytname *\[/))) {
46 left = substr(right, 1, m-1) "tname(";
47 right = substr(right, m+RLENGTH);
51 for (i = 1; i <= n; i++) {
52 c = substr(right, i, 1);
53 if (c == "]" && --depth == 0)
59 left = left substr(right, 1, i-1) ")";
60 right = substr(right, i+1);
66 # If we are in the yytname definition, collect all the string literals in the
67 # array initializer. The first yyntokens strings are terminal symbols, the
68 # rest are nonterminals.
77 s = substr($0, 1, x - 1);
78 $0 = substr($0, x + 2);
80 gsub("\1", "\\\"", s);
82 # Bison will remove quotes from the token strings at runtime unless the
83 # token contains an unescaped backslash, an apostrophe, or a comma. It is
84 # silly store all these quote characters if they aren't going to be used.
85 if (s ~ /^\\"[^\'\\,]*\\"$/)
86 s = substr(s, 3, length(s) - 4);
88 if (num_tokens < yyntokens)
89 tokens[num_tokens++] = s;
91 nterms[num_nterms++] = s;
95 # At the end of the yytname definition, output our replacement function.
96 in_table && $0 ~ /^};/ {
97 print "#if !defined(UINT_LEAST8_MAX) || !defined(UINT_LEAST16_MAX)";
98 print "# include <inttypes.h>";
100 print "#ifndef assert";
101 print "# include <assert.h>";
103 print "#ifndef offsetof";
104 print "# include <stddef.h>";
107 print "static const char *tname(unsigned x)";
110 count = bucketsort(sorted_tokens, tokens);
111 token_table = build_strtab(sorted_tokens, count, token_offsets);
115 if ((count = bucketsort(sorted_nterms, nterms))) {
116 nterm_table = build_strtab(sorted_nterms, count, nterm_offsets);
117 offset_max = token_size + TMAX;
124 print "\tstruct tname_data {";
126 print "\t\tuint_least16_t map[" num_tokens + num_nterms "];";
127 print "\t\tchar tab[" token_size + nterm_size "];";
129 print "\t\tuint_least16_t map[" num_tokens "];";
130 print "\t\tchar tab[" token_size "];";
134 print "\n\tenum { O = offsetof(struct tname_data, tab) };";
135 print "\tstatic const struct tname_data data = {";
137 print_offsets(token_offsets, tokens, num_tokens, log10(offset_max));
140 print_offsets(nterm_offsets, nterms, num_nterms, log10(offset_max), token_size);
144 sub("\1$", "\"", token_table);
145 gsub("\1", "\"\n\t\t\"\\0\" \"", token_table);
146 gsub("\2", "\\", token_table);
147 print "\t\t}, \"" token_table;
151 sub("\1$", "\"", nterm_table);
152 gsub("\1", "\"\n\t\t\"\\0\" \"", nterm_table);
153 gsub("\2", "\\", nterm_table);
154 print "\t\t\"\\0\" \"" nterm_table;
160 print "\n\tassert(x < sizeof data.map / sizeof data.map[0]);"
161 print "\treturn (char *)&data + data.map[x];";
181 # build_strtab(strings, count, offsets)
183 # Generate a string table. Each token in the strings array (indexed from 0
184 # through count-1) is appended to a string, with a \1 byte terminating each
187 # The table is suffix-compressed: if the token exists as a suffix of one that
188 # is already in the table, no new entry is needed. The input strings array
189 # must be sorted from longest to shortest in order to find all possible suffix
192 # The offsets array is populated with the actual offsets (for C code),
193 # indexed by each token, and the resulting table is returned.
195 # The caller must transform the \1 characters to NUL terminators in the C code.
196 function build_strtab(in_strings, in_count, out_offsets, i, s, ret)
200 for (i = TLEN = 0; i < in_count; i++) {
202 if ((n = index(ret, s "\1")) > 0) {
203 out_offsets[s] = real_length(substr(ret, 1, n-1));
206 out_offsets[s] = TMAX = TLEN;
207 TLEN += real_length(s) + 1;
214 # print_offsets(offsets, strings, count, w, adj)
216 # Outputs the initializer portion of the tname offset array.
218 # Each token in the strings array (indexed from 0 through count-1) is used to
219 # index the offsets array, and the resulting value is printed, with commas
222 # The w value specifies the minimum field width for the printed offsets, used
223 # to achieve a visually pleasing alignment of output.
225 # If adj is nonzero, it is added to all the printed offset values, and a comma
226 # is printed before the first line.
227 function print_offsets(in_offsets, in_strings, in_count, in_w, in_adj, i, t, s)
229 t = in_adj ? "\t,\t" : "\t\t";
232 for (i = 0; i < in_count; i++) {
233 s = s sprintf("%" (in_w+1) "d+O,", (in_offsets[in_strings[i]] + in_adj));
237 if ((i+1) % 8 == 0) {
248 # bucketsort(dst, src)
250 # Sort the elements of src by descending string length,
251 # placing them into dst[0] ... dst[n].
253 # Returns the number of elements.
254 function bucketsort(dst, src, buckets, max, count, i, t)
258 if (i > max) { max = i }
262 for (i = max; i > 0; i--) {
271 i = length(t = src[t])
272 dst[buckets[i]++] = t
280 # Calculate the length of s with backslash sequences counted as one character.
281 function real_length(s, t)
284 return t - gsub(/\\./, "&", s) - gsub("\2\2", "&", s);