-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkr_ex1-24.c
147 lines (136 loc) · 3.05 KB
/
kr_ex1-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
#include <stdio.h>
#define MAXPARENS 100 // max number unmatched parenthesis
#define UNMATCHEDRIGHTPAR 1
#define UNMATCHEDLEFTPAR 2
#define TOOMANYPARENS 3
int readchar(void);
int readstring(void);
int skipcomment(void);
int parensmatch(char a, char b);
/* check balanced (), [], and {} taking into account char constants,
strings and comments */
int main() {
int c = '\0';
int extrachar = 0;
int npar = 0; /* index parenthesis list */
char parensList[MAXPARENS];
c = getchar();
while (c != EOF) {
if (c == '\'') { /* reading const char */
c = readchar();
if (c != '\'') {
extrachar = 1;
printf("\n-- unbalanced char --\n");
}
}
else if (c == '\"') {
c = readstring();
if (c != '\"') { /* reading a string */
extrachar = 1;
printf("\n-- unbalanced string --\n");
}
}
else if (c == '/')
if ((c = getchar()) == '*') { /* skipping a comment */
c = skipcomment();
if (c != '/') {
extrachar = 1;
printf("\n-- unbalanced comment --\n");
}
}
else {
if (c != EOF)
extrachar = 1;
}
else if (c == '(' || c == '[' || c == '{') {
if (npar < MAXPARENS)
parensList[npar++] = c;
else {
printf("\n reached max number (%d) of unmatched parenthesis\n", MAXPARENS);
return TOOMANYPARENS;
}
}
else if (c == ')' || c == ']' || c == '}') {
if (npar > 0 && parensmatch(parensList[npar-1], c) == 1) {
parensList[npar-1] = '\0';
--npar;
}
else {
printf("\n unmatched parenthesis of type: \'%c\' \n", c);
return UNMATCHEDRIGHTPAR;
}
}
/* skipping the char allows finding multiple errors */
if (c != EOF && extrachar == 0)
c = getchar();
else
extrachar = 0; /* reset the error to allow finding next one and avoid infinite loop */
}
parensList[npar] = '\0';
if (npar == 0)
printf("\n all parenthesis are matched\n");
else
printf("\n unmatched parenthesis: \n%s\n", parensList);
}
int readchar(void)
{
int c;
if ((c = getchar()) == EOF || c == '\'') /* EOF signals unbalanced char */
;
else if (c == '\\') { //escaped char
if ((c = getchar()) != EOF) {
c = getchar();
}
}
else { // single char
c = getchar();
}
return c;
}
int readstring(void)
{
int c;
c = getchar();
while (c != '\"' && c != EOF) {
if (c == '\\') {
c = getchar();
if (c != EOF) {
c = getchar();
}
}
else {
c = getchar();
}
}
return c;
}
int skipcomment(void)
{
int c;
int insidecomment = 1;
c = getchar();
while (c != EOF && insidecomment == 1) {
if (c == '*') {
c = getchar();
if (c == '/') {
insidecomment = 0;
}
else if (c != EOF) {
c = getchar();
}
}
else {
c = getchar();
}
}
return c;
}
int parensmatch (char a, char b)
{
if ((a == '(' && b == ')') ||
(a == '[' && b == ']') ||
(a == '{' && b == '}'))
return 1;
else
return 0;
}