我听说精神在将字符串转换为int时非常快.
但是我无法创建一个可以这样做的简单函数.就像是
int string_to_int(string&s){/*?????*/}
任何人都可以使用提升精神来填补这个功能.
顺便说一句,我正在使用boost 1.34而不是最新版本.
我试图解析一个嘈杂的输入,理想情况下,我将能够看到一个节是否匹配规则,如果它确实得到我需要的数据并丢弃其余的.
我想要的数据如下.
Event: Newstate
Channel: SIP/104-000001bb
ChannelState: 6
ChannelStateDesc: Up
Run Code Online (Sandbox Code Playgroud)
我想确保事件是否为新状态.
我需要频道状态.其余的我不关心(刚才)所以我想忽略它,我希望它是灵活的,并接受重要的东西之间的任何旧垃圾,真的我不想说忽略这一行,而是忽略事件和信道状态结束之间的任何内容,我捕获值.
到目前为止我有:
typedef boost::fusion::vector2<std::string, std::string> vect;
qi::rule<std::string::iterator, vect(), space> rule_ =
lit("Event: ") >> *char_("a-zA-Z") >>
qi::omit[ *char_ ] >>
"ChannelState: " >> *char_("0-9") >>
qi::omit[ *char_ ];
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,这不起作用,当我这样做时,我总是会回复:
vect v;
bool r=qi::parse(it, str.end(), rule_, v);
Run Code Online (Sandbox Code Playgroud)
编辑:Boost版本1.42编译器g ++ 4.4精神0x2020
我正在尝试将键值字符串解析为结构.一些键值可能不存在或者可能顺序不同,所以我想boost::fusion用来调整结构,然后用at_key<>指令解析它.
#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/fusion/sequence.hpp>
using namespace std;
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phx = boost::phoenix;
using boost::fusion::at_key;
typedef string::const_iterator iter_type;
struct Couple {
int a;
int b;
Couple() : a(0), b(0) {}
};
namespace keys {
struct first;
struct second;
}
BOOST_FUSION_ADAPT_ASSOC_STRUCT(
Couple,
(int, a, keys::first)
(int, b, keys::second)
)
struct G: qi::grammar< iter_type, Couple(), ascii::space_type >
{
G() : G::base_type( start_rule ) …Run Code Online (Sandbox Code Playgroud) 通过boost :: spirit :: qi :: symbols文档的开头段落,我假设从语义动作向qi :: symbols添加符号并不太难.不幸的是,它似乎并不像我想象的那么简单.
以下测试代码表明了问题:
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <string>
namespace qi = boost::spirit::qi;
typedef qi::symbols<char, unsigned int> constants_dictionary;
template <typename Iter> struct parser : public qi::grammar<Iter, qi::space_type> {
parser(constants_dictionary &dict) : parser::base_type(start) {
start = qi::lit("@") >> ((+qi::char_) >> qi::uint_)[dict.add(qi::_1, qi::_2)];
}
qi::rule<Iter> start;
};
int main() {
constants_dictionary dict;
parser<std::string::const_iterator> prsr(dict);
std::string test = "@foo 3";
parse(test.begin(), test.end(), prsr, qi::space);
}
Run Code Online (Sandbox Code Playgroud)
从VS2010中提供与qi :: _ 2相关的类型错误:
C:\Users\k\Coding\dashCompiler\spirit_test.cpp(12) : error …Run Code Online (Sandbox Code Playgroud) 问题很简单,我写了一个词法分析器,使用boost :: spirit,但是我似乎找不到方法来生成EOF令牌. - 那怎么会这样做呢?
我试图解析terminfo定义文本文件.我是Boost.Spirit的新手.我从简单的语法开始,只解析注释行,空行和终端定义.正如语法中的代码注释所示,取消注释[_val = _1]for definitionbreak编译.为什么?我可以修理吗?
如果我忽略实际的terminfo文件,我希望下面的代码解析这种文本:
# comment line
first definition line
second
third line
# another comment line
Run Code Online (Sandbox Code Playgroud)
码:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_eol.hpp>
#include <boost/spirit/include/qi_eoi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <vector>
#include <iostream>
#include <string>
namespace termcxx
{
namespace parser
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace px = boost::phoenix;
//using qi::double_;
using ascii::space;
//using px::ref;
using px::construct;
//using qi::eps;
//using qi::lit;
using qi::_val;
using qi::_1;
using ascii::char_;
using qi::eol; …Run Code Online (Sandbox Code Playgroud) 我正在继续学习 Boost Spirit 库,但我有一些无法编译的示例问题。您可以在此处找到示例的来源:source place。您也可以查看此代码并在Coliru上编译结果。
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <fstream>
#include <string>
using namespace boost::spirit;
using boost::phoenix::val;
///////////////////////////////////////////////////////////////////////////////
// Token definition
///////////////////////////////////////////////////////////////////////////////
template <typename Lexer>
struct example4_tokens : lex::lexer<Lexer>
{
example4_tokens()
{
identifier = "[a-zA-Z_][a-zA-Z0-9_]*";
constant = "[0-9]+";
if_ = "if";
else_ = "else";
while_ = "while";
this->self = lex::token_def<>('(') | ')' | '{' | '}' | '=' | ';' | constant;
this->self += if_ | else_ | while_ …Run Code Online (Sandbox Code Playgroud) 我有一个模板函数,它将字符串转换为数字如下:
template <typename RetType,
typename Parser =
typename boost::spirit::traits::create_parser<RetType>::type>
inline std::enable_if_t<std::is_arithmetic<RetType>::value, RetType>
cast(const std::string &input)
{
RetType result;
if(input.empty())
{
// handle this
}
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!bsq::parse(itBeg, itEnd, Parser(), result) || itBeg != itEnd)
{
// handle that
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个类似于上面的函数,它将解析表示某个基数中的数字的字符串
template <typename RetType, unsigned Radix,
typename Parser =
typename boost::spirit::traits::create_parser<RetType>::type>
inline std::enable_if_t<std::is_arithmetic<RetType>::value, RetType>
cast(const std::string &input)
{
RetType result;
if(input.empty())
{
// handle this
}
auto itBeg = input.cbegin();
auto itEnd …Run Code Online (Sandbox Code Playgroud) 如何使用boost.spirit x3解析为如下结构:
struct person{
std::string name;
std::vector<std::string> friends;
}
Run Code Online (Sandbox Code Playgroud)
来自boost.spirit v2我会使用语法,但由于X3不支持语法,我不知道如何干净.
编辑:如果有人可以帮我编写一个解析字符串列表的解析器并返回一个person第一个字符串是名称并且字符串的res在friends向量中,那将是很好的.
我有一个用于解析标识符的解析器foo, bar, baz和一个用于解析嵌套标识符的解析器,foo::bar, foo::bar.baz, foo::bar.baz.baham
它们都解析为相同的ast结构,如下所示:
struct identifier : x3::position_tagged{
std::vector <std::string> namespaces;
std::vector <std::string> classes;
std::string identifier;
};
Run Code Online (Sandbox Code Playgroud)
解析器identifier看起来像这样:
#define VEC_ATR x3::attr(std::vector<std::string>({})) //ugly hack
auto const identifier_def =
VEC_ATR
>> VEC_ATR
>> id_string;
Run Code Online (Sandbox Code Playgroud)
对于nested_identifier这样的:
auto const nested_identifier_def =
x3::lexeme[
(+(id_string >> "::") >> +(id_string >> ".") > id_string)
| (+(id_string >> "::") >> VEC_ATR > id_string)
| (VEC_ATR >> +(id_string >> ".") > id_string)
| identifier
];
Run Code Online (Sandbox Code Playgroud)
我知道这个宏让我感到羞耻.标识符解析器工作正常,但
nested_identifier有一个奇怪的行为,如果我尝试解析像foo::bar::baz落在了解析器的AST对象,拥有所有的命名空间,在这种情况下 …