-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefine-processor.c
74 lines (62 loc) · 1.49 KB
/
define-processor.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
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include "table-lookup.h"
#define MAXWORDSIZE 100
int getword(char *word, int lim);
int main()
{
char word[MAXWORDSIZE];
char previous_word[MAXWORDSIZE];
bool in_macro_definition = false;
bool got_name = false;
bool in_macro_undefinition = false;
while(getword(word, MAXWORDSIZE) != EOF) {
if (in_macro_definition) {
in_macro_definition = false;
got_name = true;
} else if (got_name) {
install(previous_word, word);
got_name = false;
} else if (in_macro_undefinition) {
undef(word);
in_macro_undefinition = false;
} else if (strcmp("#define", word) == 0) {
in_macro_definition = true;
} else if (strcmp("#undef", word) == 0) {
in_macro_undefinition = true;
}
strcpy(previous_word, word);
}
printtable();
}
/* getword: get next word or character from input */
int getword(char *word, int lim)
{
int c;
char *w = word;
// skip spaces, except newlines
while (isspace(c = getchar()) && c != '\n') {
;
}
if (c == EOF) {
return EOF;
}
*w++ = c;
/* this doesn't apply in our case, since we want #define */
/* 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];
}