首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >促进精神X3:无法创建具有可选列表的AST

促进精神X3:无法创建具有可选列表的AST
EN

Stack Overflow用户
提问于 2022-09-10 17:31:50
回答 1查看 62关注 0票数 0

我试图解析一个可选的事物列表,后面跟着一个分号(这是一个简化的例子)。下面是一个示例程序:

代码语言:javascript
复制
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <iostream>
#include <optional>
#include <string>
#include <vector>

namespace x3=boost::spirit::x3;

namespace ast
{
  struct doubleOrString : x3::variant<double,std::string>{
     using base_type::base_type;
     using base_type::operator=;
  };
 
  struct pass {
     std::vector<doubleOrString> dOrs;
  };
}//ast namespace

BOOST_FUSION_ADAPT_STRUCT(ast::pass,dOrs)

x3::rule<class doubleOrString_class, ast::doubleOrString> const doubleOrString = "doubleOrString";
x3::rule<class pass_class, ast::pass> const pass = "pass";
x3::rule<class es_class, std::optional<ast::pass>> const es = "es";

auto const doubleOrString_def =x3::double_ | x3::string("AString");
auto const pass_def = doubleOrString % x3::lit(",");
auto const es_def = (pass > x3::lit(";")) | x3::lit(";");

BOOST_SPIRIT_DEFINE(doubleOrString,pass,es);

int main()
{
    using boost::spirit::x3::ascii::space;
    typedef std::string::const_iterator iterator_type;

    std::string str;
    while (getline(std::cin, str))
    {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

        std::optional<ast::pass> esdOrs;
        iterator_type iter = str.begin();
        iterator_type const end = str.end();
        bool r = phrase_parse(iter, end, es, space, esdOrs);
        

        if (r && iter == end)
        {
            std::cout << "\nGood!\n";
        }
        else
        {
            std::cout << "-------------------------\n";
            std::cout << "Parsing failed\n";
            std::cout << "-------------------------\n";
        }
    }

    std::cout << "Bye... :-) \n\n";
    return 0;
}

但是,它无法使用错误进行编译:

usr/include/boost/spirit/home/x3/support/ast/variant.hpp:184:17:错误:不匹配“operator=”(操作数类型为“boost::spirit::x3::variant

r> >>::variant_type‘{aka’boost::variant}和‘std::optionalclient::ast::pass’)

26 184 var = std::forward(rhs);

我做错了什么?这个错误信息不多。如果我只是做一些可选的事情,它似乎会很好。

我使用以下命令(文件名为r1.cpp):

代码语言:javascript
复制
g++ --std=c++17 r1.cpp -o r1 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-11 18:56:37

使用以下MRE(最小可重现性示例):

代码语言:javascript
复制
#include <vector>

#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>

namespace x3=boost::spirit::x3;

namespace ast
{
  struct doubleOrString : x3::variant<double,std::string>{
     using base_type::base_type;
     using base_type::operator=;
  };
 
  struct pass {
     std::vector<doubleOrString> dOrs;
  };
}//ast namespace

BOOST_FUSION_ADAPT_STRUCT(ast::pass,dOrs)

x3::rule<class doubleOrString_class, ast::doubleOrString> const doubleOrString = "doubleOrString";
x3::rule<class pass_class, ast::pass> const pass = "pass";
x3::rule<class es_class, std::optional<ast::pass>> const es = "es";

auto const doubleOrString_def =x3::double_ | x3::string("AString");
auto const pass_def = doubleOrString % x3::lit(",");
auto const es_def = (pass > x3::lit(";")) | x3::lit(";");

BOOST_SPIRIT_DEFINE(doubleOrString,pass,es);

我没有编译器错误。请提供显示错误的MRE。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73673899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档