I have a propositional formula, e.g., in this String format:
(~d \/ x) /\ (y \/ ~b) /\ (~y \/ a \/ b)
I wrote a parser like this:
import scala.util.parsing.combinator._
class CNFParser extends JavaTokenParsers with RegexParsers {
def expr: Parser[Any] = term~rep("/\\"~term)
def term: Parser[Any] = value~rep("\\/"~value)
def value: Parser[Any] = ident | "~"~ident | "("~expr~")"
}
object Test_02 extends CNFParser {
def main(args: Array[String]): Unit = {
println("input: " + "(~d \\/ x) /\\ (y \\/ ~b) /\\ (~y \\/ a \\/ b)")
println(parseAll(expr, "(~d \\/ x) /\\ (y \\/ ~b) /\\ (~y \\/ a \\/ b)"))
}
}
Well, the parsed output looks like:
[1.41] parsed: (((((~(((~~d)~List((\/~x)))~List()))~))~List())~List((/\~((((~((y~List((\/~(~~b))))~List()))~))~List())), (/\~((((~(((~~y)~List((\/~a), (\/~b)))~List()))~))~List()))))
I am trying several ways, by using the operations ^^, to get rid of these "extra" parentheses and stuff, but without success.
Actually, the result that I want to get is to convert the formula in a .dimacs format, where each letter/word is a number, the \/ operator becomes a space between the literals and the \/ becomes a newline (where a value 0 is inserted at the end of each line). Concretely, for my example here - if x = 1, y = 2, a = 3, b = 4, d = 5- then the resulting file must look like this:
c filename.cnf
p cnf 5 3
-5 1 0
2 -4 0
-2 3 4
Any hint how I can continue to achieve this is really welcomed! Thanks.