Topic: SHORT CIRCUIT EXPRESSIONS


Author: cflatter@nrao.edu (Chris Flatters)
Date: 28 Dec 1994 20:42:39 GMT
Raw View
In article A2096wk@toronto.can.ipguild.org,  sunil.puri@toronto.can.ipguild.org writes:
>I have been studying compiler theory and would like to know how a C++ compiler
>(or a C compiler for this matter) would evaluate an expression of the form
>(E1 || E2 && E3) since it (C/C++) employs the use of short-circuit expressions
>(according to the standard). Since the above expression also reads
>(E3 && (E1 || E2)), then would a C/C++ compiler be allowed to re-arrange the
>expression so has to avoid evaluating (E1 || E2) unneccessarily?

No. Firstly && has higher precedence than || (see the rules on page 388 of
the ARM) so that the fully paranthesised version of the expression would
be (E1 || (E2 && E3)).  Secondly, both && and || guarantee left-to-right
evaluation so E1 must be evaluated before E2 && E3 and E2 must be evaluated
before E3.

---
------------------------------------------------------------------------------
Chris Flatters      cflatter@nrao.edu
------------------------------------------------------------------------------
Well, our problem stems from the fact that we, basically, allow every planet
and moon-base this side of Alpha Centauri to make their own version of Spam.
       "Mystery Meat"
       Man... or Astroman?






Author: sunil.puri@toronto.can.ipguild.org
Date: Wed, 28 Dec 94 13:51:02
Raw View
I have been studying compiler theory and would like to know how a C++ compiler
(or a C compiler for this matter) would evaluate an expression of the form
(E1 || E2 && E3) since it (C/C++) employs the use of short-circuit expressions
(according to the standard). Since the above expression also reads
(E3 && (E1 || E2)), then would a C/C++ compiler be allowed to re-arrange the
expression so has to avoid evaluating (E1 || E2) unneccessarily?


Sunil Puri

sunil.puri@toronto.can.ipguild.org




Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 28 Dec 1994 13:53:09 -0600
Raw View
sunil.puri@toronto.can.ipguild.org writes:


>I have been studying compiler theory and would like to know how a C++ compiler
>(or a C compiler for this matter) would evaluate an expression of the form
>(E1 || E2 && E3) since it (C/C++) employs the use of short-circuit expressions
>(according to the standard). Since the above expression also reads
>(E3 && (E1 || E2)), then would a C/C++ compiler be allowed to re-arrange the
>expression so has to avoid evaluating (E1 || E2) unneccessarily?

First of all && has a higher precedence than ||, so your expression,
(E1 || E2 && E3) must be evaluated as (E1 || (E2 && E3)).

Second, unless you have overloaded one of the operators, C++ follows
the C language specification for the evalutation of expressions using
|| or &&.

To evaluate (E1 || E2 && E3)

1) Evaluate expression E1.

2) If E1 is true (normally non-zero), then the entire expression is
true. Expressions E2 and E3 MUST NOT be evaluated.

3) If we reach this point, then E1 is known to be false; Evaluate E2.

4) If E2 is false, then the entire expression is false. Expression E3
MUST NOT be evaluated.

5) At this point E2 is known to be true. Evaluate E3 and use its value.



I somehow feel that this discussion does not really belong in comp.std.c++.