-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathparse.c
160 lines (142 loc) · 3.23 KB
/
parse.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
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include "tally.h"
#include <stdio.h>
//#include <stdlib.h>
void done();
struct { int begin, end; } bindings[100];
void bind(int b);
void bind_num(int b, int n);
void bind_end(int b);
void write_selected(char *buf, int len);
#define YY_DIE(args) fprintf(stderr, args); done(); abort();
#define YY_OUTPUT(args) write_selected args;
#define YY_noDEBUG 1
void YY_DOUBLE_BEGIN(char *rule) {push(rule);}
void YY_DOUBLE_END(char *rule, char *text) {pop(rule);}
void YY_TRIPLE_BEGIN(char *rule) {}
void YY_TRIPLE_END(char *rule, char *text) {
if (strncmp(rule, "begin_", 6) == 0 || strncmp(rule, "start_", 6) == 0) {
push(text);
} else if (strncmp(rule, "end_", 4) == 0 || strncmp(rule, "stop_", 5) == 0) {
pop(text);
} else {
pip(text);
}
}
#include "parse.leg.c"
#include "tally.c"
FILE *binding_file;
FILE *selected_file;
void bind(int b) {
bindings[b].begin = yythisthunk->mybegin;
bindings[b].end = yythisthunk->myend;
}
void bind_num(int b, int n) {
bindings[b].begin = -2;
bindings[b].end = n;
}
void bind_end(int b) {
bindings[b].end = yythisthunk->myend;
}
void unbind() {
for (int i=0; i<=99; i++) {
bindings[i].begin = -1;
bindings[i].end = -1;
}
}
void write_binding() {
if ((bindings[0].begin!=-1) && (bindings[1].begin!=-1)) {
int max;
for (max=99; max>1; max--) {
if (bindings[max].begin != -1) {
break;
}
}
for (int i=0; i<=max; i++) {
if (bindings[i].begin == -1) {
fprintf(binding_file, "\t");
} else if (bindings[i].begin == -2) {
fprintf(binding_file, "%d%s", bindings[i].end, i!=max ? "\t" : "\n");
} else {
yyText(bindings[i].begin, bindings[i].end); fprintf(binding_file, "%s%s", yytext, i!=max ? "\t" : "\n");
}
}
}
unbind();
}
void write_selected(char *buf, int len){
if (issel()) {
fwrite(buf, 1, len, selected_file);
desel();
}
}
int accepts = 0;
int depths[1000];
int maxdepth=0;
void *profile_thread(void *id) {
while (1) {
usleep(1000);
int depth = yythunkpos;
if (depth >= 1000) {
depth = 999;
}
depths[depth]++;
if (maxdepth < depth) {
maxdepth = depth;
}
}
}
void profile_start() {
for (int i=0; i<1000; i++) {
depths[i]=0;
}
pthread_t clock;
pthread_create(&clock, NULL, profile_thread, 0);
}
void progress () {
fflush(stdout);
FILE *fp = fopen("tally.txt", "w");
fprintf(fp, "%llu\n", yyaccepted+yypos);
fclose(fp);
fflush(stdout);
fp = fopen("profile.txt", "w");
for (int i=0; i<=maxdepth; i++) {
fprintf(fp, "%d ", depths[i]);
}
fprintf(fp, "\n");
fclose(fp);
}
void done() {
progress();
dot();
unlink("pid.txt");
fclose(binding_file);
fclose(selected_file);
}
void interrupt(int sig) {
fprintf(stderr, "parse terminated by interrupt\n");
done();
exit(-1);
}
int main() {
signal(SIGINT, interrupt);
binding_file = fopen("bindings.txt", "w");
selected_file = fopen("selected.txt", "w");
unbind();
profile_start();
FILE *fp = fopen("pid.txt", "w");
fprintf(fp, "%d\n", getpid());
fclose(fp);
while (yyparse()) {
write_binding();
if (!(++accepts % 1000)) {
progress();
dot();
}
}
done();
return 0;
}