我正在做一个IRC消息解析器,Boost.Spirit但是当我尝试解析输入时,我得到一个(非常长的)错误.我遵循了" 罗马数字 "的例子.另外,我使用的是g++4.7带-std=c++11.只有当我打电话时发生错误phrase_parse()的test.cpp,不是我做的一个实例message_grammar.
语法类是:
class message_grammar : qi::grammar<std::string::const_iterator, std::string()>
{
public:
message_grammar() : base_type(m_message)
{
using qi::_val;
using qi::_1;
using boost::spirit::ascii::char_;
using qi::lit;
qi::rule<std::string::const_iterator, std::string()> alpha, graph, number, special, user,
nick, chn, channel;
alpha = qi::as_string[qi::alpha];
graph = qi::as_string[qi::graph];
number = qi::as_string[char_('0', '9')];
chn = qi::as_string[(char_('#') | char_('$'))];
special = qi::as_string[
char_('-') | char_('[') | char_(']') | char_('\\')
| char_('`') | char_('^') | char_('{') | char_('}')
];
user …Run Code Online (Sandbox Code Playgroud) 我需要从c ++对象解析并生成一些文本.
语法是:
command #param #param #param
Run Code Online (Sandbox Code Playgroud)
有一组命令,其中一些没有参数等.参数主要是数字.
问题是:我应该使用Boost Spirit来完成这项任务吗?或者只是简单地将每行评估函数标记为从字符串比较命令,读取其他参数并从中创建cpp对象?
如果你建议使用Spirit或任何其他解决方案,如果你能提供一些与我的问题类似的例子,那将是很好的.我已阅读并尝试了Boost Spirit doc中的所有示例.
我是错误"LNK1179:无效或损坏的文件:重复COMDAT"的受害者,这些 来源让我相信,通过不使用phoenix我可以避免此错误.
(这是我之前的问题的后续行动.)我想boost::phoenix用其他东西替换.也许,boost::bind但我不知道如何让它访问karma::_val.
以下代码无法在VC9上编译
错误C2825:'F':当后跟'::'时必须是类或命名空间
#include <boost/config/warning_disable.hpp>
#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <string>
#include <list>
namespace karma = boost::spirit::karma;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
class Item
{
public:
typedef std::vector<int> Values;
Item(const std::string & i, const Values & v) : m_id(i), m_values(v) {}
std::string getId() const { …Run Code Online (Sandbox Code Playgroud) 我试图解析以下形式的文件:
// comment bla bla
[sectionname]
key = value
key2=value2
// comment
key = value
[anothersection]
...
Run Code Online (Sandbox Code Playgroud)
使用以下代码.不幸的是,它报告最后一个eol是一个错误,尽管最后的所有eol都应该被接受:(*qi :: eol> - (sectionGrammar>*(+ qi :: eol> sectionGrammar))>*qi :: eol) ,
此外,我真的不知道如何正确解析注释而不采用下一个键值对所需的eol,这是我没有放入Skipper的原因(仅ascii :: blank).
我的最后一个问题是我不知道如何在不复制它们的情况下向boost :: ptr_vector添加节.
这是我的代码:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp> // for more detailed error information
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/bind.hpp>
#include <boost/spirit/home/phoenix/core/argument.hpp>
#include <boost/foreach.hpp>
#include "txt.hpp"
// Only use in global namespace!
BOOST_FUSION_ADAPT_STRUCT(
wc3lib::map::Txt::Section,
(wc3lib::string, name)
(wc3lib::map::Txt::Pairs, entries)
)
namespace wc3lib
{
namespace map …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用boost :: spirit为类C语言编写解析器,它使用继承的属性来传输有关变量范围的信息.例如,"namespace a {var b}"会将"a"作为属性传递给"var b"的解析器.
我在使用继承属性编译此代码时遇到基本解析器时遇到问题:
#ifndef CPARSER_DEF_HPP
#define CPARSER_DEF_HPP
#include <string>
#include <boost/spirit/include/qi.hpp>
namespace encoding = boost::spirit::ascii;
using boost::spirit::unused_type;
using boost::spirit::qi::rule;
template <typename Iterator>
struct cparser : boost::spirit::qi::grammar<
Iterator,
std::string(std::string),
encoding::space_type
>
{
rule<Iterator, std::string(std::string), encoding::space_type> start;
rule<Iterator, std::string(std::string), encoding::space_type> sym_list;
cparser() :
cparser::base_type(start)
{
sym_list = encoding::string(boost::spirit::qi::_r1);
start = sym_list(boost::spirit::qi::_r1);
}
};
#endif
Run Code Online (Sandbox Code Playgroud)
此解析器在main()中实例化cparser<std::string::const_iterator> parser.
我相信这个解析器应该接受一个std :: string作为它的继承属性,解析匹配该字符串的输入,然后将该字符串作为合成属性返回.这个示例代码没有编译,我无法弄清楚原因.我一直在使用GCC和Clang编译,启用了C++ 11.任何一个编译器的输出都是巨大的(大约1000行),我无法理解它.使用有问题boost::spirit::qi::_r1吗?std::string(std::string)规则声明中的问题是什么?
在此先感谢您的帮助.
我有以下语法:
#include <boost/spirit.hpp>
struct point_grammar
: public boost::spirit::grammar<point_grammar>
{
template <typename Scanner>
struct definition
{
boost::spirit::rule<Scanner> E, S, V;
definition(const point_grammar &self)
{
using namespace boost::spirit;
E = S >> V;
S = '@' >> +(~ch_p('@') - V);
V = str_p(".PV@") | str_p(".CV@");
}
const boost::spirit::rule<Scanner> &start()
{
return E;
}
};
};
Run Code Online (Sandbox Code Playgroud)
编译时,编译器显示以下警告:
/usr/include/boost/spirit.hpp:18:4:警告:"此标头已弃用.请使用:boost/spirit/include/classic.hpp"
但是当改变#include for boost/spirit/include/classic.hpp时,我有以下错误:
(在'<'标记之前的预期模板名称)在以下行中::: public boost :: spirit :: grammar.
我能做什么?
我试图解析一个字符串,其中包含一些特殊的单词和整数.让特殊词成为"HHH".需要在它之前跳过数据并在之后解析数据.这是我的尝试:
#include <iostream>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
int main ()
{
std::string input = "asd eee rrr HHH 456";
std::string::iterator strbegin = input.begin();
int result;
bool ok = qi::phrase_parse(
strbegin, input.end(),
(*(qi::char_ - qi::lit("HHH")) >> qi::lit("HHH") >> qi::int_),
qi::space,
result);
std::cout << ok << std::endl;
std::cout << result << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
main.cpp:14: instantiated from here
/usr/include/boost/spirit/home/qi/numeric/detail/numeric_utils.hpp:454: error: no matching function for call to mpl_::void_::void_(int)
/usr/include/boost/mpl/void.hpp:29: note: candidates are: mpl_::void_::void_()
/usr/include/boost/mpl/void.hpp:29: note: mpl_::void_::void_(const mpl_::void_&)
/usr/include/boost/spirit/home/qi/numeric/detail/numeric_utils.hpp: In static member …Run Code Online (Sandbox Code Playgroud) 将重复语句的结果存储到std :: vector会导致编译错误:
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:172:12: error: ambiguous
class template instantiation for ‘struct boost::spirit::qi::detail::pass_through_container_base<std::vector<Vertex3d<float> >, Vertex3d<float>, Vertex3d<float>, mpl_::bool_<false>, void>’
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:103:12: error: candidates are: struct boost::spirit::qi::detail::pass_through_container_base<Container, ValueType, Attribute, Sequence, typename boost::enable_if<boost::fusion::traits::is_sequence<Attribute> >::type>
struct pass_through_container_base<Container, ValueType, Attribute
^
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:136:12: error: struct boost::spirit::qi::detail::pass_through_container_base<Container, ValueType, Attribute, Sequence, typename boost::enable_if<boost::spirit::traits::is_container<T2> >::type>
struct pass_through_container_base<
^
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:172:12: error: invalid use of incomplete type ‘struct boost::spirit::qi::detail::pass_through_container_base<std::vector<wc3lib::Vertex3d<float> >, wc3lib::Vertex3d<float>, wc3lib::Vertex3d<float>, mpl_::bool_<false>, void>’
struct pass_through_container
^
/usr/include/boost/spirit/home/qi/detail/pass_container.hpp:50:12: error: declaration of ‘struct boost::spirit::qi::detail::pass_through_container_base<std::vector<wc3lib::Vertex3d<float> >, wc3lib::Vertex3d<float>, wc3lib::Vertex3d<float>, mpl_::bool_<false>, void>’
struct pass_through_container_base
Run Code Online (Sandbox Code Playgroud)
以下代码用于语法:
qi::rule<Iterator, long32(), …Run Code Online (Sandbox Code Playgroud) 我想弄清楚如何解决以下问题.
我有以下格式的结构:
struct Data
{
time_t timestamp;
string id;
boost::optional<int> data1;
boost::optional<string> data2;
// etc...
};
Run Code Online (Sandbox Code Playgroud)
这应该用以下格式的单行字符串解析:
human_readable_timestamp;id;key1=value1 key2=value2.....
Run Code Online (Sandbox Code Playgroud)
当然,键的排序不必与结构中元素的顺序相匹配.
Boost :: Spirit是否适合此类数据?我该如何处理?我已经完成了这些示例,但我无法从示例中获得符合我要求的代码.
我正在使用boost spirit x3进行解析.我完成了语法,解析器按预期解析.现在我想添加错误处理,所以我必须为我的语法添加期望点.我的问题是我何时可以使用期望运算符>而不是"跟随"运算符>>?我只能用它像a > b如果a >> b在语法的另一部分永远不会发生?