-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsing.py
37 lines (27 loc) · 941 Bytes
/
parsing.py
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
"""Module for parsing first-order logic CNF formulas"""
from .base import Atom, Clause, Literal
import re
class DimacsParser:
"""A parser for parsing DIMACS-formatted list of clauses"""
def __init__(self):
pass
def parse(self, text):
"""Parse the specified DIMACS-formatted file into clauses"""
pattern = r"(-?\d+)"
clauses = list()
clause = list()
atoms = dict()
for match in re.findall(pattern, text):
result = int(match)
if result == 0:
clause = Clause(clause)
clauses.append(clause)
clause = list()
else:
name = str(abs(result))
pos = result > 0
if name not in atoms:
atoms[name] = Atom(name)
literal = Literal(atoms[name], pos)
clause.append(literal)
return clauses