summaryrefslogtreecommitdiff
path: root/examples/debflex.awk
blob: b1eda49c4f25c0ae86dda2bbc8f0f1e67e0c1755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Clarify the flex debug trace by substituting first line of each rule.
# Francois Pinard <pinard@iro.umontreal.ca>, July 1990.
#
# Rewritten to process correctly \n's in scanner input.
# BEGIN section modified to correct a collection of rules.
# Michal Jaegermann <michal@phys.ualberta.ca>, December 1993
#
# Sample usage:
#	flex -d PROGRAM.l
#	gcc -o PROGRAM PROGRAM.c -lfl
#	PROGRAM 2>&1 | gawk -f debflex.awk PROGRAM.l
#
# (VP's note: this script presently does not work with either "old" or
#  "new" awk; fixes so it does will be welcome)

BEGIN {
    # Insure proper usage.

    if (ARGC != 2) {
	print "usage: gawk -f debflex.awk FLEX_SOURCE <DEBUG_OUTPUT";
	exit (1);
    }

    # Remove and save the name of flex source.

    source = ARGV[1];
    ARGC--;

    # Swallow the flex source file.

    line = 0;
    section = 1;
    while (getline <source) {

	# Count the lines.

	line++;

	# Count the sections.  When encountering section 3,
	# break out of the awk BEGIN block.

	if (match ($0, /^%%/)) {
	    section++;
	    if (section == 3) {
		break;
	    }
	}
	else {
	    # Only the lines in section 2 which do not begin in a
	    # tab or space might be referred to by the flex debug
	    # trace.  Save only those lines.

	    if (section == 2 && match ($0, /^[^ \t]/)) {
		rules[line] = $0;
	    }
	}
    }
    dashes = "-----------------------------------------------------------";
    collect = "";
    line = 0;
}

# collect complete rule output from a scanner
$0 !~ /^--/ {
    collect = collect "\n" $0;
    next;
}
# otherwise we have a new rule - process what we got so far
{
    process();
}
# and the same thing if we hit EOF
END {
    process();
}

function process() {

    # splitting this way we loose some double dashes and
    # left parentheses from echoed input - a small price to pay
    n = split(collect, field, "\n--|[(]");

    # this loop kicks in only when we already collected something
    for (i = 1; i <= n; i++) {
	if (0 != line) {
	    # we do not care for traces of newlines.
	    if (0 == match(field[i], /\"\n+\"[)]/)) {
		if (rules[line]) {
		    text = field[i];
		    while ( ++i <= n) {
			text = text field[i];
		    }
		    printf("%s:%d: %-8s -- %s\n",
			   source, line, text, rules[line]);
		}
		else {
		    print;
		    printf "%s:%d: *** No such rule.\n", source, line;
		}
	    }
	    line = 0;
	    break;
	}
	if ("" != field[i]) {
	    if ("end of buffer or a NUL)" == field[i]) {
		print dashes;  # Simplify trace of buffer reloads
		continue;
	    }
	    if (match(field[i], /accepting rule at line /)) {
		# force interpretation of line as a number
		line = 0 + substr(field[i], RLENGTH);
		continue;
	    }
	    # echo everything else
	    printf("--%s\n", field[i]);
	}
    }
    collect = "\n" $0;  # ... and start next trace
}