4

質問: スピリット-一般リスト

こんにちは皆さん、

私の主題が正しいかどうかはわかりませんが、テストコードはおそらく私が達成したいことを示しています。

私は次のようなものを解析しようとしています:

  • '%40'から'@'
  • '%3C'から'<'

以下に最小限のテストケースがあります。なぜこれが機能しないのかわかりません。おそらく私が間違いを犯しているのかもしれませんが、私にはわかりません。

使用:コンパイラ:gcc 4.6ブースト:現在のトランク

私は次のコンパイル行を使用します:

g++ -o main -L/usr/src/boost-trunk/stage/lib -I/usr/src/boost-trunk -g -Werror -Wall -std=c++0x -DBOOST_SPIRIT_USE_PHOENIX_V3 main.cpp


#include <iostream>
#include <string>

#define BOOST_SPIRIT_UNICODE

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

typedef boost::uint32_t uchar; // Unicode codepoint

namespace qi = boost::spirit::qi;

int main(int argc, char **argv) {

    // Input
    std::string input = "%3C";
    std::string::const_iterator begin = input.begin();
    std::string::const_iterator end = input.end();

    using qi::xdigit;
    using qi::_1;
    using qi::_2;
    using qi::_val;

    qi::rule<std::string::const_iterator, uchar()> pchar =
        ('%' > xdigit > xdigit) [_val = (_1 << 4) + _2];

    std::string result;
    bool r = qi::parse(begin, end, pchar, result);
    if (r && begin == end) {
        std::cout << "Output:   " << result << std::endl;
        std::cout << "Expected: < (LESS-THAN SIGN)" << std::endl;
    } else {
        std::cerr << "Error" << std::endl;
        return 1;
    }

    return 0;
}

よろしく、

MatthijsMöhlmann

4

1 に答える 1

2

qi::xdigitは、あなたが思っていることをしません: 生の文字を返します (つまり'0'、 ではありません0x00)。

qi::uint_parserおまけとして、解析をはるかに簡単にすることで、有利に活用できます。

typedef qi::uint_parser<uchar, 16, 2, 2> xuchar;
  • phoenix に頼る必要はありません (Boost の古いバージョンで動作するようにします)
  • 一度に両方の文字を取得します (それ以外の場合は、整数符号の拡張を防ぐために大量のキャストを追加する必要があるかもしれません)

修正されたサンプルを次に示します。

#include <iostream>
#include <string>

#define BOOST_SPIRIT_UNICODE

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

typedef boost::uint32_t uchar; // Unicode codepoint

namespace qi = boost::spirit::qi;

typedef qi::uint_parser<uchar, 16, 2, 2> xuchar;
const static xuchar xuchar_ = xuchar();


int main(int argc, char **argv) {

    // Input
    std::string input = "%3C";
    std::string::const_iterator begin = input.begin();
    std::string::const_iterator end = input.end();

    qi::rule<std::string::const_iterator, uchar()> pchar = '%' > xuchar_;

    uchar result;
    bool r = qi::parse(begin, end, pchar, result);

    if (r && begin == end) {
        std::cout << "Output:   " << result << std::endl;
        std::cout << "Expected: < (LESS-THAN SIGN)" << std::endl;
    } else {
        std::cerr << "Error" << std::endl;
        return 1;
    }

    return 0;
}

出力:

Output:   60
Expected: < (LESS-THAN SIGN)

「<」は確かにASCII 60です

于 2011-11-10T13:47:30.513 に答える