一般情况:我无法弄清楚为什么我的Spirit语法/语义动作没有编译.
有时,编译器会抱怨分配或类型不兼容,我不知道什么是错的.问题出现在两个主要方面:
编译器错误不是完全易处理的,文档是错误的,或者我误解了它.
无论如何,有没有办法确切地找出Spirit传入我的语义行为的方式?
struct mybase { int a,b; };
struct myderived : mybase { int c,d; };
BOOST_FUSION_ADAPT_STRUCT(mybase, (int,a)(int,b));
BOOST_FUSION_ADAPT_STRUCT(myderived, (int,a)(int,b)(int,c)(int,d));
auto base_expr = int_ >> int_; // avoids assigning to struct attribute
rule<decltype(f), mybase() , space_type> base_ = int_ >> int_;
rule<decltype(f), myderived(), space_type> derived_ = base_ >> int_ >> int_;
myderived data;
bool ok = phrase_parse(f,l,derived_,space,data);
Run Code Online (Sandbox Code Playgroud)
此代码无法编译,存在大量难以理解的错误.
(松散地改编自精神总表上的帖子)
我有一个关于Spirit Qi的编译问题,它抱怨value_type不是标识符的成员.出于某种原因,Qi的属性系统将标识符视为容器类型,并尝试枚举它的值类型.
这是与此问题类似的问题,但是,我认为原因是单个成员结构,可能与此错误有关.
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
using namespace boost::spirit::qi;
struct identifier
{
std::wstring name;
};
struct problem
{
identifier _1;
identifier _2;
identifier _3;
};
BOOST_FUSION_ADAPT_STRUCT(
identifier,
(std::wstring, name)
)
BOOST_FUSION_ADAPT_STRUCT(
problem,
(identifier, _1)
(identifier, _2)
(identifier, _3)
)
int main(int argc, char* argv[])
{
rule<std::wstring::const_iterator, identifier()> gr_identifier = eps >> raw[lexeme[(alpha | '_') >> *(alnum | '_')]];
// Ok, compiles
/*rule<std::wstring::const_iterator, problem()> gr_problem = gr_identifier
>> …Run Code Online (Sandbox Code Playgroud) 我想解析一个句子,其中一些字符串可能不加引号,"引用"或"引用".下面的代码几乎可以工作 - 但它无法匹配收尾报价.我猜这是因为qq参考.修改在代码中被注释,修改引用"引用"或"引用"也解析并帮助显示原始问题与结束引用.该代码还描述了确切的语法.
要完全清楚:不带引号的字符串解析.引用的字符串'hello'将解析打开的引号',所有字符 hello,但然后无法解析最终引用'.
我做了另一次尝试,类似于boost教程中的开始/结束标记匹配,但没有成功.
template <typename Iterator>
struct test_parser : qi::grammar<Iterator, dectest::Test(), ascii::space_type>
{
test_parser()
:
test_parser::base_type(test, "test")
{
using qi::fail;
using qi::on_error;
using qi::lit;
using qi::lexeme;
using ascii::char_;
using qi::repeat;
using namespace qi::labels;
using boost::phoenix::construct;
using boost::phoenix::at_c;
using boost::phoenix::push_back;
using boost::phoenix::val;
using boost::phoenix::ref;
using qi::space;
char qq;
arrow = lit("->");
open_quote = (char_('\'') | char_('"')) [ref(qq) = _1]; // Remember what the opening quote was
close_quote = lit(val(qq)); …Run Code Online (Sandbox Code Playgroud)