问题
As like the description on Boost.Spirit, the only difference between lexeme and no_skip is the pre_skip.
But after some test, I'm still confusing about the exactly meaning for pre_skip.
So what kind of condition will make a difference, maybe a example can help me understand it much better.
Thanks!
回答1:
Pre-skip ignores whitespace at the start of the expression.
Contrasting:
Live On Coliru
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
static std::string const input = " 42j";
int main() {
auto run_test = [](auto p) {
auto f = input.begin(), l = input.end();
int i;
return qi::phrase_parse(f, l, p, qi::space, i)
? std::to_string(i)
: "unparsed";
};
std::cout << "no_skip: " << run_test(qi::no_skip[ qi::int_ >> 'j' ]) << "\n";
std::cout << "lexeme: " << run_test(qi::lexeme[ qi::int_ >> 'j' ]) << "\n";
}
Prints:
no_skip: unparsed
lexeme: 42
As you can see lexeme
will silently eat the leading white space. That's the pre-skip.
来源:https://stackoverflow.com/questions/63234428/boost-spirit-lexeme-vs-no-skip