# Generate one or more C array initializers to encode simple tree structures
# in a compact format.
#
-# Each nonempty line of the input file is either an option specification
-# or # a tree specification. An option specification (described later)
-# begins with an @ character. Other lines specify tree nodes.
+# Each nonempty line of the input file is either a comment, an option
+# specification or a tree specification. Each line is distinguished by
+# its first character. A # character introduces a comment, an @ character
+# introduces an option specification (described later), and all other
+# nonempty lines are tree nodes.
#
# The first field of a tree specification must be a valid C identifier,
# optionally followed by a comma. The identifiers used on non-leaf nodes
}
BEGIN {
+ # Check if "\\\\" in substitutions gives just one backslash.
+ bs = "x"; sub(/x/, "\\\\", bs);
+ bs = (length(bs) == 1 ? "\\\\" : "\\");
+
opts["strtab"] = 1;
depth = max_depth = 0;
indent_stack[0] = 0;
}
+# Comments
+NF == 0 { next }
+$0 ~ /^#/ { next }
+
# Options
sub(/^@/, "", $0) {
if (NF == 1) {
next
}
-NF == 0 { next }
{ indent = index($0, $1) - 1 }
indent > 0 {
}
}
- gsub(/\1/, "\"\n\t\"\\0\" \"", entry_strtab);
+ gsub("\1", "\"\n\t\"" bs "0\" \"", entry_strtab);
sub(/^"/, "", entry_strtab);
sub(/\n[^\n]*$/, ";", entry_strtab);
print "\nstatic const char tree_strtab[] =" entry_strtab
# placing them into dst[0] ... dst[n].
#
# Returns the number of elements.
-function bucketsort(dst, src, buckets, max, count, i, t)
+function bucketsort(dst, src, max, count, i, t)
{
+ # Note: ULTRIX 4.5 nawk does not support local array parameters
+ split("", bucketsort_buckets);
+
for (t in src) {
i = length(src[t])
if (i > max) { max = i }
- buckets[i]++
+ bucketsort_buckets[i]++
}
for (i = max; i > 0; i--) {
- if (i in buckets) {
- t = buckets[i]
- buckets[i] = count
+ if (i in bucketsort_buckets) {
+ t = bucketsort_buckets[i]
+ bucketsort_buckets[i] = count
count += t
}
}
for (t in src) {
i = length(t = src[t])
- dst[buckets[i]++] = t
+ dst[bucketsort_buckets[i]++] = t
}
return count