所以有一个像remixsettings_bits=1; wysiwyg=1,2,3,abc; remixclosed_tabs=0; remixgroup_closed_tabs=786432; remixlang=0; remixchk=5; remixsid=35d4f9907281708019490d07728c27ca5c10e5de7a869c322222225e3219e; audio_vol=100
我想知道如何使用boost :: spirit 解析tham到map name
< - > value
并且能够使用boost :: spirit写回来的字符串?
更新: 所以我做了什么:
#include <iostream>
#include <sstream>
#include <string>
#include <map>
//...
std::map<std::string, std::string> user_control::parse_cookie( std::string cookie_data )
{
std::map<std::string, std::string> parsed_cookie;
std::string token, token2;
std::istringstream iss(cookie_data);
while ( getline(iss, token, ' ') )
{
std::string name, val;
std::istringstream iss2(token);
int num = 0 ;
while ( getline(iss2, token2, '=') )
{
if ( num == 0)
{
name = …
Run Code Online (Sandbox Code Playgroud) 我是boost :: spirit的新手.我编写了程序来解析一个SQL语句,比如"select*from table where conditions".它编译失败.报告了大量模板错误.那么,有人会帮助我吗?
#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
struct db_select {
void exec() {}
std::string filed;
std::string table;
std::string condition;
};
std::ostream& operator<<(std::ostream& os, const db_select& se) {
return os << "filed: " << se.filed << " table: " << se.table << " condition: " << se.condition;
}
template <class Iterator>
struct selecter : qi::grammar<Iterator, db_select (), ascii::space_type> {
selecter() : selecter::base_type(se) {
se %= "select" >> +qi::char_ …
Run Code Online (Sandbox Code Playgroud) 我试图添加一个大于运算符>
到ast:代码与文档中的代码95%相同.
下面有两个兴趣点
term
该编译失败,因为我还没有理解语义动作尚未:不知道如何绑定lhs
的lhs > rhs
,通过凤和语义动作.对于Spirit的常规用户来说,解决方案应该是微不足道的,但我仍在学习,到目前为止只能通过示例.
任何帮助,将不胜感激.TIA.
码
#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/config/warning_disable.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/classic_symbols.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp> // std::regex not fully implemented in stdc++ yet
#include <string>
#include <map>
#include <utility>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
struct binary_op;
struct unary_op;
struct nil …
Run Code Online (Sandbox Code Playgroud) 我看到一个错误,我没有看到解决方案.一,相关代码:
namespace C {
struct RangeEntry {
size_t byte;
boost::optional<size_t> bit;
};
struct Range {
RangeEntry firstPart;
boost::optional<RangeEntry> secondPart;
boost::optional<size_t> shift;
};
}
BOOST_FUSION_ADAPT_STRUCT(
C::RangeEntry,
(size_t, byte)
(boost::optional<size_t>, bit)
)
BOOST_FUSION_ADAPT_STRUCT(
C::Range,
(C::RangeEntry , firstPart)
(boost::optional<C::RangeEntry> , secondPart)
(boost::optional<size_t> , shift)
)
... Declare the rules ...
karma::rule<Iterator, C::Range()> range;
karma::rule<Iterator, C::RangeEntry()> range_part;
... Define rules ...
range_part %= no_delimit[ulong_ << -(lit(":") << ulong_)];
range %= no_delimit[range_part << -(lit("-") << range_part)] << -(lit("<<") << ulong_);
Run Code Online (Sandbox Code Playgroud)
在该range %=
部分,我得到编译错误
/usr/include/boost/spirit/home/karma/numeric/detail/numeric_utils.hpp:504:30: …
Run Code Online (Sandbox Code Playgroud) 为什么使用以下规则的解析器返回一个空容器?有3条规则.一个用于解析除双引号之外的字符串,第二个解析一对(例如"col1":2),第三个解析这些对的向量.MSVS2012中以下程序的输出是
parse success
result: '' : 0
result: '' : 0
result: '' : 0
Run Code Online (Sandbox Code Playgroud)
.
namespace parsers
{
spirit::qi::rule< iterator, column_name_t() > quoted_string =
spirit::qi::lexeme["\"" >> +~spirit::qi::char_("\"") >> "\""];
spirit::qi::rule< iterator, column_and_aggregate(), spirit::qi::space_type > agg_pair =
quoted_string//::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )]
> ':'
// A rule validation technic is used below.
> spirit::int_[spirit::qi::_pass = (spirit::qi::_1 >=AVG && spirit::qi::_1<=SUM)];//::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )];
spirit::qi::rule< iterator, column_and_aggregate_container(), spirit::qi::space_type > aggregates_parser =
'{'
> agg_pair/*[phoenix::push_back(spirit::qi::_val, spirit::qi::_1)]*/ % ',' // N.B.!!! list-redux technic …
Run Code Online (Sandbox Code Playgroud) 我想创建解析器来解析以alpha或_开头的标识符,它可能在正文中有alpha,num或_
这是我到目前为止:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/home/support/iterators/line_pos_iterator.hpp>
#include <boost/spirit/repository/include/qi_confix.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
using namespace boost::spirit;
#include <boost/fusion/include/adapt_struct.hpp>
////////////////////////////////
// extra facilities
struct get_line_f
{
template <typename> struct result { typedef size_t type; };
template <typename It> size_t operator()(It const& pos_iter) const
{
return get_line(pos_iter);
}
};
struct Position
{
Position()
: line(-1)
{
}
size_t line;
};
struct Identifier : public Position
{
Identifier()
: Position()
, name()
{
}
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT(Identifier,
(std::string, name) …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个小程序来处理一个大文本文件并进行一些替换.问题是它永远不会停止分配新的内存,所以最终它会耗尽内存.我已经将它简化为一个简单的程序,只需计算行数(参见下面的代码),同时仍然分配越来越多的内存.我必须承认,我对提升和提升精神知之甚少.你能告诉我我做错了什么吗?太感谢了!
#include <string>
#include <iostream>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
// Token ids
enum token_ids {
ID_EOL= 100
};
// Token definition
template <typename Lexer>
struct var_replace_tokens : boost::spirit::lex::lexer<Lexer> {
var_replace_tokens() {
this->self.add ("\n", ID_EOL); // newline characters
}
};
// Functor
struct replacer {
typedef bool result_type;
template <typename Token>
bool operator()(Token const& t, std::size_t& lines) const {
switch (t.id()) {
case ID_EOL:
lines++;
break;
}
return true;
}
};
int main(int argc, char **argv) { …
Run Code Online (Sandbox Code Playgroud) 我尝试使用qi::uint_parser<int>()
。但这是一样的qi::uint_
。它们都匹配从0
到的整数std::numeric_limits<unsigned int>::max()
。
被qi::uint_parser<int>()
设计成这个样子?我应该使用什么解析器来匹配从0
到的整数范围std::numeric_limits<int>::max()
?谢谢。
(1)假设我们要解析一个简单的递归块{}
.
{
Some text.
{
{
Some more text.
}
Some Text again.
{}
}
}
Run Code Online (Sandbox Code Playgroud)
这个递归解析器非常简单.
x3::rule<struct idBlock1> const ruleBlock1{"Block1"};
auto const ruleBlock1_def =
x3::lit('{') >>
*(
ruleBlock1 |
(x3::char_ - x3::lit('}'))
) >>
x3::lit('}');
BOOST_SPIRIT_DEFINE(ruleBlock1)
Run Code Online (Sandbox Code Playgroud)
(2)然后块变得更复杂.它也可能被包围[]
.
{
Some text.
[
{
Some more text.
}
Some Text again.
[]
]
}
Run Code Online (Sandbox Code Playgroud)
我们需要在某处存放我们拥有的开放式支架.由于x3没有本地,我们可能会使用attribute(x3::_val
).
x3::rule<struct idBlock2, char> const ruleBlock2{"Block2"};
auto const ruleBlock2_def = x3::rule<struct _, char>{} =
(
x3::lit('{')[([](auto& ctx){x3::_val(ctx)='}';})] |
x3::lit('[')[([](auto& ctx){x3::_val(ctx)=']';})]
) …
Run Code Online (Sandbox Code Playgroud) 我正在使用Boost :: Spirit在我的C++ GUI应用程序中为非技术用户构建简单的"数据过滤"语言.语言与普通英语非常相似,可以解析为AST.我被要求尽可能方便用户进程,所以我希望提供类似CLang的错误消息("无法识别'tokken',你的意思是'令牌'?")和自动完成.
现在的问题是如何使用Boost :: Spirit语法为两个目标生成可能的令牌列表(我将使用简单的字符串距离算法来满足第一个要求)?
我的想法到目前为止:
这个解决方案的问题是建议还会建议对给定地点无效的令牌.如果我添加(我会)可定义的标识符,我手头有更大的问题...
还有一个约束:我希望只在一个地方定义这种语言的语法; 如果语法改变了,我想在重新编译后自动完成注意这个改变
boost-spirit ×10
c++ ×10
boost ×5
parsing ×3
autocomplete ×1
decltype ×1
memory-leaks ×1
numeric ×1
recursion ×1
result-of ×1