Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class AbstractBinaryOperation implements Expression {
- public Expression firstOperand;
- public Expression secondOperand;
- public AbstractBinaryOperation(Expression left, Expression right) {
- this.firstOperand = left;
- this.secondOperand = right;
- }
- protected abstract boolean calculate(boolean left, boolean right);
- public boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j) {
- boolean evaluatedFirstOperand = false, evaluatedSecondOperand = false;
- try {
- evaluatedFirstOperand = firstOperand.evaluate(a, b, c, d, e, f, g, h, i, j);
- evaluatedSecondOperand = secondOperand.evaluate(a, b, c, d, e, f, g, h, i, j);
- } catch (NullPointerException ex) {
- System.out.println("Parsing error!");
- System.exit(1);
- }
- return calculate(evaluatedFirstOperand, evaluatedSecondOperand);
- }
- }
- public abstract class AbstractUnaryOperation implements Expression {
- public Expression singleOperand;
- public AbstractUnaryOperation(Expression currentOperand) {
- this.singleOperand = currentOperand;
- }
- protected abstract boolean calculate(boolean operand);
- public boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j) {
- return calculate(singleOperand.evaluate(a, b, c, d, e, f, g, h, i, j));
- }
- }
- public class And extends AbstractBinaryOperation {
- public And(Expression left, Expression right) {
- super(left, right);
- }
- protected boolean calculate(boolean left, boolean right) {
- return left && right;
- }
- }
- public class Const implements Expression {
- private boolean value;
- public Const(boolean value) {
- this.value = value;
- }
- public boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j) {
- return value;
- }
- }
- public interface Expression {
- boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j);
- }
- public class ExpressionParser implements Parser {
- private String expression;
- private char name;
- private int currentIndex = 0;
- private boolean value = false;
- private int differenceInBrackets = 0;
- private enum Token {
- AND, CLOSE_BRACE, CONST, BEGIN, ERR, NOT, OPEN_BRACE, OR, VAR
- }
- private Token currentToken = Token.ERR;
- private void skipSpaces() {
- while (currentIndex < expression.length() && Character.isWhitespace(expression.charAt(currentIndex))) {
- ++currentIndex;
- }
- }
- private void nextToken() {
- skipSpaces();
- if (currentIndex >= expression.length()) {
- currentToken = Token.BEGIN;
- return;
- }
- char currentSymbol = expression.charAt(currentIndex);
- switch (expression.charAt(currentIndex)) {
- case '(':
- currentToken = Token.OPEN_BRACE;
- differenceInBrackets++;
- break;
- case ')':
- currentToken = Token.CLOSE_BRACE;
- differenceInBrackets--;
- break;
- case '&':
- currentToken = Token.AND;
- break;
- case '|':
- currentToken = Token.OR;
- break;
- case '~':
- currentToken = Token.NOT;
- break;
- default:
- if (Character.isDigit(currentSymbol)) {
- if (currentIndex + 1 < expression.length()) {
- if (Character.isDigit(expression.charAt(currentIndex + 1)) || Character.isLetter(expression.charAt(currentIndex + 1))) {
- System.out.println("Parsing error!");
- System.exit(1);
- }
- }
- if (currentSymbol == '0') {
- value = false;
- } else {
- value = true;
- }
- currentToken = Token.CONST;
- } else if (Character.isLetter(expression.charAt(currentIndex))) {
- if (currentIndex + 1 < expression.length()) {
- if (Character.isDigit(expression.charAt(currentIndex + 1)) || Character.isLetter(expression.charAt(currentIndex + 1))) {
- System.out.println("Parsing error!");
- System.exit(1);
- }
- }
- name = currentSymbol;
- currentToken = Token.VAR;
- } else {
- System.out.println("Parsing error!");
- System.exit(1);
- }
- }
- ++currentIndex;
- }
- private Expression unary() {
- nextToken();
- Expression res = null;
- switch (currentToken) {
- case CONST:
- res = new Const(value);
- nextToken();
- break;
- case VAR:
- res = new Variable(name);
- nextToken();
- break;
- case OPEN_BRACE:
- res = or();
- nextToken();
- break;
- case NOT:
- res = new Not(unary());
- break;
- }
- return res;
- }
- private Expression and() {
- Expression res = unary();
- while (true) {
- if (currentToken == Token.AND) {
- res = new And(res, and());
- } else {
- return res;
- }
- }
- }
- private Expression or() {
- Expression res = and();
- while (true) {
- if (currentToken == Token.OR) {
- res = new Or(res, and());
- } else {
- return res;
- }
- }
- }
- public Expression parse(final String expression) {
- assert expression != null : "Expression is null";
- currentIndex = 0;
- this.expression = expression;
- currentToken = Token.ERR;
- Expression result = or();
- if (differenceInBrackets != 0) {
- System.out.println("Parsing error!");
- System.exit(1);
- }
- return result;
- }
- }
- public class Not extends AbstractUnaryOperation {
- public Not(Expression operand) {
- super(operand);
- }
- @Override
- protected boolean calculate(boolean operand) {
- return !operand;
- }
- }
- public class Or extends AbstractBinaryOperation {
- public Or(Expression left, Expression right) {
- super(left, right);
- }
- @Override
- protected boolean calculate(boolean left, boolean right) {
- return left || right;
- }
- }
- public interface Parser {
- Expression parse(String expression);
- }
- public class Variable implements Expression {
- private char name;
- public Variable(char name) {
- this.name = name;
- }
- public boolean evaluate(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g, boolean h, boolean i, boolean j) {
- switch(name) {
- case 'a':
- return a;
- case 'b':
- return b;
- case 'c':
- return c;
- case 'd':
- return d;
- case 'e':
- return e;
- case 'f':
- return f;
- case 'g':
- return g;
- case 'h':
- return h;
- case 'i':
- return i;
- case 'j':
- return j;
- default:
- System.out.println("Parsing error!");
- System.exit(1);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement