| 1 |
XREF(AWK) Philip L. Bewig XREF(AWK) |
| 2 |
|
| 3 |
NAME |
| 4 |
|
| 5 |
xref(awk) - produce a cross reference listing of an awk program |
| 6 |
|
| 7 |
SYNOPSIS |
| 8 |
|
| 9 |
awk -f xref.awk [ file ... ] |
| 10 |
|
| 11 |
DESCRIPTION |
| 12 |
|
| 13 |
XREF(AWK) takes as input a valid awk program and produces as out- |
| 14 |
put a cross-reference listing of all variables and function calls |
| 15 |
which appear in the program. |
| 16 |
|
| 17 |
For ordinary variables and array variables, a line of the form |
| 18 |
|
| 19 |
count var(func) lines ... |
| 20 |
|
| 21 |
is produced, where "count" is the number of times the variable is |
| 22 |
used, "var" is the name of the variable, "func" is the function |
| 23 |
name to which the variable is local (a null "func" indicates that |
| 24 |
the variable is global), and "lines" is the number of each line |
| 25 |
where the variable appears. Appearances of the variable in a |
| 26 |
function's parameter list are ignored. The number of lines shown |
| 27 |
may differ from "count" if the variable appears more than once on |
| 28 |
the same line. |
| 29 |
|
| 30 |
For functions, a line of the form |
| 31 |
|
| 32 |
count func(define) lines ... |
| 33 |
|
| 34 |
is produced, where "count" is the number of times the function is |
| 35 |
called, "func" is the name of the function, "define" is the lime |
| 36 |
number where the function is defined, and "lines" is the number of |
| 37 |
each line where the function is called. As for variables, the |
| 38 |
number of lines shown may differ from "count." |
| 39 |
|
| 40 |
Output lines for variables and functions are intermixed and are |
| 41 |
sorted by name. Though terse, the output is informative, easy to |
| 42 |
read, and amenable to further processing. |
| 43 |
|
| 44 |
EXAMPLE |
| 45 |
|
| 46 |
The cross-reference listing produced by running xref.awk against |
| 47 |
itself is shown below: |
| 48 |
|
| 49 |
5 NR() 39 45 50 53 68 |
| 50 |
8 RLENGTH() 119 120 123 124 127 128 132 133 |
| 51 |
10 act() 31 34 36 40 51 54 56 63 65 67 |
| 52 |
1 arr(asplit) 90 |
| 53 |
2 arr(inarray) 93 94 |
| 54 |
1 asplit(89) 6 |
| 55 |
3 braces() 55 57 58 |
| 56 |
2 flines() 53 79 |
| 57 |
1 fs(asplit) 89 |
| 58 |
3 funcname() 42 52 61 |
| 59 |
16 i() 76 77 78 79 81 82 83 84 90 |
| 60 |
3 inarray(92) 37 43 48 |
| 61 |
6 j() 82 83 84 |
| 62 |
3 j(inarray) 93 94 |
| 63 |
3 keywords() 10 125 129 |
| 64 |
1 lex(97) 29 |
| 65 |
31 line() 103 104 107 108 109 110 112 113 114 115 116 117 118 |
| 66 |
119 120 122 123 124 126 127 128 131 132 133 |
| 67 |
6 lines() 39 45 50 83 84 |
| 68 |
4 local() 41 59 60 64 |
| 69 |
3 machine() 17 30 31 |
| 70 |
2 n(asplit) 89 90 |
| 71 |
15 names() 37 38 43 44 48 49 77 78 79 81 82 83 84 |
| 72 |
3 nextstate() 30 62 73 |
| 73 |
4 nnames() 38 44 49 76 |
| 74 |
4 state() 23 30 31 73 |
| 75 |
1 str(asplit) 89 |
| 76 |
3 symb() 29 30 31 |
| 77 |
2 temp() 59 60 |
| 78 |
2 temp_asplit() 89 90 |
| 79 |
31 tok() 37 38 39 41 42 43 44 45 47 48 49 50 52 53 64 101 105 |
| 80 |
113 115 117 119 123 125 127 129 132 |
| 81 |
1 val(inarray) 94 |
| 82 |
5 xnames() 39 45 50 77 82 |
| 83 |
|
| 84 |
For readability, some lines have been folded. |
| 85 |
|
| 86 |
SOURCE CODE |
| 87 |
|
| 88 |
# xref.awk - cross reference an awk program |
| 89 |
|
| 90 |
BEGIN { |
| 91 |
|
| 92 |
# create array of keywords to be ignored by lexer |
| 93 |
asplit("BEGIN:END:atan2:break:close:continue:cos:delete:" \ |
| 94 |
"do:else:exit:exp:for:getline:gsub:if:in:index:int:" \ |
| 95 |
"length:log:match:next:print:printf:rand:return:sin:" \ |
| 96 |
"split:sprintf:sqrt:srand:sub:substr:system:while", |
| 97 |
keywords,":") |
| 98 |
|
| 99 |
# build the symbol-state table |
| 100 |
split("00:00:00:00:00:00:00:00:00:00:" \ |
| 101 |
"20:10:10:12:12:11:07:00:00:00:" \ |
| 102 |
"08:08:08:08:08:33:08:00:00:00:" \ |
| 103 |
"08:44:08:36:08:08:08:00:00:00:" \ |
| 104 |
"08:44:45:42:42:41:08",machine,":") |
| 105 |
|
| 106 |
# parse the input and store an intermediate representation |
| 107 |
# of the cross-reference information |
| 108 |
|
| 109 |
# set up the machine |
| 110 |
state = 1 |
| 111 |
|
| 112 |
# run the machine |
| 113 |
for (;;) { |
| 114 |
|
| 115 |
# get next symbol |
| 116 |
symb = lex() |
| 117 |
nextstate = substr(machine[state symb],1,1) |
| 118 |
act = substr(machine[state symb],2,1) |
| 119 |
|
| 120 |
# perform required action |
| 121 |
if ( act == "0" ) |
| 122 |
; # do nothing |
| 123 |
else if ( act == "1" ) { |
| 124 |
if ( ! inarray(tok,names) ) |
| 125 |
names[++nnames] = tok |
| 126 |
lines[tok,++xnames[tok]] = NR } |
| 127 |
else if ( act == "2" ) { |
| 128 |
if ( tok in local ) { |
| 129 |
tok = tok "(" funcname ")" |
| 130 |
if ( ! inarray(tok,names) ) |
| 131 |
names[++nnames] = tok |
| 132 |
lines[tok,++xnames[tok]] = NR } |
| 133 |
else { |
| 134 |
tok = tok "()" |
| 135 |
if ( ! inarray(tok,names) ) |
| 136 |
names[++nnames] = tok |
| 137 |
lines[tok,++xnames[tok]] = NR } } |
| 138 |
else if ( act == "3" ) { |
| 139 |
funcname = tok |
| 140 |
flines[tok] = NR } |
| 141 |
else if ( act == "4" ) |
| 142 |
braces++ |
| 143 |
else if ( act == "5" ) { |
| 144 |
braces-- |
| 145 |
if ( braces == 0 ) { |
| 146 |
for ( temp in local ) |
| 147 |
delete local[temp] |
| 148 |
funcname = "" |
| 149 |
nextstate = 1 } } |
| 150 |
else if ( act == "6" ) { |
| 151 |
local[tok] = 1 } |
| 152 |
else if ( act == "7" ) |
| 153 |
break |
| 154 |
else if ( act == "8" ) { |
| 155 |
print "error: xref.awk: line " NR ": aborting" \ |
| 156 |
> "/dev/con" |
| 157 |
exit 1 } |
| 158 |
|
| 159 |
# finished with current token |
| 160 |
state = nextstate } |
| 161 |
|
| 162 |
# finished parsing, now ready to print output |
| 163 |
for ( i = 1; i <= nnames; i++ ) { |
| 164 |
printf "%d ", xnames[names[i]] |"sort +1" |
| 165 |
if ( index(names[i],"(") == 0 ) |
| 166 |
printf "%s(%d)", names[i], flines[names[i]] |"sort +1" |
| 167 |
else |
| 168 |
printf "%s", names[i] |"sort +1" |
| 169 |
for ( j = 1; j <= xnames[names[i]]; j++ ) |
| 170 |
if ( lines[names[i],j] != lines[names[i],j-1] ) |
| 171 |
printf " %d", lines[names[i],j] |"sort +1" |
| 172 |
printf "\n" |"sort +1" } |
| 173 |
|
| 174 |
} # END OF PROGRAM |
| 175 |
|
| 176 |
function asplit(str,arr,fs, n) { n = split(str,temp_asplit,fs) |
| 177 |
for ( i = 1; i <= n; i++ ) arr[temp_asplit[i]]++ } |
| 178 |
|
| 179 |
function inarray(val,arr, j) { |
| 180 |
for ( j in arr ) |
| 181 |
if ( arr[j] == val ) return j |
| 182 |
return "" } |
| 183 |
|
| 184 |
function lex() { |
| 185 |
|
| 186 |
for (;;) { |
| 187 |
|
| 188 |
if ( tok == "(eof)" ) return 7 |
| 189 |
|
| 190 |
while ( length(line) == 0 ) |
| 191 |
if ( getline line == 0 ) { |
| 192 |
tok = "(eof)"; return 7 } |
| 193 |
|
| 194 |
sub(/^[ \t]+/,"",line) # remove white space, |
| 195 |
sub(/^"([^"]|\\")*"/,"",line) # quoted strings, |
| 196 |
sub(/^\/([^\/]|\\\/)+\//,"",line) # regular expressions, |
| 197 |
sub(/^#.*/,"",line) # and comments |
| 198 |
|
| 199 |
if ( line ~ /^function/ ) { |
| 200 |
tok = "function"; line = substr(line,9); return 1 } |
| 201 |
else if ( line ~ /^{/ ) { |
| 202 |
tok = "{"; line = substr(line,2); return 2 } |
| 203 |
else if ( line ~ /^}/ ) { |
| 204 |
tok = "}"; line = substr(line,2); return 3 } |
| 205 |
else if ( match(line,/^[A-Za-z_][A-Za-z_0-9]*\[/) ) { |
| 206 |
tok = substr(line,1,RLENGTH-1) |
| 207 |
line = substr(line,RLENGTH+1) |
| 208 |
return 5 } |
| 209 |
else if ( match(line,/^[A-Za-z_][A-Za-z_0-9]*\(/) ) { |
| 210 |
tok = substr(line,1,RLENGTH-1) |
| 211 |
line = substr(line,RLENGTH+1) |
| 212 |
if ( ! ( tok in keywords ) ) return 6 } |
| 213 |
else if ( match(line,/^[A-Za-z_][A-Za-z_0-9]*/) ) { |
| 214 |
tok = substr(line,1,RLENGTH) |
| 215 |
line = substr(line,RLENGTH+1) |
| 216 |
if ( ! ( tok in keywords ) ) return 4 } |
| 217 |
else { |
| 218 |
match(line,/^[^A-Za-z_{}]/) |
| 219 |
tok = substr(line,1,RLENGTH) |
| 220 |
line = substr(line,RLENGTH+1) } } } |
| 221 |
|
| 222 |
TECHNICAL DISCUSSION |
| 223 |
|
| 224 |
Broadly, XREF(AWK) parses an awk program using a symbol-state |
| 225 |
table, in much the same way as a yacc-generated parser. The |
| 226 |
lexical analyzer recognizes seven distinct symbols: the word |
| 227 |
"function", the left brace, the right brace, identifiers used |
| 228 |
as variables, identifiers used as arrays, identifiers used as |
| 229 |
functions, and end of file. The type of symbol is returned to |
| 230 |
the parser as the value of the "lex" function, and the global |
| 231 |
variable "tok" is set to the text of the current token. |
| 232 |
|
| 233 |
The symbol-state table is stored in the "machine" array. The |
| 234 |
table can be represented as follows: |
| 235 |
|
| 236 |
symbol | 1 2 3 4 5 6 7 |
| 237 |
| |
| 238 |
state | "function" { } var array func eof |
| 239 |
-- -- -- -- -- -- -- -+- -- -- -- -- -- -- -- -- -- -- -- -- -- |
| 240 |
1 any | 20 10 10 12 12 11 07 |
| 241 |
2 "function" | 08 08 08 08 08 33 08 |
| 242 |
3 "function" name | 08 44 08 36 08 08 08 |
| 243 |
4 "function" name "{" | 08 44 45 42 42 41 08 |
| 244 |
|
| 245 |
where the first digit is the state to be entered after process- |
| 246 |
ing the current token and the second digit is an action to be |
| 247 |
performed. The actions are listed below: |
| 248 |
|
| 249 |
1 found a function call |
| 250 |
2 found a variable or array |
| 251 |
3 found a function definition |
| 252 |
4 found a left brace |
| 253 |
5 found a right brace |
| 254 |
6 found a local variable declaration |
| 255 |
7 found end of file |
| 256 |
8 found an error |
| 257 |
|
| 258 |
Each of the first six actions causes some information about the |
| 259 |
target program to be stored for later processing; the structures |
| 260 |
used will be discussed below. The seventh action causes the |
| 261 |
parser to exit. The eighth action causes errors to be reported |
| 262 |
to standard error and the program to abort. |
| 263 |
|
| 264 |
Before describing the intermediate data structures, we will |
| 265 |
discuss some of the more interesting points in the action calls. |
| 266 |
The "braces" variable keeps track of whether we are currently |
| 267 |
within a functions; it is positive within a function and zero |
| 268 |
without. When the right brace which causes the value of "braces" |
| 269 |
to go from one to zero is found, the value of "nextstate" is |
| 270 |
changed from four (scanning a function) to one (any) and the |
| 271 |
names of local variables are forgotten. The "local" array is |
| 272 |
accumulated from the variables found after the function name but |
| 273 |
before the opening left brace of the function; action two care- |
| 274 |
fully checks whether a variable is global or local before writing |
| 275 |
to the intermediate data structure. The variable "funcname" is |
| 276 |
the name of the current function when within a function and null |
| 277 |
without. |
| 278 |
|
| 279 |
The following arrays store an intermediate representation of the |
| 280 |
variable and function identifiers of the target program: |
| 281 |
|
| 282 |
names[1..nnames] = list of all identifiers, both variable and |
| 283 |
function names; for variables, the name has the form |
| 284 |
var(func), but for functions, there are no parentheses |
| 285 |
|
| 286 |
xnames[names[i]] = number of times names[i] is used |
| 287 |
|
| 288 |
lines[names[i],1..xnames[names[i]]] = list of line numbers |
| 289 |
where names[i] is used |
| 290 |
|
| 291 |
flines[names[i]] = line number where function names[i] is |
| 292 |
defined |
| 293 |
|
| 294 |
These arrays are created as the parser reads the input; when the |
| 295 |
parser is finished, the arrays are output in user-readable form. |
| 296 |
|
| 297 |
PORTABILITY |
| 298 |
|
| 299 |
XREF(AWK) will work with any implementation of nawk. The MKS |
| 300 |
ToolKit implementation requires the large-model version of awk. |
| 301 |
|
| 302 |
HISTORY |
| 303 |
|
| 304 |
Written by Phil Bewig on February 10, 1990. Inspired by |
| 305 |
Exercise 3-16 of the book "The Awk Programming Language" by |
| 306 |
Alfred V. Aho, Brian W. Kernighan and Peter J. Weinberger |
| 307 |
(Addison-Wesley: 1988). |
| 308 |
|
| 309 |
COPYRIGHT |
| 310 |
|
| 311 |
This program is placed in the public domain. However, the |
| 312 |
author requests credit when distributed. |
| 313 |
|