Остановить символы X3 из соответствующих подстрок

Как предотвратить парсер символов X3 для сопоставления частичных токенов? В приведенном ниже примере я хочу сопоставить «foo», но не «foobar». Я пытался бросить анализатор символов в lexeme директива, как для идентификатора, но тогда ничего не совпадает.

Спасибо за любые идеи!

#include <string>
#include <iostream>
#include <iomanip>

#include <boost/spirit/home/x3.hpp>int main() {

boost::spirit::x3::symbols<int> sym;
sym.add("foo", 1);

for (std::string const input : {
"foo",
"foobar",
"barfoo"})
{
using namespace boost::spirit::x3;

std::cout << "\nParsing " << std::left << std::setw(20) << ("'" + input + "':");

int v;
auto iter = input.begin();
auto end  = input.end();
bool ok;
{
// what's right rule??

// this matches nothing
// auto r = lexeme[sym - alnum];

// this matchs prefix strings
auto r = sym;

ok = phrase_parse(iter, end, r, space, v);
}

if (ok) {
std::cout << v << " Remaining: " << std::string(iter, end);
} else {
std::cout << "Parse failed";
}
}
}

2

Решение

Ци раньше имел distinct в их хранилище.

X3 нет.

То, что решает проблему для показанного вами случая, — это простое предварительное утверждение:

auto r = lexeme [ sym >> !alnum ];

Вы могли бы сделать distinct помощник тоже легко, например:

auto kw = [](auto p) { return lexeme [ p >> !(alnum | '_') ]; };

Теперь вы можете просто разобрать kw(sym),

Жить на Колиру

#include <iostream>
#include <boost/spirit/home/x3.hpp>

int main() {

boost::spirit::x3::symbols<int> sym;
sym.add("foo", 1);

for (std::string const input : { "foo", "foobar", "barfoo" }) {

std::cout << "\nParsing '" << input << "': ";

auto iter      = input.begin();
auto const end = input.end();

int v = -1;
bool ok;
{
using namespace boost::spirit::x3;
auto kw = [](auto p) { return lexeme [ p >> !(alnum | '_') ]; };

ok = phrase_parse(iter, end, kw(sym), space, v);
}

if (ok) {
std::cout << v << " Remaining: '" << std::string(iter, end) << "'\n";
} else {
std::cout << "Parse failed";
}
}
}

Печать

Parsing 'foo': 1 Remaining: ''

Parsing 'foobar': Parse failed
Parsing 'barfoo': Parse failed
3

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector