Re: index_is_out_of_bounds problem?

Philipp Schwaha <[email protected]>
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <[email protected]>
Hi Jens,

On 07/19/2016 08:09 PM, Jens Kallup wrote:
> Hello,
>
> I habe the following spirit grammar snippet:
> I don't know, why the error message index_out_of_bounds appears.
>
the count is off since you should not count the intervening character 
literals, which do not have an attribute (see 
http://www.boost.org/doc/libs/1_61_0/libs/spirit/doc/html/spirit/qi/reference/char/char.html)

> Can You Help
> Jens
>
>              rfc_header
>                  = tok.kw_http >> '/' >> double_
>                                >> ' ' >> int_
>                                >> ' ' >> tok.identifier
>                        [
>                        qi::_val = phx::construct<expression_ast>(
>                             std::string("header1")
>                            , qi::_3
>                            , qi::_5
>                            , qi::_7
these should be:
                             , qi::_2
                             , qi::_3
                             , qi::_4

>                            , qi::_val)

_val will not be set to the value you expect. It will hold the value due 
to the default constructor.

Have a look at the simple test program in the attachment. If you change
[ std::cout <<  _1 << " " << _2 << " " << _val << "\n" ]
to
[ std::cout <<  _1 << " " << _3 << " " << _val << "\n" ]
it will fail to compile.

Also examine that the the output due to '_val' is in a default 
constructed state (0 0).
With the semantic actions attached you can turn on attribute propagation 
by using the '%=' operator instead just '=' in the rule assignment (also 
see the small demo program if you change it to '=' again).

Cheers
	Philipp

>                        ]
>                  ;
>
> ------------------------------------------------------------------------------
> What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
> patterns at an interface-level. Reveals which users, apps, and protocols are
> consuming the most bandwidth. Provides multi-vendor support for NetFlow,
> J-Flow, sFlow and other flows. Make informed decisions using capacity planning
> reports.http://sdm.link/zohodev2dev
> _______________________________________________
> Spirit-general mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/spirit-general
>

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev

_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general
indices.cpp (text/x-c++src, 1.3 KB)
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

template<typename IteratorType, typename AttributeType>
struct simple_grammar
  : boost::spirit::qi::grammar<IteratorType, AttributeType()> {
  simple_grammar() :
    simple_grammar::base_type(start) {
    using namespace boost::spirit::qi ;
    start %= (double_ >> ' ' >> double_)
      // this one works
      [ std::cout <<  _1 << " " << _2 << " " << _val << "\n" ]
      // this is will not work
      // [ std::cout <<  _1 << " " << _3 << " " << _val << "\n" ]
      ;
  }
  typedef boost::spirit::qi::rule<IteratorType, AttributeType()> rule_type ;
  rule_type start ;
} ;


int main() {
  typedef std::string input_type ;
  typedef input_type::const_iterator iterator_type ;
  typedef boost::fusion::vector<double, double> attribute_type ;
  typedef simple_grammar<iterator_type, attribute_type> grammar_type ;

  input_type input("1 2") ;
  iterator_type iterator = input.begin() ;
  iterator_type finish = input.end() ;

  attribute_type result ;

  bool ret_val =
    boost::spirit::qi::parse(iterator, finish, grammar_type(), result) ;
    
  if (ret_val && (iterator == finish)) {
    std::cout << "successuflly parsed\n" ;
  } else {
    std::cout << "error parsing\n" ;
  }

  std::cout << result << "\n" ;
  return 0 ;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.