I'm writing a small parser, which will have an OR operator and an AND operator. When you see a series of ORs and ANDs, which do you expect will be more binding? Given the expression a & b | c
, do you expect it to mean (a&b)|c
or a&(b|c)
? Can you give any reason to prefer one over the other?
Do what everyone else does; AND binds tighter than OR (see e.g. C Operator Precedence Table). This is the convention that everyone expects, so adopt the principle of least surprise.
This choice isn't arbitrary. It stems from the fact that AND and OR follow a similar relationship to multiply and add, respectively; see e.g. http://en.wikipedia.org/wiki/Boolean_logic#Other_notations.
Note also that users of your language should be heavily encouraged to use parentheses to make their intentions clear to readers of their code. But that's up to them!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With