看起来您可以使用简单的岛语法来删除注释、字符串文字和折叠空格(制表符,'\n')。由于我正在使用 AX †,因此我为您编写了一个快速语法‡。您可以使用 Boost.Spirit 编写一组类似的规则。
#include <axe.h>
#include <string>
template<class I>
std::string clean_text(I i1, I i2)
{
// rules for non-recursive comments, and no line continuation
auto endl = axe::r_lit('\n');
auto c_comment = "/*" & axe::r_find(axe::r_lit("*/"));
auto cpp_comment = "//" & axe::r_find(endl);
auto comment = c_comment | cpp_comment;
// rules for string literals
auto esc_backslash = axe::r_lit("\\\\");
auto esc_quote = axe::r_lit("\\\"");
auto string_literal = '"' & *(*(axe::r_any() - esc_backslash - esc_quote)
& *(esc_backslash | esc_quote)) & '"';
auto space = axe::r_any(" \t\n");
auto dont_care = *(axe::r_any() - comment - string_literal - space);
std::string result;
// semantic actions
// append everything matched
auto append_all = axe::e_ref([&](I i1, I i2) { if(i1 != i2) result += std::string(i1, i2); });
// append a single space
auto append_space = axe::e_ref([&](I i1, I i2) { if(i1 != i2) result += ' '; });
// island grammar for text
auto text = *(dont_care >> append_all
& *comment
& *string_literal >> append_all
& *(space % comment) >> append_space)
& axe::r_end();
if(text(i1, i2).matched)
return result;
else
throw "error";
}
所以现在你可以进行文本清理:
std::string text; // this is your function
text = clean_text(text.begin(), text.end());
您可能还需要为多余的 ';'、空块 {} 等创建规则。您可能还需要合并字符串文字。您需要走多远取决于生成函数的方式,您最终可能会编写相当大一部分的 C 语法。
† AX 库即将在 boost 许可下发布。
‡我没有测试代码。