-
Notifications
You must be signed in to change notification settings - Fork 0
/
vread.c
322 lines (293 loc) · 7.33 KB
/
vread.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* Exercise 6-2. Write a program that reads a C program and prints in
* alphabetical order each group of variable names that are identical in the
* first 6 characters, but different somewhere thereafter. Don’t count words
* within strings and comments. Make 6 a parameter that can be set from the
* command line. */
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "string.h"
#define NEXCEPT_MAX 100 // max number of keys
#define LENWORD_MAX 200 // max word length
#define NVAR_MAX 10000
char *var[NVAR_MAX];
int pvar;
char *except[NEXCEPT_MAX] = {
"static",
"extern",
"int",
"char",
"long",
"size_t",
"double",
"float",
"unsigned",
"signed",
"auto",
"register",
"volatile",
"short",
"void",
"const",
"if",
"break",
"for",
"case",
"struct",
"return"
};
int pexcept;
struct tnode {
char *word;
struct tnode *left;
struct tnode *right;
};
char nidchar = 6; // the number of identical character
void argread(int argc, char **argv);
char getword(char *word);
int getch(void);
void ungetch(int c);
void error(const char *msg);
void addexcept(const char *name);
char *stralloc(const char *str);
struct tnode *addtree(struct tnode *node, const char *word);
void savevar(struct tnode *node);
void qsort_v(char **v, int left, int right);
void swap(char **v, int p0, int p1);
void vprint(void);
int findstr(const char **v, const char *str);
int main(int argc, char *argv[]) {
struct tnode *root;
char word[LENWORD_MAX];
argread(argc, argv);
/* calculate pexcept */
while (except[pexcept])
pexcept++;
root = NULL;
while (getword(word) > 0)
root = addtree(root, word);
savevar(root);
qsort_v(var, 0, pvar - 1);
qsort_v(except, 0, pexcept - 1);
vprint();
return 0;
}
void argread(int argc, char **argv) {
if (argc > 1)
nidchar = atoi(*++argv);
if (argc > 2)
printf("warning: too many arguments.\n");
}
/* get word.
* supposing word length < LENWORD_MAX, so no length checking
* skip:
* - function name (following a '(');
* - preprocessing line, but meantime add the defined name in exception list;
* - comment;
* - const string;
* - words in '[]';*/
char getword(char *word) {
int c, i;
i = 0;
while ((c=getch()) != EOF) {
if (c == '/')
/* single-line comment */
if ((c=getch()) == '/') {
while ((c=getch()) != EOF && c != '\n');
ungetch(c);
/* multi-line comment */
} else if (c == '*')
while (1) {
/* find terminator */
while ((c=getch()) != EOF && c != '*');
if (c == EOF)
error("endless comment.");
else // c == '*'
if ((c=getch()) == '/')
break;
}
else
error("unrecognised '/'.");
/* skip single quote.
* do not care about the escape character's legality */
else if (c == '\'') {
while ((c = getch()) != EOF && c != '\'');
if (c == EOF)
error("endless single quote.");
/* skip double quote */
} else if(c == '\"') {
while ((c = getch()) != EOF && c!= '\"');
if (c == EOF)
error("endless double quote.");
/* skip characters in bracket */
} else if (c == '[') {
while ((c = getch()) != EOF && c != ']');
if (c == EOF)
error("endless bracket.");
/* preprocessor */
} else if (c == '#') {
while ((c = getch()) != EOF && isalpha(c))
word[i++] = c;
ungetch(c);
word[i] = '\0';
/* add the name in #define into exception list */
if (!strcmp(word, "define")) {
/* skip spaces */
while ((c = getch()) != EOF && isspace(c));
if (c == EOF)
error("invalid use of #define.");
ungetch(c);
/* validate the name */
if (!(isalpha(c) || c == '_'))
error("invalid variable name.");
i = 0;
/* get name */
while ((c = getch()) != EOF && (isalnum(c) || c == '_'))
word[i++] = c;
ungetch(c);
word[i] = '\0';
addexcept(word);
/* skip other preprocessor */
} else {
while ((c = getch()) != EOF && c != '\n');
ungetch(c);
}
/* validate the word leading by alphabet or '_' */
} else if (isalpha(c) || c == '_') {
ungetch(c);
i = 0;
/* get word */
while ((c = getch()) != EOF && (isalnum(c) || c == '_'))
word[i++] = c;
ungetch(c);
word[i] = '\0';
/* word == "struct".
* except structure tag */
if (!strcmp(word, "struct")) {
/* skip spaces */
while ((c = getch()) != EOF && isspace(c));
/* have tag */
if (isalpha(c) || c == '_') {
ungetch(c);
i = 0;
while ((c = getch()) != EOF && (isalnum(c) || c == '_'))
word[i++] = c;
if (c == EOF)
error("invalid structure.");
ungetch(c);
word[i] = '\0';
addexcept(word);
}
} else {
/* skip spaces */
while ((c = getch()) != EOF && isspace(c));
ungetch(c);
/* if next non-space character is '(',
* then current word is a function name, skip it. */
if (c != '(') {
return i;
}
}
}
}
return 0; // EOF
}
#define STKCH_SIZE 1000
int stkch[STKCH_SIZE];
int pstkch;
int getch(void) {
return pstkch ? stkch[--pstkch] : getchar();
}
void ungetch(int c) {
if (pstkch < STKCH_SIZE)
stkch[pstkch++] = c;
else
error("no more stack space for ungetch.");
}
void error(const char *msg) {
printf("error: %s\n", msg);
exit(1);
}
/* add name to exception list */
void addexcept(const char *name) {
except[pexcept++] = stralloc(name);
}
char *stralloc(const char *str) {
char *p;
p = (char *)malloc(strlen(str) + 1);
/* if memory is used up */
if (p)
strcpy(p, str);
else
error("memory is used up.");
return p;
}
struct tnode *addtree(struct tnode *node, const char *word) {
int cond;
if (!node) {
node = (struct tnode *)malloc(sizeof(struct tnode));
node->word = stralloc(word);
node->left = NULL;
node->right = NULL;
} else {
if ((cond = strcmp(node->word, word)) > 0)
node->left = addtree(node->left, word);
else if (cond < 0)
node->right = addtree(node->right, word);
}
return node;
}
void savevar(struct tnode *node) {
if (node) {
var[pvar++] = node->word;
savevar(node->left);
savevar(node->right);
}
}
void qsort_v(char **v, int left, int right) {
int last, i, cond;
if (left >= right)
return;
swap(v, left, (left + right) / 2);
last = left;
for (i = left+1; i <= right; i++) {
if ((cond = strcmp(v[left], v[i])) > 0)
swap(v, ++last, i);
}
swap(v, left, last);
qsort_v(v, left, last);
qsort_v(v, last + 1, right);
}
void swap(char **v, int p0, int p1) {
void *temp;
temp = v[p0];
v[p0] = v[p1];
v[p1] = temp;
}
void vprint(void) {
int i;
i = 0;
if (pvar) {
/* print the first variable that isn't in exception list */
for (; i < pvar && findstr((const char **)except, var[i]) >= 0; i++);
printf("\n%s ", var[i++]);
for (; i < pvar; i++)
if (findstr((const char **)except, var[i]) < 0) {
/* long enough to use strncmp */
if (strlen(var[i]) >= nidchar && strlen(var[i - 1]) >= nidchar)
printf(strncmp(var[i], var[i - 1], nidchar) ?
"\n%s " : "%s ", var[i]);
else
printf("\n%s ", var[i]);
}
putchar('\n');
} else
error("empty variables.");
}
int findstr(const char **v, const char *str) {
int i;
for (i = 0; v[i]; i++)
if (!strcmp(v[i], str))
return i;
return -1;
}