Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Disregarding parentheses in C Message-ID: <6378@mimsy.UUCP> Date: Tue, 21-Apr-87 09:34:03 EST Article-I.D.: mimsy.6378 Posted: Tue Apr 21 09:34:03 1987 Date-Received: Wed, 22-Apr-87 02:39:39 EST References: <200@m10ux.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 65 Keywords: C parentheses evaluation order parsing In article <200@m10ux.UUCP> mnc@m10ux.UUCP (Michael Condict) writes some nice definitions that might possibly mislead someone, so I am adding this note. >Now finally, the main point: The source of this whole unpleasant business >is that C is defined as though its "+" and "*" operators were associative [and commutative] >(int or float, it doesn't matter to the discussion). >To summarize, C does not allow compilers to "disregard parentheses" >in general. It only allows the reparenthesization of the expression >in a manner that would be mathematically equivalent if the operators >were "ideal", i.e., perfectly accurate and never over- or underflowing. The summary is correct. I think, though, that a few more examples are in order, just to show what else can be done by C compilers: C also believes in distribution: (a * b) + (a * c) can become a * (b + c) (or vice versa!) or even (c + b) * a Multipliers and addends can be propagated madly: a + (((((i * 5) + j) * 6) + k) * /* sizeof (short): */ 2) is a likely candidate, having been constructed by the declaration and reference short a[4][5][6]; a[i][j][k] which might be transformed as a + (i * 60) + (j * 12) + (k * 2) = a + (i * 64) - (i * 4) + (j * 16) - (j * 4) + (k * 2) = a + (i * 64) + (j * 16) - (i + j) * 4 + (k * 2) = a + ((i * 4) + j) * 16 - (i + j) * 4 + (k * 2) which can be compiled using only shift and add/sub instructions (in case you are comping for, e.g., a processor with no integer multiply). (Merging the shifts, the last step, is useful if the processor does not have a barrel shifter.) According to K&R p. 185, Expressions involving a commutative and associative operator ( * , + , & , | , ^ ) may be rearranged arbitrarily, even in the presence of parentheses; to force a particular order of evaluation an explicit temporary must be used. I have no idea how the corresponding idea is phrased in the dpANS, but at least they provided *some* sort of escape (the dreaded unary plus). -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) UUCP: seismo!mimsy!chris ARPA/CSNet: chris@mimsy.umd.edu