我想对 C 源代码进行一些转换。我需要一个在 linux 上从源代码生成完整 AST 的工具,以便我可以在这个 AST 上应用我的转换,然后将其转换回 C 源代码。我尝试了ELSA,但它没有被编译。(我使用的是 Ubuntu 8.4)。谁能推荐一个更好的工具/应用程序?
11 回答
我会推荐clang。它有一个相当完整的 C 实现,带有大多数 gcc 扩展,并且代码非常易于理解。他们的 C++ 实现是不完整的,但如果你只关心从 C 代码生成 AST 应该没问题。根据您想要做什么,您可以使用 clang 作为库并直接使用 AST,或者让 clang 将它们转储到控制台。
请参阅pycparser - 用于 C 的纯 Python AST 生成器。
要获得 AST XML 输出,您可以尝试使用MarpaX :: Languages::C::AST中的 cscan 。输出将如下所示:
xml
<cscan>
<typedef_hash>
<typedef id="GLenum" before="unsigned int" after="" file="/usr/include/GL/gl.h"/>
...
Our DMS Software Reengineering Toolkit has been used on huge C systems, parsing, analyzing, transforming, and regenerating C code. Runs on Windows, and will run on Linux under Wine, but it does handle Linux-style (GCC) C code.
I can't emphasize enough the ability to round-trip the C source code: parse, build trees, transform, regenerate compilable C code with the comments and either prettyprinted or with the original programmer's indentation. Few of the other answers here suggest systems that can do that robustly.
The fact that DMS is designed to carry out program transformations (as opposed to other systems suggested in answers here) is also a great advantage. DMS provide tree-pattern matches and rewrites; it augments this with full control and data flow analyis to be used to extend the conditions that you'd like to match. A tool intending to be a compiler is just that, and you'll have a very hard time persuading it not to be a compiler, and an instead to be a transformation engine as the OP requested.
See https://stackoverflow.com/a/2173477/120163 for example ASTs produced by DMS.
www.antlr.org
“我尝试了 ELSA,但它没有被编译。(我使用的是 Ubuntu 8.4)”
来自 scottmcpeak.com/elkhound/ 的 Elkhound 和 Elsa 源代码版本 2005.08.22b 已过时(旧的 C++ 样式 .h 头文件)。
Elsa 正在工作并且是 Oink 的一部分:http ://www.cubewano.org/oink/#Gettingthecode 我刚刚让它在 Ubuntu 9.10 下工作。
使用 gcc 并为其编写自定义后端怎么样?我从来没有做过,也没有研究过 gcc 源代码,所以我不知道这有多难。