I'm quite new to Boost Spirit. Ideally I'd like to make sure two values in my grammar at the same using Phoenix. A dumbed down version of what I'm trying to get working would be a tuple where both ints are equal.
I'd like a string of "14,14,test" to parse but "14,12,test" to fail since 14 is not equal to 12. I'd like the code below to print:
Good: (14 14 test)
Fail
Currently both inputs will pass since I'm allowing and qi::int_ to parse for the second value without any checks.
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/spirit/include/qi_matches.hpp>
#include <string>
#include <vector>
#include <algorithm>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
int main(){
std::vector<std::string> test_inputs = {"14,14,test","14,12,test"};
std::for_each(test_inputs.begin(),test_inputs.end(),[](const std::string& input){
boost::fusion::vector<int,int,std::string> result;
int i(0);
auto res = qi::parse(input.begin(),input.end(),
//works but also parses "14,12,test"
qi::int_[phx::ref(i) = qi::_1] >> qi::lit(",") >> qi::int_ >> qi::lit(",") >> +qi::char_,
//Fails to compile
//qi::int_[phx::ref(i) = qi::_1] >> qi::lit(",") >> qi::int_(phx::val(i)) >> qi::lit(",") >> +qi::char_,
//qi::int_[phx::ref(i) = qi::_1] >> qi::lit(",") >> qi::int_(phx::ref(i)) >> qi::lit(",") >> +qi::char_,
result);
if(res){
std::cout << "Good: " << result << std::endl;
}
else{
std::cout << "Fail" << std::endl;
}
});
}
I can use phoenix::ref to capture first value but I can't figure out to test my second qi::int_ parser for the value of of i. I've tried using phoenix::val and phoenix::ref with no luck compiling. Ideally I'd like to capture the value in the second int and thought the int_ parser would take a lazy literal from Phoenix.
Thanks for any and all help on this.
phx::ref
twice that you have marked as not compiling works if you defineBOOST_SPIRIT_USE_PHOENIX_V3
when using g++ 4.8.1 and boost 1.54. I would recommend using that define always, it solves lots of problems. – llonesmiz