-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ec4bfb
commit f1348e1
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/main/java/me/paultristanwagner/satchecking/parse/DimacsLexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package me.paultristanwagner.satchecking.parse; | ||
|
||
import static me.paultristanwagner.satchecking.parse.TokenType.IDENTIFIER; | ||
import static me.paultristanwagner.satchecking.parse.TokenType.INTEGER; | ||
|
||
public class DimacsLexer extends Lexer { | ||
|
||
public DimacsLexer(String input) { | ||
super(input); | ||
|
||
registerTokenTypes(IDENTIFIER, INTEGER); | ||
|
||
initialize(input); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/me/paultristanwagner/satchecking/parse/DimacsParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package me.paultristanwagner.satchecking.parse; | ||
|
||
import me.paultristanwagner.satchecking.sat.CNF; | ||
import me.paultristanwagner.satchecking.sat.Clause; | ||
import me.paultristanwagner.satchecking.sat.Literal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static me.paultristanwagner.satchecking.parse.TokenType.IDENTIFIER; | ||
import static me.paultristanwagner.satchecking.parse.TokenType.INTEGER; | ||
|
||
public class DimacsParser implements Parser<CNF> { | ||
|
||
@Override | ||
public ParseResult<CNF> parseWithRemaining(String string) { | ||
Lexer lexer = new DimacsLexer(string); | ||
|
||
lexer.consume(IDENTIFIER); | ||
|
||
lexer.consume(IDENTIFIER); | ||
|
||
lexer.require(INTEGER); | ||
int variableCount = Integer.parseInt(lexer.getLookahead().getValue()); | ||
lexer.consume(INTEGER); | ||
|
||
lexer.require(INTEGER); | ||
int clauseCount = Integer.parseInt(lexer.getLookahead().getValue()); | ||
lexer.consume(INTEGER); | ||
|
||
List<Clause> clauses = new ArrayList<>(); | ||
for (int i = 0; i < clauseCount; ++i) { | ||
List<Literal> literals = new ArrayList<>(); | ||
|
||
while (lexer.hasNextToken()) { | ||
lexer.require(INTEGER); | ||
int literalId = Integer.parseInt(lexer.getLookahead().getValue()); | ||
lexer.consume(INTEGER); | ||
|
||
if (literalId == 0) { | ||
break; | ||
} | ||
|
||
Literal literal; | ||
if (literalId >= 0) { | ||
literal = new Literal("l" + literalId); | ||
} else { | ||
literal = new Literal("l" + -literalId, true); | ||
} | ||
|
||
literals.add(literal); | ||
} | ||
|
||
clauses.add(new Clause(literals)); | ||
} | ||
|
||
return new ParseResult<>(new CNF(clauses), lexer.getCursor(), lexer.getRemaining().isEmpty()); | ||
} | ||
} |