为 boost::spirit 中的解析器属性提供默认值
我一直在一个项目中实现 boost::spirit ,我的挑战之一是直接解析到以下类型的容器中:
map<string, string>
我快到了。我遇到的问题是自动分配 std::pair 的键值。也就是说,输入字符串中的每个标记都有一个预先确定的键,我希望在解析标记时将其自动插入到该对中。
我想我已经很接近了,但也许不是......这是(截断的)语法:
command =
string( "select" )
;
key = string( "command" ) | qi::attr( std::string("command") );
command_pair = key >> ' ' >> command;
start =
command_pair >> *command_pair
;
qi::rule<Iterator, std::string()> command;
qi::rule<Iterator, std::pair<std::string, std::string>()> command_pair;
qi::rule<Iterator,parserMap()> start;
最终结果是在命令行上键入:
select
并让解析器插入“命令”作为键,就像我键入的一样:
command select
因此,访问 map["command"] 元素将返回值“select”。
问题是,我无法让 qi::attr() 完成这项工作。也就是说,如果我输入“command select”,而不仅仅是“select”,它就会起作用。
I've been implementing boost::spirit into a project and one of my challenges is to parse directly into a container of the type:
map<string, string>
I'm almost there. The issue I've run up against is assigning the key value of the std::pair automatically. That is, each token in my input string has a pre-determined key, and I want that to be automatically inserted ito the pair when the token is parsed.
I think I'm close, but maybe not... Here's the (truncated) grammar:
command =
string( "select" )
;
key = string( "command" ) | qi::attr( std::string("command") );
command_pair = key >> ' ' >> command;
start =
command_pair >> *command_pair
;
qi::rule<Iterator, std::string()> command;
qi::rule<Iterator, std::pair<std::string, std::string>()> command_pair;
qi::rule<Iterator,parserMap()> start;
The end result is to type on the command line:
select
and have the parser insert "command" as the key, as though I'd typed:
command select
thus, accessing the map["command"] element will return a value of "select".
The problem is, I can't get qi::attr() to do the job. That is, it works if I type "command select", but not just "select".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来我让事情变得比需要的更困难了。
解决方案位于我未引用的部分代码中。我使用 parse 而不是phrase_parse() 来调用我的语法。启用自动空格跳过。
It would appear I was making it more difficult than need be.
The solution lay in a part of the code I hadn't quoted. I was calling my grammar using parse and not phrase_parse(). Enabling automatic space-skipping.