使用额外参数访问boost变体

时间:2015-04-06 15:11:11

标签: c++ boost abstract-syntax-tree boost-spirit boost-variant

我正在尝试通过布尔公式实现Tseitin转换。我们的想法是使用转换规则在CNF中转换布尔公式。例如,如果公式f = l OR r,我们为每个公式分配一个新变量,让我们说v_f,v_l和v_r,我们将f = l OR r变换为(!v_f OR v_l OR v_r) AND {{ 1}}和(v_f OR !v_l)

我使用类似(v_f OR !v_r)v_fv_l的内容替换为v_rpp1。{{1} }是一个变量,它允许我们现在可以使用下一个变量):

p2

在主要内容中,我将此转换称为:m

但我遇到如下错误:int m = 0; std::vector<std::vector<int> > formules; struct op_or {}; struct op_and {}; struct op_not {}; struct op_impl {}; typedef int var; template <typename tag> struct binop; template <typename tag> struct unop; typedef boost::variant<var, boost::recursive_wrapper<unop <op_not> >, boost::recursive_wrapper<binop<op_and> >, boost::recursive_wrapper<binop<op_or> >, boost::recursive_wrapper<binop<op_impl> > > expr; template <typename tag> struct binop { explicit binop(const expr& l, const expr& r) : oper1(l), oper2(r) { } expr oper1, oper2; }; template <typename tag> struct unop { explicit unop(const expr& o) : oper1(o) { } expr oper1; }; struct tseitin : boost::static_visitor<void> { tseitin() {} void operator()(const var& v, int p = 0) {} void operator()(const binop<op_and>& b, int p = m++) { proceed(0, b.oper1, b.oper2, p); } void operator()(const binop<op_or>& b, int p = m++) { proceed(1, b.oper1, b.oper2, p);} void operator()(const unop<op_not>& u, int p = m++) {} void proceed(int nop, const expr& l, const expr& r, int p) { int p1 = m+1; int p2 = m+2; m += 2; // Do the transformation recurse(l, p1); recurse(r, p2); } private: template<typename T, typename U> bool recurse(T const& v, U const& p) { return boost::apply_visitor(*this, v, p); } }; 并且:boost::apply_visitor(tseitin(), result, 0);

我真的不明白这个错误,你有什么想法吗?

正如您可能已经注意到的那样,我使用How to calculate boolean expression in Spirit和boost精神教程来实现转换操作符。

我需要整个代码,现在就让我吧。

提前致谢!

1 个答案:

答案 0 :(得分:2)

  

我真的不明白这个错误,你有什么想法吗?

您正在调用boost::variant::apply_visitor的二进制版本。但是你传递了一个变量和一个int。正如错误消息所宣布的那样,int不可访问。

要使这项工作,只需绑定参数:

boost::apply_visitor(boost::bind(tseitin(), _1, 0), result);

这意味着_1占位符将接收变量元素,并传递第二个参数。

您还希望在recurse中执行此操作:

bool recurse(T const& v, U const& p) 
    { return boost::apply_visitor(boost::bind(*this, _1, p), v); }

更新

以下是链接答案的演示,但使用额外的绑定参数而不是有状态的printer仿函数:

struct printer : boost::static_visitor<void>
{
    //
    void operator()(std::ostream& os, const var& v) const { os << v; }

    void operator()(std::ostream& os, const binop<op_and>& b) const { print(os, " & ", b.oper1, b.oper2); }
    void operator()(std::ostream& os, const binop<op_or >& b) const { print(os, " | ", b.oper1, b.oper2); }

    void print(std::ostream& os, const std::string& op, const expr& l, const expr& r) const
    {
        os << "(";
        boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), l);
        os << op;
        boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), r);
        os << ")";
    }

    void operator()(std::ostream& os, const unop<op_not>& u) const
    {
        os << "(";
        os << "!";
        boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), u.oper1);
        os << ")";
    }
};

完整代码:

<强> Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/variant/recursive_wrapper.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>

namespace qi    = boost::spirit::qi;
namespace phx   = boost::phoenix;

struct op_or  {};
struct op_and {};
struct op_not {};

typedef std::string var;
template <typename tag> struct binop;
template <typename tag> struct unop;

typedef boost::variant<var,
        boost::recursive_wrapper<unop <op_not> >,
        boost::recursive_wrapper<binop<op_and> >,
        boost::recursive_wrapper<binop<op_or> >
        > expr;

template <typename tag> struct binop
{
    explicit binop(const expr& l, const expr& r) : oper1(l), oper2(r) { }
    expr oper1, oper2;
};

template <typename tag> struct unop
{
    explicit unop(const expr& o) : oper1(o) { }
    expr oper1;
};

struct eval : boost::static_visitor<bool>
{
    eval() {}

    //
    bool operator()(const var& v) const
    {
        if (v=="T" || v=="t" || v=="true" || v=="True")
            return true;
        else if (v=="F" || v=="f" || v=="false" || v=="False")
            return false;
        return boost::lexical_cast<bool>(v);
    }

    bool operator()(const binop<op_and>& b) const
    {
        return recurse(b.oper1) && recurse(b.oper2);
    }
    bool operator()(const binop<op_or>& b) const
    {
        return recurse(b.oper1) || recurse(b.oper2);
    }
    bool operator()(const unop<op_not>& u) const
    {
        return !recurse(u.oper1);
    }

    private:
    template<typename T>
        bool recurse(T const& v) const
        { return boost::apply_visitor(*this, v); }
};

struct printer : boost::static_visitor<void>
{
    //
    void operator()(std::ostream& os, const var& v) const { os << v; }

    void operator()(std::ostream& os, const binop<op_and>& b) const { print(os, " & ", b.oper1, b.oper2); }
    void operator()(std::ostream& os, const binop<op_or >& b) const { print(os, " | ", b.oper1, b.oper2); }

    void print(std::ostream& os, const std::string& op, const expr& l, const expr& r) const
    {
        os << "(";
        boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), l);
        os << op;
        boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), r);
        os << ")";
    }

    void operator()(std::ostream& os, const unop<op_not>& u) const
    {
        os << "(";
        os << "!";
        boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), u.oper1);
        os << ")";
    }
};

bool evaluate(const expr& e)
{ return boost::apply_visitor(eval(), e); }

std::ostream& operator<<(std::ostream& os, const expr& e)
{ boost::apply_visitor(boost::bind(printer(), boost::ref(os), _1), e); return os; }

template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, expr(), Skipper>
{
        parser() : parser::base_type(expr_)
        {
            using namespace qi;

            expr_  = or_.alias();

            or_  = (and_ >> '|'  >> or_ ) [ _val = phx::construct<binop<op_or > >(qi::_1, qi::_2) ] | and_   [ _val = qi::_1 ];
            and_ = (not_ >> '&' >> and_)  [ _val = phx::construct<binop<op_and> >(qi::_1, qi::_2) ] | not_   [ _val = qi::_1 ];
            not_ = ('!' > simple       )  [ _val = phx::construct<unop <op_not> >(qi::_1)     ]     | simple [ _val = qi::_1 ];

            simple = (('(' > expr_ > ')') | var_);
            var_ = qi::lexeme[ +(alpha|digit) ];

            BOOST_SPIRIT_DEBUG_NODE(expr_);
            BOOST_SPIRIT_DEBUG_NODE(or_);
            BOOST_SPIRIT_DEBUG_NODE(and_);
            BOOST_SPIRIT_DEBUG_NODE(not_);
            BOOST_SPIRIT_DEBUG_NODE(simple);
            BOOST_SPIRIT_DEBUG_NODE(var_);
        }

        private:
        qi::rule<It, var() , Skipper> var_;
        qi::rule<It, expr(), Skipper> not_, and_, or_, simple, expr_;
};

int main()
{
    const std::string inputs[] = {
        std::string("true & false;"),
        std::string("true & !false;"),
        std::string("!true & false;"),
        std::string("true | false;"),
        std::string("true | !false;"),
        std::string("!true | false;"),

        std::string("T&F;"),
        std::string("T&!F;"),
        std::string("!T&F;"),
        std::string("T|F;"),
        std::string("T|!F;"),
        std::string("!T|F;"),
        std::string("") // marker
    };

    for (const std::string *i = inputs; !i->empty(); ++i)
    {
        typedef std::string::const_iterator It;
        It f(i->begin()), l(i->end());
        parser<It> p;

        try
        {
            expr result;
            bool ok = qi::phrase_parse(f,l,p > ';',qi::space,result);

            if (!ok)
                std::cerr << "invalid input\n";
            else
            {
                std::cout << "result:\t" << result << "\n";
                std::cout << "evaluated:\t" << evaluate(result) << "\n";
            }

        } catch (const qi::expectation_failure<It>& e)
        {
            std::cerr << "expectation_failure at '" << std::string(e.first, e.last) << "'\n";
        }

        if (f!=l) std::cerr << "unparsed: '" << std::string(f,l) << "'\n";
    }

    return 0;
}