I'm playing around with Spirit parsers to parse strings like id 1234
. It works perfectly with an inline start = qi::lit("id") >> qi::int_;
but not if I want to put that into a separate qi::grammar-based struct. See cases 1, 2 and 3 in the below example:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iostream>
#include <fstream>
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
struct Context{};
template <typename Iterator, typename SkipParser>
struct IdGrammar : qi::grammar<Iterator, SkipParser>
{
explicit IdGrammar(Context& out) : IdGrammar::base_type(start, "IdGrammar")
{
start = qi::lit("id") >> qi::int_;
}
qi::rule<Iterator, SkipParser> start;
};
template <typename Iterator, typename SkipParser>
struct MyGrammar : qi::grammar<Iterator, SkipParser>
{
explicit MyGrammar(Context& out)
: MyGrammar::base_type(start, "MyGrammar")
{
IdGrammar<Iterator, SkipParser> idGrammar(out);
// start = idGrammar >> *(',' >> idGrammar); // 1 = Parsing fails
start = idGrammar; // 2 = Parsing fails
// start = qi::lit("id") >> qi::int_; // 3 = Parsing succeeds
start.name("the start");
qi::on_error<qi::fail>(
start,
phoenix::ref(std::cout) << phoenix::val("Parsing error: expecting ") << qi::_4 // what failed?
<< phoenix::val(" here: \"")
<< phoenix::construct<std::string>(qi::_3, qi::_2) // iterators to error-pos, end
<< phoenix::val("\"")
<< std::endl);
}
qi::rule<Iterator, SkipParser> start;
};
int main()
{
typedef std::string::const_iterator iterator_type;
Context ctx;
MyGrammar<iterator_type, qi::space_type> roman_parser(ctx); // Our grammar
std::string str = "id 5012";
iterator_type iter = str.begin(), end = str.end();
bool r = phrase_parse(iter, end, roman_parser, qi::space);
if (r && iter == end)
{
std::cout << "Parsing succeeded\n";
}
else
{
std::string rest(iter, end);
std::cout << "Parsing failed\n";
std::cout << "stopped at: \"" << rest << "\"\n";
}
}
The output for the failing cases (1 and 2) is:
Parsing failed
stopped at: "id 5012"
What makes the difference here? Mind that I removed any assignment of the integer result to keep the example minimal - assuming that is unrelated to the problem.
The lifetime of idGrammar
must be longer than just the constructor scope.
Make it a member variable:
template <typename Iterator, typename SkipParser>
struct MyGrammar : qi::grammar<Iterator, SkipParser>
{
explicit MyGrammar(Context& out)
: MyGrammar::base_type(start, "MyGrammar")
, idGrammar(out)
{
start = idGrammar >> *(',' >> idGrammar); // 1 = Now parsing succeeds
// start = idGrammar; // 2 = Now parsing succeeds
// start = qi::lit("id") >> qi::int_; // 3 = Parsing succeeds
start.name("the start");
qi::on_error<qi::fail>(
start,
phoenix::ref(std::cout) << phoenix::val("Parsing error: expecting ") << qi::_4 // what failed?
<< phoenix::val(" here: \"")
<< phoenix::construct<std::string>(qi::_3, qi::_2) // iterators to error-pos, end
<< phoenix::val("\"")
<< std::endl);
}
qi::rule<Iterator, SkipParser> start;
IdGrammar<Iterator, SkipParser> idGrammar;
};