12

いくつかのチュートリアル (例: http://boost-spirit.com/home/articles/qi-example/nabialek-trick/ ) に従って、Nabialek トリックを使用して動的パーサーを作成したいと考えています。解析はすでに正常に機能していますが、属性が転送されません。https://stackoverflow.com/a/9109972/2524462のような説明は、属性は可能であるが引数ではないことを示唆しています。

これは、文字列と数値を構造体に解析するほんの一例です。私の問題を紹介するためだけです。このメソッドは、後で動的パーサーが実際に必要となる大規模なシステムで使用する必要があります。

質問: Nabialek トリックを使用して属性を転送するにはどうすればよいですか?

私は精神の専門家ではないので、ご容赦ください。gcc 4.8.1 とブースト 1.54 を使用しています。

#define BOOST_SPIRIT_DEBUG
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

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

//------------------------------------------------------------------------------
// Data structure
struct myline {
  myline()
      : _n(0), _s("") {
  }

  myline(int n, std::string s)
      : _n(n), _s(s) {
  }

  void set(int n, std::string s) {
    _n = n;
    _s = s;
  }

  int _n;
  std::string _s;
};

BOOST_FUSION_ADAPT_STRUCT(::myline, (int, _n) (std::string, _s))

//------------------------------------------------------------------------------
// Parser grammar
template<typename It, typename Skipper = qi::space_type>
struct parser: qi::grammar<It, myline(), Skipper> {
  parser()
      : parser::base_type(start) {
    using namespace qi;

    start = line;

    string %= qi::lexeme["'" >> *~qi::char_("'") >> "'"];

    one = (string >> "@" >> qi::int_)[_val = phx::construct<myline>(_2, _1)];
    two = (qi::int_ >> "@" >> string);

    keyword.add("one", &one)("two", &two);

    line = keyword[_a = _1] >> qi::lazy(*_a);

    on_error<fail>(
        start,
        std::cout << phx::val("Error! Expecting ") << _4
        << phx::val(" here: \"") << phx::construct<std::string>(_3, _2)
        << phx::val("\"\n"));

    BOOST_SPIRIT_DEBUG_NODES((start)(line)(one)(two))
  }

private:
  template<typename Attr> using Rule = qi::rule<It, Attr(), Skipper>;

  Rule<myline> start, one, two;
  qi::rule<It, myline, Skipper, qi::locals<Rule<myline>*> > line;

  Rule<std::string> string;

  qi::symbols<char, Rule<myline>*> keyword;
};

//------------------------------------------------------------------------------
int main() {
  for (const std::string input : std::vector<std::string> { "one 'test'@1",
                                                            "two 2@'test'" }) {
    auto f(std::begin(input)), l(std::end(input));
    const static parser<decltype(f)> p;

    myline parsed_script;
    bool ok = qi::phrase_parse(f, l, p, qi::space, parsed_script);

    if (!ok) {
      std::cout << "invalid input\n";
    }

    std::cout << parsed_script._n << ": " << parsed_script._s << std::endl;

    if (f != l) {
      std::cout << "unparsed: '" << std::string(f, l) << "'" << std::endl;
    }
  }
}

解析結果:

<start>
  <try>one 'test'@1</try>
  <line>
    <try>one 'test'@1</try>
    <one>
      <try> 'test'@1</try>
      <success></success>
      <attributes>[[1, [t, e, s, t]]]</attributes>
    </one>
    <success></success>
    <attributes>[]</attributes><locals>(0x43b0e0)</locals>
  </line>
  <success></success>
  <attributes>[[0, []]]</attributes>
</start>
<start>
  <try>two 2@'test'</try>
  <line>
    <try>two 2@'test'</try>
    <two>
      <try> 2@'test'</try>
      <success></success>
      <attributes>[[2, [t, e, s, t]]]</attributes>
    </two>
    <success></success>
    <attributes>[]</attributes><locals>(0x43b110)</locals>
  </line>
  <success></success>
  <attributes>[[0, []]]</attributes>
</start>
4

1 に答える 1

17

あなたはスピリットクラスで多くの注意を払ってきました:)

いくつかの問題がありました:

  1. ルールの属性宣言lineが間違っていました:

    qi::rule<It, myline, Skipper, qi::locals<Rule<myline>*> > line;
    

    する必要があります

    qi::rule<It, myline(), Skipper, qi::locals<Rule<myline>*> > line;
    
  2. セマンティック アクションが存在する場合、属性の自動伝播は禁止されます。詳細については、最近の回答を参照してください: Boost.spirit: parsing number char and string。したがって、以下を使用して、Spirit の自動ルール動作を明示的に行う必要があります%=

    line = keyword[_a = _1] >> qi::lazy(*_a);
    

    する必要があります

    // NOTE the %=
    line %= omit [ keyword[_a = _1] ] >> qi::lazy(*_a);
    

    ノート:

    • ルールに従うことが%=できますstring(セマンティックアクションは、自動属性伝播を意味しません)
    • を属性に実際に割り当てることができないため、キーワード一致の結果を明示的 に指定する必要があります。omit[]Rule<>*myline

修正版は次のとおりです。

#define BOOST_SPIRIT_DEBUG
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

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

//------------------------------------------------------------------------------
// Data structure
struct myline {
  myline()
      : _n(0), _s("") {
  }

  myline(int n, std::string s)
      : _n(n), _s(s) {
  }

  void set(int n, std::string s) {
    _n = n;
    _s = s;
  }

  int _n;
  std::string _s;
};

BOOST_FUSION_ADAPT_STRUCT(::myline, (int, _n) (std::string, _s))

//------------------------------------------------------------------------------
// Parser grammar
template<typename It, typename Skipper = qi::space_type>
struct parser: qi::grammar<It, myline(), Skipper> {
  parser()
      : parser::base_type(start) {
    using namespace qi;

    start  = line;

    string = qi::lexeme["'" >> *~qi::char_("'") >> "'"];

    one    = (string >> "@" >> qi::int_) [_val           = phx::construct<myline>(_2, _1)];
    two    = (qi::int_ >> "@" >> string);

    keyword.add("one", &one)("two", &two);

    // NOTE the %=
    line %= omit [ keyword[_a = _1] ] >> qi::lazy(*_a);

    on_error<fail>(
        start,
        std::cout << phx::val("Error! Expecting ") << _4
        << phx::val(" here: \"") << phx::construct<std::string>(_3, _2)
        << phx::val("\"\n"));

    BOOST_SPIRIT_DEBUG_NODES((start)(line)(one)(two))
  }

private:
  template<typename Attr> using Rule = qi::rule<It, Attr(), Skipper>;

  Rule<myline> start, one, two;
  qi::rule<It, myline(), Skipper, qi::locals<Rule<myline>* > > line;

  Rule<std::string> string;

  qi::symbols<char, Rule<myline>* > keyword;
};

//------------------------------------------------------------------------------
int main() {
  for (const std::string input : std::vector<std::string> { "one 'test1'@1",
                                                            "two 2@'test2'" }) {
    auto f(std::begin(input)), l(std::end(input));
    const static parser<decltype(f)> p;

    myline parsed_script;
    bool ok = qi::phrase_parse(f, l, p, qi::space, parsed_script);

    if (!ok) {
      std::cout << "invalid input\n";
    }

    std::cout << parsed_script._n << ": " << parsed_script._s << std::endl;

    if (f != l) {
      std::cout << "unparsed: '" << std::string(f, l) << "'" << std::endl;
    }
  }
}

版画:

<start>
  <try>one 'test1'@1</try>
  <line>
    <try>one 'test1'@1</try>
    <one>
      <try> 'test1'@1</try>
      <success></success>
      <attributes>[[1, [t, e, s, t, 1]]]</attributes>
    </one>
    <success></success>
    <attributes>[[1, [t, e, s, t, 1]]]</attributes><locals>(0x6386c0)</locals>
  </line>
  <success></success>
  <attributes>[[1, [t, e, s, t, 1]]]</attributes>
</start>
1: test1
<start>
  <try>two 2@'test2'</try>
  <line>
    <try>two 2@'test2'</try>
    <two>
      <try> 2@'test2'</try>
      <success></success>
      <attributes>[[2, [t, e, s, t, 2]]]</attributes>
    </two>
    <success></success>
    <attributes>[[2, [t, e, s, t, 2]]]</attributes><locals>(0x6386f0)</locals>
  </line>
  <success></success>
  <attributes>[[2, [t, e, s, t, 2]]]</attributes>
</start>
2: test2
于 2013-07-18T08:51:04.753 に答える