- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想解析一个类似 c-struct 的声明,它有一些标量或数组作为成员。然后可以为 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"?)。以下是完全等价的:http://paste.ubuntu.com/10049892/
你似乎对 skipper 感到困惑。你不能有用地使用
*lit(' ')
因为空格已经被跳过了
变量名规则
varname =
(*lit(' ') >> lexeme[+(char_ - '[') ]) |
(*lit(' ') >> lexeme[+(char_ - ';') ] >> ';');
如果你没有 [
,这会一直吃到行尾。这甚至包括 ;
。修复它,例如喜欢
varname = lexeme[+(char_ - "[;") ];
关于发现的 skipper 困惑,我建议简化:
vartype = +graph;
varname = +(graph - char_("[;"));
vardim = '[' >> int_ >> "]";
我没有使用 lexeme[]
,而是从 vartype
和 varname
中删除了 Skipper
(< em>另见 Boost spirit skipper issues )
请注意,我还从 varname
和 vardim
规则中删除了 ';'
。我是说。说真的,无论如何,';
' 从来都不是其中的一部分!
相反,只需将 ';'
放在 field
规则中,使 vardim
可选:
field =
vartype >>
varname >>
-vardim >>
';' >>
eol;
用 spirit 调试你的规则!
#define BOOST_SPIRIT_DEBUG
BOOST_SPIRIT_DEBUG_NODES((body)(field)(strucname)(varname)(vartype)(varname)(vardim))
一般观察:语法似乎与空格无关。使用 qi::blank
作为 skipper 有点反模式。 (例如,我遇到了一个惊喜,因为我使用了原始字符串文字,但它没有解析,因为它以换行符开头。)。解决这个问题留给读者作为练习:)
总而言之,这是修改后的有效示例:
//#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++ - 使用 boost::spirit 解析类似 c-struct 的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28314722/
学习SQL。有一个简单的带有字段标题的桌面游戏。我想根据标题进行搜索。如果我有一款名为 Age of Empires III: Dynasties 的游戏,并且我使用 LIKE 和参数 Age of
我正在尝试为以下数据结构创建镜头。我正在使用lens-family . data Tree = Tree { _text :: String, _subtrees ::
我发现很难理解这一点。比如说,在 Python 中,如果我想要一个根据用户输入在循环中修改的列表,我会有这样的内容: def do_something(): x = [] while(
我有一个像这样的 mysql 查询 SELECT group_name FROM t_groups WHERE group_name LIKE '%PCB%'; 结果是 group_name ----
我的数据库表中有超过一百万条记录。当我使用like时非常慢,当我使用match against时他们丢失了一些记录。 我创建帮助表: 标签列表 tag_id tag_name tag_rel_me
我在我的一个 Java 项目中使用 JXBrowser 来简单显示 googlemaps 网页,以便我可以在那里跟踪路线,但最近我想改进该项目,但我的问题是 JXBrowser 的许可证过期(只有一个
小问题:如何将 mysql_escape_string 变量包含在 like 子句中? "SELECT * FROM table WHERE name LIKE '%". %s . "%'" 或
我尝试使用几个jquery消息插件,例如alertify . 但我注意到的主要事情是系统消息框会停止后台功能,直到用户响应。其他插件没有此功能。 有没有办法将此功能添加到 jquery 插件中?可以扩
我是 Ruby 新手。我过去使用过 shell。我正在将 shell 程序转换为 ruby。我有以下命令 cmd="cat -n " + infile + " | grep '127.0.0.1
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
当我研究 Rust 时,我试图编写一个 Rust 函数来查看任何可迭代的字符串。 我最初的尝试是 fn example_1(iter: impl Iterator); fn example_2(ite
我必须在我的项目中使用代码拆分。但无论如何,第一次初始下载有一些代码。 现在我想向最终用户展示代码下载(.cache.html - 或其他代码拆分)的进度,例如 gmail 启动进度。 请你帮帮我。
我今天找到了一个错误,它最终是由我代码中的以下片段引起的(我试图在列表中仅过滤“PRIMARY KEY”约束): (filter #(= (% :constraint_type "PRIMARY KE
我正在尝试在关键字段上实现检查约束。关键字段由 3 个字符的前缀组成,然后附加数字字符(可以手动提供,但默认是从序列中获取整数值,然后将其转换为 nvarchar)。关键字段定义为 nvarhcar(
我正在尝试使用以下方式创建 List 实例: List listOne = new ArrayList(); List listTwo = new ArrayList(){}; List listTh
我过去曾为 iOS 开发过,最近转向了 mac 开发。我开始了一个“感受”事物的项目,但遇到了一个问题。我试图创建一个 NSTableView 来显示多个项目,包括一个标签、一个 2 UIImageV
我正在尝试编写一个查询,该查询将返回哪些主机缺少某个软件: Host Software A Title1 A
AFAIK,在三种情况下别名是可以的 仅限定符或符号不同的类型可以互为别名。 struct 或 union 类型可以为包含在其中的类型设置别名。 将 T* 转换为 char* 是可以的。 (不允许相反
\s 似乎不适用于 sed 's/[\s]\+//' tempfile 当它为工作时 sed 's/[ ]\+//' tempfile 我正在尝试删除由于命令而出现在每行开头的空格: nl -s ')
我正在使用 ocamlgraph 在 ocaml 中编写程序,并想知道是否要将其移植到 F# 我有哪些选择?谢谢。 最佳答案 QuickGraph .Net 最完整的图形库之一 关于F# 图形库(类似
我是一名优秀的程序员,十分优秀!