7

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.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Kryten
  • 3,843
  • 5
  • 37
  • 42
  • Something sounds wrong when you want to add the same code to the end of every function... – mpen Nov 16 '09 at 07:32
  • I don't want to add the same code everytime, I was just using the above as an example – Kryten Nov 16 '09 at 08:39
  • @Mark: Depends on what its for. Could be e.g. for some aspect oriented c++. – Georg Fritzsche Nov 16 '09 at 08:46
  • This is VERY difficult to do. If it could be done we'd have awesome c++ refactoring and intellisense tools. We don't have any and there is huge demand for them. – deft_code Nov 16 '09 at 16:48

7 Answers7

4

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.

Community
  • 1
  • 1
brianegge
  • 29,240
  • 13
  • 74
  • 99
2

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

Ahmed
  • 7,148
  • 12
  • 57
  • 96
  • While there is an ANTLR C++ parser, I don't know of anyone that has used it for something production. And I don't think it produces a symbol table, without which you really can't do anything with C++. – Ira Baxter Apr 02 '10 at 05:10
2

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.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • After yo uhave an abstract syntax tree, you will need a symbol table so you know the meaning of symbols. And this is extremely hard to get right. ASTs are NOT enough. – Ira Baxter Apr 02 '10 at 05:09
  • @Ira: The OP sounded like s?he just wanted to append some code to functions. If that doesn't reference variables it should still be doable. Also it might be that they have a good enough assumption on how the code looks. – Joey Apr 02 '10 at 06:55
2

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.

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
2

A robust C++ parser is available with our DMS Software Reengineering Toolkit. It parses a variety of C++ dialects including ANSI, GNU 3/4, MSVS6 and MSVisual Studio 2005 and managaged C++.

It builds ASTs and symbol tables (the latter is way harder than you might think). You can navigate the ASTs, transform into different valid C++ programs, and regenerate code including comments.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
1

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

albert
  • 8,285
  • 3
  • 19
  • 32
Alon
  • 4,862
  • 19
  • 27
1

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.

Steve Gilham
  • 11,237
  • 3
  • 31
  • 37