0

I'm trying to use spirit to parse a simple logical expression. eg: variable_name == value_name && ... After reading the docs (spirit, qi), it's still defeating me to parse a simple variable-like tokens that may include '_'. I get ASAN violations in my build, or segfaults on coliru.

The problem is the line: auto term_eq_ = +(qi::alpha|qi::char_('_'))

What's wrong with this?

#include <boost/spirit/include/qi.hpp>

namespace qi    = boost::spirit::qi;

std::string SpiritAsan(std::string expr_string)
{

    auto f = expr_string.begin();
    auto l = expr_string.end();

    // Seg fault or ASAN violaton:
    auto term_eq_ = +(qi::alpha|qi::char_('_'));  // close to what i want, variable-like "read a token"
    //auto term_eq_ = +qi::char_("A-Z");    //another try, seg fault

    // Works fine:   
    //auto term_eq_ = +(qi::alpha|qi::int_);  //  dumb, but has a alternate '|'


    try
    {
        bool ok = qi::parse(f,l,term_eq_ );

        if (!ok)
            return "invalid input";
        else
            return "result:  Parsed! (some at least), remaining:" + std::string(f,l);

    }
    catch (const qi::expectation_failure<decltype(f)>& e)
    {
        return "expectation_failure";
    }
}

int main()
{
    std::cout << SpiritAsan("Some_Attrib == SomeVal");

    return 0;
}

Live here: http://coliru.stacked-crooked.com/a/14a193daebdc9ed1

Thanks for your help.

MartinP
  • 688
  • 1
  • 8
  • 17
  • https://stackoverflow.com/a/22027181/2417774 – llonesmiz Mar 30 '20 at 21:40
  • Thanks to @sehe & @llonesmiz, I now see we cant use `auto x = parser_expr`. Using `qi::copy(parser_expr)` works. Run that fix here: http://coliru.stacked-crooked.com/a/3124abb803901797 . – MartinP Mar 31 '20 at 09:05
  • My mistake was that I've seen too much X3 info, hunting for actual explanations of how this stuff works. While the spirit docs are long/plentiful, it remains like magic unless it is explained *how it works*. Best of those I've found is the video on X3 (https://www.youtube.com/watch?v=XZKZZy_v7hE&t=61s). I don't see much docs on lifetime, for example. What I've seen so far are oblique references of solutions to problems that seem unstated. Such as `qi::copy()`. I love the idea of spirit, but the docs... well. I'd like more on how it actually works. Not just what it does (when it works). – MartinP Mar 31 '20 at 09:08

0 Answers0