-
Notifications
You must be signed in to change notification settings - Fork 1
/
count-c-keywords.c
189 lines (165 loc) · 3.9 KB
/
count-c-keywords.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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
struct key {
char *word;
int count;
};
struct key keytab[] = {
{ "auto",0 },
{ "break", 0},
{ "case", 0},
{ "char", 0},
{ "const", 0},
{ "continue", 0},
{ "default", 0},
{ "int", 0},
/* ... */
{ "unsigned", 0},
{ "void", 0},
{ "volatile", 0},
{ "while", 0}
};
#define NKEYS (sizeof keytab / sizeof keytab[0])
#define MAXWORD 100
int getword(char*, int);
int binsearch(char *, struct key *, int);
/* too lazy to get old code for getch and ungetc,
* instead use built in getchar and ungetc */
/* count C keywords */
int main()
{
unsigned int n;
int index_of;
char word[MAXWORD];
while (getword(word, MAXWORD) != EOF) {
if (isalpha(word[0])) {
if ((index_of = binsearch(word, keytab, NKEYS)) >= 0) {
keytab[index_of].count++;
}
}
}
for(n = 0; n < NKEYS; n++) {
if (keytab[n].count > 0) {
printf("%4d %s\n",
keytab[n].count, keytab[n].word);
}
}
return 0;
}
/* binsearch: find word in tab0]...tab[n-1] */
int binsearch(char *word, struct key tab[], int n)
{
int cond;
int low, high, mid;
low = 0;
high = n - 1;
while(low <= high) {
mid = (low+high) / 2;
if ((cond = strcmp(word, tab[mid].word)) < 0) {
high = mid -1;
} else if (cond > 0) {
low = mid + 1;
} else {
return mid;
}
}
return -1;
}
/* getword: get next word or character from input */
int getword(char *word, int lim)
{
int c;
char *w = word;
while (isspace(c = getchar())) {
;
}
if (c != EOF) {
*w++ = c;
}
// If the char is a quote, assume we are starting a string
// constant. We need to skip chars until we reach the end
// of the string constant.
if (c == '"') {
while((c = getchar()) != '"' && c != EOF && c!= '\n') {
;
}
// if the current character isn't a closing quote, we have
// an unterminated string constant.
if (c != '"') {
printf("Unterminated string constant.\n");
return -1;
}
*w = '\0';
return c;
}
// if we have "/*" assume that we are starting a comment.
if (c == '/') {
// look for an asterisk to see if we are starting a comment
if ((c = getchar()) == '*') {
// now we have to find the end of the comment
while((c = getchar()) != EOF) {
// if the current char is an asterisk, we may have found
// the end of the comment
if (c == '*') {
// are we at the end?
if ((c = getchar()) == '/') {
// we found the end of our comment, break out of the loop
break;
} else {
ungetc(c, stdin);
}
}
}
if (c != '/') {
printf("Unterminated c style comment.\n");
return -1;
}
*w = '\0';
return c;
} else {
// not a comment
ungetc(c, stdin);
}
}
// if we see a # it's a preprocessor directive
if (c == '#') {
// now we have to find the end of the directive
while((c = getchar()) != EOF) {
// if the current char is a line continuation ('/'),
// and the next char is a newline, we need to swallow
// that newline since the directive continues.
if (c == '/') {
//is the next char a newline?
if ((c = getchar()) != '\n') {
// if not, unswallow the char, it's not a line
// continuation
ungetc(c, stdin);
}
} else if (c == '\n') {
// we've found the end of our directive
break;
}
}
if (c == EOF) {
printf("Unterminated directive.\n");
return -1;
}
*w = '\0';
return c;
}
if (!isalpha(c)) {
*w = '\0';
return c;
}
for( ; --lim > 0; w++) {
// a word ends when we have a char that's not a letter,
// number, or underscore
if (!isalnum(*w = getchar()) && *w != '_') {
ungetc(*w, stdin);
break;
}
}
*w = '\0';
return word[0];
}