Sujet : Re: Operator precedence
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.awkDate : 25. May 2024, 22:31:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v2tlcc$320fr$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 24.05.2024 06:32, Kaz Kylheku wrote:
On 2024-05-24, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
After pondering a bit more I think this is my personal resume...
>
On 24.05.2024 01:53, Janis Papanagnou wrote:
[...]
Moreover, what seams reasonable (to me) is that _unary_ operators
should (typically/always?) bind tighter than _binary_ operators!
>
An example may make things probably more apparent; in the expression
>
5 * -3 ^ 2
>
it would never occur to me to not consider the "-3" as an entity.
Because of the typical inherently tight coupling of unary operators.
>
(Assuming an interpretation of
>
5 * -(3 ^ 2)
>
would look just wrong to me.)
That interpretation is required if - 3 ^ 2 is to correspond to:
2
-3
Indeed the ^ circumflex is intended to mean "we don't have support
for superscripts in this notation, but pretend that the 2 is raised up".
(In TeX/LaTeX, ^ is used for marking up superscripts: x^y.)
Now in the C grammar, we have multiplication also occuping a
precedence rung between unary plus/minus and additive expressions.
Thus -A*B means (-A)*B, whereas X - A*B means X-(A*B).
In this case it doesn't matter because the unary minus is
a kind of product. -A can be regarded as a shorthand for (-1)*A.
More importantly the identity (-A)*B = -(A*B) holds.
So far so good, only it doesn't (IMO) provide a rationale for
exponentiation.
This creates a problem if we naively wedge exponentiation into
the grammar, by sticking it into a precedence level above
multiplication, but below unary.
The identity does not hold in exponentiation: (-A)**B
is not -(A**B).
Looks like a non-sequitur to me.
Janis