-1

I have Expression object which has the following:

  1. Operator
  2. parameter
  3. value

Each simple Expression mentioned can be combined into a compound expression.

public SimpleExpresssion createcompound(SimpleExpression simple1,SimpleExpression simple2)    
    {
        CompoundExpression ce = new CompoundExpression();
        ce.lhs(simple1);
        ce.rhs(simple2);
        ce.operator(AND);    
    }

A complex example would look like ((1AND2)OR(3OR4)) where 1,2,3,4 are Expression objects. I am looking for a logic to evaluate the expression based on the parenthesis preference in the expression. Note: CompoundExpression is a extended class of Expression so the final output is a Expression object. Is it solvable easily? If not what are my options

Amarendra Reddy
  • 243
  • 2
  • 17

2 Answers2

1

A complex expression ((1AND2)OR(3OR4)) can be rewritten to a prefix notation:

OR(AND(1,2), OR(3,4))

Therefore, the only thing you need is a constructor or a factory method of CompoundExpression with 3 parameters: an operator, left and right abstract expressions:

CompoundExpression(Operator o, Expression left, Expression right) {
    this.operator = operator;
    this.left = left;
    this.right = right;
}

Then, assuming that you have 4 simple expressions, building the resulting expression is simple:

Expression result = new CompoundExpression(OR, 
    new CompoundExpression(AND, simple1, simple2), 
    new CompoundExpression(OR, simple3, simple4)
);
pkalinow
  • 1,619
  • 1
  • 17
  • 43
-1
  1. Convert the Expression to equivalent postfix expression.
  2. Evaluate the postfix expression using a stack.
Amarendra Reddy
  • 243
  • 2
  • 17