X3:如何创建解析器来读取集合?

2024-02-05

如何创建一个规则来读取 3 个一组的整数。即……

1 2 3                   OK, 1 set of 3 ints
1 2 3 4 5 6             OK, 2 sets of 3 ints
1 2 3 4 5               ERROR, 1 set of 3 ints, 1 short for 2nd
1 2 3 4 5 6 7 8 9       OK, 3 sets of 3 ints
1 2 3 4 5 6 7 8 9 10    ERROR, 3 sets of 3 ints, 1 short for 4th

我对融合适应结构有疑问(如何使参数数量可变)...

BOOST_FUSION_ADAPT_STRUCT(client::ast::number, n1, n2, n3, n4, n5, n6)

并且不知道为什么这个规则不起作用。

... = *(int_ >> int_ >> int_);

这是我的尝试...http://coliru.stacked-crooked.com/a/cb10e8096c95fc55 http://coliru.stacked-crooked.com/a/cb10e8096c95fc55

//#define BOOST_SPIRIT_X3_DEBUG
#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

namespace client { 
    namespace ast {

        struct number {
            int n1, n2, n3, n4, n5, n6;
        };

        struct comment {
            std::string text;
            bool dummy;
        };

        struct input {
            std::vector<comment> comments;  
            std::vector<number> numbers;
        };
    } 
}

BOOST_FUSION_ADAPT_STRUCT(client::ast::comment, text, dummy)
BOOST_FUSION_ADAPT_STRUCT(client::ast::number, n1, n2, n3) // , n4, n5, n6) error
BOOST_FUSION_ADAPT_STRUCT(client::ast::input, comments, numbers)

namespace client {      
    namespace parser {

        namespace x3 = boost::spirit::x3;
        using namespace x3;

        typedef std::string::const_iterator It;

        using namespace x3;

        auto const comment = rule<struct _c, ast::comment> {"comment"} = lexeme[*(char_ - eol)] >> attr(false);
        auto const number  = rule<struct _n, ast::number> {"number"}   = int_ >> int_ >> int_;
        // auto const number  = rule<struct _n, ast::number> {"number"}   = *(int_ >> int_ >> int_); error

        auto lines = [](auto p) { return *(p >> eol); };

        auto const input = 
            repeat(1)[comment] >> eol >>
            lines(number);
    }
}

int main() {
    namespace x3 = boost::spirit::x3;

    std::string const iss("any char string here\n1 2 3\n1 2 3 4 5 6");

    auto iter = iss.begin(), eof = iss.end();

    client::ast::input types;

    bool ok = phrase_parse(iter, eof, client::parser::input, x3::blank, types);

    if (iter != eof) {
        std::cout << "Remaining unparsed: '" << std::string(iter, eof) << "'\n";
    }
    std::cout << "Parsed: " << (100.0 * std::distance(iss.begin(), iter) / iss.size()) << "%\n";
    std::cout << "ok = " << ok << std::endl;

    for (auto& item : types.comments) { std::cout << "comment: " << boost::fusion::as_deque(item) << "\n"; }
    for (auto& item : types.numbers)  { std::cout << "number:  " << boost::fusion::as_deque(item) << "\n"; }
}

Printing

Parsed: 71.0526%
ok = 1
comment: (any char string here 0)
number:  (1 2 3)

运算符 Kleene-star 合成为容器属性(文档显示:vector<T>)

你的结构number不是容器属性。所以。

另外,我完全不清楚你想要实现什么目标。你的结构应该是 6 个整数,但你想解析 3 个整数的组?是什么meaning团体的?我可能会这样做:

struct number {
    struct group { int n1, n2, n3; };
    std::vector<group> groups;
};

BOOST_FUSION_ADAPT_STRUCT(client::ast::number::group, n1, n2, n3)
BOOST_FUSION_ADAPT_STRUCT(client::ast::number, groups)

这与解析器表达式兼容

 *(int_ >> int_ >> int_)

现场演示

两个注意事项:

  1. 再次需要虚拟属性(考虑using number = std::vector<number_group>;)
  2. 有一个失踪的\n在输入末尾(考虑eol | eoi)

Live On Coliru http://coliru.stacked-crooked.com/a/07c176a99c2d5371

//#define BOOST_SPIRIT_X3_DEBUG
#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

namespace client { 
    namespace ast {

        struct number {
            struct group { int n1, n2, n3; };
            std::vector<group> groups;
            bool dummy;
        };

        struct comment {
            std::string text;
            bool dummy;
        };

        struct input {
            std::vector<comment> comments;  
            std::vector<number> numbers;
        };
    } 
}

BOOST_FUSION_ADAPT_STRUCT(client::ast::comment, text, dummy)
BOOST_FUSION_ADAPT_STRUCT(client::ast::number::group, n1, n2, n3)
BOOST_FUSION_ADAPT_STRUCT(client::ast::number, groups, dummy)
BOOST_FUSION_ADAPT_STRUCT(client::ast::input, comments, numbers)

namespace client {      
    namespace parser {

        namespace x3 = boost::spirit::x3;
        using namespace x3;

        typedef std::string::const_iterator It;

        using namespace x3;

        auto const comment = rule<struct _c, ast::comment> {"comment"} = lexeme[*(char_ - eol)] >> attr(false);
        auto const number  = rule<struct _n, ast::number> {"number"}   = *(int_ >> int_ >> int_) >> attr(false);

        auto lines = [](auto p) { return *(p >> eol); };

        auto const input = 
            repeat(1)[comment] >> eol >>
            lines(number);
    }
}

int main() {
    namespace x3 = boost::spirit::x3;

    std::string const iss("any char string here\n1 2 3\n1 2 3 4 5 6\n");

    auto iter = iss.begin(), eof = iss.end();

    client::ast::input types;

    bool ok = phrase_parse(iter, eof, client::parser::input, x3::blank, types);

    if (iter != eof) {
        std::cout << "Remaining unparsed: '" << std::string(iter, eof) << "'\n";
    }
    std::cout << "Parsed: " << (100.0 * std::distance(iss.begin(), iter) / iss.size()) << "%\n";
    std::cout << "ok = " << ok << std::endl;

    for (auto &item : types.comments) {
        std::cout << "comment: " << boost::fusion::as_deque(item) << "\n";
    }
    for (auto& item : types.numbers) {
        std::cout << "number:  ";
        for (auto& g : item.groups)
            std::cout << boost::fusion::as_deque(g) << " ";
        std::cout << "\n";
    }
}

Prints

Parsed: 100%
ok = 1
comment: (any char string here 0)
number:  (1 2 3) 
number:  (1 2 3) (4 5 6) 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

X3:如何创建解析器来读取集合? 的相关文章

  • 如何使用 openSSL 函数验证 PEM 证书的密钥长度

    如何验证以这种方式生成的 PEM 证书的密钥长度 openssl genrsa des3 out server key 1024 openssl req new key server key out server csr cp server
  • 在 C++ 代码中转换字符串

    我正在学习 C 并开发一个项目来练习 但现在我想在代码中转换一个变量 字符串 就像这样 用户有一个包含 C 代码的文件 但我希望我的程序读取该文件并插入将其写入代码中 如下所示 include
  • 在 Mono 中反序列化 JSON 数据

    使用 Monodroid 时 是否有一种简单的方法可以将简单的 JSON 字符串反序列化为 NET 对象 System Json 只提供序列化 不提供反序列化 我尝试过的各种第三方库都会导致 Mono Monodroid 出现问题 谢谢 f
  • C# 中一次性对象克隆会导致内存泄漏吗?

    检查这个代码 class someclass IDisposable private Bitmap imageObject public void ImageCrop int X int Y int W int H imageObject
  • 如何向 Mono.ZeroConf 注册服务?

    我正在尝试测试 ZeroConf 示例http www mono project com Mono Zeroconf http www mono project com Mono Zeroconf 我正在运行 OpenSuse 11 和 M
  • Android NDK 代码中的 SIGILL

    我在市场上有一个 NDK 应用程序 并获得了有关以下内容的本机崩溃报告 SIGILL信号 我使用 Google Breakpad 生成本机崩溃报告 以下是详细信息 我的应用程序是为armeabi v7a with霓虹灯支持 它在 NVIDI
  • MVC 5 中具有 ASP.NET Identity 的 Autofac 不会验证 OWIN 管道中的安全标记

    我在 MVC 5 中设置了 AutoFac 来与 ASP NET Identity 一起使用 表面上一切似乎都工作正常 即用户可以创建帐户并登录 但后来我发现 当安全标记更改时 用户不会注销 通过在 AspNetUsers 表中进行暴力破解
  • 为什么这个 makefile 在“make clean”上执行目标

    这是我当前的 makefile CXX g CXXFLAGS Wall O3 LDFLAGS TARGET testcpp SRCS main cpp object cpp foo cpp OBJS SRCS cpp o DEPS SRCS
  • Unity手游触摸动作不扎实

    我的代码中有一种 错误 我只是找不到它发生的原因以及如何修复它 我是统一的初学者 甚至是统一的手机游戏的初学者 我使用触摸让玩家从一侧移动到另一侧 但问题是我希望玩家在手指从一侧滑动到另一侧时能够平滑移动 但我的代码还会将玩家移动到您点击的
  • wordexp 失败时我们需要调用 wordfree 吗?

    wordexp 失败时我们需要调用 wordfree 吗 在某些情况下 调用 wordfree 似乎会出现段错误 例如 当 wordfree 返回字符串为 foo bar 的错误代码时 这在手册页中并不清楚 我已经看到在某些错误情况下使用了
  • 如何防止 Blazor NavLink 组件的默认导航

    从 Blazor 3 1 Preview 2 开始 应该可以防止默认导航行为 https devblogs microsoft com aspnet asp net core updates in net core 3 1 preview
  • 在 azure blob 存储中就地创建 zip 文件

    我将文件存储在 Blob 存储帐户内的一个容器中 我需要在第二个容器中创建一个 zip 文件 其中包含第一个容器中的文件 我有一个使用辅助角色和 DotNetZip 工作的解决方案 但由于 zip 文件的大小最终可能达到 1GB 我担心在进
  • Unity c# 四元数:将 y 轴与 z 轴交换

    我需要旋转一个对象以相对于现实世界进行精确旋转 因此调用Input gyro attitude返回表示设备位置的四元数 另一方面 这迫使我根据这个四元数作为默认旋转来计算每个旋转 将某些对象设置为朝上的简单方法如下 Vector3 up I
  • 让网络摄像头在 OpenCV 中工作

    我正在尝试让我的网络摄像头在 Windows 7 64 位中的 OpenCV 版本 2 2 中捕获视频 但是 我遇到了一些困难 OpenCV 附带的示例二进制文件都无法检测到我的网络摄像头 最近我发现这篇文章表明答案在于重新编译一个文件 o
  • 如何在多线程应用程序中安全地填充数据并 Refresh() DataGridView?

    我的应用程序有一个 DataGridView 对象和一个 MousePos 类型的列表 MousePos 是一个自定义类 它保存鼠标 X Y 坐标 类型为 Point 和该位置的运行计数 我有一个线程 System Timers Timer
  • 如何从 Boost.PropertyTree 复制子树

    我有一些boost property tree ptree 我需要树来删除一些具有特定标签名称的元素 例如 xml 表示源ptree如下
  • 读取依赖步行者输出

    I am having some problems using one of the Dlls in my application and I ran dependency walker on it i am not sure how to
  • .NET 和 Mono 之间的开发差异

    我正在研究 Mono 和 NET C 将来当项目开发时我们需要在 Linux 服务器上运行代码 此时我一直在研究 ASP NET MVC 和 Mono 我运行 Ubuntu 发行版 想要开发 Web 应用程序 其他一些开发人员使用 Wind
  • 如果找不到指定的图像文件,显示默认图像的最佳方式?

    我有一个普通的电子商务应用程序 我将 ITEM IMAGE NAME 存储在数据库中 有时经理会拼错图像名称 为了避免 丢失图像 IE 中的红色 X 每次显示产品列表时 我都会检查服务器中是否有与该产品相关的图像 如果该文件不存在 我会将其
  • 如何在 ASP.NET Core 中注入泛型的依赖关系

    我有以下存储库类 public class TestRepository Repository

随机推荐