This project has moved. For the latest updates, please go here.

Operator precedence with unary operations.

May 25, 2012 at 5:16 PM

I'm having problems getting my grammar to work with unary operations. For instance, if I enter "-5/2" I get -(5/2), not (-5)/2 which I would like to get. In this particular case the difference does not matter, however, I still think fixing this is important for the grammar.

However, it's also important that 1-5/2 get's parsed as 1-(5/2) and not (1-5)/2.

Currently I have the following operator's registered:

            RegisterOperators(1, "||");
            RegisterOperators(2, "&&");
            RegisterOperators(3, "|");
            RegisterOperators(4, "^");
            RegisterOperators(5, "&");
            RegisterOperators(6, "==", "!=");
            RegisterOperators(7, "<", ">", "<=", ">=", "is");
            RegisterOperators(8, "<<", ">>");
            RegisterOperators(9, "+", "-");
            RegisterOperators(10, "*", "/", "%");
            RegisterOperators(11, "!");
            RegisterOperators(-1, "?");
            RegisterOperators(-2, "=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=");
The not operator already works as I want it to, however, the "+" and "-" kind of needs to be where they are in order to make sure that the "*", "/" and "%" is interpreted correctly. How should I solve this? Btw, if you need more of the grammar just let me know, I just figured I didn't need to drop more code here than necessarily.

May 25, 2012 at 5:48 PM

I believe i've made a special feature for this case, "ImplyPrecedence" hint that you can put on Unary operator (actually your put it into Unary expression's Rule). Play with it.

May 25, 2012 at 6:00 PM

I've tried the following:

UnaryExpr.Rule = LUnOp + Expr;
UnaryExpr.AddHintToAll(ImplyPrecedenceHere(12, Associativity.Right));
with different values in the ImplyPrecedenceHere (both negative and positive, and using different Associativity), however I cannot seem to get it working nomatter what I pass it. Am I using it wrong?

May 25, 2012 at 7:50 PM

I figured it out:

UnaryExpr.Rule = ImplyPrecedenceHere(30) + LUnOp + Expr;