-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path6-2.c
235 lines (212 loc) · 5.55 KB
/
6-2.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
/*
* Exercise 6-2. Write a program that reads a C program and prints in
* alphabetical order each group variable names that are identical in the first
* 6 characters, but different somewhere thereafter. Don't count words within
* string and comments. Make 6 a parameter that can be set from the command
* line.
*
* By Faisal Saadatmand
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h> /* for malloc && atoi */
#include <string.h>
#define MAXWORD 100
#define BUFSIZE 100
#define NKEYS (int) (sizeof keytab / sizeof keytab[0])
/* types */
struct tnode { /* the tree node: */
char *word; /* points to the text */
int match; /* number of occurrences */
struct tnode *left; /* left child */
struct tnode *right; /* right child */
};
struct key {
char *word;
int count;
};
/* functions */
int getword(char *, int);
struct key *binsearch(char *, struct key *, int);
struct tnode *addtree(struct tnode *, char *, size_t n);
struct tnode *talloc(void); /* alocate memory to new tree node */
char *strDup(char *); /* copy string into safe place */
void checkmatch(char *, struct tnode *, size_t, int *);
void printtree(struct tnode *);
void freetree(struct tnode *);
/* globals */
int buf[BUFSIZE]; /* buffer from ungetch */
int bufp = 0; /* next free position in buf */
struct key keytab[] ={
{ "auto", 0 },
{ "break", 0 },
{ "case", 0 },
{ "char", 0 },
{ "const", 0 },
{ "continue", 0 },
{ "default", 0 },
{ "do", 0 },
{ "double", 0 },
{ "else", 0 },
{ "enum", 0 },
{ "extern", 0 },
{ "float", 0 },
{ "for", 0 },
{ "goto", 0 },
{ "if", 0 },
{ "int", 0 },
{ "long", 0 },
{ "register", 0 },
{ "return", 0 },
{ "short", 0 },
{ "signed", 0 },
{ "sizeof", 0 },
{ "static", 0 },
{ "struct", 0 },
{ "switch", 0 },
{ "typeof", 0 },
{ "union", 0 },
{ "unsigned", 0 },
{ "void", 0 },
{ "volatile", 0 },
{ "while", 0 },
};
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w, size_t n)
{
int cond;
static int found;
if (!p) { /* a new word has arrived */
p = talloc(); /* make a new node */
p->word = strDup(w); /* copy data to it */
p->match = *(&found); /* p->match = value pointed to by &found */
p->left = p->right = NULL;
} else if ((cond = strcmp(w, p->word)) < 0) { /* less than ? */
checkmatch(w, p, n, &found);
p->left = addtree(p->left, w, n); /* go left */
} else if (cond > 0) { /* greater than */
checkmatch(w, p, n, &found);
p->right = addtree(p->right, w, n); /* go right */
}
found = 0; /* reset */
return p;
}
/* checkmatch: set current node's flag variable and the found variable to 1, if
* w matches a word in the tree */
void checkmatch(char *w, struct tnode *p, size_t n, int *found)
{
if (!strncmp(w, p->word, n)) /* is w a match? */
p->match = *found = 1; /* mark the current and the next nodes */
}
/* printree: in-order print of tree p */
void printree(struct tnode *p)
{
if (!p) /* exist condition */
return;
printree(p->left);
if (p->match)
printf(" %s\n", p->word);
printree(p->right);
}
/* talloc: make a tnode */
struct tnode *talloc(void)
{
return malloc(sizeof(struct tnode));
}
/* freetree: free allocated heap memory of node tree */
void freetree(struct tnode *node)
{
if (!node)
return;
freetree(node->left);
freetree(node->right);
free(node->word);
free(node);
}
/*strDup: make a duplicate of s */
char *strDup(char *s)
{
char *p;
p = malloc(strlen(s) + 1); /* +1 for '\0' */
if (p)
strcpy(p, s);
return p;
}
/* binsearch: find word in tab[0]...tab[n - 1] */
struct key *binsearch(char *word, struct key *tab, int n)
{
int cond;
struct key *low = &tab[0];
struct key *high = &tab[n];
struct key *mid;
while (low < high) {
mid = low + (high - low) / 2;
if ((cond = strcmp(word, mid->word)) < 0)
high = mid;
else if (cond > 0)
low = mid + 1;
else
return mid;
}
return NULL;
}
/* getword: get next word or character from input */
int getword(char *word, int lim)
{
int c, getch(void);
void ungetch(int);
char *w = word;
while (isspace(c = getch()))
;
if (c != EOF)
*w++ = c;
if (isalpha(c) || c == '_' || c == '#') {
for ( ; --lim > 0; ++w)
if (!isalnum(*w = getch()) && *w != '_') {
ungetch(*w);
break;
}
} else if (c == '\'') /* skip character constants */
while ((c = getch()) != '\'')
;
else if (c == '\"') { /* skip string constants */
while ((c = getch()) != '\"')
if (c == '\\')
getch();
} else if (c == '/' && (c = getch()) == '*') /* skip comments */
while ((c = getch()) != EOF)
if (c == '*' && (c = getch()) == '/')
break;
*w ='\0';
return c;
}
/* get a (possibly pushed back) character */
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
/* push character back on input */
void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
int main(int argc, char *argv[])
{
struct tnode *root; /* root node */
char word[MAXWORD]; /* currently read word */
size_t nChar; /* number of characters to match */
nChar = (--argc == 1) ? atoi(*++argv) : 6; /* Note: no input error check */
root = NULL;
while (getword(word, MAXWORD) != EOF)
if ((isalpha(word[0]) || word[0] == '_') && strlen(word) >= nChar &&
!binsearch(word, keytab, NKEYS)) /* skip reserved words */
root = addtree(root, word, nChar);
printree(root);
/* clean up */
freetree(root);
root = NULL;
return 0;
}