使用 boost::spirit 解析类似 c-struct 的声明
Parse c-struct-like declaration with boost::spirit
我想解析一个类似 c 结构的声明,它有一些标量或数组作为成员。然后可以为 HDF5 序列化生成具有此 c 结构定义的 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)\r\n(\r\n int id[1];\r\n int cp[1];\r\n double pos[3];\r\n)";
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] 只需要数组而不是标量。那么如何解决这个问题呢?
首先,所有你的语义动作都是多余的,因为它们只是复制了标准的属性传播规则。 (Boost Spirit: "Semantic actions are evil"?). The following is exactly equivalent: 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 spirit skipper issues)
请注意,我还从 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
作为船长有点反模式。 (例如,我 运行 感到惊讶,因为我使用了原始字符串文字,但它没有解析,因为它以换行符开头。)。解决此问题留作 reader :)
的练习
总而言之,这是修改后的有效示例:
//#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)\r\n(\r\n int id;\r\n int cp;\r\n double pos[3];\r\n)";
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 结构的声明,它有一些标量或数组作为成员。然后可以为 HDF5 序列化生成具有此 c 结构定义的 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)\r\n(\r\n int id[1];\r\n int cp[1];\r\n double pos[3];\r\n)";
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] 只需要数组而不是标量。那么如何解决这个问题呢?
首先,所有你的语义动作都是多余的,因为它们只是复制了标准的属性传播规则。 (Boost Spirit: "Semantic actions are evil"?). The following is exactly equivalent: 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 spirit skipper issues)请注意,我还从
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
作为船长有点反模式。 (例如,我 运行 感到惊讶,因为我使用了原始字符串文字,但它没有解析,因为它以换行符开头。)。解决此问题留作 reader :) 的练习
总而言之,这是修改后的有效示例:
//#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)\r\n(\r\n int id;\r\n int cp;\r\n double pos[3];\r\n)";
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 ] ;