Boost spirit:使用语义动作和phoenix时的参数类型
boost spirit: parameter type when using semantic actions and phoenix
我使用boost spirit来解析数学表达式,并遇到了一个问题,我将其提取到以下代码中。
有一个简单的词法分析器,它只有一个令牌,有一个保存匹配字符串的属性。解析器定义了一个规则,该规则旨在获取令牌的属性并使用它调用函数。函数调用的结果应该是规则的属性值。
这个编译失败(calc_something:不能将参数1从const boost::spirit:: _type转换为const std::string &) -显然是因为qi::_1的类型没有正确推断。但是,将动作更改为简单的"count <<"七::_1"的作品。
我对鼓舞精神相当陌生,但我已经设法使我的语法行为正确。现在,我需要得到解析值,我被困在这里,并将感谢任何帮助,我可以得到。
// spiritTest.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
namespace qi = boost::spirit::qi;
namespace lex = boost::spirit::lex;
namespace phoenix = boost::phoenix;
template <typename Lexer>
class TestLexer : public lex::lexer<Lexer>
{
public:
TestLexer()
{
number = "(\d*\.)?\d+([eE][-+]?\d+)?";
self = number;
}
lex::token_def<std::string> number;
};
int calc_something(const std::string & s)
{
return 5;
}
template <typename Iterator>
class Parser : public qi::grammar<Iterator, int>
{
public:
template <typename TokenDef>
Parser(const TokenDef& tok) : Parser::base_type(value)
{
// the following line causes error C2664: 'calc_something' : cannot convert parameter 1 from 'const boost::spirit::_1_type' to 'const std::string &'
value = tok.number [qi::_val = calc_something(qi::_1)];
// the following line works as expected
//value = tok.number [std::cout << qi::_1 << std::endl];
}
qi::rule<Iterator, int> value;
};
int _tmain(int argc, _TCHAR* argv[])
{
typedef const char* base_iterator_type;
typedef lex::lexertl::token<base_iterator_type> token_type;
typedef lex::lexertl::lexer<token_type> lexer_type;
typedef TestLexer<lexer_type> TestLexer;
typedef TestLexer::iterator_type iterator_type;
typedef Parser<iterator_type> Parser;
TestLexer lexer;
Parser parser(lexer);
const char * formula = "530";
bool result = lex::tokenize_and_parse(formula, formula + strlen(formula), lexer, parser);
return 0;
}
我没有spirit lex
的经验,但我认为它类似于qi
,所以你想使用phoenix function
来做到这一点:
#include <boost/spirit/include/phoenix_function.hpp>
struct calc_something_impl
{
template <typename T1>
struct result { typedef int type; };
int operator()(const std::string & s) const
{
return 5;
}
};
boost::phoenix::function<calc_something_impl> calc_something;
eddi已经发现了部分问题;我还做了另外两个修改:
- 您的语法和规则的签名必须使用合成属性的函数调用语法,即
<Iterator, int()>
- 虽然我不能让懒惰的凤凰函数eddi详细工作(它编译,但从未被调用),切换到凤凰V3使它工作。在你的第一个精灵之前添加:
#define BOOST_SPIRIT_USE_PHOENIX_V3 1
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 提升phoenix值和函数返回值类型
- Boost spirit:使用语义动作和phoenix时的参数类型
- 在Boost Phoenix中获取局部变量的类型