-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyword.c
233 lines (207 loc) · 4.6 KB
/
keyword.c
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* count C keywords */
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "stdlib.h"
#define NKEY (sizeof(keytab) / sizeof(keytab[0]))
struct {
char *kw;
int count;
} keytab[] = {
"auto", 0,
"sizeof", 0,
"break", 0,
"char", 0,
"int", 0,
"double", 0,
"register", 0,
"const", 0,
"struct", 0,
"long", 0,
"unsigned", 0,
"continue", 0,
"return", 0,
"exit", 0,
"switch", 0,
"default", 0,
"volatile", 0,
"while", 0,
"do", 0,
"goto", 0,
"for", 0,
"case", 0,
"else", 0,
"void", 0,
"if", 0
};
enum {KEY=1};
void qsort_key(int left, int right);
void swap(void **, void **);
char *stralloc(const char *s);
void *alloc(int n, int size);
void scankey(void);
int getch(void);
void ungetch(int c);
void error(const char *msg);
int findkey(const char *key);
void checkout(void);
int main(void) {
qsort_key(0, NKEY-1);
scankey();
checkout();
return 0;
}
void qsort_key(int left, int right) {
int i, last;
if (left >= right)
return;
swap((void **)&keytab[left].kw, (void **)&keytab[(left+right) / 2].kw);
last = left;
for (i = left+1; i <= right; i++)
if (strcmp(keytab[left].kw, keytab[i].kw) > 0)
swap((void **)&keytab[++last].kw, (void **)&keytab[i].kw);
swap((void **)&keytab[left].kw, (void **)&keytab[last].kw);
qsort_key(left, last);
qsort_key(last+1, right);
}
void swap(void **s, void **t) {
void *temp;
temp = *s;
*s = *t;
*t = temp;
}
char *stralloc(const char *s) {
char *p;
p = alloc(strlen(s) + 1, sizeof(char));
strncpy(p, s, strlen(s)+1);
return p;
}
#define ALCSTKSIZE 10000
char alcstk[ALCSTKSIZE];
char *alcstkp = alcstk;
void *alloc(int n, int size) {
if (alcstkp - alcstk + n*size < ALCSTKSIZE) {
alcstkp += n * size;
return alcstkp - n * size;
}
error("no more stack space for alloc.");
return NULL;
}
/* keyword separators or not */
#define MAXKEYLEN 20
const char *qualifier = "()*;,:{}[]";
void scankey(void) {
int c, keyidx;
char key_[MAXKEYLEN];
char *key = key_;
while ((c = getch()) != EOF) {
switch (c) {
case '/':
switch ((c = getch())) {
case EOF:
return;
case '/': // single-line comment
while ((c = getch()) != EOF && c != '\n')
;
if (c == EOF)
return;
break;
case '*': // multi-line comment
while (1) {
while ((c = getch()) != EOF && c == '*')
;
if (c == '*' && (c = getch()) == '/')
break;
else if (c == EOF)
error("endless comment.");
}
break;
default:
error("unrecognised '/'.");
}
break;
case '\'': // skip single quote
while ((c = getch()) != '\'' && c != EOF)
;
if (c == EOF)
error("endless single quote.");
break;
case '\"': // skip double quote
while ((c = getch()) != '\"' && c != EOF)
;
if (c == EOF)
error("endless double quote.");
break;
case '#':
while ((c = getch()) != '\n' && c != EOF)
;
if (c == EOF)
return;
break;
default:
/* skip variables prefixed by '_' */
if (c == '_') {
while ((c = getch()) != EOF && isalnum(c))
;
if (c == EOF)
return;
/* try getting keyword */
} else if (isalpha(c)) {
ungetch(c);
while ((c = getch()) != EOF && isalpha(*key++ = c))
;
/* skip variables suffixed by '_' */
if (c == '_')
break;
else if (c == EOF)
return;
ungetch(c);
*--key = '\0';
/* matched */
if ((keyidx = findkey(key_)) >= 0)
keytab[keyidx].count++; // count
}
break;
}
key = key_;
}
}
/* binary search */
int findkey(const char *key) {
int left, right;
left = 0;
right = NKEY;
if (*key == '\0')
return -1;
while (left <= right)
if (strcmp(keytab[(left+right) / 2].kw, key) < 0)
left = (left+right) / 2 + 1;
else if (strcmp(keytab[(left+right) / 2].kw, key) > 0)
right = (left+right) / 2 - 1;
else
return (left+right) / 2;
return -1; // not found
}
void checkout(void) {
for (int i = 0; i < NKEY; i++) {
printf("%s", keytab[i].kw);
printf(strlen(keytab[i].kw)>=8 ? "\t" : "\t\t");
printf("%d\n", keytab[i].count);
}
}
#define CHSTKSIZE 1000
int stack[CHSTKSIZE];
int stkp;
int getch(void) {
return stkp ? stack[--stkp] : getchar();
}
void ungetch(int c) {
if (stkp < CHSTKSIZE)
stack[stkp++] = c;
else
error("no more stack spaces for `getch` and `ungetch`.");
}
void error(const char *msg) {
printf("error: %s\n", msg);
exit(1);
}