This project has moved and is read-only. For the latest updates, please go here.

Operator precedence & braces

Sep 6, 2012 at 8:41 AM
Edited Sep 6, 2012 at 4:08 PM

Hi again!

I have a strange problem concerning operator precedence and braces. I do not want to post the grammar here, as it is quite complex. So I just describe my problem, maybe any of you guys had a similar problem and can point me in the right direction or give me some hint.

Consider the following lines of code:

var0 = not var1 + var2
var0 = not (var1 + var2)

"not" has highest precedence, "+" a much lower one. The point is, the first line works. It results in "var0 = (not var1) + var2", which is correct. The second line, however, leads to a parsing error at the plus-operator. If I change the precedence of "not" to be lower than that of "+", parsing works fine, but clearly expressions without braces would then be parsed incorrectly due to the wrong operator precedence.

It seems that at the plus operator, the parser checks the precedence, and as "not" has higher precedence than "+", it reduces var1, which is not correct because there is an opening brace beforehand.

I am thankful for any hint!

Sep 6, 2012 at 5:48 PM
Edited Sep 6, 2012 at 6:11 PM

that's really strange. I tried the following in expression evaluator:

 - ( 2 + 1)

and it parses ok and gives correct result -3 when executed.

I also added unary bool operator "!" to expr eval grammar with highest precedence, ran the expression

! ( true | false) 

and it works correctly, gives FALSE. Please try the same, see the difference with your grammar - something you may be missing


Sep 7, 2012 at 7:47 AM

OK, I will look into that example.

Thanks for the effort!