无法使用 Boost Spirit X3 解析空的 C++ 结构
Cannot parse an empty C++ struct with Boost Spirit X3
>我正在尝试解析头文件中定义的C++结构。我开始定义语法,但我有一个问题。
这是我的代码:
#include <boost/spirit/home/x3.hpp>
int main() {
namespace x3 = boost::spirit::x3;
// Parse "#if !defined XXX_X_" or "'#ifndef X_X"
auto Ifndef = x3::skip(x3::space)[(x3::lit('#') >> (x3::lit("ifndef") | (x3::lit("if") >> x3::lit("!defined"))))];
auto HeaderGuardFirstRow = Ifndef >> +(x3::alnum | '_');
// Parse "#define XXX_X" or "# define XXX_X"
auto Define = x3::skip(x3::space)[(x3::lit('#') >> x3::lit("define"))];
auto HeaderGuardSecondRow = Define >> +(x3::alnum | '_');
// Parse
// "
// #if !defined XXX_X_
// #define XXX_X
// "
auto HeaderGuardBegin = HeaderGuardFirstRow >> HeaderGuardSecondRow;
// Parse "#endif" or "# endif"
auto HeaderGuardEnd = x3::skip(x3::space)[x3::lit('#') >> (x3::lit("endif"))];
// Parse variable name like "xxx" or "my_var"
auto VariableName = x3::lexeme[x3::char_("a-zA-Z_") >> *(x3::alnum | x3::lit("_"))];
// Skipper for C++ comments (nested /* */ are not handled for now)
auto SingleLineComment = "//" >> *(x3::char_ - x3::eol) >> (x3::eol | x3::eoi);
auto BlockComment = "/*" >> *(x3::char_ - "*/") >> "*/";
auto Skipper = SingleLineComment | BlockComment | x3::ascii::space;
// Parse
// "
// typedef struct {
// } MyStruct;
// "
// ERROR: This parse does not work
auto StructType = -x3::lit("typedef") >> x3::skip(Skipper)[x3::lit("struct") >> x3::lit('{')] >>
x3::skip(Skipper)[x3::lit('}') >> VariableName >> x3::lit(";")];
// Header grammar. Should parse
// "
// #if !defined XXX_H
// #define XXX_H
// typedef struct {
// } MyStruct;
// #endif
// "
auto grammar = HeaderGuardBegin >> *(StructType) >> HeaderGuardEnd;
std::string data01(R"xx(
#if !defined XXX_H
#define XXX_H
#endif
)xx");
bool r = phrase_parse(
data01.begin(),
data01.end(),
grammar,
Skipper
);
std::string data02(R"xx(
#if !defined XXX_H
// Single line comment
#define XXX_H
#endif // !XXX_H
)xx");
r = phrase_parse(
data02.begin(),
data02.end(),
grammar,
Skipper
);
std::string data03(R"xx(
#if !defined XXX_H
#define XXX_H
typedef struct {
} MyStruct;
#endif
)xx");
// r = false: This parsing does not work.
r = phrase_parse(
data03.begin(),
data03.end(),
grammar,
Skipper
);
return 0;
}
在代码中有三个字符串要解析:一个只有标头保护,第二个与第一个类似,但有一些C++注释,第三个字符串具有空结构。
这是最后一个无法解析的,我不明白为什么。在我用于结构StructType
的语法中,我首先检查可选的typedef
,然后关键字struct
可以附加或不附加的{
字符,然后我搜索}
字符,后跟变量名称,后跟;
。
我不明白错误在哪里。我在解析空结构时做错了什么?
几件事:
- 船长由周围环境继承
- 您在标头保护中的"令牌"周围没有
lexeme[]
,因此它将匹配包括typedefstruct
space
因为它还包括行端。
您可以简化以下操作:
住在科里鲁
#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <iomanip>
int main() {
namespace x3 = boost::spirit::x3;
// Parse "#if !defined XXX_X_" or "'#ifndef X_X"
auto const Ifndef = (x3::lit('#') >> (x3::lit("ifndef") | (x3::lit("if") >> x3::lit("!defined"))));
auto const HeaderGuardFirstRow = Ifndef >> x3::lexeme[+(x3::alnum | '_')];
// Parse "#define XXX_X" or "# define XXX_X"
auto const Define = (x3::lit('#') >> x3::lit("define"));
auto const HeaderGuardSecondRow = Define >> x3::lexeme[+(x3::alnum | '_')];
// Parse
// "
// #if !defined XXX_X_
// #define XXX_X
// "
auto const HeaderGuardBegin = HeaderGuardFirstRow >> HeaderGuardSecondRow;
// Parse "#endif" or "# endif"
auto const HeaderGuardEnd = x3::lit('#') >> (x3::lit("endif"));
// Parse variable name like "xxx" or "my_var"
auto const VariableName = x3::lexeme[x3::char_("a-zA-Z_") >> *(x3::alnum | x3::lit("_"))];
// Skipper for C++ comments (nested /* */ are not handled for now)
auto const SingleLineComment = "//" >> *(x3::char_ - x3::eol) >> (x3::eol | x3::eoi);
auto const BlockComment = "/*" >> *(x3::char_ - "*/") >> "*/";
auto const Skipper = SingleLineComment | BlockComment | x3::ascii::space;
auto const StructType =
-x3::lit("typedef")
>> "struct" >> '{' >> '}' >> VariableName
>> ";"
;
// Header grammar.
auto grammar = HeaderGuardBegin >> *StructType >> HeaderGuardEnd;
for (std::string const data : {
R"xx(
#if !defined XXX_H
#define XXX_H
#endif
)xx",
R"xx(
#if !defined XXX_H
// Single line comment
#define XXX_H
#endif // !XXX_H
)xx",
R"xx(
#if !defined XXX_H
#define XXX_H
typedef struct {
// aloha
} MyStruct;
typedef struct { /* caramba */ } MyOtherStruct
;
#endif
)xx" }) {
auto f = data.begin(), l = data.end();
std::cout << "Parsing " << std::quoted(data) << "n";
if (phrase_parse(f, l, grammar, Skipper)) {
std::cout << "Parsedn";
} else {
std::cout << "Failed to parsen";
}
if (f!=l) {
std::cout << "Remaining unparsed: " << std::quoted(std::string(f,l)) << "n";
}
}
}
指纹
Parsing "
#if !defined XXX_H
#define XXX_H
#endif
"
Parsed
Parsing "
#if !defined XXX_H
// Single line comment
#define XXX_H
#endif // !XXX_H
"
Parsed
Parsing "
#if !defined XXX_H
#define XXX_H
typedef struct {
// aloha
} MyStruct;
typedef struct { /* caramba */ } MyOtherStruct
;
#endif
"
Parsed
sehe 在保留代码方面做得非常出色。但是由于我在我想我会发布之后对你是什么有自己的想法。我确实从中了解到船长可能非常复杂。另一方面,我通常跳过omit
.
由于您似乎希望扔掉所有起跑线和评论#
因此我将其归结为。在某些时候,我确定您将解析为一个属性。所以我从结构名称开始,因此dest
字符串。
只是另一种看待它的方式。
#include <iostream>
#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
using namespace boost::spirit::x3;
// Parse all/skip"#..."
auto const def = lit("#") >> omit[lexeme[*char_("a-zA-Z_! ")]];
// Skipper for C++ comments (nested /* */ are not handled for now)
auto const comment = ("//" >> omit[*(char_ - eol)]) | ("/*" >> omit[*(char_ - "*/")] >> "*/");
auto const skipper = *(def | comment | space);
// Parse variable name like "xxx" or "my_var"
auto const name = rule<struct name, std::string>("name") = *char_("a-zA-Z_");
auto const struct_rule = lit("typedef") >> "struct" >> '{' >> omit[*(char_-'}')] >> '}' >> name >> ';';
auto const final = skipper >> -struct_rule >> skipper;
void parse(char* in)
{
std::string str(in);
auto it = str.begin();
std::string dest;
bool r = phrase_parse(it, str.end(), final, space, dest);// , dest);
std::cout << std::boolalpha << "r: " << r << std::endl
<< std::string(it, str.end()) << std::endl
<< "DEST: " << dest << std::endl;
}
int main()
{
parse(R"xx(
#if !defined XXX_H
#define XXX_H
#endif
)xx");
parse(R"xx(
#if !defined XXX_H
// Single line comment
#define XXX_H
#endif // !XXX_H
)xx");
parse(R"xx(
#if !defined XXX_H
#define XXX_H
typedef struct {
} MyStruct;
#endif
)xx");
return 0;
}
指纹:
r: true
DEST:
r: true
DEST:
<name>
<try> MyStruct;ntt#endifn</try>
<success>;ntt#endifnt </success>
<attributes>[M, y, S, t, r, u, c, t]</attributes>
</name>
r: true
DEST: MyStruct
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- C++概念:如何使用'concept'检查模板化结构的属性?