I've written an expression parser which spits out a bunch of assembler instructions for x86, x64 and ARM.
To test it I've written a small app that generates random expressions, compiles them with GCC and compares the result with my code, so far so good.
Now I want to have my parser produce warnings similar to GCC.
I've noticed that with GCC 5.1.0
int a = 100 + 100 | 10;
GCC give a suggested parentheses warning around |
but
int b = 100 * 100 | 10;
GCC gives no warning.
but both addition and multiplication have higher precedence than bitwise OR, so why no warning on the int b = expression?
I'm very tired lol so may have overlooked something.
The ultimate answer can only come from the implementers, but I guess the reason is that there are other languages which have different preferences of those operators, and therefore users of those other languages may misinterpret the expression. For example in some Pascal dialects,
&
has the same precedence as*
and|
has the same precedence as+
, so an expression involving both+
and|
without parentheses in between may have a different interpretation (standard Pascal has no&
or|
, but the precedence ofand
andor
in standard Pascal follows the same rules). I guess that just like many languages copy the C operator precedence, others copy the Pascal one.