-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.23.c
155 lines (146 loc) · 2.74 KB
/
1.23.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
/*
* This is a program to remove comments from C programs. It will handle both
* c89 and c99 style comments.
*
* c99 style (slash slash):
* if (slash slash), getchar() until newline, putchar() newline.
* if (c == '\n') break;
*
* c89 style (slash asterisk):
* if slash, then if asterisk
* getchar() until asterisk, then if slash, else repeat
* this is the first place where we call getchar() inside the main loop
*
* It must handle quoted strings and literals correctly. This means not
* looking for comments between "s and 's.
*
* Finally, it should handle escaped characters. This means \. If we encounter
* a \, and we are not in a comment, pass it and next character. This may be
* the second place where we want to call getchar() within the main getchar()
* loop.
*
* The question is, how do we put these requirements together in order to make
* the program work correctly?
*/
#include <stdio.h>
#define IN 1
#define OUT 0
int
main()
{
/*
* The four special states we can be in.
*
* Luckily, these states are exclusive. Meaning, if we are in a quote,
* we cannot be in a constant or comment, and the same for the other
* three states.
*/
int quote = OUT;
int constant = OUT;
int comment89 = OUT;
int comment99 = OUT;
int c;
// Let's try only calling getchar() here...
while ((c = getchar()) != EOF) {
if (quote) {
putchar(c);
if (c == '"') quote = OUT;
continue;
}
if (constant) {
putchar(c);
if (c == 39) constant = OUT;
continue;
}
if (comment89) {
if (c == '*') {
if ((c = getchar()) == '/') {
comment89 = OUT;
}
}
continue;
}
if (comment99) {
if (c == '\n') comment99 = OUT;
continue;
}
switch (c) {
case '\\':
putchar(c);
c = getchar();
putchar(c);
continue;
case '/':
;
int buf = c;
c = getchar();
if (c == '*') {
comment89 = IN;
} else if (c == '/') {
comment99 = IN;
} else {
putchar(buf);
putchar(c);
}
continue;
case '"':
quote = IN;
putchar(c);
continue;
case 39:
constant = IN;
putchar(c);
continue;
default: break;
}
putchar(c);
/*y
if quote
echo
if c == \"
quote = OUT
continue
if constant
echo
if c == \'
constant = OUT
continue
if comment89
if c == *
getchar
if c == /
comment89 = OUT
continue
if comment99
if c == \n
comment99 = OUT
continue
Done with state checking, into normal mode
if c == \\
putchar
getchar
putchar
continue
if c == /
buf = /
getchar
if c == *
comment89 = IN
else if c == /
comment99 = IN
else
put buf
putchar
continue
if c == \"
quote = IN
putchar
continue
if c == '
constant = IN
putchar
continue
*/
}
return 0;
}