-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.24.c
268 lines (257 loc) · 5.02 KB
/
1.24.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
/*
* K&R exercise 1.24
* Rudimentary C syntax checker
*
* WORKS:
* escape sequences
*
* TODO:
* unbalanced parentheses
* unbalanced brackets
* unbalanced braces
* single strings
* double strings
* comments
*/
#include <stdio.h>
#define IN 1
#define OUT 0
/* Exit codes */
#define BAD_ESCAPE_SEQUENCE 2
#define UNBALANCED_PARENTHESES 3
#define UNBALANCED_BRACKETS 4
#define UNBALANCED_BRACES 5
#define TRUNCATION 6
#define CHAR_LITERAL_UNTERMINATED 7
#define COMMENT_UNTERMINATED 8
#define STRING_UNTERMINATED 9
int escape_check(int);
int
escape_check(int c)
{
int ret = 0;
int i = 0;
int j = 0;
switch (c) {
case 'a':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
case 'v':
case '\\':
case '?':
case 39 :
case '"':
ret = 1;
break;
case 'x':;
/* hex checker, upper and lower-case */
while ((i < 2 && (c = getchar()) != EOF)) {
putchar(c);
j++;
if (
(((c >= '0') && (c <= '9')) ||
((c >= 'A') && (c <= 'F')) ||
((c >= 'a') && (c <= 'f'))))
{
i++;
} else {
break;
}
if (i == 2) ret = 1;
}
break;
default:;
/* octal checker */
if (! ((c >= '0') && (c <= '7'))) break;
while (i < 2 && ((c = getchar()) != EOF)) {
putchar(c);
j++;
if ((c >= '0') && (c <= '7')) {
i++;
} else {
break;
}
}
if (i == 2) ret = 1;
break;
}
return ret;
}
int
main()
{
int err = 0;
int brace = 0;
int bracket = 0;
int char_literal = OUT;
int comment_maybe = OUT;
int comment89 = OUT;
int comment99 = OUT;
int paren = 0;
int string = OUT;
int since_newline = 0;
int lines = 1;
int c;
while ((c = getchar()) != EOF) {
if (c == '\n') {
since_newline = 0;
lines++;
} else {
since_newline++;
}
/* Take action based on special states we can be in */
if (string) {
/*
* in a string literal, we only need to check for '"',
* which ends the literal
*/
putchar(c);
if (c == '"') string = OUT;
continue;
} else if (char_literal) {
/*
* in a character literal, 3 things can happen:
* 1) we get a 39, ending the literal
* 2) we get a \, entering an escape sequence
* 3) we get anything else, and remain in the literal
*/
putchar(c);
if (c == 39) {
char_literal = OUT;
} else if (c == '\\') {
c = getchar();
putchar(c);
if (! escape_check(c)) {
err = BAD_ESCAPE_SEQUENCE;
goto out;
}
}
continue;
} else if (comment_maybe) {
/*
* This state is the result of reading a '/' while not
* in any other special state. Now, we find out if we
* are entering a comment, and if so, what kind.
*/
putchar(c);
if (c == '*') {
comment89 = IN;
} else if (c == '/') {
comment99 = IN;
}
comment_maybe = OUT;
continue;
} else if (comment89) {
/*
* in a c89 comment, x things can happen:
* 1) we get a *. then we must check for /, which ends the comment
* 2) we get anything else, continuing the comment
*/
putchar(c);
if (c == '*') {
if ((c = getchar()) == '/') {
putchar(c);
comment89 = OUT;
} else {
/* undo side effect of previous block */
ungetc(c, stdin);
}
}
continue;
} else if (comment99) {
/*
* there is no syntax to check in a c99 comment, except
* for '\n', which is the end of the comment
*/
putchar(c);
if (c == '\n') comment99 = OUT;
continue;
}
/*
* If we are not in a special state, we get here.
* These conditions are in ASCII order.
*/
switch (c) {
case '"':
string = IN;
break;
case 39:
char_literal = IN;
break;
case '(':
paren++;
break;
case ')':
paren--;
break;
case '/':;
comment_maybe = IN;
break;
case '[':
bracket++;
break;
case '\\':
putchar(c);
c = getchar();
if (! escape_check(c)) {
err = BAD_ESCAPE_SEQUENCE;
goto out;
}
break;
case ']':
bracket--;
break;
case '{':
brace++;
break;
case '}':
brace--;
break;
default: break;
}
putchar(c);
}
/* Done reading input */
if (since_newline > 0) {
c = putchar('\n');
err = TRUNCATION;
goto out;
} else if (brace != 0) {
err = UNBALANCED_BRACES;
goto out;
} else if (bracket != 0) {
err = UNBALANCED_BRACKETS;
goto out;
} else if (char_literal != OUT) {
err = CHAR_LITERAL_UNTERMINATED;
goto out;
} else if (comment_maybe != OUT && comment89 != OUT && comment99 != OUT) {
err = COMMENT_UNTERMINATED;
goto out;
} else if (paren != 0) {
err = UNBALANCED_PARENTHESES;
goto out;
} else if (string != OUT) {
err = STRING_UNTERMINATED;
goto out;
}
out:
if (err != 0) {
/*
* Line number doesn't work... at least you know the
* error took place before the reported line.
*/
fprintf(stderr, "error before or on line %d\n", lines);
fprintf(stderr, "string: %d\n", string);
fprintf(stderr, "character literal: %d\n", char_literal);
fprintf(stderr, "c89 comment: %d\n", comment89);
fprintf(stderr, "brace: %d\n", brace);
fprintf(stderr, "parentheses: %d\n", paren);
fprintf(stderr, "bracket: %d\n", bracket);
}
if (c != '\n') putchar('\n');
return err;
}