【问题标题】:boost::spirit semantic action: emit resultboost::spirit 语义动作:发出结果
【发布时间】:2013-08-15 12:52:33
【问题描述】:

我正在尝试学习 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/Literal 值对。

提前感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    要分配给qi::_val,您可能只需要

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

    或至少phoenix_operator.hpp


    关于你的锻炼目标,我想你会想了解一下

    【讨论】:

    • 感谢您的回复。我曾尝试包括 phoenix,但这会将编译器错误更改为:error C2679: binary '[' : no operator found which take a right-hand operand of type 'boost::phoenix::actor'
    • 该死,我之前忽略了明显的:std::wstring 不是解析器表达式模板,所以它肯定不会采取语义动作。使用qi::string 或者来自boost::spirit::standard_wide 的那个非常好! (引用记忆中的细节确保互联网访问极其有限)
    猜你喜欢
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    相关资源
    最近更新 更多