teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

削除

2020/03/10 11:20

投稿

kazuma-s
kazuma-s

スコア8222

answer CHANGED
@@ -1,57 +1,1 @@
1
- ```C
2
- #include <stdio.h> // getchar, printf, puts
3
- #include <stdlib.h> // exit
4
- #include <string.h> // strchr
5
-
6
- void error(const char *s) { printf("Error: %s\n", s); exit(1); }
7
-
8
- void error2(int d)
9
- {
10
- error(d == '"' ? "string literal not closed" :"character constant not closed");
11
- }
12
-
13
- int parse(int e)
14
- {
15
- static char pa[] = "([{", ren[] = ")]}";
16
- int c;
17
- char *p;
18
-
19
- while ((c = getchar()) != EOF) {
20
- if (c == '/') {
21
- if ((c = getchar()) == EOF) return c;
22
- if (c == '/') // skip "// comment"
23
- while ((c = getchar()) != EOF && c != '\n') ;
24
- else if (c == '*') // skip "/* */ comment"
25
- do {
26
- while ((c = getchar()) != EOF && c != '*') ;
27
- if (c == EOF || (c = getchar()) == EOF)
1
+ すでに回答しているのに、間違って新規の回答をしてしまいましたので、削除します。
28
- error("comment not closed");
29
- } while (c != '/');
30
- }
31
- else if (c == '"' || c == '\'') {
32
- int d = c, k = 0; // skip string literal or charcter constant
33
- do {
34
- if ((c = getchar()) == EOF || c == '\n') error2(d);
35
- if (c == '\') {
36
- if ((c = getchar()) == EOF) error2(d);
37
- if (c == d) c = 0;
38
- }
39
- k++;
40
- } while (c != d);
41
- if (d == '\'' && (k < 2 || k > 5)) error("bad character constant");
42
- }
43
- else if ((p = strchr(pa, c))) { // parse parentheses
44
- int r = ren[p - pa];
45
- if (parse(b) != r) error("paren not closed");
46
- }
47
- else if (strchr(ren, c)) break;
48
- }
49
- return c;
50
- }
51
-
52
- int main(void)
53
- {
54
- puts(parse(EOF) == EOF ? "OK" : "unexpected closing paren");
55
- }
56
- ```
57
- バグのご指摘お待ちしています。