Boost Spirit 2.4.2:无法提取字符串

2023-12-19

解决问题后Boost Spirit:错误 C2664,无法将 'const boost::phoenix::actor' 转换为 'char' https://stackoverflow.com/questions/6549791/boost-spirit-error-c2664-cannot-convert-const-boostphoenixactoreval-to, 我有另一个问题:

为什么对 js_key 和 js_string 使用下面的代码,我无法捕获打印格式为“str”或“str”的字符串。这些总是返回空白!

例如:

Input: {"a":"aa", b:'c'}实际输出:

{
str:
str:
str: b
str:
Successfully parsed the input as JSON!

预期输出:

{
str: a
str: aa
str: b
str: c
Successfully parsed the input as JSON!

请各位指教。谢谢你!

#include <map>
#include <string>
#include <vector>
#include <iostream>

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_container.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>
#include <boost/fusion/include/adapt_assoc_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;

void print_char(char c)
{
    std::cout << c << std::endl;
}

void print_str(std::string str)
{
    std::cout << "str: " << str << std::endl;
}

template <typename Iterator>
struct json_grammar : qi::grammar<Iterator, ascii::space_type>
{
    json_grammar() : json_grammar::base_type(start)
    {
        using ascii::alpha;
        using ascii::alnum;
        using qi::long_long;
        using qi::long_double;
        using qi::lit;
        using qi::char_;
        using qi::lexeme;

        // 
        start =
            char_('{')              [phoenix::bind(&print_char, qi::_1)]
            >> -(js_member % ',')
            >> char_('}')
        ;
        // 
        js_member =
            js_key                  [phoenix::bind(&print_str, qi::_1)]
            >> ':' >> js_value
        ;
        // 
        js_key = (alpha >> *alnum) | js_string
        ;
        // 
        js_string = js_single_quoted_str | js_double_quoted_str
        ;
        // 
        js_array = lit('[') >> -(js_value % ',') >> lit(']')
        ;
        // 
        js_bool = lit("true") | lit("false")
        ;
        // 
        js_null = lit("null")
        ;
        // 
        js_value = js_string        [phoenix::bind(&print_str, qi::_1)]
            | js_num | js_array | start | js_bool | js_null;
        // 
        js_single_quoted_str = (lexeme["'" >> *(char_ - "'") >> "'"]);
        // 
        js_double_quoted_str = (lexeme['"' >> *(char_ - '"') >> '"']);
        // 
        js_num = long_long | long_double;
    }

    qi::rule<Iterator, std::string(), ascii::space_type> js_key;
    qi::rule<Iterator, std::string(), ascii::space_type> js_string;

    qi::rule<Iterator, ascii::space_type> start;
    qi::rule<Iterator, ascii::space_type> js_member;
    qi::rule<Iterator, ascii::space_type> js_value;
    qi::rule<Iterator, ascii::space_type> js_single_quoted_str;
    qi::rule<Iterator, ascii::space_type> js_double_quoted_str;
    qi::rule<Iterator, ascii::space_type> js_array;
    qi::rule<Iterator, ascii::space_type> js_num;
    qi::rule<Iterator, ascii::space_type> js_bool;
    qi::rule<Iterator, ascii::space_type> js_null;
};

int main()
{
    std::string inputStr;
    json_grammar<std::string::const_iterator> jsonParser;
    bool parseOK = false;

    while(std::getline(std::cin, inputStr)) {
        if(inputStr.empty() || inputStr[0] == 'q' || inputStr[0] == 'Q')
            break;

        std::string::const_iterator iter = inputStr.begin();
        std::string::const_iterator iterEnd = inputStr.end();

        parseOK = qi::phrase_parse(iter, iterEnd, jsonParser, ascii::space);

        if(parseOK && iter == iterEnd) {
            std::cout << "Successfully parsed the input as JSON!" << std::endl;
        } else {
            std::cout << "Cannot parse the input as JSON!" << std::endl;
        }
    }

    return 0;
}

答案很简单:2个结构体js_single_quoted_str and js_double_quoted_str必须重新定义如下才能使解析器稳定:

qi::rule<Iterator, std::string(), ascii::space_type> js_single_quoted_str;
qi::rule<Iterator, std::string(), ascii::space_type> js_double_quoted_str;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Boost Spirit 2.4.2:无法提取字符串 的相关文章

  • 实体框架中的重复键异常?

    我试图捕获当我将具有给定用户名的现有用户插入数据库时 引发的异常 正如标题所说 我正在使用 EF 当我尝试将用户插入数据库时 引发的唯一异常是 UpdateException 如何提取此异常以识别其是否是重复异常或其他异常 catch Up
  • C/C++ 中随机数生成器的实现[重复]

    这个问题在这里已经有答案了 我对 C 中随机数生成器的实现有点困惑 它也与 C 中的明显不同 如果我理解正确 对 srand seed 的调用会以某种方式初始化可通过 rand 访问的隐藏变量 种子 该变量又将函数指向预先生成的序列 例如例
  • 此插件导致 Outlook 启动缓慢

    我正在使用 C NET 4 5 开发 Outlook Addin 项目 但部署后 有时 Outlook 会禁用我的插件 并显示此消息 这个插件导致 Outlook 启动缓慢 我不知道我的插件出了什么问题 这只有很少的代码 并且ThisAdd
  • 为什么 LinkedList 通常比 List 慢?

    我开始在我的一些 C 算法中使用一些 LinkedList 而不是列表 希望能够加快速度 然而 我注意到他们只是感觉更慢 像任何优秀的开发人员一样 我认为我应该尽职调查并验证我的感受 所以我决定对一些简单的循环进行基准测试 我认为用一些随机
  • 隐形打开的弹出窗口

    第二天就解决这个问题 要重现 请创建新的 WPF 应用程序 xaml
  • C# ConfigurationManager 从 app.config 检索错误的连接字符串

    我有一个简单的 WinForms 应用程序 它最终将成为一个游戏 现在 我正在研究它的数据访问层 但遇到了障碍 我创建了一个单独的项目 名为DataAccess在其中 我创建了一个本地 mdfSQL Server 数据库文件 我还创建了一个
  • 当我尝试使用 AVX 功能时,Clang 生成错误

    我使用的是 Windows 10 使用 Clang 版本 5 最近安装 当我编译以下内容时 define AVX define AVX2 include
  • 我们应该使用 Eval 还是 Databind 事件?

    当使用 Asp Net 并使用 ListView 等控件创建网站时 使用 Eval 命令是一个好习惯吗 还是应该在 databind 事件中填充文字和数据 取决于您是否想在更新事件上写回数据 在这种情况下数据绑定 如果您只想读取该数据 可以
  • 解析整数集的字符串并列出间隔

    I have 2 5 7 9 12 string 我想从中获取 2 5 7 8 9 12 列表 python中有没有内置的函数 Thanks UPD 我想 直接的答案是No 不管怎样 谢谢你的 片段 使用一个 建议者斯文 马尔纳克 s 2
  • 使用对象列表构建树

    我有一个带有属性 id 和parent id 的对象列表 我想建造一棵树来连接那些孩子和父母 1 个父对象可以有多个子对象 并且有一个对象将成为所有对象的祖先 实现该功能最快的算法是什么 我使用 C 作为编程语言 但其他语言也可以 像这样的
  • 在非指针变量和类成员上放置 new

    考虑以下示例 include
  • 打破条件变量死锁

    我遇到这样的情况 线程 1 正在等待条件变量 A 该变量应该由线程 2 唤醒 现在线程 2 正在等待条件变量 B 该变量应该由线程 1 唤醒 在我使用的场景中条件变量 我无法避免这样的死锁情况 我检测到循环 死锁 并终止死锁参与者的线程之一
  • 检索 Autofac 容器以解析服务

    在 C WindowForms 应用程序中 我启动一个 OWIN WebApp 它创建另一个类 Erp 的单例实例 public partial class Engine Form const string url http 8080 49
  • 完整日历 - 向事件对象添加额外属性

    可能是由于我缺乏理解 但我使用 PHP 返回 JSON 字符串来带回事件数据
  • printf 参数不足

    我的问题是关于缺少参数的 printf 之后的行为 printf s blah blah d int integer was given as argument and not int written 我已经知道 如果格式参数不足 则行为是
  • 如何在Linux上构建GLFW3项目?

    我已经使用 cmake 和 make 编译了 glfw3 和包含的示例 没有出现任何问题 开始编写我的第一个项目 作为 opengl 和 glfw 的新手 并且对 C 和 CMake 没有经验 我正在努力理解示例构建文件 甚至要链接哪些库和
  • Crypto++ 和压缩 EC 密钥

    如何在 Crypto 中生成压缩的 ECDSA 密钥 AutoSeededRandomPool prng ECDSA
  • JSONDecodeError:额外数据:Python [重复]

    这个问题在这里已经有答案了 我使用以下代码从文件加载 json file file name obj list with open file as f for json obj in f obj list append loads json
  • 编译器什么时候内联函数?

    在 C 中 函数仅在显式声明时才内联inline 或在头文件中定义 或者编译器是否允许内联函数 因为他们认为合适 The inline关键字实际上只是告诉链接器 或告诉编译器告诉链接器 同一函数的多个相同定义不是错误 如果您想在标头中定义函
  • 如何获取通过网络驱动器访问的文件的 UNC 路径?

    我正在 VC 中开发一个应用程序 其中网络驱动器用于访问文件 驱动器由用户手动分配 然后在应用程序中选择驱动器 这会导致驱动器并不总是映射到相同的服务器 我该如何获取此类文件的 UNC 路径 这主要是为了识别目的 这是我用来将普通路径转换为

随机推荐