ambiguous why?

Wayne Mesard mesard at bbn.com
Wed Apr 6 01:31:31 AEST 1988


>From article <1303 at PT.CS.CMU.EDU>, by edw at IUS1.CS.CMU.EDU (Eddie Wyatt):
> I got an
> error message that said something to the extent:
> 
> 	warning ambiguous assigment: assignment op taken
> 	syntax error at or near symbol *=
> 
> A simplified version of the statement is:
> 
> 	int *a, *b;
> 
> 	*a+=*b;
> 
> I thought that this should not be ambiguous since the lexer scans left to right.

K&R A.17 talks about earlier versions of C which used the form "=op"
instead of "op=".  Most(?) compilers still try to detect the old form
and issue a warning.  In my opinion this causes headaches more than it
catches obsolete code.  Anyway, the compiler wasn't sure if you wanted
"addition assignment" (+=) or the old multiplication assignment (=*) so
it complained.  Putting a space after the "=" fixes it:

	int *a, *b;

	*a+= *b;

I usually run into this in the first expression of a for statement:

	for (a=-1; ++a < N;)
	   ...

Which serves me right, since readability and good taste dictate that I
should have spaces around my assignment ops anyway.

Now here's one for you puritans and compiler whiz-kids:

K&R 7.14 says "The two parts of a compound assignment operator are
separate tokens."  Doesn't this mean that the spaces (absense or
presense of) shouldn't matter?  Indeed they are separate tokens since
the following code works:

	a  +  =  b;

-- 
unsigned *Wayne_Mesard();                     MESARD at BBN.COM
                                              BBN Labs, Cambridge, MA
After all, Sodom wasn't built in a day.



More information about the Comp.lang.c mailing list