burley@mole.gnu.ai.mit.edu (Craig Burley)Thu, 20 Jan 1994 17:45:25 GMT

From comp.compilers

Related articles
Peculiarity in Ada grammar clsi!graham@uunet.UU.NET (1994-01-19)
Re: Peculiarity in Ada grammar nebbe@lglsun.epfl.ch (1994-01-20)
Re: Peculiarity in Ada grammar burley@mole.gnu.ai.mit.edu (1994-01-20)
| List of all articles for this month |

 Newsgroups: comp.compilers From: burley@mole.gnu.ai.mit.edu (Craig Burley) Keywords: Ada, parse, design Organization: Free Software Foundation 545 Tech Square Cambridge, MA 02139 References: 94-01-071 Date: Thu, 20 Jan 1994 17:45:25 GMT

clsi!graham@uunet.UU.NET (Paul Graham) writes:

Ada has only one precedence for minus, at the cost of giving unary minus
lower precedence than multiplication operators. This has the unintuitive
effect of making
-5 mod 3 = -(5 mod 3)
while
-5 mod 3 /= (-5) mod 3

I wonder if Ada's expression grammar was designed with operator precedence
parsing in mind? Otherwise, how can the unintuitive precedence of unary
minus be explained?

Probably with one word: "FORTRAN".

In Fortran,

-5*3 = -(5*3)

Further, while FORTRAN 77 doesn't allow consecutive operators (i.e. an
expression like "5**-3" is nonconforming), some vendors that extend it to
do so end up doing things that also seem counterintuitive:

5**-3*5 = 5**(-(3*5))

The above mystery is because, while ** (exponentation) is the
highest-precedence operator of the set {**, -, *}, it cannot be evaluated
until the unary minus operator (-) is evaluated. But that cannot be
evaluated before the multiplication (*) operator is evaluated, since the
latter is higher precedence than the former.

So perhaps, like Fortran, the designers of Ada viewed unary minus as just
shorthand for binary minus with a left-hand operand of 0 (of the
appropriate type).
--
James Craig Burley, Software Craftsperson burley@gnu.ai.mit.edu
--

Post a followup to this message