Paulo Neves
Paulo Neves

Reputation: 1196

Regular expressions in C preprocessor macro

I would like to know if there is any kind of regular expression expansion within the compiler(GCC) pre processor. Basically more flexible code generation macros.

If there is not a way, how do you suggest i accomplish the same result

Upvotes: 8

Views: 5354

Answers (4)

Wayne
Wayne

Reputation: 123

You might want to look at re2c.org. It it a separate C preprocessor to generate C code to match regular expressions. I found that and your question when looking for something similar.

Upvotes: 0

over_optimistic
over_optimistic

Reputation: 1419

There is this https://github.com/graph/qc qc = Quick C it allows you to do this in your source code files that end with qc.h

$replace asdf_(\d+) => asdf_ :) $1 blabla

// and now in your code anything that matches the above regular expression
asdf_123
// will become asdf_ :) 123 blabla

And it will output a .cpp & a .h thats preprocessed. Its made to avoid the need to maintain header files. And some other things not making it backwards compatible with c++, but it outputs c++ code so you can do all the c++ things you want at the end of the day.

Edit: I made it and have a bias towards qc.

Upvotes: 0

RushPL
RushPL

Reputation: 4805

Also, if you are planning a bigger project and you know this feature will be beneficial you might want to write your own preprocessor that you can run automatically from some build system. Good example of such solution would be moc which enhances C++ for the purpose of Qt framework. Purist might of course disagree.

Upvotes: 1

Gregory Pakosz
Gregory Pakosz

Reputation: 70204

The C preprocessor can't do that.

You might want to use a template processor (for instance Mustache but there are many others) that generates what you need before passing it to the compiler.

Upvotes: 5

Related Questions