Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to do two-stage preprocessing of one file

Reply
Thread Tools

how to do two-stage preprocessing of one file

 
 
andreyvul
Guest
Posts: n/a
 
      11-13-2007
Here's an example of what I mean:
#ifndef REC
#define REC 2
#define d #define
#define i include
#elif REC == 2
#undef REC
#define REC 1
i <stdio.h>
d p(a) puts(a)
d q(a) int a
d Q main
d r(a) char**a
#elif REC == 1
#undef REC
#define REC 0
q(Q)(q(c), r(v)){p("Hello, World!\n");return 0;}
#endif
#if REC != 0
#include __FILE__
#endif

Should produce:
[stdio.h]
int main(int c, char**v){puts("Hello, World!\n"); return 0;}

 
Reply With Quote
 
 
 
 
Ark Khasin
Guest
Posts: n/a
 
      11-13-2007
andreyvul wrote:
> Here's an example of what I mean:
> #ifndef REC
> #define REC 2
> #define d #define
> #define i include
> #elif REC == 2
> #undef REC
> #define REC 1
> i <stdio.h>
> d p(a) puts(a)
> d q(a) int a
> d Q main
> d r(a) char**a
> #elif REC == 1
> #undef REC
> #define REC 0
> q(Q)(q(c), r(v)){p("Hello, World!\n");return 0;}
> #endif
> #if REC != 0
> #include __FILE__
> #endif
>
> Should produce:
> [stdio.h]
> int main(int c, char**v){puts("Hello, World!\n"); return 0;}
>

THAT you should not even attempt to do. It's a tad late to find quotes
from the standard, but basically:
- # in a context of macro definition means STRINGIZE when applied to a
macro argument; I doubt other uses are meaningful or valid
- you cannot (re)define preprocessor directives
So it is not valid C.

This is what you are doing:
- You deliberately take a bad (i.e. invalid) C-like source,
- You massage it to automatically produce a valid C source and run with it.
If that's what you want, you can use an arbitrary pair of source
language style and a corresponding preprocessor utility; that has
nothing to do with C.

YOU CAN, however, process your file in every which way you like, as you
demonstrated in this thread. Attempting to coerce a C compiler to do
preprocessing is not a terribly good idea. I'd suggest to use a
standalone PP that gives you some guarantees of what it outputs. (Google
them C preprocessors.) Or use a specific compiler you know suits your needs.
Keep in mind that:
- the compiler is under no obligation to produce preprocessor output.
- if the compiler does produce PP output, it may turn bad source into a
perfectly good source; I used to have a vivid example of that around
#define SLASH /
something /SLASH alleged comment
but I lost it. Basically, PP found a token /, then a token SLASH which
it expanded to / and output //. It compiles as start of comment (a
single token //) but in the original source they were, effectively, two
/ tokens. One might argue that PP should have produced / / but then
again, the compiler owes me nothing in that department.

If my hunch is correct and all you are trying to do is to obfuscate your
code, there are better ways of doing so.

--
Ark

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to do two-stage preprocessing of one file andreyvul C Programming 1 11-13-2007 12:53 AM
Summary: translation units, preprocessing, compiling and linking? Steven T. Hatton C++ 7 06-03-2004 06:01 PM
Preprocessing directives Ron C++ 3 11-12-2003 04:45 AM
Indentation of preprocessing directives Dave C++ 2 11-03-2003 03:34 AM



Advertisments