How do you use a variable stored in a boost spirit closure as input to a boost spirit loop parser?
By : knownbad
Date : March 29 2020, 07:55 AM
|
Boost.Spirit.x3 avoid collapsing two consecutive attributes of the same type into a vector
By : rajeev
Date : March 29 2020, 07:55 AM
Any of those help The problem is that unlike character literals, x3::space has an attribute. So you don't have an attribute of two separate character sequences separated by whitespace, but rather an attribute of one big character sequence which includes the whitespace. The omit directive is what you're after, and with that single addition your 'not working code' works. :-] code :
// Example program
#include <string>
#include <iostream>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
struct employee {
std::string name;
std::string location;
};
BOOST_FUSION_ADAPT_STRUCT(employee, name, location)
x3::rule<struct parse_emp_id, employee> const parse_emp = "Employee Parser";
auto parse_emp_def
= +x3::ascii::alnum
>> x3::omit[+x3::space]
>> +x3::ascii::alnum
;
BOOST_SPIRIT_DEFINE(parse_emp)
int main()
{
std::string const input = "Joe Fairbanks";
employee ret;
x3::parse(input.begin(), input.end(), parse_emp, ret);
std::cout << "Name: " << ret.name << "\tLocation: " << ret.location << '\n';
}
|
Parsing nested lists with Boost.Spirit
By : Henry Alejandro Sant
Date : March 29 2020, 07:55 AM
will be helpful for those in need You're using auto for proto expression templates in the Qi domain - that's undefined behaviour 99.9% of the time: undefined behaviour somewhere in boost::spirit::qi::phrase_parse code :
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
int main() {
using It = std::string::const_iterator;
using Skipper = qi::space_type;
for(std::string const input : { "[[\t1 , 2 ], [3, 4, 65.4]]", "[[\t1 , 2 ], [3, 4, 65.4], []]", "[]" })
{
std::cout << " ---- '" << input << "' ----\n";
auto it = input.begin();
//parse the string
using doubles = std::vector<double>;
using vectors = std::vector<doubles>;
qi::rule<It, doubles(), Skipper> doubles_ = "[" >> -(qi::double_ % ",") >> "]";
qi::rule<It, vectors(), Skipper> vectors_ = "[" >> -(doubles_ % ",") >> "]";
vectors data;
bool match = qi::phrase_parse(it, input.end(), vectors_, qi::space, data);
//check if we have a match
std::cout << "matched: " << std::boolalpha << match << '\n';
if (it != input.end())
std::cout << "unmatched part: " << std::string{it, input.end()} << '\n';
//print the result
std::cout << "result\n";
for (const auto& v : data) {
std::cout << "[";
for (double i : v)
std::cout << i << ",";
std::cout << "]\n";
}
}
}
---- '[[ 1 , 2 ], [3, 4, 65.4]]' ----
matched: true
result
[1,2,]
[3,4,65.4,]
---- '[[ 1 , 2 ], [3, 4, 65.4], []]' ----
matched: true
result
[1,2,]
[3,4,65.4,]
[]
---- '[]' ----
matched: true
result
|
Boost-Spirit: Parsing lists into different fields of a struct
By : Joao Pimenta
Date : March 29 2020, 07:55 AM
I hope this helps you . I don't really like using semantic actions, but given the choice of AST and input, I don't think there's much of a choice. I'd simplify the grammar: code :
rule = '(' >> (int_[head_(_val, _1)] % ',') >> ')'
>> '{' >> +(
'X' >> int_[X_(_val, _1)]
| 'Y' >> int_[Y_(_val, _1)]
| 'Z' >> int_[Z_(_val, _1)]
)
>> '}';
template <std::vector<int> my_struct::*m> struct push {
void operator()(my_struct& ms, int v) const { (ms.*m).push_back(v); }
};
px::function<push<&my_struct::head> > head_;
px::function<push<&my_struct::X> > X_;
px::function<push<&my_struct::Y> > Y_;
px::function<push<&my_struct::Z> > Z_;
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;
struct my_struct {
std::vector<int> head;
std::vector<int> X;
std::vector<int> Y;
std::vector<int> Z;
};
template<typename Iterator>
struct my_parser : qi::grammar<Iterator, my_struct()> {
my_parser() : my_parser::base_type(start) {
using namespace qi;
using px::push_back;
rule = '(' >> (int_[head_(_val, _1)] % ',') >> ')'
>> '{' >> +(
'X' >> int_[X_(_val, _1)]
| 'Y' >> int_[Y_(_val, _1)]
| 'Z' >> int_[Z_(_val, _1)]
)
>> '}';
start = skip(space) [rule];
}
private:
template <std::vector<int> my_struct::*m> struct push {
void operator()(my_struct& ms, int v) const { (ms.*m).push_back(v); }
};
px::function<push<&my_struct::head> > head_;
px::function<push<&my_struct::X> > X_;
px::function<push<&my_struct::Y> > Y_;
px::function<push<&my_struct::Z> > Z_;
qi::rule<Iterator, my_struct()> start;
qi::rule<Iterator, my_struct(), qi::space_type> rule;
};
int main() {
using It = boost::spirit::istream_iterator;
It f(std::cin >> std::noskipws), l;
my_struct data;
if (parse(f, l, my_parser<It>{}, data)) {
std::cout << "Parsed:";
std::copy(data.head.begin(), data.head.end(), std::ostream_iterator<int>(std::cout << "\nhead: ", " " ));
std::copy(data.X.begin(), data.X.end(), std::ostream_iterator<int>(std::cout << "\nX: ", " " ));
std::copy(data.Y.begin(), data.Y.end(), std::ostream_iterator<int>(std::cout << "\nY: ", " " ));
std::copy(data.Z.begin(), data.Z.end(), std::ostream_iterator<int>(std::cout << "\nZ: ", " " ));
std::cout << "\n";
} else {
std::cout << "Parse failed\n";
}
if (f != l)
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
Parsed:
head: 111 222 333
X: 231 54
Y: 227 1112
Z: 41156
Remaining unparsed input: '
'
|
Boost::spirit attribute types not collapsing
By : Harish
Date : March 29 2020, 07:55 AM
|