How to correctly specify binding conditions in Spirit X3?

I am new to writing parsers. I am trying to create a parser that can extract US zip codes from input text. I have created the following parser templates that do most of what I want. I can match 5 digit zip codes or 9 digit zip codes (90210-1234) as expected.

However, this prevents me from avoiding matching things like:

246764 (returns 46764)
578397 (returns 78397)

I wanted to specify some anchoring conditions for right and left from the above template, in the hope that I can eliminate the above examples. More specifically, I want to disallow matching when numbers or dashes are near the start or end of the candidate's zip code.

Test data ( bold entries must be matched)

12345

foo456

ba58r

246764anc

578397

90210-
15206-1
15222-1825
15212-4267-53410-2807

Complete code:

using It = std::string::const_iterator;
using ZipCode = boost::fusion::vector<It, It>;

namespace boostnamespace spirit { namespace x3 { namespace traits {
    template <>
    void move_to<It, ZipCode>(It b, It e, ZipCode& z)
    {
        z =
        {
            b,
            e
        };
}}}}}

void Parse(std::string const& input)
{
    auto start = std::begin(input);
    auto begin = start;
    auto end = std::end(input);

    ZipCode current;
    std::vector<ZipCode> matches;

    auto const fiveDigits = boost::spirit::x3::repeat(5)[boost::spirit::x3::digit];
    auto const fourDigits = boost::spirit::x3::repeat(4)[boost::spirit::x3::digit];
    auto const dash = boost::spirit::x3::char_('-');
    auto const notDashOrDigit = boost::spirit::x3::char_ - (dash | boost::spirit::x3::digit);

    auto const zipCode59 = 
        boost::spirit::x3::lexeme
        [
            -(&notDashOrDigit) >> 
            boost::spirit::x3::raw[fiveDigits >> -(dash >> fourDigits)] >> 
            &notDashOrDigit
        ];

    while (begin != end)
    {
        if (!boost::spirit::x3::phrase_parse(begin, end, zipCode59, boost::spirit::x3::blank, current))
        {
            ++begin;
        }
        else
        {
            auto startOffset = std::distance(start, boost::fusion::at_c<0>(current));
            auto endOffset = std::distance(start, boost::fusion::at_c<1>(current));
            auto length = std::distance(boost::fusion::at_c<0>(current), boost::fusion::at_c<1>(current));
            std::cout << "Matched (\"" << startOffset
                << "\", \"" 
                << endOffset
                << "\") => \""
                << input.substr(startOffset, length)
                << "\""
                << std::endl;
        }
    }
}

      

This code with the above test data gives the following output:

Matches ("0", "5") => "12345"
Matches ("29", "34") => "46764"
Agreed ("42", "47") => "78397"
Matches ("68" , "78") => "15222-1825"

If I change zipCode59 to the following, I get no hits:

auto const zipCode59 = 
    boost::spirit::x3::lexeme
    [
        &notDashOrDigit >> 
        boost::spirit::x3::raw[fiveDigits >> -(dash >> fourDigits)] >> 
        &notDashOrDigit
    ];

      

I read this question: Stop X3 characters from matching substrings . However, this question uses a symbol table. I don't think this might work for me because I am missing the ability to specify hardcoded strings. I also don't understand how the answer to this question allows you to disallow leading content.

+3


source to share


1 answer


Usage -(parser)

just makes it (parser)

optional. Using it with -(&parser)

has no effect.

Perhaps you want a negative assertion ("lookahead"), which is !(parser)

(the opposite &(parser)

).

Note that potential confusion can be caused by the difference between unary minus (negative assertion) and binary minus (shortening character sets).

The statement that the beginning of a zip code starts with a dash / number character seems ... confusing. If you want to positively assert something other than a dash or a number, it will &~char_("-0-9")

(using unary ~

to negate the character set), but it will prevent a match at the very beginning of the input.

Positive approach

Throwing away some of the complexity left and right, I naively start with something like:

using It = std::string::const_iterator;
using ZipCode = boost::iterator_range<It>;

auto Parse(std::string const& input) {
    using namespace boost::spirit::x3;
    auto dig = [](int n) { return repeat(n)[digit]; };
    auto const zip59 = dig(5) >> -('-' >> dig(4));
    auto const valid = zip59 >> !graph;

    std::vector<ZipCode> matches;
    if (!parse(begin(input), end(input), *seek[raw[valid]], matches))
        throw std::runtime_error("parser failure");

    return matches;
}

      

Which, of course, is too much:

Live On Coliru

Matched '12345'
Matched '78397'
Matched '15222-1825'
Matched '53410-2807'

      

Making heroes

To constrain it (and still match when you start typing), you could seek[&('-'|digit)]

then require a valid zip code.



I freely admit that I had to play around with things a bit before I got it β€œright”. In the process, I created a debug helper:

auto trace_as = [&input](std::string const& caption, auto parser) { 
    return raw[parser] [([=,&input](auto& ctx) { 
        std::cout << std::setw(12) << (caption+":") << " '";
        auto range = _attr(ctx);
        for (auto ch : range) switch (ch) {
            case '\0': std::cout << "\\0"; break;
            case '\r': std::cout << "\\r"; break;
            case '\n': std::cout << "\\n"; break;
            default: std::cout << ch;
        }
        std::cout << "' at " << std::distance(input.begin(), range.begin()) << "\n";
    })]; 
};

auto const valid = seek[&trace_as("seek", '-' | digit)] >> raw[zip59] >> !graph;

std::vector<ZipCode> matches;
if (!parse(begin(input), end(input), -valid % trace_as("skip", *graph >> +space), matches))
    throw std::runtime_error("parser failure");

      

Which gives the following additional diagnostic output:

Live On Coliru

       seek: '1' at 0
       skip: '\n    ' at 5
       seek: '4' at 13
       skip: 'foo456\n    ' at 10
       seek: '5' at 23
       skip: 'ba58r\n    ' at 21
       seek: '2' at 31
       skip: '246764anc\n    ' at 31
       seek: '5' at 45
       skip: '578397\n    ' at 45
       seek: '9' at 56
       skip: '90210-\n    ' at 56
       seek: '1' at 67
       skip: '15206-1\n    ' at 67
       seek: '1' at 79
       skip: '\n    ' at 89
       seek: '1' at 94
Matched '12345'
Matched '15222-1825'

      

Now that the result is what we want, cut the forests again:

Full list

Live On Coliru

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

using It = std::string::const_iterator;
using ZipCode = boost::iterator_range<It>;

auto Parse(std::string const& input) {
    using namespace boost::spirit::x3;
    auto dig = [](int n) { return repeat(n)[digit]; };
    auto const zip59 = dig(5) >> -('-' >> dig(4));
    auto const valid = seek[&('-' | digit)] >> raw[zip59] >> !graph;

    std::vector<ZipCode> matches;
    if (!parse(begin(input), end(input), -valid % (*graph >> +space), matches))
        throw std::runtime_error("parser failure");

    return matches;
}

#include <iostream>
int main() {
    std::string const sample = R"(12345
foo456
ba58r
246764anc
578397
90210-
15206-1
15222-1825
15212-4267-53410-2807)";

    for (auto zip : Parse(sample))
        std::cout << "Matched '" << zip << "'\n";
}

      

Printing

Matched '12345'
Matched '15222-1825'

      

+2


source







All Articles