简体   繁体   中英

Use a pointer as synthesized attribute in boost spirit parser

I want to write a spirit parser for a tree of objects that I cannot modify. To simplify the code I can use an Expression example:

class Expr
{
public:
    virtual ~Expr() = 0;
};
Expr::~Expr() {}

class Plus : public Expr
{
public:
    Plus (Expr* e1, Expr* e2) : _e1(e1), _e2(e2) {}
    ~Plus () {}
private:
    Expr* _e1;
    Expr* _e2;
};

class Num : public Expr
{
public:
    Num(double n) : _n(n) {}
private:
    double _n;
};

I checked the documentation and I found the useful BOOST_ADAPT_STRUCT, but using a rule declared as:

qi::rule<Iterator, Expr()> r;

I will slice the object when the rule returns for example a Plus object. So I try this way:

template <typename Iterator>
struct ExprParser : qi::grammar<Iterator, Expr*(), ascii::space_type>
{

    ExprParser()
      : ExprParser::base_type (start, "expr")
    {
        expr = qi::double_    [ qi::_val = phoenix::new_<Num>(::_1) ]
                | '('
                    >> expr
                    >> '+'
                    >> expr
                  >> ')'      [ qi::_val = phoenix::new_<Plus>(::_1, ::_2) ]
                ;

        start %= expr;
    }

    qi::rule<Iterator, Expr*(), ascii::space_type> expr;
    qi::rule<Iterator, Expr*(), ascii::space_type> start;
};

But I get a compile error in the assign inside the semantic actions. What am I doing wrong?

Edit

The answer of sehe is good! There where missing parenthesis and wrong use of placeholders. But what about memory leaks when a parsing error occurs?

As mentioned in the documentation

under 'Important', you need to use qi placeholders with Phoenix. I've tested the fixed code. You can see it live on http://liveworkspace.org/code/48dbfb55a0b7cbd4783b2f9f9a66d6d1 .

Note that extra parentheses were required to make the second semantic action compile:

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

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

class Expr
{
    public:
        virtual ~Expr() = 0;
};
Expr::~Expr() {}

class Plus : public Expr
{
    public:
        Plus (Expr* e1, Expr* e2) : _e1(e1), _e2(e2) {}
        ~Plus () {}
    private:
        Expr* _e1;
        Expr* _e2;
};

class Num : public Expr
{
    public:
        Num(double n) : _n(n) {}
    private:
        double _n;
};

template <typename Iterator>
struct ExprParser : qi::grammar<Iterator, Expr*(), ascii::space_type>
{

    ExprParser()
      : ExprParser::base_type (start, "expr")
    {
        expr = qi::double_     [ qi::_val = phoenix::new_<Num>(qi::_1) ]
                | ('('
                    >> expr
                    >> '+'
                    >> expr
                  >> ')')      [ qi::_val = phoenix::new_<Plus>(qi::_1, qi::_2) ]
                ;

        start %= expr;
    }

    qi::rule<Iterator, Expr*(), ascii::space_type> expr;
    qi::rule<Iterator, Expr*(), ascii::space_type> start;
};

int main(int argc, const char *argv[])
{
    char input[] = "3.14 + 39";
    ExprParser<const char*> p;

    const char *f = std::begin(input);
    const char *l = std::end(input);
    qi::phrase_parse(f, l, p, ascii::space);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM