-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb2-4.l
208 lines (171 loc) · 4.03 KB
/
fb2-4.l
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Companion source code for "flex & bison", published by O'Reilly
* Media, ISBN 978-0-596-15597-1
* Copyright (c) 2009, Taughannock Networks. All rights reserved.
* See the README file for license conditions and contact info.
* $Header: /home/johnl/flnb/code/RCS/fb2-4.l,v 2.1 2009/11/08 02:53:18 johnl Exp $
*/
/* fb2-4 text concordance */
%option noyywrap nodefault yylineno case-insensitive
/* the symbol table */
%{
struct symbol { /* a word */
struct ref *reflist;
char *name;
};
struct ref {
struct ref *next;
char *filename;
int flags;
int lineno;
};
/* simple symtab of fixed size */
#define NHASH 9997
struct symbol symtab[NHASH];
struct symbol *lookup(char*);
void addref(int, char*, char*, int);
char *curfilename; /* name of current input file */
%}
%%
/* skip common words */
a |
an |
and |
are |
as |
at |
be |
but |
for |
in |
is |
it |
of |
on |
or |
that |
the |
this |
to /* ignore */
[a-z]+(\'(s|t))? { addref(yylineno, curfilename, yytext, 0); }
.|\n /* ignore everything else */
%%
/* hash a symbol */
static unsigned
symhash(char *sym)
{
unsigned int hash = 0;
unsigned c;
while(c = *sym++) hash = hash*9 ^ c;
return hash;
}
int nnew, nold;
int nprobe;
struct symbol *
lookup(char* sym)
{
struct symbol *sp = &symtab[symhash(sym)%NHASH];
int scount = NHASH; /* how many have we looked at */
while(--scount >= 0) {
nprobe++;
if(sp->name && !strcmp(sp->name, sym)) { nold++; return sp; }
if(!sp->name) { /* new entry */
nnew++;
sp->name = strdup(sym);
sp->reflist = 0;
return sp;
}
if(++sp >= symtab+NHASH) sp = symtab; /* try the next entry */
}
fputs("symbol table overflow\n", stderr);
abort(); /* tried them all, table is full */
}
void
addref(int lineno, char *filename, char *word, int flags)
{
struct ref *r;
struct symbol *sp = lookup(word);
/* don't do dups */
if(sp->reflist &&
sp->reflist->lineno == lineno && sp->reflist->filename == filename) return;
r = malloc(sizeof(struct ref));
if(!r) {fputs("out of space\n", stderr); abort(); }
r->next = sp->reflist;
r->filename = filename;
r->lineno = lineno;
r->flags = flags;
sp->reflist = r;
}
/* print the references
* sort the table alphabetically
* then flip each entry's reflist to get it into forward order
* and print it out
*/
/* aux function for sorting */
static int
symcompare(const void *xa, const void *xb)
{
const struct symbol *a = xa;
const struct symbol *b = xb;
if(!a->name) {
if(!b->name) return 0; /* both empty */
return 1; /* put empties at the end */
}
if(!b->name) return -1;
return strcmp(a->name, b->name);
}
void
printrefs()
{
struct symbol *sp;
qsort(symtab, NHASH, sizeof(struct symbol), symcompare); /* sort the symbol table */
for(sp = symtab; sp->name && sp < symtab+NHASH; sp++) {
char *prevfn = NULL; /* last printed filename, to skip dups */
/* reverse the list of references */
struct ref *rp = sp->reflist;
struct ref *rpp = 0; /* previous ref */
struct ref *rpn; /* next ref */
do {
rpn = rp->next;
rp->next = rpp;
rpp = rp;
rp = rpn;
} while(rp);
/* now print the word and its references */
printf("%10s", sp->name);
for(rp = rpp; rp; rp = rp->next) {
if(rp->filename == prevfn) {
printf(" %d", rp->lineno);
} else {
printf(" %s:%d", rp->filename, rp->lineno);
prevfn = rp->filename;
}
}
printf("\n");
}
}
main(argc, argv)
int argc;
char **argv;
{
int i;
if(argc < 2) { /* just read stdin */
curfilename = "(stdin)";
yylineno = 1;
yylex();
} else
for(i = 1; i < argc; i++) {
FILE *f = fopen(argv[i], "r");
if(!f) {
perror(argv[1]);
return (1);
}
curfilename = argv[i]; /* for addref */
yyrestart(f);
yylineno = 1;
yylex();
fclose(f);
}
printf("old = %d, new = %d, total = %d, probes = %d, avg = %1.2f\n",
nold, nnew, nold+nnew, nprobe, (float)nprobe / (nold+nnew));
printrefs();
}