-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqubs.c
473 lines (402 loc) · 11.4 KB
/
qubs.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "hash.h"
#include "memory.h"
#include "bdd.h"
#define ID_VAL 0
#define ID_AND 1
#define ID_OR 2
#define ID_NOT 3
#define ID_EXISTS 4
#define ID_FORALL 5
#define ID_IMPLIES 6
#define ID_EQUIVALENT 7
typedef struct _string_t {
int identifier;
size_t len;
char * str;
struct string_t * left;
struct string_t * right;
} string_t;
static char const_zero[] = "0";
static char const_one[] = "1";
static char const_empty[] = "e";
#define ALLOW_ALL 0
#define ALLOW_VAL 1
static int input(string_t *t, size_t depth, const char *nodename, size_t arg,
int restriction);
static int build_bdd(const string_t *t, bdd_t *bdd, bdd_node_t *cur,
bdd_node_t *zerob, bdd_node_t *oneb, hash_t *fixedval);
static void destroy_input(string_t *s);
static void var_element_free(void *elem);
#define EVALMODE_SAT 1
#define EVALMODE_TRUTH 2
#define EVALMODE_CONTRADICTION 3
int evalmode = 0;
bdd_opfunc_t opfunc = NULL;
bdd_opfunc_t opfuncs[4] = { NULL, NULL, NULL, NULL };
int interactive = 0;
int verbose = 0;
int output = 0;
hash_t *var_exists;
list_t *var_list;
size_t line_number;
int
main(int argc, char *argv[])
{
string_t root;
bdd_t *bdd;
hash_t *v;
int help = 0;
int c = 0, result;
size_t size;
opfuncs[1] = bdd_sat;
opfuncs[2] = bdd_true;
opfuncs[3] = bdd_false;
do {
if ((c = getopt(argc, argv, "stcvio")) > 0)
switch (c) {
case 's':
if (evalmode == 0) evalmode = EVALMODE_SAT;
else help = 1;
break;
case 't':
if (evalmode == 0) evalmode = EVALMODE_TRUTH;
else help = 1;
break;
case 'c':
if (evalmode ==
0) evalmode = EVALMODE_CONTRADICTION;
else help = 1;
break;
case 'i':
interactive = 1;
break;
case 'v':
verbose++;
break;
case 'o':
output = 1;
break;
default:
help = 1;
break;
}
} while (c > 0);
if (help) {
printf("%s [ -s | -t | -c ]\n", argv[0]);
printf("\tEvaluates a QBF term in postfix notation.\n");
printf("\n\tParameters:\n");
printf("\t\t-s\tsatisfiability (default)\n");
printf("\t\t-t\ttautology\n");
printf("\t\t-c\tcontradiction\n");
printf("\t\t-i\tinteractive\n");
printf("\t\t-v\tverbose (can be specified multiple times)\n");
printf("\t\t\t1 = query written to terminal\n");
printf("\t\t\t2 = plus progress output\n");
printf("\t\t\t3 = plus optimized BDD output\n");
printf("\t\t\t4 = plus unoptimized BDD output\n");
printf("\t\t-o\toutput truth table\n");
printf("\n");
exit(-1);
}
if (evalmode == 0) evalmode = EVALMODE_SAT;
opfunc = opfuncs[evalmode];
mem_init;
if (verbose > 1) {
printf("Reading input... ");
fflush(stdout);
}
var_exists = hash_create(VAR_HASH_SIZE);
var_list = list_create();
line_number = 0;
if (input(&root, 0, "START", 0, ALLOW_ALL) < 0) {
fprintf(stderr, "Syntax error in line %lu.\n", line_number);
exit(-1);
}
hash_destroy(var_exists);
if (verbose > 1) {
printf("done.\nBuilding BDD... ");
fflush(stdout);
}
bdd = bdd_create();
bdd->start = bdd_init_node(bdd, NULL, &no, &yes);
v = hash_create(1024);
build_bdd(&root, bdd, bdd->start, &no, &yes, v);
hash_destroy(v);
destroy_input(&root);
if (verbose > 1) {
printf("done.\n");
if (verbose > 3) dump_bdd(bdd);
printf("Compacting BDD... ");
fflush(stdout);
}
bdd_compact(&bdd, verbose > 1);
if (verbose > 1) {
printf("done.\nCalculating result... ");
fflush(stdout);
}
result = opfunc(bdd->start);
if (verbose > 0) {
if (verbose > 1) printf("done.\n");
printf("%s: %s\n", (evalmode == 1) ? "satisfiable" :
(evalmode == 2) ? "tautology" : "contradiction",
result > 0 ? "yes" : "no");
if (verbose > 2) dump_bdd(bdd);
}
if (output) {
if (((evalmode == 2) && (result > 0)) || bdd_true(bdd->start))
printf("Everything matches.\n");
else
if (((evalmode == 3) && (result > 0)) ||
bdd_false(bdd->start))
printf("No matches.\n");
else
bdd_output_candidates(bdd, var_list);
}
if (verbose > 1) {
printf("Freeing memory... ");
fflush(stdout);
}
list_destroywithelements(var_list, var_element_free);
size = bdd_destroy(bdd);
if (verbose > 1) {
printf("done.\n");
printf("Size of optimized BDD: %lu\n", size);
}
mem_done;
exit(result);
}
int
input(string_t *t, size_t depth, const char *nodename, size_t arg,
int restriction)
{
size_t slen;
char buf[256];
#define check_input(l, r, b, p, a) if (input(l, r, b, p, a) < 0) return -1
do {
if (interactive)
printf("(depth: %02zu; %5s arg: %zu) ",
depth, nodename, arg);
if (fgets(buf, sizeof(buf), stdin) == NULL) {
fprintf(stderr, "Error in depth: %02zu in %s\n", depth,
nodename);
return -1;
}
line_number++;
slen = strlen(buf);
while (slen > 0 && buf[slen-1] <= ' ') {
buf[(slen--)-1] = 0;
}
if (buf[0] == '#') slen = 0;
if (slen <= 0) buf[0] = 0;
} while (slen <= 0);
memset(t, 0, sizeof(string_t));
if (strcmp(buf, "AND") == 0) {
t->identifier = ID_AND;
t->left = mem_malloc(sizeof(string_t));
t->right = mem_malloc(sizeof(string_t));
check_input((string_t *)t->left, depth+1, buf, 1, ALLOW_ALL);
check_input((string_t *)t->right, depth+1, buf, 2, ALLOW_ALL);
} else
if (strcmp(buf, "OR") == 0) {
t->identifier = ID_OR;
t->left = mem_malloc(sizeof(string_t));
t->right = mem_malloc(sizeof(string_t));
check_input((string_t *)t->left, depth+1, buf, 1, ALLOW_ALL);
check_input((string_t *)t->right, depth+1, buf, 2, ALLOW_ALL);
} else
if (strcmp(buf, "NOT") == 0) {
t->identifier = ID_NOT;
t->left = t->right = mem_malloc(sizeof(string_t));
check_input((string_t *)t->left, depth+1, buf, 1, ALLOW_ALL);
} else
if (strcmp(buf, "EXISTS") == 0) {
t->identifier = ID_EXISTS;
t->left = mem_malloc(sizeof(string_t));
t->right = mem_malloc(sizeof(string_t));
check_input((string_t *)t->left, depth+1, buf, 1, ALLOW_VAL);
check_input((string_t *)t->right, depth+1, buf, 2, ALLOW_ALL);
} else
if (strcmp(buf, "FORALL") == 0) {
t->identifier = ID_FORALL;
t->left = mem_malloc(sizeof(string_t));
t->right = mem_malloc(sizeof(string_t));
check_input((string_t *)t->left, depth+1, buf, 1, ALLOW_VAL);
check_input((string_t *)t->right, depth+1, buf, 2, ALLOW_ALL);
} else
if ((strcmp(buf, "IMPLIES") == 0) || (strcmp(buf, "IF") == 0)) {
t->identifier = ID_IMPLIES;
t->left = mem_malloc(sizeof(string_t));
t->right = mem_malloc(sizeof(string_t));
check_input((string_t *)t->left, depth+1, buf, 1, ALLOW_ALL);
check_input((string_t *)t->right, depth+1, buf, 2, ALLOW_ALL);
} else
if ((strcmp(buf, "EQUIVALENT") == 0) || (strcmp(buf, "IFF") == 0)) {
t->identifier = ID_EQUIVALENT;
t->left = mem_malloc(sizeof(string_t));
t->right = mem_malloc(sizeof(string_t));
check_input((string_t *)t->left, depth+1, buf, 1, ALLOW_ALL);
check_input((string_t *)t->right, depth+1, buf, 2, ALLOW_ALL);
} else {
t->identifier = ID_VAL;
t->str = mem_malloc(slen+1);
memcpy(t->str, buf, slen+1);
t->len = slen;
if (!hash_get(var_exists, t->str)) {
char *var;
hash_put(var_exists, t->str, "", 1);
var = mem_malloc(slen+1);
memcpy(var, buf, slen+1);
list_append(var_list, var);
}
}
if ((restriction == ALLOW_VAL) && (t->identifier != ID_VAL)) {
fprintf(stderr, "Syntax error: value expected.\n");
abort();
}
return 0;
}
static void
destroy_input(string_t *s)
{
if (s->left != NULL)
switch (s->identifier) {
case ID_NOT:
destroy_input((string_t *)s->left);
mem_free(s->left);
break;
default:
destroy_input((string_t *)s->left);
destroy_input((string_t *)s->right);
mem_free(s->left);
mem_free(s->right);
break;
}
else mem_free(s->str);
}
static int
build_bdd(const string_t *t, bdd_t *bdd, bdd_node_t *cur,
bdd_node_t *zerob, bdd_node_t *oneb, hash_t *fixedval)
{
bdd_node_t *n1, *n2;
char *hv;
string_t *s1;
if (t != NULL)
switch (t->identifier) {
case ID_VAL:
/* printf("cur: %s\n", t->str); */
hv = hash_get(fixedval, t->str);
/*printf("hv: %s\n", hv);*/
strlcpy(cur->var, t->str, sizeof(cur->var));
if ((hv == NULL) || (strcmp(hv, const_empty) == 0)) {
cur->iszero = zerob;
cur->isone = oneb;
} else {
if (strcmp(hv, const_one) == 0) {
cur->iszero = oneb;
cur->isone = oneb;
} else {
cur->iszero = zerob;
cur->isone = zerob;
}
}
break;
case ID_NOT:
if (build_bdd((string_t *)t->left, bdd, cur,
oneb, zerob, fixedval) < 0) return -1;
break;
case ID_OR:
n1 = bdd_init_node(bdd, cur, zerob, oneb);
if (n1 == NULL) return -1;
cur->iszero = n1;
if (build_bdd((string_t *)t->left, bdd, n1,
zerob, oneb, fixedval) < 0) return -1;
if (build_bdd((string_t *)t->right, bdd, cur,
n1, oneb, fixedval) < 0) return -1;
break;
case ID_AND:
n1 = bdd_init_node(bdd, cur, zerob, oneb);
if (n1 == NULL) return -1;
cur->isone = n1;
if (build_bdd((string_t *)t->right, bdd, n1,
zerob, oneb, fixedval) < 0) return -1;
if (build_bdd((string_t *)t->left, bdd, cur,
zerob, n1, fixedval) < 0) return -1;
break;
case ID_EXISTS:
s1 = (string_t *)t->left;
if (s1->identifier != ID_VAL) {
fprintf(stderr,
"Variable expected for EXISTS.\n");
abort();
}
hash_put(fixedval, s1->str, const_zero, 2);
n1 = bdd_init_node(bdd, cur, zerob, oneb);
if (n1 == NULL) return -1;
cur->iszero = n1;
/* XXX use bdd_merge */
if (build_bdd((string_t *)t->right, bdd, n1,
zerob, oneb, fixedval) < 0) return -1;
hash_put(fixedval, s1->str, const_one, 2);
/* XXX use bdd_merge */
if (build_bdd((string_t *)t->right, bdd, cur,
n1, oneb, fixedval) < 0) return -1;
hash_put(fixedval, s1->str, const_empty, 2);
break;
case ID_FORALL:
s1 = (string_t *)t->left;
if (s1->identifier != ID_VAL) {
fprintf(stderr,
"Variable expected for FORALL.\n");
abort();
}
hash_put(fixedval, s1->str, const_one, 2);
n1 = bdd_init_node(bdd, cur, zerob, oneb);
if (n1 == NULL) return -1;
cur->isone = n1;
/* XXX use bdd_merge */
if (build_bdd((string_t *)t->right, bdd, n1,
zerob, oneb, fixedval) < 0) return -1;
hash_put(fixedval, s1->str, const_zero, 2);
/* XXX use bdd_merge */
if (build_bdd((string_t *)t->right, bdd, cur,
zerob, n1, fixedval) < 0) return -1;
hash_put(fixedval, s1->str, const_empty, 2);
break;
case ID_IMPLIES:
n1 = bdd_init_node(bdd, cur, zerob, oneb);
if (n1 == NULL) return -1;
cur->iszero = n1;
if (build_bdd((string_t *)t->left, bdd, n1,
oneb, zerob, fixedval) < 0) return -1;
if (build_bdd((string_t *)t->right, bdd, cur,
n1, oneb, fixedval) < 0) return -1;
break;
case ID_EQUIVALENT:
n1 = bdd_init_node(bdd, cur, zerob, oneb);
if (n1 == NULL) return -1;
n2 = bdd_init_node(bdd, cur, zerob, oneb);
if (n2 == NULL) return -1;
cur->iszero = n1;
cur->isone = n2;
if (build_bdd((string_t *)t->right, bdd, n1,
oneb, zerob, fixedval) < 0) return -1;
if (build_bdd((string_t *)t->right, bdd, n2,
zerob, oneb, fixedval) < 0) return -1;
if (build_bdd((string_t *)t->left, bdd, cur,
n1, n2, fixedval) < 0) return -1;
break;
default:
return -1;
}
return 0;
}
static void
var_element_free(void *elem)
{
mem_free(elem);
}