home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18646 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  4.4 KB

  1. Path: sparky!uunet!olivea!spool.mu.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question to test general C knowledge
  5. Message-ID: <28061@dog.ee.lbl.gov>
  6. Date: 18 Dec 92 19:54:16 GMT
  7. References: <1992Dec10.214319.6692@leland.Stanford.EDU> <fjeske.0knf@amiganet.chi.il.us>
  8. Organization: Lawrence Berkeley Laboratory, Berkeley CA
  9. Lines: 89
  10. NNTP-Posting-Host: 128.3.112.15
  11.  
  12. In article <fjeske.0knf@amiganet.chi.il.us>
  13. fjeske@amiganet.chi.il.us (Felix Jeske) writes:
  14. >        i=++i;
  15. >should be broken up like when taking operator precedence into account ...
  16.  
  17. As Wayne Throop and others have pointed out, precedence and order of
  18. evaluation have little to do with each other.  Moreover, ANSI C does
  19. not even *have* precedence.  (It achieves the same goal by careful
  20. use of an explicit grammar.)
  21.  
  22. As an illustration, consider the sub-expression:
  23.  
  24.     /* assume x, y, z are some variables */
  25.  
  26.     x + y + z
  27.  
  28. This involves no precedence at all, but it does have an (unspecified)
  29. evaluation order.[1]  If this is not obvious, replace it with:
  30.  
  31.     f() + g() + h()
  32.  
  33. In this case the parentheses after f, g, and h bind more tightly than
  34. the additions, and it happens to be true that f, g, and h must all be
  35. called before the final sum can be computed---but a compiler is free to
  36. use, say:
  37.  
  38.     call f
  39.     temp1 <- result
  40.     call g
  41.     temp2 <- temp1 + result
  42.     call h
  43.     temp1 <- temp2 + result
  44.     /* result is now in temp1 */
  45.  
  46. Here the sum f() + g() has been computed *before* h() is called.
  47.  
  48. Now, as it happens, I have been unable to think of a way to write a
  49. compiler such that `i = ++i' would act as if it were `i += 2'---a truly
  50. dumb compiler will simply emit all preincrements before handling the
  51. rest of the expression, and a truly smart one will notice that i
  52. appears twice (and, one hopes, emit a warning) and only generate one
  53. increment.  But I have not considered VLIW machines---I am not
  54. familiar enough with compilation techniques for these---in which
  55. it would be possible to run two independent additions simultaneously.[2]
  56.  
  57. The only proposal I have seen that justifies `i = ++i' as being defined
  58. in ANSI C is (I think) Chris Volpe's, which hinges on the definition of
  59. `modify'.
  60.  
  61. Again, grammar-level concepts such as `precedence' and `associativity'
  62. are useful in determining a particular parse (a `tree' of operations,
  63. if you will); but once you have that, semantics take over, and semantics
  64. are a separate issue.  The problem with `i = ++i' lies in semantics,
  65. and the grammars involved in getting there are not important.
  66.  
  67. -----
  68. [1] Note that it has associativity, not precedence.  `Precedence' is a
  69.     thing humans and some computer grammars use to decide how to parse
  70.     ambiguous sentences (sentences with more than one valid meaning).
  71.     We take `a = b + c * d' to mean `a = b + (c * d)', and we can
  72.     instruct a computer to do so as well, but there are other ways to
  73.     accomplish this.  `Associativity' is a related concept in which
  74.     no operation is `more powerful' than another, but we decide to
  75.     evaluate right-to-left or left-to-right, or to reject the sentence
  76.     entirely.  Subtraction is considered left-associative because we
  77.     take `a = b - c - d' to mean `a = (b - c) - d' rather than
  78.     `a = b - (c - d)', but again, we can specify associativity to a
  79.     computer parser in several ways.  The ANSI C standard uses a
  80.     long-winded method; most people, and some grammars, prefer the
  81.     operator-precedence style as being easier to remember and carry
  82.     out.
  83.  
  84. [2] To stop old Al E. from spinning in his grave, `simultaneous' really
  85.     means `the times overlap':  A VLIW, or Very Long Instruction Word,
  86.     machine typically has several separate integer and floating point
  87.     ALUs, and a single instruction can start all of them on different
  88.     tasks.  A dumb VLIW compiler, if one existed, might hand the `++i'
  89.     job to one ALU and the `i = i + 1' job---derived from the rule
  90.     that `x = ++y' sets x to `old-y + 1'---to a second.  In this case
  91.     the two results would come out at about the same time and would
  92.     collide in trying to `land in' i.  If bits were eggs, one would
  93.     wind up with a mess of yolk on the floor.
  94.  
  95.     Of course, bits are not eggs---but if you program as if they were,
  96.     you will not break any bits, whatever they may be, and thus you
  97.     will never have to clean up the mess.
  98. -- 
  99. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  100. Berkeley, CA        Domain:    torek@ee.lbl.gov
  101.