-
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 0d1efd4
Showing
2 changed files
with
85 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); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
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) { | ||
Clause clause = parseClause(lexer); | ||
clauses.add(clause); | ||
} | ||
|
||
return new ParseResult<>(new CNF(clauses), lexer.getCursor(), lexer.getRemaining().isEmpty()); | ||
} | ||
|
||
private Clause parseClause(Lexer lexer) { | ||
List<Literal> literals = new ArrayList<>(); | ||
|
||
while(lexer.hasNextToken() && !lexer.getLookahead().getValue().equals("0")) { | ||
Literal literal = parseLiteral(lexer); | ||
literals.add(literal); | ||
} | ||
|
||
lexer.require(INTEGER); | ||
int zero = Integer.parseInt(lexer.getLookahead().getValue()); | ||
lexer.consume(INTEGER); | ||
|
||
if (zero != 0) { | ||
throw new SyntaxError("Expected 0, got " + zero, lexer.getCursor()); | ||
} | ||
|
||
return new Clause(literals); | ||
} | ||
|
||
private Literal parseLiteral(Lexer lexer) { | ||
lexer.require(INTEGER); | ||
int literalId = Integer.parseInt(lexer.getLookahead().getValue()); | ||
lexer.consume(INTEGER); | ||
|
||
if (literalId >= 0) { | ||
return new Literal("l" + literalId); | ||
} else { | ||
return new Literal("l" + -literalId, true); | ||
} | ||
} | ||
} |