Thu, 20 Jan 1994 10:17:39 GMT

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) |

Newsgroups: | comp.compilers |

From: | nebbe@lglsun.epfl.ch (Robb Nebbe) |

Keywords: | Ada, parse, design |

Organization: | Ecole Polytechnique Federale de Lausanne |

References: | 94-01-071 |

Date: | Thu, 20 Jan 1994 10:17:39 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?*

Here is an example that is "intuitive" and depends on the the precedence

of unary adding operators in Ada:

7 - 5 mod 3 <=> 7 - (5 mod 3) = 5

now if I eliminate the 7 I have

- 5 mod 3 <=> -( 5 mod 3) = -2

which just has the effect of subtracting 7. If unary minus had a higher

precedence than multiplying operators then the second case would have a

result of 2 which to me seems unintuitive.

A simple solution is to use rem instead of mod (rem is the equivalent of %

in C) which gives:

-5 rem 3 <=> -(5 rem 3) = (-5) rem 3 = -2

If unary adding operators had a higher precedence than multiplying

operators it would just switch when you get an "unintuitive" result making

the decision a bit arbitrary. Especially since it doesn't make any

difference for the other multiplying operators.

I think the major reason for having the same precedence for both binary

and unary adding operators is to increase readability : A + -B isn't

allowed because it was deemed more readable to write A - B or A + (-B).

The obvious parsing problem prevented by this is allowing a binary adding

operator followed by an unary adding operator which would make the case of

A--B ambiguous; is it A - (-B) or A followed by the comment "--B"?

Disclamer: I'm not sure which came first , using "--" to introduce

comments or not allowing a binary adding operator followed by a unary

adding operator.

For more information you should look at the Ada rationale or the language

reference manual (ftp ajpo.sei.cmu.edu in public/rationale and

public/lrm).

-Robb

--

Post a followup to this message

Return to the
comp.compilers page.

Search the
comp.compilers archives again.