你如何从 Boost Spirit X3 词法解析器中获取字符串?
How do you get a string out of a Boost Spirit X3 lexeme parser?
进行语义操作的最简单方法是什么,该操作基于boost::spirit::x3::lexeme
从典型的标识符解析器中提取字符串?
我认为可以绕过需要解压缩属性并仅在输入流中使用迭代器,但显然x3::_where
没有做我认为它做的事情。
以下结果output
为空。我希望它包含"foobar_hello"。
namespace x3 = boost::spirit::x3;
using x3::_where;
using x3::lexeme;
using x3::alpha;
auto ctx_to_string = [&](auto& ctx) {
_val(ctx) = std::string(_where(ctx).begin(), _where(ctx).end());
};
x3::rule<class identifier_rule_, std::string> const identifier_rule = "identifier_rule";
auto const identifier_rule_def = lexeme[(x3::alpha | '_') >> *(x3::alnum | '_')][ctx_to_string];
BOOST_SPIRIT_DEFINE(identifier_rule);
int main()
{
std::string input = "foobar_hello";
std::string output;
auto result = x3::parse(input.begin(), input.end(), identifier_rule, output);
}
我是否需要以某种方式从x3::_attr(ctx)
中的 boost::fusion 对象中提取字符串,还是我做错了什么?
您可以简单地使用自动属性传播,这意味着您不需要语义操作(1(
住在科里鲁
#include <iostream>
#include <iomanip>
#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
namespace P {
x3::rule<class identifier_rule_, std::string> const identifier_rule = "identifier_rule";
auto const identifier_rule_def = x3::lexeme[(x3::alpha | x3::char_('_')) >> *(x3::alnum | x3::char_('_'))];
BOOST_SPIRIT_DEFINE(identifier_rule)
}
int main() {
std::string const input = "foobar_hello";
std::string output;
auto result = x3::parse(input.begin(), input.end(), P::identifier_rule, output);
}
指纹
<identifier_rule>
<try>foobar_hello</try>
<success></success>
<attributes>[f, o, o, b, a, r, _, h, e, l, l, o]</attributes>
</identifier_rule>
注意 我将
'_'
更改为x3::char_('_')
以捕获下划线(x3::lit
不会捕获它匹配的内容(
如果你坚持语义动作,
- 考虑使用
rule<..., std::string, true>
强制自动传播 - 不要假设
_where
指向你所希望的:http://coliru.stacked-crooked.com/a/336c057dabc86a84 使用
x3::raw[]
公开受控源迭代器范围 (http://coliru.stacked-crooked.com/a/80a69ae9b99a4c61(auto ctx_to_string = [](auto& ctx) { std::cout << "nSA: '" << _attr(ctx) << "'" << std::endl; _val(ctx) = std::string(_attr(ctx).begin(), _attr(ctx).end()); }; x3::rule<class identifier_rule_, std::string> const identifier_rule = "identifier_rule"; auto const identifier_rule_def = x3::raw[ lexeme[(x3::alpha | '_') >> *(x3::alnum | '_')] ] [ctx_to_string]; BOOST_SPIRIT_DEFINE(identifier_rule)
请注意,现在
char_('_')
不再起作用请考虑使用内置属性帮助程序:http://coliru.stacked-crooked.com/a/3e3861330494e7c9
auto ctx_to_string = [](auto& ctx) { using x3::traits::move_to; move_to(_attr(ctx), _val(ctx)); };
请注意,这类似于内置属性传播,尽管它比让 Spirit 管理它要灵活得多。
(1(必修环节:振奋精神:"语义行为是邪恶的"?
相关文章:
- 获取字符串的长度并将其分配给数组
- 调用CreateProcess()并获取字符串的返回值
- 你如何从 Boost Spirit X3 词法解析器中获取字符串?
- 使用 ReadProcessMemory 获取字符串值的访问冲突
- 如何从输入中获取字符串向量?
- 如何获取字符串输入n次?
- property_tree获取字符串形式的数组值
- 使用 ctypes 和 wchar_t 时如何从 C++ 获取字符串到 python?
- 获取字符串大小的各种方法之间的比较
- 方法获取字符串
- 在getline()之后获取字符串
- 为什么我的拆卸C 代码使用指令指针和偏移来获取字符串文字
- 无法使用 Poco 获取字符串的路径
- 如何在qtablewidget中获取字符串的行号
- 如何获取字符串和字节进行比较
- 恒定字符阵列的获取字符串长度函数(strlen)不是恒定表达式
- 我需要创建一个函数来获取字符串中的这些数字
- 如何从地址获取字符串?
- 为什么即使尚未初始化,获取字符串[0]也是有效的?
- 获取C++字符串的位