-
Notifications
You must be signed in to change notification settings - Fork 0
/
DimacsParser.h
70 lines (61 loc) · 1.67 KB
/
DimacsParser.h
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
//
// Dimacs parser.
//
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cassert>
#include "common.h"
#ifndef DPLL_DIMACSPARSER_H
#define DPLL_DIMACSPARSER_H
class DimacsParser {
public:
/**
* Parse a dimacs file.
* @param file_name dimacs file name
* @return a parsed formula (if succeeds)
*/
static formula parse(const std::string &file_name) {
std::ifstream fin(file_name);
if (!fin) {
std::cerr << "file not found: " << file_name << "'" << std::endl;
std::exit(1);
}
int n = 0, m = 0;
while (!fin.eof()) {
char ch;
fin >> ch;
if (ch == 'c') { // c-line: comment
char buf[1024];
fin.getline(buf, 1024);
} else if (ch == 'p') { // p-line: clauses will begin
std::string buf;
fin >> buf;
assert(buf == "cnf");
fin >> n >> m;
break;
} else { // unexpected line
std::cerr << "parse error at char '" << ch << "'" << std::endl;
std::exit(1);
}
}
// clauses begin
std::vector<clause> clauses;
for (int i = 0; i < m; i++) {
clause c;
while (!fin.eof()) {
int v;
fin >> v;
if (v == 0) {
clauses.push_back(c);
break;
}
assert(VAR(v) <= n);
c.push_back(v);
}
}
assert(clauses.size() == m);
return formula(n, clauses);
}
};
#endif //DPLL_DIMACSPARSER_H