🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3302

Re: Macro replacement

#3302
From: Larry Jones
Date: Mon, 25 Jan 1993 23:48
68 lines
3476 bytes
In article <1993Jan22.121437.10384@csqx.cs.rhbnc.ac.uk>, andreww@cscx.cs.rhbnc.ac.uk (Andrew Waters) writes:
> The example given in the standard (section 3.8.3.5) makes use of an ambiguity
> pointed out in the rational.

No, it doesn't.

> #define x	2
> #define f(a)	f(x * (a))
> #define g	f
> #define	t(a)	a
> 
> t(t(g)(0))
> 
> 1.  Examine the string and find the outermost macro name "t"
> 2.  Search for the argument "t(g)(0)"
> 3.  Expand the arguments - finding the inner macro name "t"
> 4.  Search for the argument "g"
> 5.  Expand the arguments - finding the macro name "g" which expands to "f"
> 6.  Rescan the replacement list of "g"  i.e. rescan "f".  There is a macro f
>     but it requires an open parenthesis which we do not have therefore we cannot
>     expand any further.

Correct so far, but note that f is *not* a nested instance of a macro
being replaced, so it is *not* marked as unavailable for further expansion.

> 7.  Rescan the replacement list of innermost "t" i.e. rescan "f".  This has
>     already been expanded fully.
> 8.  Rescan the replacement list of outermost "t" i.e. rescan "f(0)".  Now what
>     happens.  Clearly there is a macro call however "f" is a macro name which
>     has not been replaced and therefore is not available for further replacement.

But, as I noted above, it has not been marked unavailable, so it *is*
expanded, leading to the "f(2 * (0))" result shown in the example in
the standard.  The example given in the Rationale is quite different:

#define f(a)  a * g
#define g(a)  f(a)

f(2)(9)

1.  Examine the string and find the macro name "f" followed by "("
2.  Identify the argument "2"
3.  Expand the arguments - nothing to do
4.  Substitute arguments into replacement list "2 * g"
5.  Rescan the replacement list with the rest of the source file
6.  Find the macro name "g" followed by "("
7.  Identify the argument "9"
8.  Expand the arguments - again, nothing to do
9.  Substitute arguments into replacement list "f(9)"
10. Rescan the replacement list with the rest of the source file
11. Find the macro name "f" followed by "("

This is the ambiguity: is "f" at this point a nested replacement?

Since this "f" came from the expansion of "g" which in turn came from
the expansion of "f", you can certainly argue that it is and thus it
should not be replaced and it should be marked unavailable for further
replacement.  On the other hand, you can argue that as soon as you went
past the end of "g"'s expansion into the rest of the source file you are
no longer expanding "g" or "f" and thus this "f" is *not* a nested
replacement and should be replaced normally.  The Standard does not take
sides in this argument; either result is acceptable.
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070
larry.jones@sdrc.com
I always send Grandma a thank-you note right away.  ...Ever since she
sent me that empty box with the sarcastic note saying she was just
checking to see if the Postal Service was still working. -- Calvin
Technical Details
Message-ID: <560@heimdall.sdrc.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!psinntp!heimdall!thor!scjones
References: <SJA.93Jan16184647@lk-hp-2.hut.fi> <1ja816INNhlh@chnews.intel.com> <1993Jan22.121437.10384@csqx.cs.rhbnc.ac.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!psinntp!heimdall!thor!scjones
From: scjones@thor.sdrc.com (Larry Jones)
Newsgroups: comp.std.c
Subject: Re: Macro replacement
Message-ID: <560@heimdall.sdrc.com>
Date: 25 Jan 93 23:48:43 GMT
References: <SJA.93Jan16184647@lk-hp-2.hut.fi> <1ja816INNhlh@chnews.intel.com> <1993Jan22.121437.10384@csqx.cs.rhbnc.ac.uk>
Sender: news@heimdall.sdrc.com
Lines: 68