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

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

问题描述

我想开始一个涉及转换 C 代码的项目,但我想要包含预处理器指令。我不想通过编写我自己的 C 解析器重新发明轮子,所以任何人都知道一个前端可以解析 C 预处理器和 C 代码,并产生一个可用于重新生成(或漂亮打印)原始源的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.\n");
#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天全站免登陆