回答編集履歴
1
削除
test
CHANGED
@@ -1,113 +1 @@
|
|
1
|
-
```C
|
2
|
-
|
3
|
-
#include <stdio.h> // getchar, printf, puts
|
4
|
-
|
5
|
-
#include <stdlib.h> // exit
|
6
|
-
|
7
|
-
#include <string.h> // strchr
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
void error(const char *s) { printf("Error: %s\n", s); exit(1); }
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
void error2(int d)
|
16
|
-
|
17
|
-
{
|
18
|
-
|
19
|
-
error(d == '"' ? "string literal not closed" :"character constant not closed");
|
20
|
-
|
21
|
-
}
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
int parse(int e)
|
26
|
-
|
27
|
-
{
|
28
|
-
|
29
|
-
static char pa[] = "([{", ren[] = ")]}";
|
30
|
-
|
31
|
-
int c;
|
32
|
-
|
33
|
-
char *p;
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
while ((c = getchar()) != EOF) {
|
38
|
-
|
39
|
-
if (c == '/') {
|
40
|
-
|
41
|
-
if ((c = getchar()) == EOF) return c;
|
42
|
-
|
43
|
-
if (c == '/') // skip "// comment"
|
44
|
-
|
45
|
-
while ((c = getchar()) != EOF && c != '\n') ;
|
46
|
-
|
47
|
-
else if (c == '*') // skip "/* */ comment"
|
48
|
-
|
49
|
-
do {
|
50
|
-
|
51
|
-
while ((c = getchar()) != EOF && c != '*') ;
|
52
|
-
|
53
|
-
|
1
|
+
すでに回答しているのに、間違って新規の回答をしてしまいましたので、削除します。
|
54
|
-
|
55
|
-
error("comment not closed");
|
56
|
-
|
57
|
-
} while (c != '/');
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
else if (c == '"' || c == '\'') {
|
62
|
-
|
63
|
-
int d = c, k = 0; // skip string literal or charcter constant
|
64
|
-
|
65
|
-
do {
|
66
|
-
|
67
|
-
if ((c = getchar()) == EOF || c == '\n') error2(d);
|
68
|
-
|
69
|
-
if (c == '\') {
|
70
|
-
|
71
|
-
if ((c = getchar()) == EOF) error2(d);
|
72
|
-
|
73
|
-
if (c == d) c = 0;
|
74
|
-
|
75
|
-
}
|
76
|
-
|
77
|
-
k++;
|
78
|
-
|
79
|
-
} while (c != d);
|
80
|
-
|
81
|
-
if (d == '\'' && (k < 2 || k > 5)) error("bad character constant");
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
else if ((p = strchr(pa, c))) { // parse parentheses
|
86
|
-
|
87
|
-
int r = ren[p - pa];
|
88
|
-
|
89
|
-
if (parse(b) != r) error("paren not closed");
|
90
|
-
|
91
|
-
}
|
92
|
-
|
93
|
-
else if (strchr(ren, c)) break;
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
return c;
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
int main(void)
|
104
|
-
|
105
|
-
{
|
106
|
-
|
107
|
-
puts(parse(EOF) == EOF ? "OK" : "unexpected closing paren");
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
```
|
112
|
-
|
113
|
-
バグのご指摘お待ちしています。
|