I'm trying to write a parser using Boost::Spirit, and I have the parser written and compiling. The problem is, when I try to compile the parsing function, the compiler throws out a bunch of template errors. Here's the Qi grammar:
template<typename Iterator>
struct etf_parser : qi::grammar<Iterator, std::map<std::string, etfnode>(), ascii::space_type> {
etf_parser() : etf_parser::base_type(start) {
using qi::int_;
using qi::lit;
using qi::double_;
using qi::bool_;
using qi::lexeme;
using ascii::char_;
quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];
dataVal %= (quoted_string | double_ | int_ | bool_ | listObj | pairObj | mapObj);
pairObj %= ('<' >> dataVal >> ',' >> dataVal >> '>');
listObj %= '{' >> dataVal % ',' >> '}';
mapKey %= +qi::char_("a-zA-Z_-0-9.");
mapPair %= mapKey >> lit('=') >> dataVal;
mapObj %= '(' >> mapPair % ',' >> ')';
start %= mapPair >> ';';
}
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
// Data value parsers
qi::rule<Iterator, etfnode(), ascii::space_type> dataVal;
qi::rule<Iterator, std::vector<etfnode>(), ascii::space_type> listObj;
qi::rule<Iterator, std::pair<etfnode, etfnode>(), ascii::space_type> pairObj;
qi::rule<Iterator, std::map<std::string, etfnode>(), ascii::space_type> mapObj;
qi::rule<Iterator, std::pair<std::string, etfnode>(), ascii::space_type> mapPair;
qi::rule<Iterator, std::string(), ascii::space_type> mapKey;
qi::rule<Iterator, std::map<std::string, etfnode>(), ascii::space_type> start;
};
And here's the parsing function. When I comment out the qi::parse call, the code compiles fine:
ETFDocument::ETFDocument(std::string content) {
etf_parser<std::string::const_iterator> parser;
std::map<std::string, rwnode> results;
std::string::const_iterator begin = content.begin();
std::string::const_iterator end = content.end();
bool result = qi::parse(begin, end, parser, results);
if(result) printf("Parsing succeeded\n"); else printf("Parsing failed\n");
m_root = etfnode(results);
}
The compiler spits out the following error when I try to compile:
In file included from /usr/include/boost/spirit/home/qi/nonterminal.hpp:14:0,
from /usr/include/boost/spirit/home/qi.hpp:20,
from /usr/include/boost/spirit/include/qi.hpp:16,
from libmcg/etf.cpp:8:
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp: In instantiation of ‘bool boost::spirit::qi::rule<Iterator, T1, T2, T3, T4>::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Context = boost::spirit::context<boost::fusion::cons<std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >&, boost::fusion::nil>, boost::spirit::locals<> >; Skipper = boost::spirit::unused_type; Attribute = std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >; Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; T1 = std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >(); T2 = boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >, 0l>; T3 = boost::spirit::unused_type; T4 = boost::spirit::unused_type]’:
/usr/include/boost/spirit/home/qi/reference.hpp:43:71: required from ‘bool boost::spirit::qi::reference<Subject>::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; Context = boost::spirit::context<boost::fusion::cons<std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >&, boost::fusion::nil>, boost::spirit::locals<> >; Skipper = boost::spirit::unused_type; Attribute = std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >; Subject = const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type>]’
/usr/include/boost/spirit/home/qi/parse.hpp:86:82: required from ‘bool boost::spirit::qi::parse(Iterator&, Iterator, const Expr&, Attr&) [with Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; Expr = etf_parser<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >; Attr = std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >]’
libmcg/etf.cpp:113:53: required from here
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:303:17: error: no match for call to ‘(const function_type {aka const boost::function<bool(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, boost::spirit::context<boost::fusion::cons<std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >&, boost::fusion::nil>, boost::fusion::vector0<> >&, const boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >&)>}) (__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type>::context_type&, const boost::spirit::unused_type&)’
In file included from /usr/include/boost/function/detail/maybe_include.hpp:33:0,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:67,
from /usr/include/boost/function.hpp:64,
from /usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:16,
from /usr/include/boost/spirit/home/qi/nonterminal.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:20,
from /usr/include/boost/spirit/include/qi.hpp:16,
from libmcg/etf.cpp:8:
/usr/include/boost/function/function_template.hpp:1021:7: note: candidate is:
/usr/include/boost/function/function_template.hpp:754:17: note: boost::function4<R, T1, T2, T3, T4>::result_type boost::function4<R, T1, T2, T3, T4>::operator()(T0, T1, T2, T3) const [with R = bool; T0 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&; T1 = const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&; T2 = boost::spirit::context<boost::fusion::cons<std::map<std::basic_string<char>, boost::recursive_wrapper<ETFDocument::etfnode> >&, boost::fusion::nil>, boost::fusion::vector0<> >&; T3 = const boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >&; boost::function4<R, T1, T2, T3, T4>::result_type = bool]
/usr/include/boost/function/function_template.hpp:754:17: note: no known conversion for argument 4 from ‘const boost::spirit::unused_type’ to ‘const boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >&’
As far as I can tell, it's looking for a Skipper, but getting boost::spirit::unused_type instead. I'm not sure why this would happen, since I specified a Skipper in my parser definition. I'm using boost v1.49.0 on gcc 4.7.1.
EDIT: Here's the definition for etfnode. There's a typedef at the beginning of the cpp file (containing the other code fragments) that aliases "etfnode" to "rwnode".
enum DataType {
DT_INT,
DT_STRING,
DT_FLOAT,
DT_BOOL,
DT_LIST,
DT_PAIR,
DT_MAP
};
struct etfnode;
typedef boost::recursive_wrapper<etfnode> rwnode;
typedef boost::variant<
int,
std::string,
double,
bool,
std::vector<rwnode>,
std::pair<rwnode, rwnode>,
std::map<std::string, rwnode> > etfvalue;
struct etfnode {
DataType type;
etfvalue value;
etfnode(const std::string& s);
etfnode(const int i);
etfnode(const double d);
etfnode(const bool b);
etfnode(const std::vector<rwnode>& n);
etfnode(const std::pair<rwnode, rwnode>& p);
etfnode(const std::map<std::string, rwnode>& p);
etfnode();
};
And a test string:
foo = 6;
bar = <"bar", 16.5>;
baz = {
(
foobar = "foo",
bar = 12
),
"foobar"
};
I think the most important culprit was the fact that you were using qi::parse instead of qi::phrase_parse, while your grammar explicitely uses a skipper.
I also rewrote the defintion of the etfvalue recursive variant. I'm not sure whether your version should have worked, but at least now, you can just use etfnode in all places where you'd expect to. It looks more consistent to me this way.
Here's code that compiles fine for me. It parses the sample input (see main()) with the following output:
Parsing succeeded
Unparsed remaining: ';'
If you actually wanted to accept the trailing ;, fix the main rule to be more like
start = *(mapPair >> ';'); // or *(mapPair >> (';'|qi::eoi))
Good luck!
//add streaming operators for etfnode and etfvalue if you want to debug this:
//#define BOOST_SPIRIT_DEBUG
#include <map>
#include <string>
#include <boost/variant/recursive_wrapper.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
enum DataType {
DT_INT,
DT_STRING,
DT_FLOAT,
DT_BOOL,
DT_LIST,
DT_PAIR,
DT_MAP
};
struct etfnode;
typedef boost::variant<
int,
std::string,
double,
bool,
boost::recursive_wrapper<std::vector<etfnode> >,
boost::recursive_wrapper<std::pair<etfnode, etfnode> >,
boost::recursive_wrapper<std::map<std::string, etfnode> >
> etfvalue;
struct etfnode {
DataType type;
etfvalue value;
etfnode(const std::string& s) { value = s; type = DT_STRING; }
etfnode(const int i) { value = i; type = DT_INT; }
etfnode(const double d) { value = d; type = DT_FLOAT; }
etfnode(const bool b) { value = b; type = DT_BOOL; }
etfnode(const std::vector<etfnode>& n) { value = n; type = DT_LIST; }
etfnode(const std::pair<etfnode, etfnode>& p) { value = p; type = DT_PAIR; }
etfnode(const std::map<std::string, etfnode>& p) { value = p; type = DT_MAP; }
etfnode() { }
};
template<typename Iterator>
struct etf_parser : qi::grammar<Iterator, std::map<std::string, etfnode>(), ascii::space_type> {
etf_parser() : etf_parser::base_type(start) {
using qi::int_;
using qi::lit;
using qi::double_;
using qi::bool_;
using qi::lexeme;
using ascii::char_;
quoted_string = lexeme['"' >> +(char_ - '"') >> '"'];
dataVal = (quoted_string | double_ | int_ | bool_
| listObj | pairObj
| mapObj
);
listObj = '{' >> dataVal % ',' >> '}';
pairObj = lit('<') >> dataVal >> ',' >> dataVal >> '>';
mapKey = +qi::char_("a-zA-Z_-0-9.");
mapPair = mapKey >> lit('=') >> dataVal;
mapObj = '(' >> mapPair % ',' >> ')';
start = mapPair % ';';
BOOST_SPIRIT_DEBUG_NODE(quoted_string);
BOOST_SPIRIT_DEBUG_NODE(dataVal);
BOOST_SPIRIT_DEBUG_NODE(listObj);
//BOOST_SPIRIT_DEBUG_NODE(pairObj);
BOOST_SPIRIT_DEBUG_NODE(mapObj);
BOOST_SPIRIT_DEBUG_NODE(mapKey);
BOOST_SPIRIT_DEBUG_NODE(mapPair);
BOOST_SPIRIT_DEBUG_NODE(start);
}
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
// Data value parsers
qi::rule<Iterator, etfnode(), ascii::space_type> dataVal;
qi::rule<Iterator, std::vector<etfnode>(), ascii::space_type> listObj;
qi::rule<Iterator, std::pair<etfnode, etfnode>(), ascii::space_type> pairObj;
qi::rule<Iterator, std::map<std::string, etfnode>(), ascii::space_type> mapObj;
qi::rule<Iterator, std::pair<std::string, etfnode>(), ascii::space_type> mapPair;
qi::rule<Iterator, std::string(), ascii::space_type> mapKey;
qi::rule<Iterator, std::map<std::string, etfnode>(), ascii::space_type> start;
};
int main()
{
etf_parser<std::string::const_iterator> parser;
std::map<std::string, etfnode> results;
std::string content = "foo = 6;\n"
"bar = <\"bar\", 16.5>;\n"
"baz = {\n"
" (\n"
" foobar = \"foo\",\n"
" bar = 12\n"
" ),\n"
" \"foobar\"\n"
"};";
std::string::const_iterator begin = content.begin();
std::string::const_iterator end = content.end();
bool result = qi::phrase_parse(begin, end, parser, ascii::space, results);
if(result) printf("Parsing succeeded\n"); else printf("Parsing failed\n");
if (begin!=end)
std::cout << "Unparsed remaining: '" << std::string(begin,end) << "'\n";
//m_root = etfnode(results);
}
Related
A follow up to this question Handling boost spirit expression on string. Again thanks #sehe! But when I try to wrap all the grammar and ast in the namespaces and call them from within a class, I get an error by the phrase_parse api saying its expecting 6 arguments instead of 5.
The code I'm trying to implement:
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/variant.hpp>
#include <iomanip>
#include <list>
#include <string>
namespace client { namespace ast {
struct signed_;
struct program;
using operand = boost::variant<unsigned int, bool,
boost::recursive_wrapper<signed_>,
boost::recursive_wrapper<program>>;
struct signed_ { char sign; operand operand_; };
struct operation { char operator_; operand operand_; };
struct program { operand first; std::list<operation> rest; };
}} // namespace client::ast
BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, sign, operand_)
BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, operator_, operand_)
BOOST_FUSION_ADAPT_STRUCT(client::ast::program, first, rest)
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST evaluator
///////////////////////////////////////////////////////////////////////////
struct eval {
using result_type = int;
int operator()(auto const& n) const { return n; }
int operator()(operation const& x, int lhs) const {
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.operator_) {
case '+': return lhs + rhs;
case '-': return lhs - rhs;
case '*': return lhs * rhs;
case '/': return lhs / rhs;
case '|': return lhs | rhs;
case '&': return lhs & rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(signed_ const& x) const {
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.sign) {
case '-': return -rhs;
case '+': return +rhs;
case '!': return !rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(program const& x) const {
int state = boost::apply_visitor(*this, x.first);
for (auto& oper : x.rest) {
state = (*this)(oper, state);
}
return state;
}
};
}} // namespace client::ast
namespace client
{
namespace qi = boost::spirit::qi;
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ast::program(), qi::space_type> {
calculator() : calculator::base_type(expression) {
using namespace qi::labels;
expression = term >> *(qi::char_("-+|&") >> term);
term = factor >> *(qi::char_("*/!") >> factor);
str = '"' >> *~qi::char_('"') >> '"';
equality_comparison = (str >> '=' >> '=' >> str)[_val = (_1 == _2)];
inequality_comparison = (str >> '!' >> '=' >> str)[_val = (_1 != _2)];
factor = inequality_comparison | equality_comparison | qi::uint_ | qi::bool_ | qi::char_("-+!") >> factor |
'(' >> expression >> ')';
}
private:
qi::rule<Iterator, ast::program(), qi::space_type> expression, term;
qi::rule<Iterator, ast::operand(), qi::space_type> factor, equality_comparison , inequality_comparison;
qi::rule<Iterator, std::string()> str;
};
}
class Expression_handler{
public:
int Execute_expression(std::string str){
using iterator_type = std::string::const_iterator;
using calculator = client::calculator<iterator_type>;
using ast_program = client::ast::program;
using ast_eval = client::ast::eval;
calculator const calc; // Our grammar
ast_eval const eval; // Evaluates the program
ast_program program;
auto f(std::begin(str)), l(std::end(str));
bool r = phrase_parse(f, l, calc, client::qi::space, program);
if (r && f == l) {
std::cout << "\nResult: " << quoted(str) << " -> " << eval(program) << std::endl;
return (eval(program));
}
else{
std::cout << "\nFailed at: " << quoted(std::string (f, l)) << "\n";
}
return 0;
};
};
int main(){
Expression_handler exp;
exp.Execute_expression("\"abc\" == \"abc\"");
return 0;
}
The error generated for this code:
ion_handler.cpp:37:24: warning: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’
37 | int operator()(auto const& n) const { return n; }
| ^~~~
In file included from /usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:36,
from /usr/include/boost/spirit/home/qi/nonterminal.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:21,
from /usr/include/boost/spirit/include/qi.hpp:16,
from Expression_handler.cpp:5:
/usr/include/boost/spirit/home/qi/reference.hpp: In instantiation of ‘bool boost::spirit::qi::reference<Subject>::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Iterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; Context = boost::spirit::context<boost::fusion::cons<client::ast::program&, boost::fusion::nil_>, boost::spirit::locals<> >; Skipper = boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >; Attribute = client::ast::program; Subject = const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >, client::ast::program(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0>, boost::spirit::unused_type, boost::spirit::unused_type>]’:
/usr/include/boost/spirit/home/qi/parse.hpp:168:45: required from ‘bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const Expr&, const Skipper&, boost::spirit::qi::skip_flag, Attr&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; Expr = client::calculator<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> > >; Skipper = boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0>; Attr = client::ast::program]’
/usr/include/boost/spirit/home/qi/parse.hpp:201:32: required from ‘bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const Expr&, const Skipper&, Attr&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; Expr = client::calculator<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> > >; Skipper = boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0>; Attr = client::ast::program]’
Expression_handler.cpp:114:30: required from here
/usr/include/boost/spirit/home/qi/reference.hpp:43:35: error: no matching function for call to ‘boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >, client::ast::program(), boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0>, boost::spirit::unused_type, boost::spirit::unused_type>::parse(__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, const __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, boost::spirit::context<boost::fusion::cons<client::ast::program&, boost::fusion::nil_>, boost::spirit::locals<> >&, const boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >&, client::ast::program&) const’
43 | return ref.get().parse(first, last, context, skipper, attr_);
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/boost/spirit/home/qi/nonterminal.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:21,
from /usr/include/boost/spirit/include/qi.hpp:16,
from Expression_handler.cpp:5:
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:282:14: note: candidate: ‘template<class Context, class Skipper, class Attribute> bool boost::spirit::qi::rule<Iterator, T1, T2, T3, T4>::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Context = Context; Skipper = Skipper; Attribute = Attribute; Iterator = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; T1 = client::ast::program(); T2 = boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0>; T3 = boost::spirit::unused_type; T4 = boost::spirit::unused_type]’
282 | bool parse(Iterator& first, Iterator const& last
| ^~~~~
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:282:14: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:36,
from /usr/include/boost/spirit/home/qi/nonterminal.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:21,
from /usr/include/boost/spirit/include/qi.hpp:16,
from Expression_handler.cpp:5:
/usr/include/boost/spirit/home/qi/reference.hpp:43:35: note: cannot convert ‘first’ (type ‘__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >’) to type ‘__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >&’
43 | return ref.get().parse(first, last, context, skipper, attr_);
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/boost/spirit/home/qi/nonterminal.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:21,
from /usr/include/boost/spirit/include/qi.hpp:16,
from Expression_handler.cpp:5:
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:333:14: note: candidate: ‘template<class Context, class Skipper, class Attribute, class Params> bool boost::spirit::qi::rule<Iterator, T1, T2, T3, T4>::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&, const Params&) const [with Context = Context; Skipper = Skipper; Attribute = Attribute; Params = Params; Iterator = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; T1 = client::ast::program(); T2 = boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0>; T3 = boost::spirit::unused_type; T4 = boost::spirit::unused_type]’
333 | bool parse(Iterator& first, Iterator const& last
| ^~~~~
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:333:14: note: template argument deduction/substitution failed:
In file included from /usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:36,
from /usr/include/boost/spirit/home/qi/nonterminal.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:21,
from /usr/include/boost/spirit/include/qi.hpp:16,
from Expression_handler.cpp:5:
/usr/include/boost/spirit/home/qi/reference.hpp:43:35: note: candidate expects 6 arguments, 5 provided
43 | return ref.get().parse(first, last, context, skipper, attr_);
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Which other argument is it expecting and how do I fix this?
Your string isn't const, so the iterator type doesn't match the one you declared. The key message is:
boost/spirit/home/qi/reference.hpp|43 col 35| note: cannot convert ‘first’ (type ‘__gnu_cxx::__normal_iterator<char*, __cxx11::basic_string<char> >’) to type ‘__gnu_cxx::__normal_iterator<const char*, __cxx11::basic_string<char> >&’
Simply add const or const&:
int Execute_expression(std::string const& str)
Or don't use auto to declare f and l:
std::string::const_iterator f = std::begin(str), l = std::end(str);
Live On Coliru
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/variant.hpp>
#include <iomanip>
#include <list>
#include <string>
namespace client { namespace ast {
struct signed_;
struct program;
using operand = boost::variant<unsigned int, bool,
boost::recursive_wrapper<signed_>,
boost::recursive_wrapper<program>>;
struct signed_ { char sign; operand operand_; };
struct operation { char operator_; operand operand_; };
struct program { operand first; std::list<operation> rest; };
}} // namespace client::ast
BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, sign, operand_)
BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, operator_, operand_)
BOOST_FUSION_ADAPT_STRUCT(client::ast::program, first, rest)
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST evaluator
///////////////////////////////////////////////////////////////////////////
struct eval {
using result_type = int;
int operator()(auto const& n) const { return n; }
int operator()(operation const& x, int lhs) const {
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.operator_) {
case '+': return lhs + rhs;
case '-': return lhs - rhs;
case '*': return lhs * rhs;
case '/': return lhs / rhs;
case '|': return lhs | rhs;
case '&': return lhs & rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(signed_ const& x) const {
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.sign) {
case '-': return -rhs;
case '+': return +rhs;
case '!': return !rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(program const& x) const {
int state = boost::apply_visitor(*this, x.first);
for (auto& oper : x.rest) {
state = (*this)(oper, state);
}
return state;
}
};
}} // namespace client::ast
namespace client
{
namespace qi = boost::spirit::qi;
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ast::program(), qi::space_type> {
calculator() : calculator::base_type(expression) {
using namespace qi::labels;
expression = term >> *(qi::char_("-+|&") >> term);
term = factor >> *(qi::char_("*/!") >> factor);
str = '"' >> *~qi::char_('"') >> '"';
str_eq = (str >> "==" >> str)[_val = (_1 == _2)];
str_neq = (str >> "!=" >> str)[_val = (_1 != _2)];
factor = str_neq | str_eq | qi::uint_ | qi::bool_ |
qi::char_("-+!") >> factor | '(' >> expression >> ')';
}
private:
qi::rule<Iterator, ast::program(), qi::space_type> expression, term;
qi::rule<Iterator, ast::operand(), qi::space_type> factor, str_eq, str_neq;
qi::rule<Iterator, std::string()> str;
};
}
struct Expression_handler {
static int Execute_expression(std::string const& str) {
using iterator_type = std::string::const_iterator;
using calculator = client::calculator<iterator_type>;
namespace ast = client::ast;
calculator const calc; // Our grammar
ast::eval const eval; // Evaluates the program
ast::program program;
auto f(std::begin(str)), l(std::end(str));
bool r = phrase_parse(f, l, calc, client::qi::space, program);
if (r && f == l) {
std::cout << "Result: " << quoted(str, '\'') << " -> " << eval(program)
<< std::endl;
return eval(program);
} else {
std::cout << "Failed at: " << quoted(std::string(f, l), '\'') << "\n";
}
return 0;
};
};
int main(){
Expression_handler exp;
exp.Execute_expression(R"("abc" == "abc")");
exp.Execute_expression(R"("abc" != "abc")");
exp.Execute_expression(R"("abc" == "cba")");
exp.Execute_expression(R"("abc" != "cba")");
}
Prints
Result: '"abc" == "abc"' -> 1
Result: '"abc" != "abc"' -> 0
Result: '"abc" == "cba"' -> 0
Result: '"abc" != "cba"' -> 1
I have this example code, which parses the string str correctly.
How to I make it work if there any extra characters before and/or after the string? For example if I did str = std::string("AAA") + str + std::string("AAA")
frame.h
#define BOOST_SPIRIT_USE_PHOENIX_V3
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
namespace ascii = boost::spirit::ascii;
struct frame
{
std::string addr;
std::string func;
std::string file;
std::string fullname;
std::string line;
std::map<std::string, std::string> kv;
};
template <typename Iterator>
struct argsArray : qi::grammar<Iterator, std::map<std::string, std::string>()>
{
argsArray() : argsArray::base_type(query)
{
query =
qi::lit("args=[") >> pair >> *(qi::lit(',') >> pair) >> qi::lit(']');
pair = qi::lit("{name=") >> quoted_string >> qi::lit(",value=") >>
quoted_string >> qi::lit("}");
key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
quoted_string %= boost::spirit::lexeme['"' >> +(qi::char_ - '"') >> '"'];
}
qi::rule<Iterator, std::map<std::string, std::string>()> query;
qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
qi::rule<Iterator, std::string()> key;
qi::rule<Iterator, std::string()> quoted_string;
};
template <typename Iterator>
struct frameParser : qi::grammar<Iterator, frame(), ascii::space_type>
{
frameParser() : frameParser::base_type(frame_rule)
{
static const auto _addr = phx::bind(&frame::addr, qi::_r1);
static const auto _func = phx::bind(&frame::func, qi::_r1);
static const auto _file = phx::bind(&frame::file, qi::_r1);
static const auto _fullname = phx::bind(&frame::fullname, qi::_r1);
static const auto _line = phx::bind(&frame::line, qi::_r1);
static const auto _kv = phx::bind(&frame::kv, qi::_r1);
func = qi::lit("func=") >> quoted_string;
addr = qi::lit("addr=") >> quoted_string;
file = qi::lit("file=") >> quoted_string;
fullname = qi::lit("fullname=") >> quoted_string;
line = qi::lit("line=") >> quoted_string;
func_rule = func[_func = qi::_1];
addr_rule = addr[_addr = qi::_1];
file_rule = file[_file = qi::_1];
fullname_rule = fullname[_fullname = qi::_1];
line_rule = line[_line = qi::_1];
kv_rule = arrTest[_kv = qi::_1];
quoted_string %= boost::spirit::lexeme['"' >> +(qi::char_ - '"') >> '"'];
frame_rule = qi::lit("frame={") >>
(addr_rule(qi::_val) ^ qi::lit(',') ^ func_rule(qi::_val) ^
qi::lit(',') ^ file_rule(qi::_val) ^ qi::lit(',') ^
fullname_rule(qi::_val) ^ qi::lit(',') ^ line_rule(qi::_val) ^
qi::lit(',') ^ kv_rule(qi::_val)) >>
qi::lit('}');
BOOST_SPIRIT_DEBUG_NODES(
(frame_rule)(func_rule)(addr_rule)(fullname_rule)(line_rule))
}
qi::rule<Iterator, void(frame&), ascii::space_type> func_rule, addr_rule,
file_rule, fullname_rule, line_rule, kv_rule;
qi::rule<Iterator, frame(), ascii::space_type> frame_rule;
qi::rule<Iterator, std::string()> addr, func, file, fullname, line;
qi::rule<Iterator, std::string()> quoted_string;
argsArray<Iterator> arrTest;
};
test.cc
#include <iostream>
#include "gtest/gtest.h"
#include "parser/frame.h"
TEST(ParseFrameString, Test1)
{
std::string str = R"(frame={addr="0x0000000000414008",)"
R"(func="main",)"
R"(args=[{name="argc",value="1"},)"
R"({name="argv",value="0x7fffffffe1a8"}],)"
R"(file="/home/stiopa/development/gdbFront/main.cc",)"
R"(fullname="/home/stiopa/development/gdbFront/main.cc",)"
R"(line="90"}")";
typedef std::string::const_iterator It;
const frameParser<It> g;
It iter(str.begin()), end(str.end());
frame frame;
bool r = phrase_parse(iter, end, g, boost::spirit::ascii::space, frame);
EXPECT_EQ(r, true);
EXPECT_EQ(frame.addr, "0x0000000000414008");
EXPECT_EQ(frame.func, "main");
std::map<std::string, std::string> kv{{"argc", "1"},
{"argv", "0x7fffffffe1a8"}};
EXPECT_EQ(frame.kv, kv);
EXPECT_EQ(frame.file, "/home/stiopa/development/gdbFront/main.cc");
EXPECT_EQ(frame.fullname, "/home/stiopa/development/gdbFront/main.cc");
EXPECT_EQ(frame.line, "90");
}
The simple, low-tech solution would be to use qi::seek from the repository:
#include <boost/spirit/repository/include/qi_seek.hpp>
namespace qir = boost::spirit::repository::qi;
And then:
bool r = phrase_parse(iter, end, qir::seek[g], boost::spirit::ascii::space, frame);
DEMO
Live On Coliru
#define BOOST_SPIRIT_USE_PHOENIX_V3
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
namespace qi = boost::spirit::qi;
namespace qir = boost::spirit::repository::qi;
namespace phx = boost::phoenix;
namespace ascii = boost::spirit::ascii;
struct frame
{
std::string addr;
std::string func;
std::string file;
std::string fullname;
std::string line;
std::map<std::string, std::string> kv;
};
template <typename Iterator>
struct argsArray : qi::grammar<Iterator, std::map<std::string, std::string>()>
{
argsArray() : argsArray::base_type(query)
{
query =
qi::lit("args=[") >> pair >> *(qi::lit(',') >> pair) >> qi::lit(']');
pair = qi::lit("{name=") >> quoted_string >> qi::lit(",value=") >>
quoted_string >> qi::lit("}");
key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
quoted_string %= boost::spirit::lexeme['"' >> +(qi::char_ - '"') >> '"'];
}
qi::rule<Iterator, std::map<std::string, std::string>()> query;
qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
qi::rule<Iterator, std::string()> key;
qi::rule<Iterator, std::string()> quoted_string;
};
template <typename Iterator>
struct frameParser : qi::grammar<Iterator, frame(), ascii::space_type>
{
frameParser() : frameParser::base_type(frame_rule)
{
static const auto _addr = phx::bind(&frame::addr, qi::_r1);
static const auto _func = phx::bind(&frame::func, qi::_r1);
static const auto _file = phx::bind(&frame::file, qi::_r1);
static const auto _fullname = phx::bind(&frame::fullname, qi::_r1);
static const auto _line = phx::bind(&frame::line, qi::_r1);
static const auto _kv = phx::bind(&frame::kv, qi::_r1);
func = qi::lit("func=") >> quoted_string;
addr = qi::lit("addr=") >> quoted_string;
file = qi::lit("file=") >> quoted_string;
fullname = qi::lit("fullname=") >> quoted_string;
line = qi::lit("line=") >> quoted_string;
func_rule = func[_func = qi::_1];
addr_rule = addr[_addr = qi::_1];
file_rule = file[_file = qi::_1];
fullname_rule = fullname[_fullname = qi::_1];
line_rule = line[_line = qi::_1];
kv_rule = arrTest[_kv = qi::_1];
quoted_string %= boost::spirit::lexeme['"' >> +(qi::char_ - '"') >> '"'];
frame_rule = qi::lit("frame={") >>
(addr_rule(qi::_val) ^ qi::lit(',') ^ func_rule(qi::_val) ^
qi::lit(',') ^ file_rule(qi::_val) ^ qi::lit(',') ^
fullname_rule(qi::_val) ^ qi::lit(',') ^ line_rule(qi::_val) ^
qi::lit(',') ^ kv_rule(qi::_val)) >>
qi::lit('}');
BOOST_SPIRIT_DEBUG_NODES(
(frame_rule)(func_rule)(addr_rule)(fullname_rule)(line_rule))
}
qi::rule<Iterator, void(frame&), ascii::space_type> func_rule, addr_rule,
file_rule, fullname_rule, line_rule, kv_rule;
qi::rule<Iterator, frame(), ascii::space_type> frame_rule;
qi::rule<Iterator, std::string()> addr, func, file, fullname, line;
qi::rule<Iterator, std::string()> quoted_string;
argsArray<Iterator> arrTest;
};
#include <iostream>
//#include "parser/frame.h"
int main()
{
std::string str = R"(frame={addr="0x0000000000414008",)"
R"(func="main",)"
R"(args=[{name="argc",value="1"},)"
R"({name="argv",value="0x7fffffffe1a8"}],)"
R"(file="/home/stiopa/development/gdbFront/main.cc",)"
R"(fullname="/home/stiopa/development/gdbFront/main.cc",)"
R"(line="90"}")";
str = "AAA" + str + "AAA";
typedef std::string::const_iterator It;
const frameParser<It> g;
It iter(str.begin()), end(str.end());
frame frame;
bool r = phrase_parse(iter, end, qir::seek[g], boost::spirit::ascii::space, frame);
assert(r == true);
assert(frame.addr == "0x0000000000414008");
assert(frame.func == "main");
std::map<std::string, std::string> kv{{"argc", "1"},
{"argv", "0x7fffffffe1a8"}};
assert(frame.kv == kv);
assert(frame.file == "/home/stiopa/development/gdbFront/main.cc");
assert(frame.fullname == "/home/stiopa/development/gdbFront/main.cc");
assert(frame.line == "90");
}
Tests still pass.
And here's a free code review. Please see
Boost Spirit: "Semantic actions are evil"?
Boost spirit skipper issues
you didn't parse the delimiting ',' correctly at all. You must require it, unless end-of-frame ('}'). You cannot accept multiple in a row
Live On Coliru
#define BOOST_SPIRIT_USE_PHOENIX_V3
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
namespace qi = boost::spirit::qi;
namespace qir = boost::spirit::repository::qi;
namespace phx = boost::phoenix;
namespace ascii = boost::spirit::ascii;
struct frame {
std::string addr;
std::string func;
std::string file;
std::string fullname;
std::string line;
std::map<std::string, std::string> kv;
};
BOOST_FUSION_ADAPT_STRUCT(frame, addr, func, file, fullname, line, kv)
template <typename Iterator>
struct argsArray : qi::grammar<Iterator, std::map<std::string, std::string>()>
{
argsArray() : argsArray::base_type(query)
{
query = "args=[" >> pair >> *(',' >> pair) >> ']';
pair = "{name=" >> quoted_string >> ",value=" >> quoted_string >> "}";
key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
quoted_string = '"' >> +(qi::char_ - '"') >> '"';
}
private:
qi::rule<Iterator, std::map<std::string, std::string>()> query;
qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
qi::rule<Iterator, std::string()> key;
qi::rule<Iterator, std::string()> quoted_string;
};
template <typename Iterator>
struct frameParser : qi::grammar<Iterator, frame(), ascii::space_type>
{
frameParser() : frameParser::base_type(frame_rule)
{
quoted_string = '"' >> +(qi::char_ - '"') >> '"';
delim = (&qi::lit('}')) | ',';
field_rule = qi::lexeme [ qi::lit(qi::_r1) >> '=' ] >> quoted_string >> delim;
kv_rule = arrTest >> delim;
frame_rule = "frame={" >>
(field_rule(+"addr") ^
field_rule(+"func") ^
field_rule(+"file") ^
field_rule(+"fullname") ^
field_rule(+"line") ^
kv_rule
) >> '}';
BOOST_SPIRIT_DEBUG_NODES((frame_rule)(field_rule))
}
private:
qi::rule<Iterator> delim;
qi::rule<Iterator, std::string(char const*), ascii::space_type> field_rule;
qi::rule<Iterator, std::map<std::string, std::string>()> kv_rule;
qi::rule<Iterator, frame(), ascii::space_type> frame_rule;
qi::rule<Iterator, std::string()> quoted_string;
argsArray<Iterator> arrTest;
};
#include <iostream>
//#include "parser/frame.h"
int main()
{
std::string str = R"(frame={addr="0x0000000000414008",)"
R"(func="main",)"
R"(args=[{name="argc",value="1"},)"
R"({name="argv",value="0x7fffffffe1a8"}],)"
R"(file="/home/stiopa/development/gdbFront/main.cc",)"
R"(fullname="/home/stiopa/development/gdbFront/main.cc",)"
R"(line="90"}")";
str = "AAA" + str + "AAA";
typedef std::string::const_iterator It;
const frameParser<It> g;
It iter(str.begin()), end(str.end());
frame frame;
bool r = phrase_parse(iter, end, qir::seek[g], boost::spirit::ascii::space, frame);
if (iter != end)
std::cout << "Remaining unparsed: '" << std::string(iter,end) << "'\n";
assert(r == true);
assert(frame.addr == "0x0000000000414008");
assert(frame.func == "main");
std::map<std::string, std::string> kv{{"argc", "1"},
{"argv", "0x7fffffffe1a8"}};
assert(frame.kv == kv);
assert(frame.file == "/home/stiopa/development/gdbFront/main.cc");
assert(frame.fullname == "/home/stiopa/development/gdbFront/main.cc");
assert(frame.line == "90");
}
Prints:
Remaining unparsed: '"AAA'
Tests still pass.
Note your original sample input had a trailing ", which you simply ignored.
I am trying to create a Spirit Karma grammar that is composed of several rules. This grammar is intended to create a string of the format "(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)". The rule to print out each individual struct that I call RowData uses BOOST_FUSION_ADAPT_STRUCT to print out all of the fields of that struct. If the grammar only includes that rule, the grammar works fine. However, I am using this struct as the value of a std::map. The key in an integer but I do not care about that value and want to drop it. I have created rules to parse the std::map but the rule that handles the std::pair fails to compile for a BOOST_SPIRIT_ASSERT_MATCH. I have created a small bit of code that generates this issue. The line is pairRule = bs::karma::omit << rowDataRule; If anybody has an idea what the issue is or how I could do this differently, I would appreciate the help.
Edit: I am using gcc 4.8.3 on OpenSUSE 13.2 but get the same error with gcc 4.8.2 on Ubuntu 14.04 LTS.
main.cpp
#include <iostream>
#include <map>
#include <boost/cstdint.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/make_tuple.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/karma.hpp>
namespace bs = boost::spirit;
struct RowData
{
RowData() :
field0(0),
field1(0),
field2(0),
field3(0)
{
}
boost::uint64_t field0;
boost::uint64_t field1;
boost::uint64_t field2;
boost::uint64_t field3;
};
BOOST_FUSION_ADAPT_STRUCT(
RowData,
(boost::uint64_t, field0)
(boost::uint64_t, field1)
(boost::uint64_t, field2)
(boost::uint64_t, field3)
)
template <typename OutputIterator>
struct RowDataGrammar :
bs::karma::grammar< OutputIterator, std::map<boost::uint64_t, RowData>() >
{
RowDataGrammar() : RowDataGrammar::base_type(allRowsRule)
{
rowDataRule =
bs::karma::lit("(") <<
bs::karma::ulong_ <<
bs::karma::lit(", ") <<
bs::karma::ulong_ <<
bs::karma::lit(", ") <<
bs::karma::ulong_ <<
bs::karma::lit(", ") <<
bs::karma::ulong_ <<
bs::karma::lit(")");
// I only want the value from the map. The key is dropped.
pairRule = bs::karma::omit << rowDataRule;
allRowsRule = pairRule % ", ";
}
private:
bs::karma::rule< OutputIterator, RowData() > rowDataRule;
bs::karma::rule< OutputIterator, std::pair<boost::uint64_t, RowData>() > pairRule;
bs::karma::rule< OutputIterator, std::map<boost::uint64_t, RowData>() > allRowsRule;
};
int main(int argc, char** argv)
{
std::map<boost::uint64_t, RowData> rowMap;
RowData rowData;
rowData.field0 = 0;
rowData.field1 = 1;
rowData.field2 = 2;
rowData.field3 = 3;
rowMap.insert(std::make_pair(10, rowData));
rowData.field0 = 6;
rowData.field1 = 7;
rowData.field2 = 8;
rowData.field3 = 9;
rowMap.insert(std::make_pair(20, rowData));
std::string generatedString;
std::back_insert_iterator<std::string> sink(generatedString);
RowDataGrammar< std::back_insert_iterator<std::string> > grammar;
bs::karma::generate(sink, grammar, rowMap);
std::cout << "output :" << generatedString << std::endl;
}
Error message:
In file included from /usr/include/boost/fusion/support/tag_of.hpp:16:0,
from /usr/include/boost/fusion/support/category_of.hpp:11,
from /usr/include/boost/fusion/adapted/struct/detail/extension.hpp:13,
from /usr/include/boost/fusion/adapted/struct/adapt_struct.hpp:19,
from karmaTest.cpp:5:
/usr/include/boost/spirit/home/karma/nonterminal/rule.hpp: In instantiation of ‘static void boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>::define(boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>&, const Expr&, mpl_::false_) [with Auto = mpl_::bool_<false>; Expr = boost::proto::exprns_::expr<boost::proto::tagns_::tag::shift_left, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::omit>, 0l>&, boost::spirit::karma::rule<std::back_insert_iterator<std::basic_string<char> >, RowData(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l>; OutputIterator = std::back_insert_iterator<std::basic_string<char> >; T1 = std::pair<long unsigned int, RowData>(); T2 = boost::spirit::unused_type; T3 = boost::spirit::unused_type; T4 = boost::spirit::unused_type; mpl_::false_ = mpl_::bool_<false>]’:
/usr/include/boost/spirit/home/karma/nonterminal/rule.hpp:229:19: required from ‘boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>& boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>::operator=(const Expr&) [with Expr = boost::proto::exprns_::expr<boost::proto::tagns_::tag::shift_left, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::omit>, 0l>&, boost::spirit::karma::rule<std::back_insert_iterator<std::basic_string<char> >, RowData(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l>; OutputIterator = std::back_insert_iterator<std::basic_string<char> >; T1 = std::pair<long unsigned int, RowData>(); T2 = boost::spirit::unused_type; T3 = boost::spirit::unused_type; T4 = boost::spirit::unused_type]’
karmaTest.cpp:54:18: required from ‘RowDataGrammar<OutputIterator>::RowDataGrammar() [with OutputIterator = std::back_insert_iterator<std::basic_string<char> >]’
karmaTest.cpp:84:62: required from here
/usr/include/boost/spirit/home/karma/nonterminal/rule.hpp:185:13: error: no matching function for call to ‘assertion_failed(mpl_::failed************ (boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>::define(boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>&, const Expr&, mpl_::false_) [with Auto = mpl_::bool_<false>; Expr = boost::proto::exprns_::expr<boost::proto::tagns_::tag::shift_left, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::omit>, 0l>&, boost::spirit::karma::rule<std::back_insert_iterator<std::basic_string<char> >, RowData(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l>; OutputIterator = std::back_insert_iterator<std::basic_string<char> >; T1 = std::pair<long unsigned int, RowData>(); T2 = boost::spirit::unused_type; T3 = boost::spirit::unused_type; T4 = boost::spirit::unused_type; mpl_::false_ = mpl_::bool_<false>]::error_invalid_expression::************)(boost::proto::exprns_::expr<boost::proto::tagns_::tag::shift_left, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::omit>, 0l>&, boost::spirit::karma::rule<std::back_insert_iterator<std::basic_string<char> >, RowData(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l>))’
BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
^
/usr/include/boost/spirit/home/karma/nonterminal/rule.hpp:185:13: note: candidate is:
/usr/include/boost/mpl/assert.hpp:82:5: note: template<bool C> int mpl_::assertion_failed(typename mpl_::assert<C>::type)
int assertion_failed( typename assert<C>::type );
^
/usr/include/boost/mpl/assert.hpp:82:5: note: template argument deduction/substitution failed:
/usr/include/boost/spirit/home/karma/nonterminal/rule.hpp:185:13: note: cannot convert ‘boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>::define(boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>&, const Expr&, mpl_::false_)::error_invalid_expression185::assert_arg<mpl_::bool_<false>, boost::proto::exprns_::expr<boost::proto::tagns_::tag::shift_left, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::omit>, 0l>&, boost::spirit::karma::rule<std::back_insert_iterator<std::basic_string<char> >, RowData(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l> >()’ (type ‘mpl_::failed************ (boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>::define(boost::spirit::karma::rule<OutputIterator, T1, T2, T3, T4>&, const Expr&, mpl_::false_) [with Auto = mpl_::bool_<false>; Expr = boost::proto::exprns_::expr<boost::proto::tagns_::tag::shift_left, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::omit>, 0l>&, boost::spirit::karma::rule<std::back_insert_iterator<std::basic_string<char> >, RowData(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l>; OutputIterator = std::back_insert_iterator<std::basic_string<char> >; T1 = std::pair<long unsigned int, RowData>(); T2 = boost::spirit::unused_type; T3 = boost::spirit::unused_type; T4 = boost::spirit::unused_type; mpl_::false_ = mpl_::bool_<false>]::error_invalid_expression::************)(boost::proto::exprns_::expr<boost::proto::tagns_::tag::shift_left, boost::proto::argsns_::list2<const boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::omit>, 0l>&, boost::spirit::karma::rule<std::back_insert_iterator<std::basic_string<char> >, RowData(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l>)’) to type ‘mpl_::assert<false>::type {aka mpl_::assert<false>}’
BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Expr);
As cv_and_he said, I was using omit incorrectly. An old coworker I reached out to told me the same. The corrected code is below.
main.cpp
#include <iostream>
#include <map>
#include <boost/cstdint.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/make_tuple.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/karma.hpp>
namespace bs = boost::spirit;
struct RowData
{
RowData() :
field0(0),
field1(0),
field2(0),
field3(0)
{
}
boost::uint64_t field0;
boost::uint64_t field1;
boost::uint64_t field2;
boost::uint64_t field3;
};
BOOST_FUSION_ADAPT_STRUCT(
RowData,
(boost::uint64_t, field0)
(boost::uint64_t, field1)
(boost::uint64_t, field2)
(boost::uint64_t, field3)
)
template <typename OutputIterator>
struct RowDataGrammar :
bs::karma::grammar< OutputIterator, std::map<boost::uint64_t, RowData>() >
{
RowDataGrammar() : RowDataGrammar::base_type(allRowsRule)
{
rowDataRule =
bs::karma::lit("(") <<
bs::karma::ulong_ <<
bs::karma::lit(", ") <<
bs::karma::ulong_ <<
bs::karma::lit(", ") <<
bs::karma::ulong_ <<
bs::karma::lit(", ") <<
bs::karma::ulong_ <<
bs::karma::lit(")");
// I only want the value from the map. The key is dropped.
pairRule = bs::karma::omit[bs::karma::ulong_] << rowDataRule;
allRowsRule = pairRule % ", ";
}
private:
bs::karma::rule< OutputIterator, RowData() > rowDataRule;
bs::karma::rule< OutputIterator, std::pair<boost::uint64_t, RowData>() > pairRule;
bs::karma::rule< OutputIterator, std::map<boost::uint64_t, RowData>() > allRowsRule;
};
int main(int argc, char** argv)
{
std::map<boost::uint64_t, RowData> rowMap;
RowData rowData;
rowData.field0 = 0;
rowData.field1 = 1;
rowData.field2 = 2;
rowData.field3 = 3;
rowMap.insert(std::make_pair(10, rowData));
rowData.field0 = 6;
rowData.field1 = 7;
rowData.field2 = 8;
rowData.field3 = 9;
rowMap.insert(std::make_pair(20, rowData));
std::string generatedString;
std::back_insert_iterator<std::string> sink(generatedString);
RowDataGrammar< std::back_insert_iterator<std::string> > grammar;
bs::karma::generate(sink, grammar, rowMap);
std::cout << "output :" << generatedString << std::endl;
}
I need to parse following EBNF expression with Boost::Spirit.
period ::= date_part [time_part] , date_part [time_part]
time_part ::= hours:minutes[:seconds]
date_part ::= day.month.year
For example, 10.06.2014 10:00:15, 11.07.2014. I made my grammar in two ways, but can't exactly get working example.
1) First attempt
struct Parser: grammar<std::string::const_iterator, space_type>
{
Parser(): Parser::base_type(datetime_)
{
using boost::spirit::int_;
using boost::spirit::qi::_1;
using boost::spirit::qi::_2;
using boost::spirit::qi::_val;
datetime_ =
(date_ >> time_)
[
_val =
phoenix::construct<ptime>
(
date(_1[2]), _1[1], _1[0]),
hours(_2[0]) + minutes(_2[1]) + seconds[_2[0]]
)
|
_val =
phoenix::construct<ptime>
(
date(_1[2]), _1[1], _1[0]),
seconds(0)
)
];
date_ %= int_ % '.';
time_ %= int_ % ':';
BOOST_SPIRIT_DEBUG_NODE(datetime_);
BOOST_SPIRIT_DEBUG_NODE(date_);
BOOST_SPIRIT_DEBUG_NODE(time_);
}
rule<std::string::const_iterator, std::vector<int>(), space_type> date_, time_;
rule<std::string::const_iterator, ptime(), space_type> datetime_;
}
Parser parser;
std::string strTest("10.06.2014 10:00:15, 11.07.2014");
std::string::const_iterator it_begin(strTest.begin());
std::string::const_iterator it_end(strTest.end());
bool result = phrase_parse(it_begin, it_end, parser, space);
Errors:
/media/Data/Projects/Qt/Planner/parser.h:108: ошибка: no matching function for call to 'boost::gregorian::date::date(boost::phoenix::detail::make_index_composite<boost::phoenix::actor<boost::spirit::argument<0> >, int>::type)'
And so on. I can't cast boost::spirit::argument<0> to int or date::years_type. I tryed date((int)_1[2]), (int)_1[1], (int)_1[0])) and dynamic_cast<int>(_1[2]), but with no success (.
2) Second attempt
struct Parser: grammar<std::string::const_itearator, space_type>
{
Parser(ConditionTree& a_lTree):
Parser::base_type(time_period_),
m_lTree(a_lTree)
{
using boost::spirit::int_;
using boost::spirit::qi::_1;
using boost::spirit::qi::_2;
using boost::spirit::qi::_3;
using boost::spirit::qi::_4;
using boost::spirit::qi::_5;
using boost::spirit::qi::_val;
time_period_ = ( datetime_ > ',' > datetime_ ) [ _val = phoenix::construct<time_period>((int)_1, (int)_3) ];
datetime_ = (date_ >> time_duration_) [ _val = phoenix::construct<ptime>((int)_1, (int)_2) | _val = phoenix::construct<ptime>((int)_1, seconds(0)) ] ;
date_ = (int_ > '.' > int_ > '.' > int_) [ _val = phoenix::construct<date>((int)_5, (int)_3, (int)_1) ];
time_duration_ = (int_ > ':' > int_ > ':' > int_) [ _val = phoenix::construct<time_duration>((int)_1, (int)_3, (int)_5, 0)];
BOOST_SPIRIT_DEBUG_NODE(time_period_);
BOOST_SPIRIT_DEBUG_NODE(datetime_);
BOOST_SPIRIT_DEBUG_NODE(date_);
BOOST_SPIRIT_DEBUG_NODE(time_duration_);
}
rule<std::string::const_itarator, time_period(), space_type> time_period_;
rule<std::string::const_itarator, ptime(), space_type> datetime_;
rule<std::string::const_itarator, date(), space_type> date_;
rule<std::string::const_itarator, time_duration(), space_type> time_duration_;
ConditionTree& m_lTree;
};
Error:
/media/Data/Projects/Qt/Planner/parser.h:114: ошибка: invalid cast from type 'const _1_type {aka const boost::phoenix::actor<boost::spirit::argument<0> >}' to type 'int'...
Why I can't cast boost::spirit::argument<0> to int????
Better question, why would you be able to cast a placeholder type to a specific primitive type?
The place holder is a lazy actor only, so you should use Phoenix cast_ to cast it, if at all (hint: this should not be necessary): Live On Coliru
Output
<period_>
<try>10.06.2014 10:00:15,</try>
<date_>
<try>10.06.2014 10:00:15,</try>
<success> 10:00:15, 11.07.201</success>
<attributes>[[10, 6, 2014]]</attributes>
</date_>
<time_>
<try> 10:00:15, 11.07.201</try>
<success>, 11.07.2014</success>
<attributes>[[10, 0, 15]]</attributes>
</time_>
<date_>
<try> 11.07.2014</try>
<success></success>
<attributes>[[11, 7, 2014]]</attributes>
</date_>
<time_>
<try></try>
<fail/>
</time_>
<success></success>
<attributes>[[[[10, 6, 2014], [10, 0, 15]], [[11, 7, 2014], [empty]]]]</attributes>
</period_>
Parse success
Full Sample
#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
namespace Ast {
using boost::optional;
struct date { unsigned day, month, year; };
struct time { unsigned hours, minutes, seconds; };
struct date_time { date date_part; optional<time> time_part; };
struct period { date_time start, end; };
}
BOOST_FUSION_ADAPT_STRUCT(Ast::date, (unsigned,day)(unsigned,month)(unsigned,year))
BOOST_FUSION_ADAPT_STRUCT(Ast::time, (unsigned,hours)(unsigned,minutes)(unsigned,seconds))
BOOST_FUSION_ADAPT_STRUCT(Ast::date_time, (Ast::date,date_part)(Ast::optional<Ast::time>, time_part))
BOOST_FUSION_ADAPT_STRUCT(Ast::period, (Ast::date_time,start)(Ast::date_time,end))
template <typename Iterator>
struct Parser : qi::grammar<Iterator, Ast::period(), qi::space_type>
{
int test;
Parser() : Parser::base_type(period_)
{
using namespace qi;
static const int_parser<unsigned, 10, 2, 2> _2digit = {};
static const int_parser<unsigned, 10, 4, 4> _4digit = {};
time_ = _2digit >> ":" >> _2digit >> ":" >> _2digit;
date_ = _2digit >> "." >> _2digit >> "." >> _4digit;
date_time_ = date_ >> -time_;
period_ = date_time_ >> "," >> date_time_;
BOOST_SPIRIT_DEBUG_NODES((period_)(time_)(date_))
}
private:
qi::rule<Iterator, Ast::period(), qi::space_type> period_;
qi::rule<Iterator, Ast::date(), qi::space_type> date_;
qi::rule<Iterator, Ast::time(), qi::space_type> time_;
qi::rule<Iterator, Ast::date_time(), qi::space_type> date_time_;
};
int main()
{
using It = std::string::const_iterator;
Parser<It> parser;
std::string input("10.06.2014 10:00:15, 11.07.2014");
It f(input.begin()), l(input.end());
Ast::period parsed;
bool ok = qi::phrase_parse(f, l, parser, qi::space, parsed);
if (ok)
{
std::cout << "Parse success\n";
}
else
{
std::cout << "Parse failed\n";
}
if (f!=l)
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
I must have missed something with the boost::recursive_wrapper thing, I always get an error:
error: no matching function for call to 'boost::tuples::tuple, std::allocator >, client::compare_attr_op_t, std::basic_string, std::allocator >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>::tuple(client::expression_value&)'
for the line defining the "expression" in the grammar: expression %= attribute_test | boolean_expression;
Any idea?
namespace client {
enum compare_attr_op_t {
cao_eq,
cao_neq
};
enum boolean_op_t {
bo_and,
bo_or,
bo_not
};
struct expression_value;
typedef boost::tuple<std::string, compare_attr_op_t, std::string> attribute_test_value;
typedef boost::tuple< expression_value, boolean_op_t, expression_value > boolean_expression_value;
typedef boost::variant< attribute_test_value, boost::recursive_wrapper<boolean_expression_value> > sub_expression_value;
struct expression_value {
sub_expression_value value;
};
}
BOOST_FUSION_ADAPT_STRUCT(
client::expression_value,
(client::sub_expression_value, value)
)
namespace client {
struct compare_attr_ : qi::symbols<char, compare_attr_op_t>
{
compare_attr_()
{
add
("=" , cao_eq)
("!=" , cao_neq)
;
}
} compare_attr;
struct boolean_op_ : qi::symbols<char, boolean_op_t>
{
boolean_op_()
{
add
("&" , bo_and)
("|" , bo_or)
;
}
} boolean_op;
template <typename Iterator>
struct attribute_conditions : qi::grammar<Iterator, expression_value(), ascii::space_type>
{
attribute_conditions() : attribute_conditions::base_type(expression)
{
using qi::eps;
using qi::lit;
using qi::_val;
using qi::lexeme;
using qi::_1;
using qi::_2;
using qi::_3;
using ascii::char_;
using ascii::alnum;
using ascii::alpha;
expression %= attribute_test | boolean_expression;
boolean_expression %= expression >> boolean_op >> expression;
attribute_test %= (attribute_name >> compare_attr >> attribute_value)[std::cout << _1 << ' ' << _2 << ' ' << _3];
attribute_name %= alpha >> *alnum;
attribute_value %= lexeme['"' > +(char_ - '"') > '"'];
}
qi::rule<Iterator, expression_value(), ascii::space_type> expression;
qi::rule<Iterator, boolean_expression_value(), ascii::space_type> boolean_expression;
qi::rule<Iterator, attribute_test_value(), ascii::space_type> attribute_test;
qi::rule<Iterator, std::string(), ascii::space_type> attribute_name;
qi::rule<Iterator, std::string(), ascii::space_type> attribute_value;
};
}
I threw it all and started from scratch, giving me this nice and working implementation:
namespace client {
enum compare_attr_op_t {
cao_eq,
cao_neq
};
enum boolean_op_t {
bo_and,
bo_or,
bo_not
};
struct expression_value;
struct or_op_value;
struct and_expression_value;
struct and_op_value;
struct not_op_value;
typedef boost::tuple<std::string, compare_attr_op_t, std::string> attribute_test_value;
typedef boost::variant<attribute_test_value, boost::recursive_wrapper<expression_value>, boost::recursive_wrapper<not_op_value> > node_value;
struct not_op_value {
boolean_op_t operation;
node_value rnode;
};
struct and_op_value {
boolean_op_t operation;
node_value rnode;
};
struct and_expression_value {
node_value lnode;
std::vector<and_op_value> ops;
};
struct or_op_value {
boolean_op_t operation;
and_expression_value rnode;
};
struct expression_value {
and_expression_value lnode;
std::vector<or_op_value> ops;
};
}
BOOST_FUSION_ADAPT_STRUCT(
client::not_op_value,
(client::boolean_op_t, operation)
(client::node_value, rnode)
)
BOOST_FUSION_ADAPT_STRUCT(
client::and_expression_value,
(client::node_value, lnode)
(std::vector<client::and_op_value>, ops)
)
BOOST_FUSION_ADAPT_STRUCT(
client::and_op_value,
(client::boolean_op_t, operation)
(client::node_value, rnode)
)
BOOST_FUSION_ADAPT_STRUCT(
client::expression_value,
(client::and_expression_value, lnode)
(std::vector<client::or_op_value>, ops)
)
BOOST_FUSION_ADAPT_STRUCT(
client::or_op_value,
(client::boolean_op_t, operation)
(client::and_expression_value, rnode)
)
namespace client {
struct compare_attr_ : qi::symbols<char, compare_attr_op_t>
{
compare_attr_()
{
add
("=" , cao_eq)
("!=" , cao_neq)
;
}
} compare_attr;
struct boolean_op_and_t : qi::symbols<char, boolean_op_t>
{
boolean_op_and_t()
{
add
("&" , bo_and)
("and" , bo_and)
;
}
} boolean_op_and;
struct boolean_op_or_t : qi::symbols<char, boolean_op_t>
{
boolean_op_or_t()
{
add
("|" , bo_or)
("or" , bo_or)
;
}
} boolean_op_or;
struct boolean_op_not_t : qi::symbols<char, boolean_op_t>
{
boolean_op_not_t()
{
add
("!" , bo_not)
("not" , bo_not)
;
}
} boolean_op_not;
template <typename Iterator>
struct attribute_conditions : qi::grammar<Iterator, expression_value(), ascii::space_type>
{
attribute_conditions() : attribute_conditions::base_type(expression)
{
using qi::eps;
using qi::lit;
using qi::_val;
using qi::lexeme;
using qi::_1;
using qi::_2;
using qi::_3;
using ascii::char_;
using ascii::alnum;
using ascii::alpha;
using ascii::no_case;
expression %= and_expression >> *(no_case[boolean_op_or] >> and_expression);
and_expression %= node >> *(no_case[boolean_op_and] >> node);
node %= attribute_test | ('(' >> expression >> ')') | not_operation;
not_operation %= no_case[boolean_op_not] >> node;
attribute_test %= (attribute_name >> no_case[compare_attr] >> attribute_value);
attribute_name %= alpha >> *char_("A-Za-z0-9_");
attribute_value %= lexeme['"' > +(char_ - '"') > '"'];
}
qi::rule<Iterator, expression_value(), ascii::space_type> expression;
qi::rule<Iterator, and_expression_value(), ascii::space_type> and_expression;
qi::rule<Iterator, not_op_value(), ascii::space_type> not_operation;
qi::rule<Iterator, node_value(), ascii::space_type> node;
qi::rule<Iterator, attribute_test_value(), ascii::space_type> attribute_test;
qi::rule<Iterator, std::string(), ascii::space_type> attribute_name;
qi::rule<Iterator, std::string(), ascii::space_type> attribute_value;
};
}
This compiles OK for me using the following includes in Visual Studio C++ 2010/Boost 1.36.0. Possibly your compiler cannot handle the templates here.
#include <boost/tuple/tuple.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>
using namespace boost::spirit;