Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #101235

Macro Expansion

From BartC <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Macro Expansion
Date 2017-01-07 18:35 +0000
Organization A noiseless patient Spider
Message-ID <o4rcbl$4cp$1@dont-email.me> (permalink)

Show all headers | View raw


I've knocked up a short script program to implement CPP macro expansion. 
It works with many of the examples I've tried, but I have little 
confidence that whatever I end up with will always work, as I'm guessing 
at the algorithms needed.

The descriptions I've seen of the process seem to me ambiguous and 
difficult to understand (maybe they're striving to be as cryptic as the 
language they are describing!).

Here is what I've been able to understand of the process; maybe someone 
can tell me if it is on the right lines.

Example:

    #define XX 20
    #define XXz 99
    #define W W+W+XX+x
    #define M(x,y)  x y #x y ## z M(a,b) W

    M(10,XX)

In the expansion of M(10,XX):

* The token sequence '10' is used for argument x

* The token sequence 'XX' is used for argument y.

* Start with the replacement token list as defined for M:

     x y #x y ## z M(a,b) W

* Replace x and y with their arguments, expanding, stringifying
   or combining as needed:

    10 20 "10" XXz M(a,b) W

   Here, argument XX was separately macro expanded to 20 before replacing
   the first y. The second y has ## so is not expanded.

* After this substitution, the token list is scanned for macro calls to
   expand. M is a macro call, but is the same name as the macro
   currently being expanded, so maps to itself (and the (a,b) following
   are just tokens).

* That leaves XXz and W macros to expand, which now results in:

    10 20 "10" 99 M(a,b) W+W+XX+x

* The result is now scanned again looking for new macros to expand.
   However, once any name (eg. M and W) has been expanded once, it is
   never expanded again (in a subsequent pass). So only XX is changed
   to result in:

    10 20 "10" 99 M(a,b) W+W+20+x

   (This agrees with what gcc -E gives, apart from an extra space before
   +x)

* Presumably that rule also applies to parameter names, so the new x at
   end stays as x, it's not changed to 10.

* The re-scanning looking for new macros continues until there are no
   macro expansions.

* The # and ## processing, for # and ## present in the original
   replacement list, is only done once. What's not clear what it will do
   with # and ## introduced as the result of macro expansion (if that is
   even possible).

* (And, # used within a macro replacement list where there are no
   parameters, is just a hash token. This seems to be implied in the
   C standard.)

Summary:

* Macro arguments need only be expanded once (K&R2 A12.3 suggests this
   could be done on each substitution, when a parameter occurs more than
   once).

* Substitution of parameter names, and #/## processing, is done once.
   Parameter names are not substituted again

* Repeated passes are now done to expand macro calls, until there are
   no macro calls left to expand.

* A macro name A can be expanded several times in one pass, but then A
   is not expanded again if re-encountered in subsequent passes.

Phew!

-- 
Bartc

Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Macro Expansion BartC <bc@freeuk.com> - 2017-01-07 18:35 +0000
  Re: Macro Expansion Thiago Adams <thiago.adams@gmail.com> - 2017-01-07 11:23 -0800
    Re: Macro Expansion Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-07 21:28 +0000
  Re: Macro Expansion Siri Cruise <chine.bleu@yahoo.com> - 2017-01-07 11:45 -0800
    Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-07 20:16 +0000
      Re: Macro Expansion Siri Cruise <chine.bleu@yahoo.com> - 2017-01-07 12:23 -0800
        Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-07 20:31 +0000
          Re: Macro Expansion Siri Cruise <chine.bleu@yahoo.com> - 2017-01-07 12:42 -0800
          Re: Macro Expansion Ian Collins <ian-news@hotmail.com> - 2017-01-08 14:42 +1300
            Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-08 13:18 +0000
  Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-07 20:15 +0000
    Re: Macro Expansion Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-07 20:42 +0000
      Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-07 22:50 +0000
        Re: Macro Expansion Ian Collins <ian-news@hotmail.com> - 2017-01-08 11:59 +1300
          Re: Macro Expansion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-07 15:10 -0800
        Re: Macro Expansion supercat@casperkitty.com - 2017-01-07 17:24 -0800
          Re: Macro Expansion Tim Rentsch <txr@alumni.caltech.edu> - 2017-01-15 21:55 -0800
            Re: Macro Expansion supercat@casperkitty.com - 2017-01-16 09:06 -0800
              Re: Macro Expansion Tim Rentsch <txr@alumni.caltech.edu> - 2017-01-31 08:51 -0800
                Re: Macro Expansion supercat@casperkitty.com - 2017-01-31 13:06 -0800
                Re: Macro Expansion Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-03 07:49 -0800
                Re: Macro Expansion supercat@casperkitty.com - 2017-02-03 12:03 -0800
                Re: Macro Expansion Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-09 09:00 -0800
        Re: Macro Expansion Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-08 01:37 +0000
          Re: Macro Expansion Jean-Marc Bourguet <jm@bourguet.org> - 2017-01-08 10:22 +0100
  Re: Macro Expansion Thiago Adams <thiago.adams@gmail.com> - 2017-01-07 12:18 -0800
    Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-07 20:28 +0000
      Re: Macro Expansion Thiago Adams <thiago.adams@gmail.com> - 2017-01-07 12:36 -0800
        Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-07 22:59 +0000
  Re: Macro Expansion Thiago Adams <thiago.adams@gmail.com> - 2017-01-08 00:20 -0800
    Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-08 13:30 +0000
      Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-10 14:00 +0000
        Re: Macro Expansion Thiago Adams <thiago.adams@gmail.com> - 2017-01-10 07:43 -0800
          Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-10 17:47 +0000
            Re: Macro Expansion Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-10 22:49 +0000
              Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-10 23:28 +0000
              Re: Macro Expansion "James R. Kuyper" <jameskuyper@verizon.net> - 2017-01-10 18:51 -0500
                Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-11 00:06 +0000
          Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-10 20:30 +0000
            Re: Macro Expansion Thiago Adams <thiago.adams@gmail.com> - 2017-04-26 10:04 -0700
              Re: Macro Expansion Keith Thompson <kst-u@mib.org> - 2017-04-26 12:00 -0700
                Re: Macro Expansion Thiago Adams <thiago.adams@gmail.com> - 2017-04-26 12:24 -0700
              Re: Macro Expansion bartc <bc@freeuk.com> - 2017-04-26 20:12 +0100
                Re: Macro Expansion Thiago Adams <thiago.adams@gmail.com> - 2017-04-26 12:34 -0700
                Re: Macro Expansion jameskuyper@verizon.net - 2017-04-26 13:46 -0700
  Re: Macro Expansion Jean-Marc Bourguet <jm@bourguet.org> - 2017-01-08 10:13 +0100
    Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-08 13:10 +0000
      Re: Macro Expansion Jean-Marc Bourguet <jm@bourguet.org> - 2017-01-08 15:51 +0100
        Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-08 17:17 +0000
          Re: Macro Expansion supercat@casperkitty.com - 2017-01-08 11:04 -0800
            Re: Macro Expansion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-08 11:10 -0800
              Re: Macro Expansion supercat@casperkitty.com - 2017-01-08 14:08 -0800
            Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-08 19:49 +0000
              Re: Macro Expansion supercat@casperkitty.com - 2017-01-08 14:17 -0800
          Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-08 19:19 +0000
          Re: Macro Expansion Jean-Marc Bourguet <jm@bourguet.org> - 2017-01-08 21:43 +0100
      Re: Macro Expansion supercat@casperkitty.com - 2017-01-08 10:43 -0800
      Re: Macro Expansion Keith Thompson <kst-u@mib.org> - 2017-01-08 12:39 -0800
  Re: Macro Expansion antispam@math.uni.wroc.pl - 2017-01-08 22:54 +0000
    Re: Macro Expansion "James R. Kuyper" <jameskuyper@verizon.net> - 2017-01-08 18:36 -0500
      Re: Macro Expansion antispam@math.uni.wroc.pl - 2017-01-09 01:15 +0000
        Re: Macro Expansion "James R. Kuyper" <jameskuyper@verizon.net> - 2017-01-08 20:49 -0500
    Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-08 23:53 +0000
      Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-09 01:11 +0000
      Re: Macro Expansion antispam@math.uni.wroc.pl - 2017-01-09 01:31 +0000
        Re: Macro Expansion BartC <bc@freeuk.com> - 2017-01-10 11:28 +0000

csiph-web