comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@merv.cs.nyu.edu (Robert Dewar)
Subject: Re: GNAT messages and the not operator (pitfall alert!)
Date: 1996/10/26
Date: 1996-10-26T00:00:00+00:00	[thread overview]
Message-ID: <dewar.846332907@merv> (raw)
In-Reply-To: 54snn6$8j5@newsbf02.news.aol.com


iJohn Herro says

"should be cluttered with unnecessary parentheses.  Everyone knows that
"**" comes before "*" and "/", which come before binary "+" and "-", and
everyone knows that the relational operators have a low precedence.  It's
also arguable that "everyone" knows that "and" comes before "or".  But I'd
say in almost every other case, unless the order of evaluation is very
obvious, use parentheses!"


Well I am never sure what "everyone knows", but if everyone using Ada knows
that "and" comes before "or", they are mistaken! The syntactic rules of
Ada REQUIRE parenthesization for mixed logical operators. I guess John
never made this argument in his own Ada code, which is why he was unaware
of this rule (which is an old Ada 83 rule).

As for always using parens, yes, everyone agrees with this rule (after
all there are very few levels of precendece in Ada anyway, John's rule
is more the kind of rule that is useful in a C environment than Ada
anyway). The trouble is that one of the other things that "everyone knows"
is that unary operators have higher precedence than binary operators, because
this is true in every other language than Ada as far as I know, and

    -a mod b

reads wrong naturally to almost everyone. It is a bad mistake in a language
to violate these rules that "everyone knows". In Ada it happened from
a kind of accidental by product of keeping the number of precedence
levels low. In fact it is VERY rarely a problem, because it is only mod
that has a problem (for every other binary operator, the minus distributes
in any case, so the precedence is unimportant), and taking mod of negative
numbers is rare, and using an explicit minus to negate the left operand
even rarer.

I well remember another case like this in SNOBOL4. In an attempt to 
duplicate normal mathematical notation more closely, where division
is represented by a low precedence horizontal line, Ralph Griswold
decided to make division have lower precedence than multiplication,
so that

    A*B / C*D

means what it looks like. Well I remember once spending quite a bit
of time puzzling over why SPITBOL was generating the "wrong code",
when in fact it was completely right, just following this unusual
rule.

I wonder if anyone ever suggested using spacing to wonder about
precedence (I am not suggesting it, it is not a good idea, however,
it gives me an idea, if a programmer uses spacing, as in the above
example of A*B / C*D in a manner that is clearly at odds with the
precedence, then perhaps a warning is a good idea?)






  reply	other threads:[~1996-10-26  0:00 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-10-24  0:00 GNAT messages and the not operator (pitfall alert!) Robert Dewar
1996-10-24  0:00 ` Robert Dewar
1996-10-25  0:00   ` whiting_ms@corning.com (Matt Whiting)
1996-10-26  0:00     ` David C. Hoos, Sr.
1996-10-27  0:00     ` Robert Dewar
1996-10-28  0:00       ` Matthew S. Whiting
1996-10-26  0:00   ` John Herro
1996-10-26  0:00     ` Robert Dewar [this message]
1996-10-29  0:00       ` John Herro
1996-10-29  0:00         ` Robert Dewar
1996-10-26  0:00     ` Matthew Heaney
1996-10-29  0:00       ` Robert Dewar
1996-10-30  0:00     ` Dr. Peter E. Obermayer
1996-10-30  0:00       ` John Herro
1996-10-30  0:00         ` Robert Dewar
1996-11-01  0:00         ` User Password expiration date extractor? Stein-Aksel Basma
1996-10-29  0:00   ` GNAT messages and the not operator (pitfall alert!) Norman H. Cohen
1996-10-29  0:00     ` whiting_ms@corning.com (Matt Whiting)
1996-10-30  0:00       ` Norman H. Cohen
1996-11-06  0:00         ` Richard A. O'Keefe
1996-11-18  0:00           ` Norman H. Cohen
1996-11-18  0:00             ` Robert Dewar
1996-10-30  0:00       ` David C. Hoos, Sr.
1996-10-30  0:00         ` whiting_ms@corning.com (Matt Whiting)
1996-10-30  0:00           ` Norman H. Cohen
1996-10-30  0:00       ` Robert Dewar
1996-11-05  0:00     ` Robert Dewar
1996-10-28  0:00 ` Cary Jamison
1996-10-29  0:00   ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox