推荐保留预处理器指令的 C 前端
我想启动一个涉及转换 C 代码的项目,但我想包含预处理器指令。我不想通过编写自己的 C 解析器来重新发明轮子,所以有人知道可以解析 C 预处理器和 C< 的前端吗? /strong> 代码,并生成可用于重新生成(或漂亮打印)原始源代码的 AST?
例如:
#define FILENAME "filename"
#include <stdio.h>
FILE *f=0;
...
if (file_is_open) {
#ifdef CAN_OPEN_IT
f = fopen(FILENAME, "r");
#else
printf("Unable to open file.\n");
#endif
}
上面的代码应该被解析为一些内存中的表示,可用于重新生成源。换句话说,它不应该像普通的C一样分两个阶段进行处理,首先处理PP指令,然后解析纯C代码。相反,它应该代表整个编译时逻辑,包括预处理器变量。
I'd like to start a project that involves transforming C code, but I'd like to include the preprocessor directives. I don't want to reinvent the wheel by writing my own C parser, so does anyone know of a front-end that can parse C preprocessor and C code, and produce an AST that can be used to re-generate (or pretty-print) the original source?
e.g.,:
#define FILENAME "filename"
#include <stdio.h>
FILE *f=0;
...
if (file_is_open) {
#ifdef CAN_OPEN_IT
f = fopen(FILENAME, "r");
#else
printf("Unable to open file.\n");
#endif
}
The above code should be parsed into some in-memory representation that can be used to re-generate the source. In other words, it should not be processed as normal C in two phases, first processing the PP directives and then parsing pure C code. Rather it should represent the whole compile-time logic including the preprocessor variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看一下 Clang。 (请参阅 http://clang.llvm.org/features.html#applications 。 )
Take a look at Clang. (See http://clang.llvm.org/features.html#applications .)
我们的DMS 软件再工程工具包具有C 前端(和 C++ 前端):
使用以 C 表示法编写的与 C 的 (尚未用于 C++),DMS 还提供:
DMS 已用于处理超大型 C 应用程序,以便提取事实并从中生成新的派生代码原始源基地。
(编辑:2016 年 2 月)
它可以处理 OP 的示例(稍作修复即可使其有效)。
这是稍作修改的源代码:
这是生成的 AST:
您可以在第 8 行将预处理器指令视为“if_directive”。
是的,DMS 也可以漂亮地打印这棵树。以下命令运行解析器以生成 AST,然后运行 DMS PrettyPrinter 以仅从树重新生成源。往返准确;你可以重新编译并得到相同的结果。评论也被保留。
您可以了解 DMS 如何处理 C++。此时,它可以处理 GCC 和 MS 方言的所有 C++14。
Our DMS Software Reengineering Toolkit has a C front end (and a C++ front end) that:
For C (not yet for C++), DMS also provides:
DMS has been used to process extremely large C applications for the purposes of extracting facts and generating new, derived code from the original source base.
(EDIT: Feb 2016)
It can handle the OP's example (with slight fixes to make it valid).
Here's the slightly revised source:
Here is the AST produced:
You can see the preprocessor directives as "if_directive" on line 8.
Yes, DMS can prettyprint this tree, too. The following command runs the parser to produce an AST, and then runs the DMS prettyprinter to regenerate source solely from the tree. The round-trip is accurate; you can recompile and get the same result. Comments are preserved, too.
You can see how DMS handles C++. At this point it handles all of C++14 for GCC and MS dialects.
以GNU gcc编译器为例,预处理源代码所需的标志为
gcc -E mysource.c
,参见此处了解更多信息。至于漂亮的打印,有 缩进 这解释了用法 这里,这有点老了,但仍然值得一提。还有 cflow 可以生成源地图。抱歉,如果我误解了您要找的内容...
Take the GNU gcc compiler, the flags required to pre-process the source is
gcc -E mysource.c
, see here for further information. As for pretty printing it, there's indent and this explains the usage here, this is a bit old, but nonetheless worthy of mention. There is also cflow that can produce a map of the source.Sorry if I misunderstood what you're looking for...
您可以查看 http://www.antlr .org/wiki/display/ANTLR3/ANTLR3+代码+生成+-+C
You can take look at the http://www.antlr.org/wiki/display/ANTLR3/ANTLR3+Code+Generation+-+C