I was trying to create a parser based on Rovo's example (see below)
creating a simple rule engine in java.
The source code of Rovo's example: https://github.com/RovoMe/RuleBasedEngine/tree/master/src/main/java/at/rovo/test/rulebasedengine
When I tried to add parenthesis function(I tried to create a parenthesis operator that extends Operation) to change the order of performance of the expression. It did not work as what I intended. I would like to know if anyone could give me a logic about how to achieve this goal?
My method is to push the left parenthesis to the stack until the iterator reads the right parenthesis. At that point, I will pop the expression and the left parenthesis and evaluate it the expression. However ,it does not work as what I think. RightParenthesis
public class RightParenthesis extends Operation {
public RightParenthesis() {
super(")");
}
public RightParenthesis getOperator() {
return new RightParenthesis();
}
public int parse (String tokens[], int pos , Deque<Expression> deque) {
// Get expression in side the parenthesis
this.leftOperand =deque.pop();
deque.pop();
deque.push(this.leftOperand);
return pos+1;
}
public boolean interpret(Map<String,?> InputValueMap) {
return true;
} }
LeftParenthesis
public class LeftParenthesis extends Operation {
public LeftParenthesis() {
super("(");
}
public LeftParenthesis getOperator() {
return new LeftParenthesis();
}
public int parse (String[] tokens, int pos , Deque<Expression> deque) {
deque.push(this);
return pos+1;
}
public boolean interpret(Map<String,?> InputValueMap) {
return true;
}
public String toString() {
return this.getSymbol();
} }