Re: C Preprocessor - macro expansion

davidm@questor.rational.com (David Moore)
Mon, 24 May 1993 21:59:09 GMT

          From comp.compilers

Related articles
C Preprocessor - macro expansion Mark_Kuschnir@gec-epl.co.uk (1993-05-24)
Re: C Preprocessor - macro expansion davidm@questor.rational.com (1993-05-24)
| List of all articles for this month |
Newsgroups: comp.compilers
From: davidm@questor.rational.com (David Moore)
Keywords: C
Organization: Rational
References: 93-05-112
Date: Mon, 24 May 1993 21:59:09 GMT

Mark_Kuschnir@gec-epl.co.uk writes:


[In ANSI C]
> #define x 2
> #define f(a) f(x * (a))
> #define z z [0]


> f(f(z));


>this should macro expand to :


> f(2 * (f(2 * (z [0]))))


>I would like to know the steps I have to go through when macro expanding
>the above example so as to meet the ANSI provisos :


>(i) the replacement sequence is rescanned until all macros have been
> replaced
>(ii) once an identifier has been used in a replacement sequence
> it won't be used again


The key seems to be in the definition of "expansion sequence". It looks
to me as if the term is meant to mean a tree.


For example, you start with f(f(z)) and after the first substitution
(using the fact that inside-out is mandated, you get:


            f (%)
                  |
              f(x*(z))


That is, the % is a place holder for the text that is in the
attached subtree.


which you now expand recursively, but with the f definition marked as
unusable. So you get at step 2 (and lumping together both
expansions to save space):


            f(%)
                |
              f(%*(%))
                  | |
                  2 z [0]


Whenever you expand, you inherit the list of used definitions from
the parent and add the expansion you have just used - actually, of
course, you just keep parent links and walk up the tree to determine
what expansions you have already used.


I had no idea C macro expansion was this complicated!
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.