-
Notifications
You must be signed in to change notification settings - Fork 2
/
peach.c
215 lines (177 loc) · 5.76 KB
/
peach.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* One argument: filename.
*
* Program checks file for missing brackets and interlocked brackets.
* Does not account for strings, e.g. "docstring 1)" is not ignored (todo).
* Maximum nesting depth is in constant MAXNEST.
*
* Because file is processed from beginning to ending, opening
* and closing brackets are handled asymmetrically. The stack
* of opening brackets can shrink and grow; the stack of closing
* brackets is really a pile, as a non-matching closing bracket
* will never be matched (going from top to bottom in the file).
*/
#define MAXNEST 1024
void tell_overflow (char c);
int f(char c);
char g(int i);
__inline__ int f(char c) {
switch(c) {
case '(': return 0;
case ')': return 1;
case '{': return 2;
case '}': return 3;
case '[': return 4;
case ']': return 5;
}
return 6;
}
__inline__ char g(int i){
switch(i) {
case 0: return '(';
case 1: return ')';
case 2: return '{';
case 3: return '}';
case 4: return '[';
case 5: return ']';
}
return '\0';
}
int
main(int argc, char *argv[])
{
FILE *testfile;
int c, i, j, k, lc, bc;
int linenumbers[6][MAXNEST]; /* open bracket stack + closed bracket dump */
int bytenumbers[6][MAXNEST]; /* open bracket stack + closed bracket dump */
int nr_open[6]; /* size of stacks */
int B_allmatch = 1; /* all brackets match */
int B_interlock = 0; /* presence of interlock, e.g. ([)] */
for (i=0;i<6;i++) {
linenumbers[i][0] = -1; /* perhaps not necessary */
bytenumbers[i][0] = -1;
nr_open[i] = 0;
}
if (argc == 1) {
testfile = stdin;
}
else if ((strcmp(argv[1], "-h"))==0) {
printf("Usage: pch FILENAME or |pch\n");
exit(0);
}
else if ((testfile = fopen(argv[1],"r")) == NULL) {
printf("File %s could not be opened\n", argv[1]);
exit(2);
}
lc = 1; /* line count */
bc = 0; /* byte count */
while ((c = fgetc(testfile)) != EOF) {
int brack = f(c);
if (c == '\n') {
lc++;
bc = 0;
continue;
}
bc++;
if (brack == 6) { /* Not A Bracket */
continue;
}
/* OPENING BRACKET */
if (brack % 2 == 0) {
if (nr_open[brack] == (MAXNEST)) {
tell_overflow(c);
break;
}
else {
linenumbers[brack][nr_open[brack] ] = lc;
bytenumbers[brack][nr_open[brack] ] = bc;
++nr_open[brack];
}
}
/* CLOSING BRACKET */
else if (brack % 2 == 1) {
/* there are open brackets of this
* type
*/
if (nr_open[brack-1] > 0) {
/* check for interlock, i.e. ([)] */
for (k=0;k<3;k++) {
int thatopen = 2*k;
int thisopen = brack-1;
if ( (nr_open[thatopen] == 0)
|| (thatopen == thisopen)
)
continue;
else {
int thisln = linenumbers[thisopen][nr_open[thisopen]-1];
int thatln = linenumbers[2*k][nr_open[thatopen]-1];
int thisbt = bytenumbers[thisopen][nr_open[thisopen]-1];
int thatbt = bytenumbers[2*k][nr_open[thatopen]-1];
if ( (thatln > thisln)
|| ((thatln == thisln) && (thatbt > thisbt))
)
{
B_interlock = 1;
if (lc == thatln) {
fprintf ( stdout
, "%d-%d,%d interlocked pair %c%c\n"
, lc
, thatbt
, bc
, g(2*k)
, c
) ;
}
else {
fprintf ( stdout
, "%d-%d, %d-%d interlocked pair %c%c\n"
, thatln
, thatbt
, lc
, bc
, g(2*k)
, c
) ;
}
}
}
}
--nr_open[brack-1];
}
/* closing bracket does not have opening */
else if (nr_open[brack-1] == 0) {
if (nr_open[brack] == (MAXNEST)) {
tell_overflow(c);
break;
}
else {
linenumbers[brack][nr_open[brack]] = lc;
bytenumbers[brack][nr_open[brack]] = bc;
++nr_open[brack];
}
}
else {}
}
}
fclose(testfile);
for (i=0;i<6;i++) {
if (nr_open[i] > 0) {
B_allmatch = 0;
for (j = 0; j < nr_open[i]; j++) {
printf("%d-%d ",linenumbers[i][j], bytenumbers[i][j]);
}
printf("unmatched %c\n",g(i));
}
}
if (B_allmatch && !B_interlock) {
printf("File is clear\n");
}
return 0;
}
void tell_overflow (char c) {
printf("Overflow on bracket %c\n", c);
printf("Nesting depth maximum equals %d\n", MAXNEST);
return;
}