I heard there are some classes/libs written in C++ that can be used for parsing C/C++ code. What I need, is to read all functions from the source file and compare them, to know where is the difference between. I don't really know how can I achieve that. What I can use is only headers/libs/classes and no software solutions.
Asked
Active
Viewed 218 times
0
-
Are you looking for something like this: http://stackoverflow.com/questions/10912349/similar-code-detector ? – W.F. Mar 19 '15 at 09:24
-
Not exactly, but yeah, very close to it. I need to write my own kind of tool. And what i need is to get functions name from 2 source file and compare are there are changes between them. But i don't really know how to do this correct. – user3416803 Mar 19 '15 at 09:29
-
1Just to make sure, you actually mean *doing this in your own C++ code*, not just in general? (Because there are quite good diff tools out there...) – DevSolar Mar 19 '15 at 09:41
-
Does this help: http://gccxml.github.io/HTML/Index.html – Mar 19 '15 at 09:42
-
1You can't just use `grep` and `diff` to parse and compare? If you only need to see where the changes are, rather than an interpretation of these changes, these utilities should do the job if used properly. – JorenHeit Mar 19 '15 at 10:03
-
1`clang` provides capabilities of dumping AST: http://clang.llvm.org/docs/IntroductionToTheClangAST.html – myaut Mar 19 '15 at 10:04
-
Yes, the problem is that i need to implement it by myself. But using third-party classess/headers is not forbidden. – user3416803 Mar 19 '15 at 10:14
-
That's a rather unusual comment - parsing C++ is far too complex for a homework assignment. And in professional settings, you would not implement this yourself. (Even Microsoft bought the C++ parser for Visual Studio!) – MSalters Mar 19 '15 at 14:06
2 Answers
4

Dmitry Ledentsov
- 3,620
- 18
- 28
-
That looks pretty good, but is it possible to include clang as a header file to use in my project? I didn't find how to do it in MSVS 2008 or so. – user3416803 Mar 19 '15 at 10:16
-
-
@user3416803 the question is still, what are you trying to do, and why, and do you really need to do it? What are you trying to achieve by diff-ing C++ source? – Dmitry Ledentsov Mar 19 '15 at 14:12
-
I've got source files of two version of project (old and new). I need C++ program, that will compare files, find functions and print what function have been modified and what is that modification. I hope it looks more clear now. – user3416803 Mar 19 '15 at 15:16
-
that's quite a complicated task. You could open a company that would build that kind of software and make good money. BTW, the python binding would be a better choice for your development, and diffing should probably be done using a [diffing](https://code.google.com/p/google-diff-match-patch/) [library](https://code.google.com/p/dtl-cpp/wiki/Tutorial), combining the information from source parsing – Dmitry Ledentsov Mar 19 '15 at 15:22
-
Lol, you probably right, but i need to do it with cpp. Looks like there are really much more examples on python on this task. – user3416803 Mar 19 '15 at 15:39
-
Well, now you know where to look for answers at least: libclang API, diff library APIs – Dmitry Ledentsov Mar 19 '15 at 15:57
3
Parsing the C++ grammar isn't a trivial task. However to not to reinvent the wheel (parsing C++ grammar tool must have been already done right?) you could use lex and bison with already defined C++ parser like: http://www.computing.surrey.ac.uk/research/dsrg/fog/CxxGrammar.y And then modify it to your needs. I guess you would also need the lex and bison basics. Start with e.g. This: http://aquamentus.com/flex_bison.html
Good luck!

W.F.
- 13,888
- 2
- 34
- 81
-
it is unlikely to lead to success, considering the complexity of today's C++. + see [c++faq](http://isocpp.org/wiki/faq/compiler-dependencies#yaccable-grammar) – Dmitry Ledentsov Mar 19 '15 at 19:14
-
@DmitryLedentsov you may be right, on the other hand as far as I know C++ can be perceived as a context-free grammar and as such the yacc-able C++ grammar can be created, if there isn't one open source sociate sooner or later will provide it. – W.F. Mar 19 '15 at 19:35
-
2
-
@Poldie: Good catch, still doesn't change the sense of using yacc-able parser here :) – W.F. Mar 20 '15 at 12:22