2

ブーストスピリットx3を使用して、文字列を構造体に解析しようとしています:

struct identifier {
    std::vector<std::string> namespaces;
    std::vector<std::string> classes;
    std::string identifier;

};

これで、次のような文字列に一致するパーサー ルールができました。

foo::bar::baz.bla.blub
foo.bar
boo::bar
foo

私のパーサールールは次のようになります。

auto const nested_identifier_def =
        x3::lexeme[
                -(id_string % "::")
                >> -(id_string % ".")
                >> id_string
        ];

whereid_stringの組み合わせを解析しalphanumます。foo.barたとえば、解析中にルールのこの部分が-(id_string % ".")文字列全体を消費するため、このルールが意図したとおりに解析できないことはわかっています。構造体で正しく解析するようにルールを変更するにはどうすればよいですか?

4

1 に答える 1

3

あなたが次のようなものであると仮定しますid_string

auto const id_string = x3::rule<struct id_string_tag, std::string>{} =
    x3::lexeme[
            (x3::alpha | '_')
        >> *(x3::alnum | '_')
    ];

次に、これがあなたが求めているものだと思います:

auto const nested_identifier_def =
       *(id_string >> "::")
    >> *(id_string >> '.')
    >>  id_string;

Online Demo

問題は、これp % delimitが の省略形であることです。つまり、区切り文字のp >> *(delimit >> p)に常に 1 を消費します。ただし、区切り文字の後にnoが消費され、代わりに次のルールのために残されるようにする必要があります。p *(p >> delimit)p

于 2016-07-22T16:55:06.173 に答える