user4812
user4812

Reputation: 6712

Using the C Preprocessor for languages other than C

The Wikipedia entry for the C Preprocessor states:

The language of preprocessor directives is agnostic to the grammar of C, so the C preprocessor can also be used independently to process other types of files.

How can this be done? Any examples or techniques?

EDIT: Yes, I'm mostly interested in macro processing. Even though it's probably not advisable or maintainable it would still be useful to know what's possible.

Upvotes: 19

Views: 7354

Answers (12)

kjfletch
kjfletch

Reputation: 5494

You can call CPP directly:

cpp <file>

Rather than calling it through gcc:

gcc -E filename

Do note however that, as mentioned in the same Wikipedia article, C preprocessor's language is not really equipped for general-purpose use:

However, since the C preprocessor does not have features of some other preprocessors, such as recursive macros, selective expansion according to quoting, string evaluation in conditionals, and Turing completeness, it is very limited in comparison to a more general macro processor such as m4.

Have you considered dabbling with a more flexible macro processing language, like the aforementioned m4 for instance?

Upvotes: 20

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143144

Many C compilers have a flag that tells them to only preprocess. With gcc it's the -E flag. eg:

$ gcc -E -                 
#define FOO foo
bar FOO baz

will output:

# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "<stdin>"

bar foo baz

With other C compilers you'll have to check the manuals to see how to swithc to preprocess-only mode.

Upvotes: 3

Stephan202
Stephan202

Reputation: 61499

The C preprocessor can also be invoked by the Glasgow Haskell Compiler (GHC) prior to compiling Haskell code, by passing the -cpp flag.

Upvotes: 1

Caleb Huitt - cjhuitt
Caleb Huitt - cjhuitt

Reputation: 14941

A while ago I did some work on a project that used imake for makefile generation. As I recall, it was basically the c preprocessor syntax to generate the make files.

Upvotes: 1

T.E.D.
T.E.D.

Reputation: 44804

I have heard of people using the C pre-processor on Ada code. Ada has no preprocessor, so you have to do something like that if you want to preprocess your code.

However, it was a concious design decision not to give it one, so doing this is very un-Ada. I wouldn't suggest anyone do this.

Upvotes: 1

Roalt
Roalt

Reputation: 8440

Yes, it can be done by parsing your own language through the gcc preprocessor (e.g. 'gcc -E').

We have done this on my job with our our, specific language. It has quite some advantages:

  • You can use C's include statements (#include) which is very powerful
  • You can use your #ifdef constructions
  • You can define Constants (#define MAGIC_NUMBER 42) or macro functions (#define min(x,y) ( (x( < (y) ? (x) : (y))

... and the other things in the c processor.

HOWEVER, you also inherit the unsafe C constructions, and having a preprocessor not integrated with your main language is the cause of it. Think about the minimum macro and doing something like :

a = 2;
b = 3;

c = min(a--, b--);

Just think what value a and b will have after the min function?

Same is true about the non-typed constants that you introduce

See the Safer C book for details.

Upvotes: 3

Jeff L
Jeff L

Reputation: 6188

Assuming you're using GCC, You can take any plain old text file, regardless of its contents, and run:

gcc -E filename

Any preprocessor directives in the file will be processed by the preprocessor and GCC will then exit.

The point is that it doesn't matter what the actual content of the text file is, since all the preprocessor cares about is its own directives.

Upvotes: 2

ChrisW
ChrisW

Reputation: 56113

Using Microsoft's compiler, I think (I just looked it up, haven't tested it) that it's the /P compiler option.

Other compilers presumably have similar options (or, for some compilers the preprocessor might actually be a different executable, which is usually run implicitly by the compiler but which you can also run explicitly separately).

Upvotes: 1

DevSolar
DevSolar

Reputation: 70263

For example, Assembler. While many assemblers have their own way to #include headers and #define macros, it can be useful to use the C preprocessor for this. GNU make, for example, has implicit rules for turning *.S files into *.s files by running the preprocessor ('cpp'), before feeding the *.s file to the GNU assembler ('as').

Upvotes: 2

dborba
dborba

Reputation: 643

Basically what it's saying is that preprocessors have nothing to do with C syntax. They are basically simple parsers that follow a set of rules. So you could use preprocessors kind of like you'd use sed or awk for some silly tasks. Don't ask me why you'd ever want to do it though.

For example, on a text file:

#define pi 3.141

pi is not an irrational number.

Then you run the preprocessor & you'd get.

3.141 is not an irrational number.

Upvotes: 0

gix
gix

Reputation: 5797

Usually you can invoke the C compiler with an option to preprocess only (and ignore any #line statements). Take this as a simple example:

<?php
function foo()
{
#ifdef DEBUG
    echo "Some debug info.";
#endif
    echo "Foo!";
}

foo();

We define a PHP source file with preprocess statements. We can then preprocess it (gcc can do this, too):

cl -nologo -EP foo.php > foo2.php

Since DEBUG is not the defined the first echo is stripped. Plus here is that lines beginning with # are comments in PHP so you don't have to preprocess them for a "debug" build.

Edit: Since you asked about macros. This works fine too and could be used to generate boilerplate code etc.

Upvotes: 2

David Thornley
David Thornley

Reputation: 57036

You could implement the C preprocessor in the compiler for another language.

You could use it to preprocess any sort of text file, but there's much better things for that purpose.

Upvotes: 0

Related Questions