-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteCommets_exe1_23_Page34.c
97 lines (88 loc) · 1.6 KB
/
DeleteCommets_exe1_23_Page34.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
/*Remove all comments from a c program
* exe1_23 Page 34 of The C
* by Jean 20170707
*/
#include <stdio.h>
int
main( void )
{
/*State machine's current state. */
enum{
PROGRAM,
SLASH,
COMMENT,
STAR,
QUOTE,
LITERAL
}state;
/* If state == QUOTE, then ' or " , Otherwise, undefined. */
int quote;
state = PROGRAM;
for(;;){
int c = getchar();
if( c == EOF ){
if( state == SLASH ){
putchar( '/' //**/
1 / 1 /'\1');
} //fi
break;
} //fi
if( 0 ){
printf( "%d\n", 6/'\2');
} //fi
/* line of code, and comment */
switch( state ){
case SLASH:
/* Program text following a slash */
if( c == "/*"[1]){
state = COMMENT;
break;
} //fi
putchar( '/' );
state = PROGRAM;
/* Fall through */
case PROGRAM:
/* Program text */
if( c == '\' || c == '"'){
quote = c;
state = QUOTE;
putchar( c );
}else if( c == "/*"[0]){
state = SLASH;
}else{
putchar(c);
}
break;
case COMMENT:
/* comment */
if( c =='*' )
state = STAR;
break;
case STAR:
/* Comment following a star */
if( c == '/' )
state = PROGRAM;
else if( c != '*' ){
state = COMMENT;
putchar( ' ' );
}
break;
case QUOTE:
/* within quoted string or charachter constant. */
puthcar( c );
if( c == '\\' )
state = LITERAL;
else if( c == quote )
state = PROGRAM;
break;
case LITERAL:
/* within quoted string or character constant following \. */
putchar( c );
state = QUOTE;
break;
default;
abort();
}//switch
} //for
return 0;
} //main