Programmatically parse and edit C++ Source Files

2020-07-15 04:25发布

I want to programmatically parse and edit C++ source files. I need to change/add code in certain sections of code (i.e. in functions, class blocks, etc). I would also (preferably) be able to get comments as well.

Part of what I want to do can be explained by the following piece of code:

CPlusPlusSourceParser cp = new CPlusPlusSourceParser(“x.cpp”);  // Create C++ Source Parser Object
CPlusPlusSourceFunction[] funcs = cp.getFunctions();  // Get all the functions

for (int i = 0; i &lt funcs.length; i++) {  // Loop through all functions
    funcs[i].append(/* … code I want to append …*/);  // Append some code to function 
}
cp.save(); // Save new source
cp.close(); // Close file

How can I do that?

I’d like to be able to do this preferably in Java, C++, Perl, Python or C#. However, I am open to other language API’s.

标签: c++ parsing
7条回答
我命由我不由天
2楼-- · 2020-07-15 04:51

You need a working grammar and parser for C++ which is, however, not too easy as this can't be constructed with most parser generators out there. But once you have a parser you can actually take the abstract syntax tree of the program and alter it in nearly any way you want.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-07-15 04:53

The Mozilla project has a tool that does this.


The Clang static analyzer is now somewhat famous for doing a good job analyzing and rewriting C++. Stroustrup wrote a paper about a research project at Texas A&M, but I don't think it's been released.

查看更多
Ridiculous、
4楼-- · 2020-07-15 04:56

have a look at the doxygen project, its a open source project, to parse and document several programming languages, C++ included. I believe using this project's lexer will get you more than half the way

查看更多
闹够了就滚
5楼-- · 2020-07-15 05:07

This is similar to AST from C code

If your comfortable with Java antlr can easily parser your code into an abstract syntax tree, and then apply transformation to that tree. A default AST transform is to simply print out the original source.

查看更多
混吃等死
6楼-- · 2020-07-15 05:12

You can use any parser generator tool to generate a c++ parser for you, but first you have to get the CFG (context free grammar) for C++ , check Antlr

Edit:

Also Antlr supports a lot of target languages

查看更多
We Are One
7楼-- · 2020-07-15 05:13

In a C# -- or general .net -- approach, you might be able to get some use out of the C++/CLI CodeDOM provider -- having not used the C++ version of this type, I don't know how well it would handle code that is template heavy.

查看更多
登录 后发表回答