-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdimacs2boole.c
More file actions
170 lines (139 loc) · 3.1 KB
/
dimacs2boole.c
File metadata and controls
170 lines (139 loc) · 3.1 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
/*------------------------------------------------------------------------*\
Translate a CNF formula in DIMACS format into the boole format expected
by limboole. The parser is not really solid. For the reverse direction
use
sed -e '/&/d' -e 's,!,-,g' -e 's,[v(],,g' -e 's,), 0,g' -e 's, | , ,g'
In particular
./dimacs2boole | \
sed -e '/&/d' -e 's,!,-,g' -e 's,[v(],,g' -e 's,), 0,g' -e 's, | , ,g'
should be the identity.
\*------------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
/*------------------------------------------------------------------------*/
static void
die (const char *fmt, ...)
{
va_list ap;
fprintf (stderr, "*** dimacs2boole: ");
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fputc ('\n', stderr);
exit (1);
}
/*------------------------------------------------------------------------*/
static int
next (FILE * file, int *lineno_ptr)
{
int res;
res = fgetc (file);
if (res == '\n')
*lineno_ptr += 1;
return res;
}
/*------------------------------------------------------------------------*/
int
main (int argc, char **argv)
{
int count_literals_per_clause;
char *name = "<stdin>";
int count_clauses;
int close_file;
FILE *file;
int lineno;
int sign;
int idx;
int ch;
int i;
file = stdin;
close_file = 0;
for (i = 1; i < argc; i++)
{
if (!strcmp (argv[i], "-h"))
{
printf ("usage: dimacs2boole [-h][<file-name>]\n");
exit (0);
}
else if (close_file)
die ("can not open two files");
else if (!(file = fopen ((name = argv[i]), "r")))
die ("can not read '%s'", argv[i]);
else
close_file = 1;
}
count_literals_per_clause = 0;
count_clauses = 0;
lineno = 1;
for (;;)
{
do
{
ch = next (file, &lineno);
}
while (isspace (ch));
if (ch == EOF)
break;
if (ch == 'c' || ch == 'p')
{
do
{
ch = next (file, &lineno);
}
while (ch != '\n' && ch != EOF);
}
else
{
if (ch == '-')
{
sign = -1;
ch = next (file, &lineno);
}
else
sign = 1;
if (!isdigit (ch))
die ("syntax error:\n%s:%d: expected digit but found '%c'", name,
lineno, ch);
idx = 0;
do
{
idx = 10 * idx + (ch - '0');
ch = next (file, &lineno);
}
while (isdigit (ch));
if (idx)
{
if (count_literals_per_clause)
printf (" | ");
else
{
if (count_clauses)
printf ("&\n");
printf ("(");
}
if (sign == -1)
printf ("!");
printf ("v%d", idx);
count_literals_per_clause++;
}
else if (count_literals_per_clause)
{
printf (")\n");
count_literals_per_clause = 0;
count_clauses++;
}
else
die ("syntax error:\n%s:%d: can not handle empty clauses", name,
lineno);
}
}
if (count_literals_per_clause)
die ("syntax error:\n%s:%d: closing 0 missing", name, lineno);
if (!count_clauses)
die ("syntax error:\n%s:%d: no clauses found", name, lineno);
if (close_file)
fclose (file);
return 0;
}