C sintax

daniel edelson daniel at sco.COM
Tue Feb 9 08:32:17 AEST 1988


In article <4083 at june.cs.washington.edu< pardo at uw-june.UUCP (David Keppel) writes:
<
...
<<< 	;-D on  (My favorite sintax: switch(x+=*a+++*b){...})  Pardo
<<
<<What, precisely, does x+=*a+++*b mean?
<
<Well, actually it is ambiguous.  There are 2 (or more?) ways to parse
<it.  I'm not sure the behavior is defined from lexer to lexer as to
<which one is preferred.  Here are some possible interpretations:
<
<    tmp = *a++ + *b;	/* means add *a to *b and increment a */
<    x += tmp;
<    switch (x) {...}
<
<the (an) other is
<
<    tmp = *a + ++*b;	/* means increment *b and add that to *a */
<    x += tmp;
<    switch (x) {...}
<
...
<		    ;-D on  (What point?)  Pardo

It is ambiguous in that there are two valid expressions that
could look taht way. But correct compilers would only give
one of those twwo parsings. There is a little-used rule 
in K&R that says that when forming tokens, you ust the longest
string that could be a token. Thus, the expression a+++b
would be (correctly) parsed as a++ + b. Parsing it
as a + ++b would violate the longest-token rule.
Things becomes even more obfiscurated in ANSI C with the 
introduction of unary plus.
-- 
uucp:   {uunet|decvax!microsoft|ucbvax!ucscc|ihnp4|amd}!sco!daniel
ARPA:   daniel at sco.COM     Inter:  daniel at saturn.ucsc.edu--------------
pingpong: a dink to the right side with lots of top spin | Disclaimed |
fishing:  flies in morning and evening, day - spinners   | as usual   |



More information about the Comp.lang.c mailing list