2

I have a problem with inserting data into a vector using phoenix::insert. The code should parse input such as "(move x y z - loc r - robot item)" into a struct Predicate with name "move" and 3 variables of type loc, 1 variable of type robot and 1 variable with default type object. All those symbols are just strings not really relevant to the problem (I believe). The problem is using phoenix::insert in the definition of the rule for predicate.

Here is the code I have:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/home/phoenix/container.hpp>
#include <boost/fusion/include/adapt_struct.hpp>


namespace client {

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

  struct Variable {
    std::string name;
    std::string type;
  };


  struct Predicate {
    std::string name;
    std::vector<Variable> vars;
  };


  struct TermList {
    std::vector<Variable> vars;

    TermList() = default;
    TermList(std::vector<std::string> names, std::string type)
    {
      for (auto& n : names)
      {
        Variable t;
        t.name = n;
        t.type = type;
        vars.push_back(t);
      }
    }

    TermList& operator=(const TermList& rhs) = default;
    TermList(const TermList& from) = default;
    TermList(TermList&& from) = default;
  };

}

BOOST_FUSION_ADAPT_STRUCT(
  client::Variable,
  (std::string, name)
  (std::string, type)
)

BOOST_FUSION_ADAPT_STRUCT(
  client::Predicate,
  (std::string, name)
  (std::vector<client::Variable>, vars)
)

BOOST_FUSION_ADAPT_STRUCT(
  client::TermList,
  (std::vector<client::Variable>, vars)
)

namespace client {

  template <typename Iterator, typename Skipper = ascii::space_type>
  struct strips_domain_grammar
  : qi::grammar<Iterator, Predicate(),
                qi::locals<std::vector<Variable>>, Skipper>
  {
    strips_domain_grammar()
    : strips_domain_grammar::base_type(predicate, "predicate")
    {

      using qi::eps;
      using qi::lit;
      using qi::lexeme;
      using qi::raw;

      using qi::on_error;
      using qi::fail;

      using phoenix::at_c;
      using phoenix::push_back;
      using phoenix::insert;
      using phoenix::begin;
      using phoenix::end;
      using phoenix::construct;
      using phoenix::val;

      using ascii::char_;
      using ascii::string;
      using ascii::alpha;
      using ascii::alnum;
      using namespace qi::labels;

      // identifier such as move or ?from
      identifier %= raw[lexeme[((alpha | char_('_') | char_('?'))
                                >> *(alnum | char_('_') | char_('-')))]];

      // x | x y | x - type | x y z - type
      term_list =
      +(identifier          [push_back(_a, _1)])
      >>
      (
       ('-' >
       identifier [qi::_val = phoenix::construct<TermList>(qi::_a, qi::_1)])
       |
       eps        [qi::_val = phoenix::construct<TermList>(qi::_a, "object")]
       )
      ;

      // (move x y z - loc r - robot item) // item is detault type - object
      predicate =
      char_('(')
      > identifier       [at_c<0>(_val) = _1]
      > +(term_list      [insert(at_c<1>(_val), end(at_c<1>(_val)),   // <- ERROR
                                 begin(at_c<0>(_1)), end(at_c<0>(_1)))])
      > ')'
      ;


      predicate.name("predicate");
      term_list.name("term list");
      identifier.name("id");

      // on_error is called only when an expectation fails (> instead of >>)
      on_error<fail>
      (
       predicate
       , std::cout
       << val("Error! Expecting ")
       << _4                               // what failed?
       << val(" here: \"")
       << construct<std::string>(_3, _2)   // iterators to error-pos, end
       << val("\"")
       << std::endl
       );
    }

    qi::rule<Iterator, std::string(), Skipper> identifier;

    qi::rule<Iterator, TermList(),
             qi::locals<std::vector<std::string>>, Skipper> term_list;

    qi::rule<Iterator, Predicate(),
             qi::locals<std::vector<Variable>>, Skipper> predicate;

  };
} // namespace client

int main(int argc, const char** argv)
{
  typedef std::string::const_iterator iterator_type;
  typedef client::strips_domain_grammar<iterator_type> domain_grammar;

  domain_grammar g;

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

    using boost::spirit::ascii::space;

    client::Predicate predicate;
    std::string::const_iterator iter = str.begin();
    std::string::const_iterator end = str.end();
    bool r = phrase_parse(iter, end, g, space, predicate);

    if (r && iter == end)
    {
      std::cout << "-------------------------\n";
      std::cout << "Parsing succeeded\n";
      std::cout << "got: " << predicate.name;
      std::cout << "\n-------------------------\n";
    }
    else
    {
      std::cout << "-------------------------\n";
      std::cout << "Parsing failed\n";
      std::cout << "-------------------------\n";
    }
  }
}

but the code leads to the following error (clang3.3 with libc++ and c++11; mac os x 10.8):

boost/spirit/home/phoenix/stl/container/container.hpp:416:16: error: void function 'operator()' should not return a value [-Wreturn-type]
           return c.insert(arg1, arg2, arg3);

As mentioned above, I believe the error is the result of using phoenix::insert in an action in the predicate rule.

I "fixed" the problem by editing the boost header and removing the return statement, but given my limited understanding of this library I would like to avoid that...

Can someone please explain the problem or suggest a different solution?

6
  • 2
    As sehe recently said: "You're hurting your potential to get quick & easy answers by leaving just enough code out to make this a puzzle."
    – user1252091
    Feb 19, 2013 at 21:51
  • @llonesmiz well... you've caught my sentiment here :)
    – sehe
    Feb 19, 2013 at 22:46
  • 1
    @Roman If you show a small, self contained example that demonstrates your problem, with actual input and expected results, I'm pretty sure that we both would be able to give a dramatically simpler approach in minutes.
    – sehe
    Feb 19, 2013 at 22:54
  • Well I changed the question, but the example is not exactly small. It is self contained though :-) Feb 22, 2013 at 0:03
  • 2
    My guess is that your problem is similar to the one shown here, that there is a disconnect between what phoenix expects as a result and what the function returns since c++11. Unfortunately I don't have access to a clang that can use libc++, so the following code is untested, but I think it should work. Program that hopefully works.
    – user1252091
    Feb 22, 2013 at 10:44

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.