Problem with #define'd macro...

Steve Emmerson steve at groucho.ucar.edu
Tue Apr 2 14:11:47 AEST 1991


In <1099 at gtenmc.UUCP> ravim at gtenmc.UUCP (Vox Populi) writes:

>In article <1991Mar20.150301.9941 at evax.arl.utexas.edu> c145gmk at utarlg.uta.edu
writes:
>>Attempting to compile the following (fragment, but displays the problem)
>>results in an 'invalid target for assignment' error on our VAX running

>>#define DIV_MOD(d,r,x,y) ( y == 0.0 ? d = 0.0, r = x : d = x/y, r = x - d*y )

>>a = 5, b = 2; DIV_MOD(quotient,remainder,a,b);

>I think the compiler error that you were getting is because, operator
>precedence is not observed here correctly.

I believe the above statement is false because 

	1) the string in question (which is an expression):

		a == b ? c=d, e=f : g=h, i=j

	   can be generated from the K&R1 grammar; and

	2) precedence is irrelevant in determining whether or not a string
	   is valid.

The expression-string can be generated from the following sequence of 
production rules (Appendix A, K&R1):

    exp ? exp : exp
    exp binop exp ? exp , exp : exp , exp
    primary == primary ? lvalue asgnop exp , lvalue asgnop exp : 
	                 lvalue asgnop exp , lvalue asgnop exp
    identifier == identifier ? identifier = primary, identifier = primary :
			       identifier = primary, identifier = primary
    a == b ? c=identifier, e=identifier :
	     g=identifier, i=identifier
    a == b ? c=d, e=f : g=h, i=j

Precedence, on the other hand, is a tool used by parsers to
disambiguate between two or more ways of generating the same string
from a given grammar.  The validity of a given string, however, is
determined solely on the basis of whether or not it can be generated by
the production rules of the grammar.  Since the above string can, then
it's valid under the K&R1 grammar.

This might seem counter intuitive and, indeed, Standard C has closed
this `loophole' by redefining the production rules in such a manner
that the above string cannot be generated.

Steve Emmerson        steve at unidata.ucar.edu        ...!ncar!unidata!steve



More information about the Comp.lang.c mailing list