c++ - boost::spirit 语义 Action :发出结果

标签 c++ parsing boost-spirit boost-spirit-qi boost-phoenix

我正在尝试学习 boost::spirit 来解析语法,但我无法准确理解如何使用该库。

基本上,如果解析器在输入流中命中“测试”,我想发出 5 作为返回值。 (稍后,我实际上想发出一对,其中一个字符串成员为“Test”,一个值成员为 5。

这是我目前所拥有的:

template <typename Iterator>
struct testgrammar
    : public grammar<Iterator, unsigned int()>
{
public:

    testgrammar() : testgrammar::base_type(start)
    {
        start = std::wstring(L"Test") [ _val = 5 ] ;
    }

    virtual ~testgrammar(){};

    rule<Iterator, unsigned int()> start;

};

在 main() 中:

std::wstring strIn = L"Test";
std::wstring::const_iterator itBegin = strIn.begin();
std::wstring::const_iterator itEnd = strIn.end();
unsigned int nData = 0;
bool bRes = parse(itBegin, itEnd, avxgrammar<std::wstring::const_iterator>(), nData);

但是,我做错了,因为编译器发出以下错误:

error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const boost::spirit::_val_type' (or there is no acceptable conversion)

我的问题是为什么我不能分配给 _val? 我应该如何从语义操作中发出这样的结构?

编辑 也许我应该澄清最终目标。我想将文本文件解析为成对的 token /文字值。

预先感谢您的帮助!

最佳答案

分配给qi::_val你可能只需要

#include <boost/spirit/include/phoenix.hpp>

或至少 phoenix_operator.hpp .


关于你的练习目标,我想你会想了解一下

关于c++ - boost::spirit 语义 Action :发出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253020/

相关文章:

c++ - 如何将 Wchar_t* 转换为 const char*

用于迭代任何集合成员字段的 C++ 模板函数

c++ - spirit 解析器属性传播规则的问题

excel - VB .NET 中的 CDbl 和小数分隔符 - 有没有办法绕过区域设置?

javascript - 使用 Javascript 编写解析器的教程

c++ - 嵌套 boost::variant 的段错误

c++ - 简单的解析器和生成器

函数调用中的 C++ 临时对象生命周期

c++无法使用指向类的指针调用子类方法

ios - iPhone编程中的函数/库,如preg_match_all(PHP)