Topic: preprocessor


Author: Hyman Rosen <hymie@prolifics.com>
Date: 1998/10/20
Raw View
Eugene Eltsine wrote:
> Please explain me how these examples should be processed by C++
> preprocessor and why?
>
> #define F      G(F,
> #define G(a,b) a##b
> F F x ))

1) The first F is read and replaced. The F in the replacement list is
   painted blue, marking it is ineligible for further replacement. I will
   indicate this by an asterisk under the token.

G(F, F x ))
  *

2) G is a function-like macro. It's arguments are identified as the two
   token sequences F and F x. The parametrs of G in the replacement list
                   *
   are both either preceded or followed by a ## operator, so they are
   replaced by the arguments verbatim.

F##F x)
*

3) The ## is deleted and tokens are pasted.

FF x)

4) The tokens are rescanned for further expansion, but nothing more is
   found. The final result is

FF x)

> b)
> #define FF     G(F,
> #define G(a,b) a##b
> FF F )

1) The FF is read and substituted.

G(F, F )

2) G is a function-like macro. It's arguments are identified as the two
   token sequences F and F. The parameters of G in the replacement list
   are both either preceded or followed by a ## operator, so they are
   replaced by the arguments verbatim.

F##F

3) The ## is deleted and tokens are pasted.

FF

4) The result is further processed, along with the rest of the file. The
   FF is read and substituted.

G(F,

5) G is a function-like macro. There is no ) token to terminate its
   invocation, so the result is ill-formed. The compiler will generate a
   diagnostic.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Eugene Eltsine" <eae@mlc.ru>
Date: 1998/10/22
Raw View
Hyman Rosen wrote:

> Eugene Eltsine wrote:
> > Please explain me how these examples should be processed by C++
> > preprocessor and why?
> >
> > #define F      G(F,
> > #define G(a,b) a##b
> > F F x ))
>
> 1) The first F is read and replaced. The F in the replacement list is
>    painted blue, marking it is ineligible for further replacement. I will
>    indicate this by an asterisk under the token.
>
> G(F, F x ))
>   *
>
> 2) G is a function-like macro. It's arguments are identified as the two
>    token sequences F and F x. The parametrs of G in the replacement list
>                    *
>    are both either preceded or followed by a ## operator, so they are
>    replaced by the arguments verbatim.
>
> F##F x)
> *

Please note, that the Standard in 16.3.1 says, that before being
substituted,each argument's preprocessing tokens are completely macro
replaced...

The main problem here is: the first macro G argument's contains an
invocation of F with asterisk, and the second also contains an invocation
of F, but without of asterisk already, because it comes from the rest of
the source file, not from the replacement list that is rescanned, so it
must
be replaced here [ minding the 16.3.4 (2): ...(not including the
rest...)...]

[Macro G implementation with a##b is not significant, and can be replaced
just by a+b, for example]

IMHO, the result should be

    FFx

(without whitespaces).

>
>
> 3) The ## is deleted and tokens are pasted.
>
> FF x)
>
> 4) The tokens are rescanned for further expansion, but nothing more is
>    found. The final result is
>
> FF x)
>
> > b)
> > #define FF     G(F,
> > #define G(a,b) a##b
> > FF F )
>
> 1) The FF is read and substituted.
>
> G(F, F )
>
> 2) G is a function-like macro. It's arguments are identified as the two
>    token sequences F and F. The parameters of G in the replacement list
>    are both either preceded or followed by a ## operator, so they are
>    replaced by the arguments verbatim.
>
> F##F
>
> 3) The ## is deleted and tokens are pasted.
>
> FF
>
> 4) The result is further processed, along with the rest of the file. The
>    FF is read and substituted.

Here we get a problem simular to the one from the first sample - it isnot
clear, if the FF token belongs to the replacement list that is
rescanned, were the FF macro should not be replaced, or to the rest
of the source file, were it should, because the first F in the invocation
of G comes from the replacement list, but the second doesn't.

By the way, the compilers we tested perform the replacement of FF
here.

>
>
> G(F,
>
> 5) G is a function-like macro. There is no ) token to terminate its
>    invocation, so the result is ill-formed. The compiler will generate a
>    diagnostic.
> ---

In general, the question is: when, while rescanning the replacement
list after substitution of some macro F, we have to read more tokens
from the rest of the source file, for example, while identifying
function-like macro G arguments, should we replace macro F when
we encounter it in these tokens, or not? (should we treat these
tokens as more tokens of the replacement list, were F substitution
is not allowed, or not?)

Thank you,
Gene
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Hyman Rosen <hymie@prolifics.com>
Date: 1998/10/23
Raw View
Eugene Eltsine wrote:
> Please note, that the Standard in 16.3.1 says, that before being
> substituted,each argument's preprocessing tokens are completely macro
> replaced...

That sentence is merely elucidating the previous one, which says that
expansion on arguments is done only for parameters in the substitution
list which are not preceded by # and are not preceded or followed by ##.
It is explaining that when the argument is expanded, it is expanded in a
statndalone context without seeing any more tokens from the file.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Eugene Eltsine" <eae@mlc.ru>
Date: 1998/10/19
Raw View
Hello!

Please explain me how these examples should be processed by C++
preprocessor
and why?

a)

#define F      G(F,
#define G(a,b) a##b

F F x ))

b)

#define FF     G(F,
#define G(a,b) a##b

FF F )

Whitespaces are significant.

Four compilers, including GCC and EDG, produce different output while
processing these samples :-)

This Standard quote explains nothing - none of compilers produce their
output strictly according this quote IMHO

ISO/IEC 14882:1998(E):

16.3.4 Rescanning and further replacement

1. After all parameters in the replacement list have been substituted, the
resulting preprocessing token sequence is rescanned with all subsequent
preprocessing tokens of the source file for more macro names to replace.

2. If the name of the macro being replaced is found during this scan of the
replacement list (not including the rest of the source file preprocessing
tokens), it is not replaced.  Further, if any nested replacements
encounter the name of the macro being replaced, it is not replaced.
These nonreplaced macro name preprocessing tokens are no longer
available for futher replacement even if they are later (re)examined in
contexts in which that macro name preprocessing token would otherwise
be replaced.

Thank you in advance,
Gene



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW@my-dejanews.com
Date: 1998/10/20
Raw View
In article <432566A0.005F3E57.00@owl.mlc.ru>,
  "Eugene Eltsine" <eae@mlc.ru> wrote:
> Please explain me how these examples should be processed by C++
> preprocessor
> and why?

Ooh, a puzzle. Sometimes I like puzzles. (Sometimes.)

> a)
>
> #define F      G(F,
> #define G(a,b) a##b
>
> F F x ))

Bring in the first token only
    F
Macro substitution
    G(F,
The G can't be replaced yet, because we haven't got enough parameters.
The F can't be replaced anymore because it matches the name of the
original macro that was replaced. I'm guessing that you didn't get
an error message, so we continue by bringing in another token.
    G(F, F
But that second F can be replaced, so we do:
    G(F, G(F,
Continue with the next token:
    G(F, G(F, x
There's still no close parens for either G, so continue
    G(F, G(F, x )
*** Now, the point of this exercise. Do we replace G? I think we do.
    G(F, Fx
That finished, we bring in the last token:
    G(F, Fx)
and do our final substitution:
    FFx
Am I right?


> b)
>
> #define FF     G(F,
> #define G(a,b) a##b
>
> FF F )

Starts out kinda similar.
    FF
    G(F,
Bring in next token
    G(F, F
and last token
    G(F, F )
Do our macro substitution
    FF
We've already done our translation of FF, so we stop here.
Am I right?

> Four compilers, including GCC and EDG, produce different output while
> processing these samples :-)

Do I match one of them? I want to WIN!

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]