Boost logo

Boost Users :

Subject: [Boost-users] Problem with Spirit Qi and inherited attributes reference
From: Anand kumar (anandr86_at_[hidden])
Date: 2011-10-14 05:22:44


Hello,
     This is my first time using boost and spirit. I am trying to find
construct a tree from a string input. While parsing I am trying to assign
each created node a number. For this purpose I am using a inherited
attribute, an integer reference. But when I check on the values assigned. It
is not the value of the integer reference rather some other value (some
consistent value). I am sure this must be something that I am overlooking.
But I am not sure what exactly.

Thanks for your help,
Anand Kumar.

Code:

struct node;

struct term {
  bool visited;
  int cnt;
  int val;

  term(bool b = false, int cn = -1, int v = numeric_limits<int>::min())
  {
    visited = b;
    cnt = cn;
    val = v;
  }
};

BOOST_FUSION_ADAPT_STRUCT(
              ::term,
              (bool, visited)
              (int, cnt)
              (int, val)
              )

typedef boost::variant<term, boost::recursive_wrapper<node>> node_impl;

struct node {
  bool visited;
  int cnt;
  vector<node_impl> children;
};

BOOST_FUSION_ADAPT_STRUCT(
              ::node,
              (bool, visited)
              (int, cnt)
              (std::vector<::node_impl>, children)
              )

template<typename Iterator>
struct node_parser : qi::grammar<Iterator, node(), ascii::space_type> {

  qi::rule<Iterator, node(), ascii::space_type> start_r;
  qi::rule<Iterator, node(int&), ascii::space_type> node_r;
  qi::rule<Iterator, node_impl(int&), ascii::space_type> node_impl_r;

  node_parser() : node_parser::base_type(start_r)
  {
    using qi::int_;
    using qi::lexeme;
    using namespace qi::labels;
    using phoenix::ref;
    int cnt = 990;

    start_r %= node_r(phoenix::ref(cnt));
    node_r = ('{' >> +node_impl_r(_r1) >> '}') [at_c<0>(_val) = false,
at_c<1>(_val) = _r1++, at_c<2>(_val) = _1];
    node_impl_r = ((lexeme['(' >> int_ >> ')'])[_val =
phoenix::construct<term>(phoenix::val(false), _r1++, _1)]) |
(node_r(_r1)[_val = _1]);
  }
};



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net