Boost :: Spiritで、セミコロンまたはオプションのセミコロン付きの改行が続くエントリを解析するにはどうすればよいですか?
入力例。各エントリはintとdoubleです。
12 1.4;
63 13.2
2423 56.4 ; 5 8.1
エントリとそれに続く空白を解析するだけのサンプルコードを次に示します。
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <boost/fusion/include/std_pair.hpp>
namespace qi = boost::spirit::qi;
typedef std::pair<int, double> Entry;
template <typename Iterator, typename Skipper>
struct MyGrammar : qi::grammar<Iterator, std::vector<Entry>(), Skipper> {
MyGrammar() : MyGrammar::base_type(entries) {
entry = qi::int_ >> qi::double_;
entries = +entry;
}
qi::rule<Iterator, Entry(), Skipper> entry;
qi::rule<Iterator, std::vector<Entry>(), Skipper> entries;
};
int main() {
typedef boost::spirit::istream_iterator It;
std::cin.unsetf(std::ios::skipws);
It it(std::cin), end;
MyGrammar<It, qi::space_type> entry_grammar;
std::vector<Entry> entries;
if (qi::phrase_parse(it, end, entry_grammar, qi::space, entries)
&& it == end) {
BOOST_FOREACH(Entry const& entry, entries) {
std::cout << entry.first << " and " << entry.second << std::endl;
}
}
else {
std::cerr << "FAIL" << std::endl;
exit(1);
}
return 0;
}
さて、私が望む方法(各エントリの後にセミコロンまたはオプションのセミコロン付きの改行が続く)を解析するために、これを置き換えました:
entries = +entry;
これで:
entries = +(entry >> (qi::no_skip[qi::eol] || ';'));
ここで、boost::spirit
演算子は次の||
ことを意味します:(aの後にオプションのbが続く)またはb。1.4
ただし、この例の入力の後にスペースがある場合はエラーになります。
12 1.4
63 13.2
スペースが一致していないのは理にかなっていno_skip
ますが、解決策を見つけることができませんでした。