Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is the output of the preprocessor deterministic ?

Reply
Thread Tools

Is the output of the preprocessor deterministic ?

 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-25-2006
Is the output of the C preprocessor deterministic ? What I mean
by that is , given 2 compilers which conform to the same standard,
will their preprocessors produce identical output given as input
the same file ? If not then how much variation is allowed ? Is it
just a bit more or less white space here and there or could could
there be larger differences ?

If the output is not deterministic then is it possible that the output
of the preprocessor of one compiler can not be processed correctly
by another compiler ?

Spiros Bousbouras

 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      06-25-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Is the output of the C preprocessor deterministic ? What I mean
> by that is , given 2 compilers which conform to the same standard,
> will their preprocessors produce identical output given as input
> the same file ? If not then how much variation is allowed ? Is it
> just a bit more or less white space here and there or could could
> there be larger differences ?
>
> If the output is not deterministic then is it possible that the output
> of the preprocessor of one compiler can not be processed correctly
> by another compiler ?


Shooting in dark...

But whitespace shouldn't stop one compiler from working with the output
from a competitor.

I think though you'll find it isn't going to be the same. For example,
if you use cpp from GCC on a Linux box then compare it to what MSVC
would give the outputs will differ, especially since the headers
themselves are different.

Tom

 
Reply With Quote
 
 
 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-25-2006

Tom St Denis wrote:

> (E-Mail Removed) wrote:
> > Is the output of the C preprocessor deterministic ? What I mean
> > by that is , given 2 compilers which conform to the same standard,
> > will their preprocessors produce identical output given as input
> > the same file ? If not then how much variation is allowed ? Is it
> > just a bit more or less white space here and there or could could
> > there be larger differences ?
> >
> > If the output is not deterministic then is it possible that the output
> > of the preprocessor of one compiler can not be processed correctly
> > by another compiler ?

>
> Shooting in dark...
>
> But whitespace shouldn't stop one compiler from working with the output
> from a competitor.
>
> I think though you'll find it isn't going to be the same. For example,
> if you use cpp from GCC on a Linux box then compare it to what MSVC
> would give the outputs will differ, especially since the headers
> themselves are different.


I specified that the input has to be the same. By that I also meant
same headers.

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      06-25-2006
(E-Mail Removed) wrote:
>
> Tom St Denis wrote:


> > I think though you'll find it isn't going to be the same.


> I specified that the input has to be the same. By that I also meant
> same headers.


#include <stdio.h>

#ifdef putchar
#define STRING "putchar is a macro"
#else
#define STRING "putchar is not a macro"
#endif

--
pete
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      06-25-2006
(E-Mail Removed) wrote:
> Is the output of the C preprocessor deterministic ? What I mean
> by that is , given 2 compilers which conform to the same standard,
> will their preprocessors produce identical output given as input
> the same file ?


Even if you ignore that there is no standard file format for
preprocessed output, and that preprocessed output need not be
obtainable at all, then no, output may be very different.

> If not then how much variation is allowed ? Is it
> just a bit more or less white space here and there or could could
> there be larger differences ?


Much larger. Some easy examples:

#ifndef __i386__
#error
#endif

#if 18446744073709551615U + 1
#error
#endif

#define f(x) g
#define g(x) f
f(1)(2)(3) /* either f(3) or g */

#define s(x) #x
s("\u0040") /* either "\"\u0040\"" or "\"\\u0040\"" */

 
Reply With Quote
 
Al Balmer
Guest
Posts: n/a
 
      06-25-2006
On Sun, 25 Jun 2006 13:55:23 GMT, pete <(E-Mail Removed)> wrote:

>(E-Mail Removed) wrote:
>>
>> Tom St Denis wrote:

>
>> > I think though you'll find it isn't going to be the same.

>
>> I specified that the input has to be the same. By that I also meant
>> same headers.

>
>#include <stdio.h>
>
>#ifdef putchar
>#define STRING "putchar is a macro"
>#else
>#define STRING "putchar is not a macro"
>#endif


He said "same headers", not "headers with the same name."

But Harald has a couple of examples.

--
Al Balmer
Sun City, AZ
 
Reply With Quote
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-25-2006

Harald van Dijk wrote:

> (E-Mail Removed) wrote:
> > Is the output of the C preprocessor deterministic ? What I mean
> > by that is , given 2 compilers which conform to the same standard,
> > will their preprocessors produce identical output given as input
> > the same file ?

>
> #define f(x) g
> #define g(x) f
> f(1)(2)(3) /* either f(3) or g */


Ok , let's concentrate on this one for the time being. The
Sun compiler gives " g (2)(3) " (without the quotes). GNU
gives "g". Could someone explain to me the intermediate
steps which give these results ? Also for "f(3)" if that's
also possible.

By the way is there a tool or option for some compiler
which will make it show all the intermediate steps in
macro expansions ? So I'm asking for something similar
to Lisp's macroexpand-1.

Spiros Bousbouras

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-25-2006
On 25 Jun 2006 06:35:42 -0700, (E-Mail Removed) wrote:

>
>Tom St Denis wrote:
>
>> (E-Mail Removed) wrote:
>> > Is the output of the C preprocessor deterministic ? What I mean
>> > by that is , given 2 compilers which conform to the same standard,
>> > will their preprocessors produce identical output given as input
>> > the same file ? If not then how much variation is allowed ? Is it
>> > just a bit more or less white space here and there or could could
>> > there be larger differences ?
>> >
>> > If the output is not deterministic then is it possible that the output
>> > of the preprocessor of one compiler can not be processed correctly
>> > by another compiler ?

>>
>> Shooting in dark...
>>
>> But whitespace shouldn't stop one compiler from working with the output
>> from a competitor.
>>
>> I think though you'll find it isn't going to be the same. For example,
>> if you use cpp from GCC on a Linux box then compare it to what MSVC
>> would give the outputs will differ, especially since the headers
>> themselves are different.

>
>I specified that the input has to be the same. By that I also meant
>same headers.


Headers are system specific. The compiler writer is allowed to take
advantage of unique system attributes. It is unreasonable to expect
one compiler to even function with another's headers. What about the
case where headers are not files but built-in to the compiler?


Remove del for email
 
Reply With Quote
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-25-2006

Barry Schwarz wrote:

> On 25 Jun 2006 06:35:42 -0700, (E-Mail Removed) wrote:
>
> >
> >Tom St Denis wrote:
> >
> >> (E-Mail Removed) wrote:
> >> > Is the output of the C preprocessor deterministic ? What I mean
> >> > by that is , given 2 compilers which conform to the same standard,
> >> > will their preprocessors produce identical output given as input
> >> > the same file ? If not then how much variation is allowed ? Is it
> >> > just a bit more or less white space here and there or could could
> >> > there be larger differences ?
> >> >
> >> > If the output is not deterministic then is it possible that the output
> >> > of the preprocessor of one compiler can not be processed correctly
> >> > by another compiler ?
> >>
> >> Shooting in dark...
> >>
> >> But whitespace shouldn't stop one compiler from working with the output
> >> from a competitor.
> >>
> >> I think though you'll find it isn't going to be the same. For example,
> >> if you use cpp from GCC on a Linux box then compare it to what MSVC
> >> would give the outputs will differ, especially since the headers
> >> themselves are different.

> >
> >I specified that the input has to be the same. By that I also meant
> >same headers.

>
> Headers are system specific. The compiler writer is allowed to take
> advantage of unique system attributes. It is unreasonable to expect
> one compiler to even function with another's headers. What about the
> case where headers are not files but built-in to the compiler?


Then you can consider the case where you have 2 compilers on the same
platform. The essence of my original question is whether macro
expansion
rules are deterministic not about unique system attributes and
differences
in the environment.

Spiros Bousbouras

 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      06-25-2006
>Is the output of the C preprocessor deterministic ? What I mean
>by that is , given 2 compilers which conform to the same standard,
>will their preprocessors produce identical output given as input
>the same file ?


There is no standardized format for the output of the preprocessor,
which is in tokens. Some may output plain text. Some may output
XML. Some may output some wierd binary format. Some may not even
HAVE a separate output format nor exist as a separate program.

>If not then how much variation is allowed ? Is it
>just a bit more or less white space here and there or could could
>there be larger differences ?


The expansion of __TIME__ and __DATE__ may be different for successive
runs of the preprocessor. Many preprocessors have predefined symbols
indicating the type of system, OS, or compiler it is, and different
preprocessors may have different predefined symbols.

Differences in system header files may result in different output.
It is possible that some preprocessors cannot handle non-precompiled
system headers, and that others have no provision for precompiled
headers.

System headers may contain special magic, some of which might be
handled by the preprocessor. If a different preprocessor tries
to handle the magic, it may report errors, or handle it incorrectly.

>If the output is not deterministic then is it possible that the output
>of the preprocessor of one compiler can not be processed correctly
>by another compiler ?


Due to possible:
- Lack of output from the preprocessor stage
- Inability to input to the compiler without preprocessing AGAIN
- Differences in preprocessor/compiler formats for preprocessed tokens
- Differences in system header files

it's very possible that output from one preprocessor can't be compiled
by another compiler, and even more likely that it won't link and run
correctly.

Gordon L. Burditt
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-deterministic output Esben Nielsen Python 3 03-29-2011 07:46 PM
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
schema non deterministic again c_capatana@hotmail.com XML 1 11-05-2003 06:18 PM
non deterministic schema problem c_capatana@hotmail.com XML 1 11-04-2003 01:47 PM



Advertisments