10

Whats the meaning of

x AND THEN y AND z

is it

x AND THEN (y AND z)

(y, z gets never evaluated if x is FALSE) or

(x AND THEN y) AND z

(if x is FALSE, y is skipped, but its possible that z is evaluated) in ada?

  • 3
    GNAT will reject to compile the first example with error message "mixed logical operators in expression". – Rommudoh Jun 07 '11 at 06:32

4 Answers4

5

@oenone's comment mentions that GNAT rejects x AND THEN y AND z, but doesn't explain why. It's true, in a sense, that and and and then have the same precedence, but that's not the whole story.

The grammar for an expression is:

expression ::=
  relation {and relation}  | relation {and then relation}
  | relation {or relation} | relation {or else relation}
  | relation {xor relation}

where { FOO } denotes zero or more occurrences of FOO.

This grammar is specifically designed to allow any one of these operators or control forms to be chained in a single expression (X and Y and Z, A and then B and then C), but to forbid mixing them. So the expression in the question, x AND THEN y AND z, is illegal, and the question of what it means doesn't even arise. The point of this rule is precisely to avoid confusion in cases like this.

You just need to write (X and then Y) and Z or X and then (Y and Z), whichever one matches what you want to do.

The same applies to mixing and and or:

    X and Y and Z  -- legal
    X and Y or  Z  -- ILLEGAL
    (X and Y) or Z -- legal
    X and (Y or Z) -- legal
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
4

The short-circuit operators have the same precedence as their strict versions.

MRAB
  • 20,356
  • 6
  • 40
  • 33
  • 1
    [AARM §4.5(17.a)](http://www.adaic.org/resources/add_content/standards/05aarm/html/AA-4-5.html) notes that detailed precedence is "implicit in the syntax rules," in this case [§4.4 Expressions](http://www.adaic.org/resources/add_content/standards/05rm/html/RM-4-4.html). – trashgod Jun 06 '11 at 01:45
  • That doesn't quite answer the question, as "AND" and "AND THEN" have the same precedence and could arguably be evaluated at "the same time", or given that AND is commutative, in an arbitrary order. *Given* the same precedence, I think the rule is that operators to right are only evaluated after "... THEN" operations to their left have been evaluated, that have not deterined the outcome of the subterm of same precedence. – Ira Baxter Jun 06 '11 at 01:57
  • 1
    @Ira Baxter: I don't follow. [§4.5(8)](http://www.adaic.org/resources/add_content/standards/05rm/html/RM-4-5.html) goes on to say that for "operators of the same precedence level, the operators are associated with their operands in textual order from left to right." What else is needed? – trashgod Jun 06 '11 at 06:34
  • @trashgod: Poster said "equal precedence" answered OP's question. My observation was you needed some kind of left-to-right evaluation rule in addition; your 4.5(8) is apparantly chapter and verse. I characterized it somewhat more loosely; you want the compiler to be evaluate as many AND subterms in any order to produce the best code, but you have to evaluate the AND THEN terms from left to right. – Ira Baxter Jun 06 '11 at 07:19
  • @Ira Baxter: Ah, thanks for clarifying. I'd chased this down not long ago, and I remembered needing both sections. I think we're in agreement. – trashgod Jun 06 '11 at 15:00
3

As Mrab & Ira & trash have said they have equal pecedence, however what has not been pointed out explicitly is that the "and then" & "or else" operators will cause the expression to return (finish evaluation) as soon as a result can be determined.

For example(in pseudocode) :

if Almost_always_true_fn() or else costly_fn() then 
  do_stuff;
end if;

Most of the time only the first function (Almost_always_true_fn) will be evaluated, and only when that returns false will costly_fn be executed.

Compare this with :

if Almost_always_true_fn() or costly_fn() then 
  do_stuff;
end if;

In this case both Almost_always_true_fn() and costly_fn() will be evaluated.

NWS.

NWS
  • 3,080
  • 1
  • 19
  • 34
3

Yes, in (x AND THEN y) AND z, the relation z will always be evaluated.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045