Topic: clause 2.9 Preprocessing numbers [lex.ppnumber]


Author: kanze@gabi-soft.de (James Kanze)
Date: Mon, 14 Jan 2002 18:29:14 GMT
Raw View
"K. Noel Belcourt" <kbelco@sandia.gov> wrote in message
news:<3C3F4ACF.EF2E6DA2@sandia.gov>...

> I'm a bit confused about production pp-number. Why can a pp-number
> accept a nondigit following a digit? I understand integral and
> floating literals well enough, but where would a token of this type
> be used? pp-number : ...

>                      pp-number nondigit

> This implies that 123_ABC is a valid preprocesing number?

No.  It implies that the compiler will break the input stream up into
this token, and then try and process it as a preprocessing number.  It
will find an illegal number, of course.

> [attempting to ask and answer my own question]

> Per clause 2.9.2, if a macro doesn't replace _ABC with something
> that permits the translation phase (7) to convert it to a valid
> integral or floating literal, then it's an error?

Right. Except that since _ABC isn't a preprocessor token (the token is
123_ABC), it can't get replaced.

The reason, of course, is to make things simpler for the lexical
scanner, and at the same time to make some unreadable constructs
illegal.  Thus, 0x12az is a single preprocessor token (and is
illegal); the compiler doesn't treat it as if it were 0x12a z.

--
James Kanze                                   mailto:kanze@gabi-soft.de
Beratung in objektorientierer Datenverarbeitung --
                             -- Conseils en informatique orient   e objet
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany, T   l.: +49 (0)69 19 86 27

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Andrea Ferro" <AndreaF@UrkaDVD.it>
Date: Mon, 14 Jan 2002 19:17:01 GMT
Raw View
"K. Noel Belcourt" <kbelco@sandia.gov> wrote in message
news:3C3F4ACF.EF2E6DA2@sandia.gov...
> I'm a bit confused about production pp-number.  Why can a pp-number
> accept a nondigit following a digit?

It can accept non digits not only at the end but also in the middle. But
it must start with a digit or a . followed by a single.

> I understand integral and floating
> literals well enough, but where would a token of this type be used?

1234L
0x1AB34

just a couple of examples.

>
> pp-number : ...
>                      pp-number nondigit
>
> This implies that 123_ABC is a valid preprocesing number?

Yep. But will then fail to become a token later in phase 7.


--

Andrea Ferro

---------
Brainbench C++ Master. Scored higher than 97% of previous takers
Scores: Overall 4.46, Conceptual 5.0, Problem-Solving 5.0
More info http://www.brainbench.com/transcript.jsp?pid=2522556

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Ruslan Abdikeev" <ruslan_abdikeevREMOVE_IT@hotmail.com>
Date: Mon, 14 Jan 2002 19:48:56 GMT
Raw View
"K. Noel Belcourt" <kbelco@sandia.gov> wrote in message
news:3C3F4ACF.EF2E6DA2@sandia.gov...
> I'm a bit confused about production pp-number.  Why can a pp-number
> accept a nondigit following a digit?
> I understand integral and floating
> literals well enough, but where would a token of this type be used?
>
> pp-number : ...
>                      pp-number nondigit
>
> This implies that 123_ABC is a valid preprocesing number?

Yes, 123_ABC is a valid preprocessing number.

>
> [attempting to ask and answer my own question]
>
> Per clause 2.9.2, if a macro doesn't replace _ABC with something that
> permits
> the translation phase (7) to convert it to a valid integral or floating
> literal, then
> it's an error?
>
> Is this correct?
>

Nope.
Macro cannot replace _ABC in 123_ABC, because the entire 123_ABC
is one pp-token.
Macro cannot affect pp-tokens, and it is a basic idea of having pp-numbers
defined
the way they are.

Idea is to protect programmers from unintended
side effects of macro invocations.
Consider the following:

Literal

  1f

is expected to have a value of float(1).

However, if someone defines a macro with name "f"

    #define f +1

it would break a lot of code, if pp-number were
stop on first nondigit, as 1f will be 1+1 after phase 4.

(S2.4/4 gives an example of parsing "1Ex" and "1E1".)

So, in your case (123_ABC), even
if someone (I guess, implementor of standard library ;-)
defined

   #define _ABC 456

it would NOT render your pp-number
   123_ABC
to literal 123456 on a phase 7.

Whether macro _ABC is defined or not,
your program is ill-formed (2.13.3/1).

Hope it makes things clearer,

Ruslan Abdikeev
Brainbench MVP for Visual C++
http://www.brainbench.com




---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 15 Jan 2002 15:08:51 GMT
Raw View
James Kanze wrote:
>
> "K. Noel Belcourt" <kbelco@sandia.gov> wrote in message
> news:<3C3F4ACF.EF2E6DA2@sandia.gov>...
...
> > This implies that 123_ABC is a valid preprocesing number?
>
> No.  It implies that the compiler will break the input stream up into
> this token, and then try and process it as a preprocessing number.  It
> will find an illegal number, of course.

Why? If 123_ABC is one of the arguments of a function-like macro, it
could end up being part of something perfectly valid, as a result of
stringizing or token pasting. Just because it parses as pp-number during
pre-processing, doesn't mean it has to be a number.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "K. Noel Belcourt" <kbelco@sandia.gov>
Date: Fri, 11 Jan 2002 14:42:58 CST
Raw View
I'm a bit confused about production pp-number.  Why can a pp-number
accept a nondigit following a digit?  I understand integral and floating

literals well enough, but where would a token of this type be used?

pp-number : ...
                     pp-number nondigit

This implies that 123_ABC is a valid preprocesing number?

[attempting to ask and answer my own question]

Per clause 2.9.2, if a macro doesn't replace _ABC with something that
permits
the translation phase (7) to convert it to a valid integral or floating
literal, then
it's an error?

Is this correct?

Thanks,

Noel Belcourt



---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Mon, 14 Jan 2002 17:02:33 GMT
Raw View
"K. Noel Belcourt" wrote:
>
> I'm a bit confused about production pp-number.  Why can a pp-number
> accept a nondigit following a digit?  I understand integral and floating
>
> literals well enough, but where would a token of this type be used?
>
> pp-number : ...
>                      pp-number nondigit
>
> This implies that 123_ABC is a valid preprocesing number?

Correct. The rules for pp-numbers are deliberately less strict than
actual numeric literals. The main purpose they serve is to allow real
numeric literals be processed as complete preprocessing tokens, rather
than as a string of individual characters. For that purpose, it's
acceptable to use a simplified definition that includes a few things
that aren't valid numeric literals, but might be failed attempts to
create one.
Parsing such a sequence as a single token allows it to pasted to another
token using the ## operator, if it occurs in the expansion of a
function-like macro.


> [attempting to ask and answer my own question]
>
> Per clause 2.9.2, if a macro doesn't replace _ABC with something that

Actually, one of the key purposes of this rule is to prevent 123_ABC
from being recognised as 123 followed by an identifier named _ABC. Since
_ABC is not parsed as an identifier, it's not subject to replacement by
the expansion of a macro with that name.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]