我正在尝试使用Boost Spirit解析函数。当我尝试将匹配函数保存到字符串中时,出现编译错误。
Parser.cpp:50:58:错误:调用重载的ref(std :: string&)是 不明确的int_ [ref(a)= _1] >> p.definedFunctions [ref(myFunction)= _1]
代码在替换时可以正常工作
[ref(myFunction) = _1]
与[std::cout<<_1]
#include <spirit/include/qi.hpp>
#include <iostream>
#ifndef PARSER_H
#define PARSER_H
namespace qi = boost::spirit::qi;
class Parser {
public:
Parser();
Parser(const Parser& orig);
virtual ~Parser();
static std::string parseFuncion(const std::string& s);
private:
qi::symbols<char, std::string> definedFunctions;
};
#endif /* PARSER_H */
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <cstdlib>
#include <sstream>
#include <spirit/include/qi.hpp>
#include <spirit/include/phoenix.hpp>
#include "Parser.h"
namespace qi = boost::spirit::qi;
Parser::Parser() {
definedFunctions.add
("^" , "pow")
(">" , "greater")
;
}
Parser::Parser(const Parser& orig) {
}
Parser::~Parser() {
}
std::string Parser::parseFuncion(const std::string& s){
using boost::spirit::qi::_1;
using boost::spirit::qi::int_;
using boost::phoenix::ref;
int a=0, b=0;
std::string myFunction;
Parser p;
const char* first = s.data();
const char* const end = first + s.size();
const bool success = boost::spirit::qi::parse(first, end,
// Implementation of 'full-date' rule from EBNF grammar.
int_[ ref(a) = _1 ] >> p.definedFunctions [ref(myFunction) = _1]
>> int_[ ref(b) = _1 ]
);
if (!success || first != end) {
throw std::logic_error("Parsing failed");
}
std::stringstream ss;
ss<<myFunction<<"("<<a<<","<<b<<")";
return ss.str();
}
答案 0 :(得分:1)
错误消息指出对ref
的调用是不明确的,因为存在std::ref
和boost::phoenix::ref
。添加
namespace phx = boost::phoenix;
顶部,然后在解析函数中使用phx::ref
。