C Preprocessor - macro expansion

Mon, 24 May 1993 12:50:51 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: Mark_Kuschnir@gec-epl.co.uk
Keywords: C, question
Organization: Compilers Central
Date: Mon, 24 May 1993 12:50:51 GMT

I am having difficulty writing a C preprocessor meeting the ANSI standard.
(Mind you the only preprocessor that I have used that meets the ANSI macro
expansion tests is the GNU gcc). My difficulty lies with macro expansion.
I understand the rules governing the '#' and '##' operators. However in
the collection of arguments and expansion of the macro I am having
difficulty producing a correct algorithm. Consider a paraphrase of the
ANSI tests :

        #define x 2
        #define f(a) f(x * (a))
        #define z z [0]


this should macro expand to :

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

my problems arise because each argument of the macro must be fully macro
expanded before use (so f(z) -> f(2 * z [0])) which means I have macro
expanded 'x' and 'z' and this expansion of 'x' prevents the next expansion
of 'x' so currently I end up with f(x * (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
(ii) once an identifier has been used in a replacement sequence
        it won't be used again

a difficulty with (ii) is illustrated by the (trivial) :

        #define A 12345
        #define abcde A A A A


this should macro expand to :

        12345 12345 12345 12345

Thanks for any help/suggestions/elaboration.

P.S. dig out the ANSI tests and try them out on your C preprocessor !

Post a followup to this message

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