Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!rochester!bullwinkle!uw-beaver!uw-june!entropy!dataio!bright From: bright@dataioDataio.UUCP (Walter Bright) Newsgroups: net.lang.c Subject: Re: unary + Message-ID: <929@dataioDataio.UUCP> Date: Tue, 11-Mar-86 13:11:43 EST Article-I.D.: dataioDa.929 Posted: Tue Mar 11 13:11:43 1986 Date-Received: Sat, 15-Mar-86 22:51:09 EST Reply-To: bright@dataio.UUCP (Walter Bright Distribution: net Organization: Data I/O Corp., Redmond WA Lines: 48 In article <1227@mtx5a.UUCP> esg@mtx5a.UUCP (ed gokhman) writes: >An expression a + (b + c) may be calculated by some >implementations as (a + b) + c. To enforce the >intended precedence of operations one should use >a + +(b + c). > >Question: what is the rational for not saying that an > implementation *must* respect parenthesising > as intended rather then providing an extra > operator simply to state "i mean these parenthesis" ? One Answer: Compilers frequently parse the expressions into an internal form which is a binary tree. a+(b+c) would look like: + / \ a + / \ b c Where are the parentheses? They're gone, they are only used in guiding the parser as it builds the tree. Thus, the optimizer doesn't know where the parentheses were. Of course, this info could be kludged onto the tree, but if the unary + operator is used: + / \ a U+ | + / \ b c Note that the U+ operator breaks up the n-ary tree of binary + operators, thus it is easy for the optimizer to not rearrange across U+ operators. Adding another operator is an easy retrofit to most compilers, far easier than kludging in parenthesis information. Languages that do not allow an optimizer to rearrange trees within certain limits cause slow code to be generated. I take advantage of the compilers I use by knowing what rearrangements they do, so that I can write code that is more aesthetically pleasing, knowing that the optimizer will rearrange it into a faster sequence. For example, a+5+6 is turned into the tree (a+5)+6. If the compiler couldn't rearrange trees, it couldn't produce the 'optimized' a+11 tree.