推荐保留预处理器指令的C前端 [英] Recommend C front-end that preserves preprocessor directives

查看:42
本文介绍了推荐保留预处理器指令的C前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始一个涉及转换C 代码的项目,但我想包括预处理器指令.我不想通过编写自己的 C 解析器来重新发明轮子,所以有人知道可以解析 C 预处理器和 C<的前端吗?/strong> 代码,并生成可用于重新生成(或漂亮打印)原始源代码的 AST?

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?

例如:

#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.
");
#endif
}

上面的代码应该被解析成一些可用于重新生成源的内存表示.换句话说,它不应该像普通C那样分两个阶段处理,首先处理PP指令,然后解析纯C代码.相反,它应该代表整个编译时逻辑,包括预处理器变量.

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.

推荐答案

看看 Clang.(参见 http://clang.llvm.org/features.html#applications .)

Take a look at Clang. (See http://clang.llvm.org/features.html#applications .)

这篇关于推荐保留预处理器指令的C前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆