Thread View: comp.lang.c.moderated
1 messages
1 total messages
Started by Chris Torek
Wed, 27 Mar 1996 12:34
Re: Explaining precedence vs. sequence
Author: Chris Torek
Date: Wed, 27 Mar 1996 12:34
Date: Wed, 27 Mar 1996 12:34
82 lines
3691 bytes
3691 bytes
In article <4ja3pu$p8r@solutions.solon.com> bcalbridge@dcccd.edu writes: >... I'm not positive on this, but I suspect that a logical operator >imposes a sequence point that forces the evaluation of the left side. >(Please confirm if this is true.) This is correct. To refer back to the subject line, probably the best way to explain the difference between `precedence' and `order of evaluation or execution' is to point out that, technically, C has *no* precedence. The grammar specified in the Standard is fully factored, so that given any expression, there is only one way to parse it. Typically, humans (and operator-precedence grammars, which mimic human parsing) use `precedence' to distinguish between two or more possible ways to group a set of items. For instance: a + b * c - d / e There are lots of possible ways to group this, and the easy way for most *people* to work out which grouping to use is for them to remember grade-school lessons that say `multiplication and division first, then addition and subtraction'. The Standard, on the other hand, simply says (in part) that an expression can include the following: multiplicative-expression: cast-expression multiplicative-expression * cast-expression multiplicative-expression / cast-expression multiplicative-expression % cast-expression additive-expression: multiplicative-expression additive-expression + multiplicative-expression additive-expression - multiplicative-expression In this case, `a', `b', `c', `d', and `e' can all be handled via `cast-expression', but to put together `b * c' and `d / e' requires using the multiplicative-expression derivations, and to put together `a + ...' and `... - e' requires using the additive-expression derivations. The resulting parse groups `b * c' and `d / e' together before allowing `a + ...' or `... - e'. Moreover, since an additive- expression can only have a multiplicative-expression on the right, the addition must be grouped before the subtraction -- the multiplicative-expression simply cannot consist of two things being subtracted. The resulting grouping matches what you would get for: (a + (b * c)) - (d / e) but the ideas of `precedence' and `associativity' have not been invoked. This is likely to leave some lingering confusion; at this point, one can bring up another example: extern int f(), g(); f() + g(); Few, if any, students will argue that there is any precedence involved here: we simply call f() and g() and add the results. If, however, f() and g() contain printf() calls or similar, it becomes obvious that evaluation order can affect the output. Thus, here we have an example of an expression with ordering, but no precedence. (The order is `unspecified', meaning that either f() runs first, then g() runs, or g() runs first, then f(), but we cannot say which.) >I wrote a short program to prove this and the instructor demoed it in >class. Unfortunately, I don't believe the instructor is familiar with >sequence points ... Further details on sequence points (and on the above) can be found in the comp.lang.c FAQ. -- In-Real-Life: Chris Torek, Berkeley Software Design Inc El Cerrito, CA Domain: torek@bsdi.com +1 510 234 3167
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads