我正在尝试学习 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
.
关于你的练习目标,我想你会想了解一下
qi::symbols<char, unsigned int>
见http://www.boost.org/doc/libs/1_54_0/libs/spirit/doc/html/spirit/qi/reference/string/symbols.html看起来您实际上可能正在寻找标记化,Spirit Lex 涵盖了这一点
- 参见
lex::tokenize
或者例如lex::tokenize_and_parse
- 请注意,默认的、最基本的 token 模型存储 token ID 和相应的源迭代器对。
- 参见
关于c++ - boost::spirit 语义 Action :发出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253020/