mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-27 15:03:21 -04:00
expression parser allows to omit the and operator
This commit is contained in:
parent
c4066a1e8f
commit
fd878a4f7b
@ -83,11 +83,19 @@ public class Parser {
|
||||
|
||||
private Expression parseAnd() throws IOException, ParseException {
|
||||
Expression ex = parseSimpleExp();
|
||||
while (tokenizer.peek() == AND) {
|
||||
tokenizer.consume();
|
||||
ex = Operation.and(ex, parseSimpleExp());
|
||||
while (true) {
|
||||
if (tokenizer.peek() == AND) {
|
||||
tokenizer.consume();
|
||||
ex = Operation.and(ex, parseSimpleExp());
|
||||
} else if (isSimpleEx(tokenizer.peek())) {
|
||||
ex = Operation.and(ex, parseSimpleExp());
|
||||
} else
|
||||
return ex;
|
||||
}
|
||||
return ex;
|
||||
}
|
||||
|
||||
private boolean isSimpleEx(Tokenizer.Token tok) {
|
||||
return tok == NOT || tok == OPEN || tok == IDENT || tok == ONE || tok == ZERO;
|
||||
}
|
||||
|
||||
private Expression parseSimpleExp() throws IOException, ParseException {
|
||||
|
@ -47,6 +47,7 @@ public class ParserTest extends TestCase {
|
||||
assertTrue(createSingle("a∧b") instanceof Operation.And);
|
||||
assertTrue(createSingle("a&b") instanceof Operation.And);
|
||||
assertTrue(createSingle("a&&b") instanceof Operation.And);
|
||||
assertTrue(createSingle("a b") instanceof Operation.And);
|
||||
}
|
||||
|
||||
public void testParseParenthesis() throws Exception {
|
||||
@ -100,6 +101,16 @@ public class ParserTest extends TestCase {
|
||||
assertEquals(new Variable("B"), simplified);
|
||||
}
|
||||
|
||||
public void testParseRegressionOmitAnd() throws Exception {
|
||||
Expression e = createSingle("B(B+A)(B+C)(A+B+C)");
|
||||
Expression simplified = QuineMcCluskey.simplify(e);
|
||||
assertEquals(new Variable("B"), simplified);
|
||||
|
||||
e = createSingle("B (B+A) (B+C) (A+B+C)");
|
||||
simplified = QuineMcCluskey.simplify(e);
|
||||
assertEquals(new Variable("B"), simplified);
|
||||
}
|
||||
|
||||
public void testParseRegression2() throws Exception {
|
||||
Expression e = createSingle("(C ∨ B) ∧ (A ∨ C) ∧ (B ∨ ¬C) ∧ (C ∨ ¬A)");
|
||||
Expression simplified = QuineMcCluskey.simplify(e);
|
||||
@ -110,6 +121,16 @@ public class ParserTest extends TestCase {
|
||||
assertEquals(new Variable("C"), expList.get(1));
|
||||
}
|
||||
|
||||
public void testParseRegression2OmitAnd() throws Exception {
|
||||
Expression e = createSingle("(C ∨ B) (A ∨ C) (B ∨ ¬C) (C ∨ ¬A)");
|
||||
Expression simplified = QuineMcCluskey.simplify(e);
|
||||
assertTrue(simplified instanceof Operation.And);
|
||||
ArrayList<Expression> expList = ((Operation) simplified).getExpressions();
|
||||
assertEquals(2, expList.size());
|
||||
assertEquals(new Variable("B"), expList.get(0));
|
||||
assertEquals(new Variable("C"), expList.get(1));
|
||||
}
|
||||
|
||||
public void testParseException() throws Exception {
|
||||
Parser p = new Parser("C+");
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user