使用 boost::spirit 解析类似 c 结构的声明
Parse c-struct-like declaration with boost::spirit
我想解析一个类似 c 结构的声明,它有一些标量或数组作为成员。然后,可以为 HDF5 序列化生成具有此 c-struct 定义的C++头文件。但是当尝试同时使用 boost::spirit 处理标量和数组时,我发现了一些困难。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
namespace fusion = boost::fusion;
struct struct_field
{
std::string type;
std::string name;
int dim;
};
struct struct_body
{
std::string name;
std::vector<struct_field> fields;
};
BOOST_FUSION_ADAPT_STRUCT(
struct_field,
(std::string, type)
(std::string, name)
(int, dim)
)
BOOST_FUSION_ADAPT_STRUCT(
struct_body,
(std::string, name)
(std::vector<struct_field>, fields)
)
template <typename Iterator, typename Skipper>
struct preprocessor :
qi::grammar<Iterator, struct_body(), Skipper>
{
preprocessor() :
preprocessor::base_type(body)
{
using namespace qi::labels;
using qi::eol;
using qi::lit;
using qi::lexeme;
using qi::int_;
using ascii::char_;
using phoenix::at_c;
using phoenix::push_back;
vartype =
*lit(' ') >> lexeme[+(char_ - ' ') [_val += _1]];
varname =
(*lit(' ') >> lexeme[+(char_ - '[') [_val += _1]]) |
(*lit(' ') >> lexeme[+(char_ - ';') [_val += _1]] >> ';');
vardim = '[' >> int_ [_val += _1] >> "];";
strucname =
"declare(" >>
lexeme[+(char_ - ')')[_val += _1]] >>
')' >>
eol;
field =
vartype [at_c<0>(_val) = _1] >>
varname [at_c<1>(_val) = _1] >>
-vardim [at_c<2>(_val) = _1] >>
eol;
body =
strucname [at_c<0>(_val) = _1] >>
'(' >> eol >>
*(field [push_back(at_c<1>(_val), _1)]) >>
')' >> -eol;
}
qi::rule<Iterator, struct_body(), Skipper> body;
qi::rule<Iterator, struct_field(), Skipper> field;
qi::rule<Iterator, std::string(), Skipper> strucname;
qi::rule<Iterator, std::string(), Skipper> vartype, varname;
qi::rule<Iterator, int(), Skipper> vardim;
};
template<typename Iterator, typename Skipper>
bool parse(Iterator &first, Iterator end, Skipper const &skipper, struct_body &mystruct)
{
preprocessor<Iterator, Skipper> g;
return qi::phrase_parse(first, end, g, skipper, mystruct);
}
int main(int argc, char **argv)
{
std::string storage = "declare(grid_point)rn(rn int id[1];rn int cp[1];rn double pos[3];rn)";
std::string::const_iterator iter = storage.begin();
std::string::const_iterator end = storage.end();
struct_body mystruct;
bool result = parse(iter, end, qi::blank, mystruct);
if (result && iter == end)
{
std::cout << mystruct.fields.size() << " fields are parsed." << std::endl;
BOOST_FOREACH(struct_field const& field, mystruct.fields)
{
std::cout << field.type << " : " << field.name << " [ " << field.dim << " ] ;" << std::endl;
}
}
}
如我们所见,所有成员都声明为数组。否则,无法正确解析标量。
declare(grid_point)
(
int id;
int cp;
double pos[3];
)
无法解析上述声明。似乎 boost::spirit 总是在 [dim] 上进行激进的比赛。实际上 [dim] 只对数组而不是标量有用。那么如何解决这个问题呢?
-
首先,所有的语义操作都是多余的,因为它们只是复制标准属性传播规则。(提升精神:"语义行为是邪恶的"?以下内容完全等效:http://paste.ubuntu.com/10049892/
-
你似乎对船长感到困惑。你不能有效地使用
*lit(' ')
因为空白已被跳过
-
变量名规则
varname = (*lit(' ') >> lexeme[+(char_ - '[') ]) | (*lit(' ') >> lexeme[+(char_ - ';') ] >> ';');
如果你没有
[
,这会吃到线的尽头.这甚至包括;
.修复它,例如喜欢varname = lexeme[+(char_ - "[;") ];
-
关于发现的船长混淆,我建议简化:
vartype = +graph; varname = +(graph - char_("[;")); vardim = '[' >> int_ >> "]";
我没有继续
lexeme[]
,而是从vartype
和varname
上掉下了Skipper
(另请参阅Boost精神船长问题) -
请注意,我还从
varname
和vardim
规则中删除了';'
。我的意思是。说真的,无论如何,';
'从来都不是其中的一部分! -
相反,只需将
';'
放在field
规则中,vardim
可选:field = vartype >> varname >> -vardim >> ';' >> eol;
-
用精神来调试你的规则!
#define BOOST_SPIRIT_DEBUG BOOST_SPIRIT_DEBUG_NODES((body)(field)(strucname)(varname)(vartype)(varname)(vardim))
-
一般观察:语法似乎与空格无关。使用
qi::blank
作为船长有点反模式。(例如,我遇到了一个惊喜,因为我使用了原始字符串文字,但它没有解析,因为它以换行符开头。解决此问题留给读者练习:)
总而言之,这是有效的修改后的示例:
住在科里鲁
//#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
struct struct_field
{
std::string type;
std::string name;
int dim;
};
struct struct_body
{
std::string name;
std::vector<struct_field> fields;
};
BOOST_FUSION_ADAPT_STRUCT(
struct_field,
(std::string, type)
(std::string, name)
(int, dim)
)
BOOST_FUSION_ADAPT_STRUCT(
struct_body,
(std::string, name)
(std::vector<struct_field>, fields)
)
template <typename Iterator, typename Skipper>
struct preprocessor :
qi::grammar<Iterator, struct_body(), Skipper>
{
preprocessor() :
preprocessor::base_type(body)
{
using namespace qi::labels;
using qi::eol;
using qi::graph;
using qi::lit;
using qi::lexeme;
using qi::int_;
using ascii::char_;
vartype = +graph;
varname = +(graph - char_("[;"));
vardim = '[' >> int_ >> "]";
strucname =
"declare" >> lit('(') >> +~char_(')') >> ')' >>
eol;
field =
vartype >>
varname >>
-vardim >>
';' >>
eol;
body =
strucname >>
'(' >> eol >>
*field >>
')' >> -eol;
BOOST_SPIRIT_DEBUG_NODES((body)(field)(strucname)(varname)(vartype)(varname)(vardim))
}
qi::rule<Iterator, struct_body(), Skipper> body;
qi::rule<Iterator, struct_field(), Skipper> field;
qi::rule<Iterator, std::string(), Skipper> strucname;
qi::rule<Iterator, int(), Skipper> vardim;
// lexemes
qi::rule<Iterator, std::string()> vartype, varname;
};
template<typename Iterator, typename Skipper>
bool parse(Iterator &first, Iterator end, Skipper const &skipper, struct_body &mystruct)
{
preprocessor<Iterator, Skipper> g;
return qi::phrase_parse(first, end, g, skipper, mystruct);
}
int main()
{
std::string const storage = "declare(grid_point)rn(rn int id;rn int cp;rn double pos[3];rn)";
std::string::const_iterator iter = storage.begin();
std::string::const_iterator end = storage.end();
struct_body mystruct;
bool result = parse(iter, end, qi::blank, mystruct);
if (result && iter == end)
{
std::cout << mystruct.fields.size() << " fields are parsed." << std::endl;
BOOST_FOREACH(struct_field const& field, mystruct.fields)
{
std::cout << field.type << " : " << field.name << " [ " << field.dim << " ] ;" << std::endl;
}
}
}
指纹
3 fields are parsed.
int : id [ 0 ] ;
int : cp [ 0 ] ;
double : pos [ 3 ] ;
要获得默认值,请将其设为
vardim = '[' >> int_ >> "]" | qi::attr(1);
field = vartype >> varname >> vardim >> ';' >> eol;
在这种情况下,输出变为
3 fields are parsed.
int : id [ 1 ] ;
int : cp [ 1 ] ;
double : pos [ 3 ] ;
- 为什么 C++ 中的以下结构声明会导致退出 127?
- 在.cpp文件之间传递结构声明,而不使用全局头文件
- 结构声明中的 if 语句C++
- 这个嵌套结构声明是什么意思?
- 在结构声明的分号之前声明的结构变量是否为全局变量
- 为什么结构声明在C++中违反了 ODR
- 错误:多个文件中的结构声明冲突
- 为什么调用定义的构造函数会导致结构声明中出现错误,我该如何修复它
- C++数据结构声明
- 相互依赖的结构声明
- 结构声明的数组
- 结构声明中的":"是什么意思
- 面向C++的main.cpp中的可视化结构声明问题
- 当某些结构字段被省略或与结构声明中的顺序不同时,如何实现正确的解析?
- 编译器在转发结构声明上出错
- C++生成器 XE4 中的结构声明
- 我无法理解c ++中的结构声明
- 嵌套结构声明以及如何访问它们
- 将包含单个数组的结构声明为packed有什么缺点吗
- 定义之前的结构声明