1

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();
    } }
Community
  • 1
  • 1
Xiaofeng Xie
  • 145
  • 9
  • Show your work, how it failed, and what you think is wrong. – Ira Baxter Sep 19 '16 at 22:33
  • Some standard pattern is: Push all tokens on a stack until you encounter a closing parenthesis, then pop tokens until you encounter an opening parenthesis and process all the tokens you just popped. – Jorn Vernee Sep 19 '16 at 22:43
  • This is what I thinking but I feel like I fall into a trap that I cannot get out from it. – Xiaofeng Xie Sep 19 '16 at 22:58
  • I don't really know this library, but the first link you give shows an implementation of `AND`, that is incompatible with the way you implement the opening parenthesis. The binary operator, will look for the first expression it can find, but what if that's a `(` ? things will not work properly. I can't really see an easy solution right now. – Jorn Vernee Sep 19 '16 at 23:26
  • My code is almost identical to the code in the second link. Do you think I can add the parenthesis feature on top of his code? – Xiaofeng Xie Sep 19 '16 at 23:32
  • 1
    They trouble with all these non-standard *ad hoc* parsing techniques is that there is no proof that they are correct: i.e. that they handle exactly and only the desired grammar. I strongly suggest you throw it away and look into either recursive descent expression parsing or the Dijkstra Shunting-yard algorithm. Both of these are known to be correct solutions. – user207421 Sep 20 '16 at 00:09
  • @EJP thank you so much. I like your guidance. – Xiaofeng Xie Sep 20 '16 at 00:40

0 Answers0