Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

c++ - How do I grok boost spirit compiler errors

I am trying to use the no_skip directive to parse input of the form:

state PASS
<tab> state FAIL

I am using ascii::blank as my skipper. I get an compile error when I wrap no_skip[ trans_assign_expr ] to parse for the tab.

How do I fix this error, and in general, how do I understand these errors so that I can fix future ones? The boost spirit documentation never covers this aspect of using spirit :(

This is the error

In file included from /usr/include/boost/spirit/home/qi/nonterminal/grammar.hpp:18:0,
                 from /usr/include/boost/spirit/include/qi_grammar.hpp:16,
                 from ../src/RJstate.cpp:9:
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp: In member function ‘bool boost::spirit::qi::rule::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Context = boost::spirit::context, boost::fusion::vector0 >, Skipper = boost::spirit::qi::detail::unused_skipper > >, Attribute = const boost::spirit::unused_type, Iterator = __gnu_cxx::__normal_iterator >, T1 = boost::proto::exprns_::expr >, 0l>, T2 = boost::spirit::unused_type, T3 = boost::spirit::unused_type, T4 = boost::spirit::unused_type]’:
/usr/include/boost/spirit/home/qi/reference.hpp:43:71:   instantiated from ‘bool boost::spirit::qi::reference::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Iterator = __gnu_cxx::__normal_iterator >, Context = boost::spirit::context, boost::fusion::vector0 >, Skipper = boost::spirit::qi::detail::unused_skipper > >, Attribute = const boost::spirit::unused_type, Subject = const boost::spirit::qi::rule >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>]’
/usr/include/boost/spirit/home/qi/directive/no_skip.hpp:64:63:   instantiated from ‘bool boost::spirit::qi::no_skip_directive::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Iterator = __gnu_cxx::__normal_iterator >, Context = boost::spirit::context, boost::fusion::vector0 >, Skipper = boost::spirit::qi::char_class >, Attribute = const boost::spirit::unused_type, Subject = boost::spirit::qi::reference >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >]’
/usr/include/boost/spirit/home/qi/detail/fail_function.hpp:45:74:   instantiated from ‘bool boost::spirit::qi::detail::fail_function::operator()(const Component&) const [with Component = boost::spirit::qi::no_skip_directive >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, Iterator = __gnu_cxx::__normal_iterator >, Context = boost::spirit::context, boost::fusion::vector0 >, Skipper = boost::spirit::qi::char_class >]’
/usr/include/boost/fusion/algorithm/query/detail/any.hpp:42:83:   [ skipping 8 instantiation contexts ]
/usr/include/boost/function/function_template.hpp:132:42:   instantiated from ‘static R boost::detail::function::function_obj_invoker4::invoke(boost::detail::function::function_buffer&, T0, T1, T2, T3) [with FunctionObj = boost::spirit::qi::detail::parser_binder >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::cons >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, boost::fusion::nil> > > >, mpl_::bool_ >, R = bool, T0 = __gnu_cxx::__normal_iterator >&, T1 = const __gnu_cxx::__normal_iterator >&, T2 = boost::spirit::context, boost::fusion::vector0 >&, T3 = const boost::spirit::qi::char_class >&]’
/usr/include/boost/function/function_template.hpp:913:60:   instantiated from ‘void boost::function4::assign_to(Functor) [with Functor = boost::spirit::qi::detail::parser_binder >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::cons >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, boost::fusion::nil> > > >, mpl_::bool_ >, R = bool, T0 = __gnu_cxx::__normal_iterator >&, T1 = const __gnu_cxx::__normal_iterator >&, T2 = boost::spirit::context, boost::fusion::vector0 >&, T3 = const boost::spirit::qi::char_class >&]’
/usr/include/boost/function/function_template.hpp:722:7:   instantiated from ‘boost::function4::function4(Functor, typename boost::enable_if_c::value>::value, int>::type) [with Functor = boost::spirit::qi::detail::parser_binder >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::cons >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, boost::fusion::nil> > > >, mpl_::bool_ >, R = bool, T0 = __gnu_cxx::__normal_iterator >&, T1 = const __gnu_cxx::__normal_iterator >&, T2 = boost::spirit::context, boost::fusion::vector0 >&, T3 = const boost::spirit::qi::char_class >&, typename boost::enable_if_c::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1064:16:   instantiated from ‘boost::function::function(Functor, typename boost::enable_if_c::value>::value, int>::type) [with Functor = boost::spirit::qi::detail::parser_binder >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::cons >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, boost::fusion::nil> > > >, mpl_::bool_ >, R = bool, T0 = __gnu_cxx::__normal_iterator >&, T1 = const __gnu_cxx::__normal_iterator >&, T2 = boost::spirit::context, boost::fusion::vector0 >&, T3 = const boost::spirit::qi::char_class >&, typename boost::enable_if_c::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1105:5:   instantiated from ‘typename boost::enable_if_c::value>::value, boost::function&>::type boost::function::operator=(Functor) [with Functor = boost::spirit::qi::detail::parser_binder >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::cons >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, boost::fusion::nil> > > >, mpl_::bool_ >, R = bool, T0 = __gnu_cxx::__normal_iterator >&, T1 = const __gnu_cxx::__normal_iterator >&, T2 = boost::spirit::context, boost::fusion::vector0 >&, T3 = const boost::spirit::qi::char_class >&, typename boost::enable_if_c::value>::value, boost::function&>::type = boost::function >&, const __gnu_cxx::__normal_iterator >&, boost::spirit::context, boost::fusion::vector0 >&, const boost::spirit::qi::char_class >&)>&]’
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:198:13:   instantiated from ‘boost::spirit::qi::rule& boost::spirit::qi::rule::operator=(const Expr&) [with Expr = boost::proto::exprns_::expr >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&, const boost::proto::exprns_::expr, 0l>&>, 2l>&, const boost::proto::exprns_::expr, 0l>&, boost::spirit::qi::rule >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l>&>, 2l>, Iterator = __gnu_cxx::__normal_iterator >, T1 = boost::proto::exprns_::expr >, 0l>, T2 = boost::spirit::unused_type, T3 = boost::spirit::unused_type, T4 = boost::spirit::unused_type, boost::spirit::qi::rule = boost::spirit::qi::rule >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>]’
../src/RJstate.cpp:49:7:   instantiated from ‘fsm_grammar::fsm_grammar() [with Iterator = __gnu_cxx::__normal_iterator >, Skipper = boost::proto::exprns_::expr >, 0l>]’
../src/RJstate.cpp:112:50:   instantiated from here
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:277:17: error: no match for call to ‘(const function_type {aka const boost::function >&, const __gnu_cxx::__normal_iterator >&, boost::spirit::context, boost::fusion::vector0 >&, const boost::spirit::qi::char_class >&)>}) (__gnu_cxx::__normal_iterator >&, const __gnu_cxx::__normal_iterator >&, boost::spirit::qi::rule >, boost::proto::exprns_::expr >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>::context_type&, const boost::spirit::qi::detail::unused_skipper > >&)’
/usr/include/boost/function/function_template.hpp:1043:7: note: candidate is:
/usr/include/boost/function/function_template.hpp:1006:1: note: boost::function4::result_type boost::function4::operator()(T0, T1, T2, T3) const [with R = bool, T0 = __gnu_cxx::__normal_iterator >&, T1 = const __gnu_cxx::__normal_iterator >&, T2 = boost::spirit::context, boost::fusion::vector0 >&, T3 = const boost::spirit::qi::char_class >&, boost::function4::result_type = bool]
/usr/include/boost/function/function_template.hpp:1006:1: note:   no known conversion for argument 4 from ‘const boost::spirit::qi::detail::unused_skipper > >’ to ‘const boost::spirit::qi::char_class >&’

This is my code

#define BOOST_SPIRIT_DEBUG

#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>

// forwards to <boost/spirit/home/qi/nonterminal/grammar.hpp>
#include <boost/spirit/include/qi_grammar.hpp>
// forwards to <boost/spirit/home/qi/nonterminal/rule.hpp>
#include <boost/spirit/include/qi_rule.hpp>

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

#include <boost/spirit/include/qi_char_class.hpp>
#include <boost/spirit/include/qi_no_skip.hpp>

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

#include "TestParser.hpp"

///////////////////////////////////////////////////////////////////////////////

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

template <typename Iterator, typename Skipper=qi::space_type>
struct fsm_grammar : qi::grammar<Iterator, Skipper>
{
   fsm_grammar() : fsm_grammar::base_type(script)
   {
      using boost::spirit::double_;
      using boost::spirit::ascii::char_;
      using boost::spirit::ascii::alnum;
      using boost::spirit::ascii::alpha;
      using boost::spirit::lit;
      using boost::spirit::qi::eol;
      using boost::spirit::qi::eoi;
      using boost::spirit::lexeme;
      using boost::spirit::qi::no_skip;
      using boost::spirit::qi::_1;
      using boost::spirit::qi::_val;

      //id = char_("a-zA-Z")[_val += _1] >> *char_("a-zA-Z_0-9")[_val += _1];
      id = lexeme[ alpha[_val += _1] >> *alnum[_val += _1] >> !alnum ];
      state_id = "state" >> id;
      start_expr = "start" >> id;
      trans_assign_expr = lit("") >> state_id >> +(char_('a','z'));
      trans_expr = state_id >> eol >> no_skip[ trans_assign_expr ];
      assign_expr = char_("a-zA-Z");
      states_list = "states" >> +(id);
      expr = (trans_expr | start_expr | states_list) >> +eol;
      script = *expr >> eoi;

      BOOST_SPIRIT_DEBUG_NODE(script);
      BOOST_SPIRIT_DEBUG_NODE(states_list);
      BOOST_SPIRIT_DEBUG_NODE(expr);
      BOOST_SPIRIT_DEBUG_NODE(start_expr);
      BOOST_SPIRIT_DEBUG_NODE(assign_expr);
      BOOST_SPIRIT_DEBUG_NODE(trans_expr);
      BOOST_SPIRIT_DEBUG_NODE(trans_assign_expr);
      BOOST_SPIRIT_DEBUG_NODE(state_id);
      BOOST_SPIRIT_DEBUG_NODE(id);

      std::string val = "PASS";
      //test_parser("  ",+char_("a-zA-Z"));
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As llonesmiz very aptly pointed out:

  • look for static asserts (****** like this in the error 'novel' ******)
  • look for comments directly (I mean, directly) above a 'failing' line. This includes the points at which template instantiations were triggered.

    If you are seeing a compilation error here stating that the forth parameter can't be converted to a required target type then you are probably trying to use a rule or a grammar with an incompatible skipper type.

    In this case, since you want to use trans_assign_expr without a skipper, you must declare it without one:

    // no skippers
    qi::rule<Iterator, std::string()> id;
    qi::rule<Iterator> state_id;
    qi::rule<Iterator> trans_assign_expr;
    

This makes it compile. The output suggests it doesn't /work/, though.

I'm not convinced this is what you want to do. Since lexeme seems to be a better option here, anyway. I'd simplify the id rule:

id = lexeme [ alpha >> *alnum ];

lexeme already has the property of locally disabling the Skipper in use.

Here is a slight change of the grammar http://liveworkspace.org/code/70f3319342c567854b9f785ac13508d6

trans_assign_expr = state_id >> +(char_('a','z'));
trans_expr        = state_id >> no_skip [ eol >> '' ] >> trans_assign_expr;

As you can see, I include the with eol in a no_skip (to avoid the skipper eating the tab). This way, the skipper is still in effect in trans_assign_expr - which makes it accept the extra space. Given the input

const char input[]="state PASS
 state FAIL more";

The output is now

<script>
  <try>state PASS
 state F</try>
  <expr>
    <try>state PASS
 state F</try>
    <trans_expr>
      <try>state PASS
 state F</try>
      <state_id>
        <try>state PASS
 state F</try>
        <id>
          <try> PASS
 state FAIL m</try>
          <success>
 state FAIL more
</success>
          <attributes>[[P, A, S, S]]</attributes>
        </id>
        <success>
 state FAIL more
</success>
        <attributes>[]</attributes>
      </state_id>
      <trans_assign_expr>
        <try> state FAIL more
</try>
        <state_id>
          <try> state FAIL more
</try>
          <id>
            <try> FAIL more
</try>
            <success> more
</success>
            <attributes>[[F, A, I, L]]</attributes>
          </id>
          <success> more
</success>
          <attributes>[]</attributes>
        </state_id>
        <success>
</success>
        <attributes>[]</attributes>
      </trans_assign_expr>
      <success>
</success>
      <attributes>[]</attributes>
    </trans_expr>
    <success></success>
    <attributes>[]</attributes>
  </expr>
  <expr>
    <try></try>
    <trans_expr>
      <try></try>
      <state_id>
        <try></try>
        <fail/>
      </state_id>
      <fail/>
    </trans_expr>
    <start_expr>
      <try></try>
      <fail/>
    </start_expr>
    <states_list>
      <try></try>
      <fail/>
    </states_list>
    <fail/>
  </expr>
  <success></success>
  <attributes>[]</attributes>
</script>
true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...