forked from DanyaLitva/CFG_bench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
371 lines (300 loc) · 9.59 KB
/
Copy pathparser.c
File metadata and controls
371 lines (300 loc) · 9.59 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <LAGraph.h>
#include <LAGraphX.h>
#include <ctype.h>
#include <parser.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LEN 256
typedef struct {
char **symbols;
int count;
} SymbolList;
typedef struct {
int lhs;
int rhs1;
int rhs2;
int index;
} ProductionRule;
typedef struct {
int u;
int term_index;
int v;
} GraphEdge;
void init_symbol_list(SymbolList *list) {
list->symbols = NULL;
list->count = 0;
}
int add_symbol(SymbolList *list, const char *symbol) {
for (int i = 0; i < list->count; ++i) {
if (strcmp(list->symbols[i], symbol) == 0) {
return i;
}
}
list->symbols = realloc(list->symbols, sizeof(char *) * (list->count + 1));
list->symbols[list->count] = strdup(symbol);
return list->count++;
}
void free_symbol_list(SymbolList *list) {
for (int i = 0; i < list->count; ++i) {
free(list->symbols[i]);
}
free(list->symbols);
init_symbol_list(list);
}
bool is_non_terminal(const char *symbol, SymbolList non_terms) {
for (int i = 0; i < non_terms.count; ++i) {
if (strcmp(non_terms.symbols[i], symbol) == 0) {
return true;
}
}
return false;
}
void process_rule(const char *line, SymbolList *non_terms, SymbolList *terms,
ProductionRule *rules, int *rule_idx) {
char *arrow = strstr(line, "->");
if (!arrow)
return;
char lhs[MAX_LINE_LEN] = {0};
strncpy(lhs, line, arrow - line);
lhs[strcspn(lhs, " \t\n")] = '\0';
int lhs_id = add_symbol(non_terms, lhs);
char *rhs = arrow + 2;
while (*rhs == ' ' || *rhs == '\t')
rhs++;
rhs[strcspn(rhs, "\n")] = '\0';
ProductionRule rule = {lhs_id, -1, -1, 0};
if (strlen(rhs) == 0) {
rules[(*rule_idx)++] = rule;
return;
}
char *symbols[2] = {NULL, NULL};
int count = 0;
char *token = strtok(rhs, " ");
while (token && count < 2) {
symbols[count++] = token;
token = strtok(NULL, " ");
}
for (int i = 0; i < count; ++i) {
SymbolList *arr =
is_non_terminal(symbols[i], *non_terms) ? non_terms : terms;
int id = add_symbol(arr, symbols[i]);
if (i == 0)
rule.rhs1 = id;
else
rule.rhs2 = id;
}
rules[(*rule_idx)++] = rule;
}
bool is_number(const char *s) {
if (s == NULL || *s == '\0')
return false;
while (*s) {
if (!isdigit(*s))
return false;
s++;
}
return true;
}
char *read_entire_file(char *path) {
FILE *file = fopen(path, "rb");
if (file == NULL) {
fprintf(stderr,
"\x1B[31m[ERROR]\033[0m Problem with opening file by %s path\n",
path);
exit(-1);
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char *buffer = (char *)malloc(file_size + 1);
if (fread(buffer, 1, file_size, file) == 0) {
fprintf(stderr,
"\x1B[31m[ERROR]\033[0m Problem with reading file by %s path\n",
path);
exit(-1);
};
buffer[file_size] = '\0';
fclose(file);
return buffer;
}
void parser(char *config_i, grammar_t *grammar, GrB_Matrix **adj_matrices) {
char *config = strdup(config_i);
char *graph_buf = read_entire_file(strtok(config, ","));
char *grammar_buf = read_entire_file(strtok(NULL, ","));
SymbolList non_terms, terms;
init_symbol_list(&non_terms);
init_symbol_list(&terms);
ProductionRule *rules = NULL;
int rule_count = 0;
int capacity = 0;
int graph_line_count = 0;
char *line = grammar_buf;
char **grammar_lines = NULL;
int grammar_lines_count = 0;
int grammar_lines_capacity = 0;
bool first = true;
while (*line) {
char *end = strchr(line, '\n');
if (end)
*end = '\0';
if (first) {
add_symbol(&non_terms, line);
first = false;
line = end ? end + 1 : line + strlen(line);
continue;
}
if (strlen(line) < 3) {
line = end ? end + 1 : line + strlen(line);
continue;
}
if (grammar_lines_count >= grammar_lines_capacity) {
grammar_lines_capacity =
grammar_lines_capacity == 0 ? 1 : grammar_lines_capacity * 2;
grammar_lines =
realloc(grammar_lines, sizeof(char *) * grammar_lines_capacity);
}
grammar_lines[grammar_lines_count++] = line;
char *arrow = strstr(line, "->");
if (!arrow) {
line = end ? end + 1 : line + strlen(line);
continue;
}
char lhs[MAX_LINE_LEN] = {0};
strncpy(lhs, line, arrow - line);
lhs[strcspn(lhs, " \t\n")] = '\0';
int lhs_id = add_symbol(&non_terms, lhs);
line = end ? end + 1 : line + strlen(line);
}
for (int i = 0; i < grammar_lines_count; i++) {
char *line = grammar_lines[i];
if (strlen(line) < 3)
continue;
if (strstr(line, "->") != NULL) {
if (rule_count >= capacity) {
capacity = capacity == 0 ? 1 : capacity * 2;
rules = realloc(rules, sizeof(ProductionRule) * capacity);
}
process_rule(line, &non_terms, &terms, rules, &rule_count);
}
}
free(grammar_lines);
GraphEdge *edges = NULL;
int edge_count = 0;
int max_node = -1;
line = graph_buf;
while (*line) {
char *end = strchr(line, '\n');
if (end)
*end = '\0';
graph_line_count++;
char *saveptr;
char *u_str = strtok_r(line, " ", &saveptr);
char *a_str = strtok_r(NULL, " ", &saveptr);
char *v_str = strtok_r(NULL, " ", &saveptr);
char *rest = strtok_r(NULL, " ", &saveptr);
if (!u_str || !a_str || !v_str || rest != NULL) {
line = end ? end + 1 : line + strlen(line);
continue;
}
if (!is_number(u_str) || !is_number(v_str)) {
fprintf(stderr, "error: %s, %s", u_str, v_str);
exit(-1);
line = end ? end + 1 : line + strlen(line);
continue;
}
int u = atoi(u_str);
int v = atoi(v_str);
int term_index = -1;
for (int j = 0; j < terms.count; ++j) {
if (strcmp(terms.symbols[j], a_str) == 0) {
term_index = j;
break;
}
}
if (u > max_node)
max_node = u;
if (v > max_node)
max_node = v;
if (term_index == -1) {
line = end ? end + 1 : line + strlen(line);
continue;
}
edges = realloc(edges, (edge_count + 1) * sizeof(GraphEdge));
edges[edge_count++] = (GraphEdge){u, term_index, v};
line = end ? end + 1 : line + strlen(line);
}
LAGraph_rule_WCNF *rules_WCNF =
calloc(rule_count, sizeof(LAGraph_rule_WCNF));
for (int i = 0; i < rule_count; ++i) {
ProductionRule r = rules[i];
rules_WCNF[i] = (LAGraph_rule_WCNF){r.lhs, r.rhs1, r.rhs2, r.index};
}
*grammar = (grammar_t){.nonterms_count = non_terms.count,
.terms_count = terms.count,
.rules_count = rule_count,
.rules = rules_WCNF};
*adj_matrices = calloc(grammar->terms_count, sizeof(GrB_Matrix));
for (size_t i = 0; i < terms.count; i++) {
GrB_Matrix_new((*adj_matrices) + i, GrB_BOOL, max_node + 1,
max_node + 1);
}
GrB_Scalar true_scalar;
GrB_Scalar_new(&true_scalar, GrB_BOOL);
GrB_Scalar_setElement_BOOL(true_scalar, true);
GrB_Index *row = malloc(sizeof(GrB_Index) * edge_count);
GrB_Index *col = malloc(sizeof(GrB_Index) * edge_count);
for (size_t i = 0; i < terms.count; i++) {
int count = 0;
for (int j = 0; j < edge_count; ++j) {
if (i == edges[j].term_index) {
row[count] = edges[j].u;
col[count] = edges[j].v;
count++;
}
}
GxB_Matrix_build_Scalar((*adj_matrices)[i], row, col, true_scalar,
count);
#ifdef DEBUG_parser
GxB_print((*adj_matrices)[i], 1);
#endif
}
free(row);
free(col);
GrB_free(&true_scalar);
#ifdef DEBUG_parser
printf("Non-terminals:\n");
for (int i = 0; i < non_terms.count; ++i)
printf("%d: %s\n", i, non_terms.symbols[i]);
// printf("\nTerminals:\n");
// for (int i = 0; i < terms.count; ++i)
// printf("%d: %s\n", i, terms.symbols[i]);
// printf("\nProductions:\n");
// for (int i = 0; i < rule_count; ++i) {
// ProductionRule r = rules[i];
// printf("[%d, %d, %d, %d]\n", r.lhs, r.rhs1, r.rhs2, r.index);
// }
// printf("\nProductions (With names):\n");
// for (int i = 0; i < rule_count; ++i) {
// ProductionRule r = rules[i];
// printf("[%d] %s -> %s %s\n", r.index, non_terms.symbols[r.lhs],
// r.rhs1 == -1 ? ""
// : r.rhs2 == -1 ? terms.symbols[r.rhs1]
// : non_terms.symbols[r.rhs1],
// r.rhs2 == -1 ? "" : non_terms.symbols[r.rhs2]);
// }
printf("\nGraph Info:\n");
printf("Number of nodes: %d\n", max_node + 1);
printf("Edges: %d\n", edge_count);
printf("Graph lines: %d\n", graph_line_count);
#endif
free_symbol_list(&non_terms);
free_symbol_list(&terms);
free(rules);
free(edges);
free(graph_buf);
free(grammar_buf);
free(config);
return;
}