Regular expressions in the C preprocessor macro

I would like to know if there is any regex extension in the compiler preprocessor (GCC). Mostly more flexible code generation macros.

If there is no way how you propose to fulfill the same result

+6
source share
4 answers

C preprocessor cannot do this.

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

+5
source

In addition, if you are planning a larger project, and you know that this function will be useful, you may need to write your own preprocessor, which can be started automatically from any build system. A good example of such a solution would be moc , which improves C ++ for the purpose of Qt. The purist, of course, may not agree.

+1
source

There is https://github.com/graph/qc qc = Quick C, this allows you to do this in source code files ending 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 the .cpp and .h that are preprocessed. This is to avoid having to store header files. And some other things do not make it backward 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 did this and dodged qc.

0
source

You might want to look at re2c.org. This is a separate C preprocessor to generate C to match regular expressions. I found this and your question when searching for something similar.

0
source

Source: https://habr.com/ru/post/898889/


All Articles