c++ - boost::spirit::qi::parse 语法未按预期工作 - 第 2 部分

标签 c++ boost boost-spirit

几天前我问了这个question

一个开放点是不清楚如何处理值(在我的示例中为 -23.0)。字符串应被解析为值(表示为字符串类型)而不是选项。

我现在尝试扩展建议的语法,但还是没有成功。我也试图放宽我的要求,所以我认为用双破折号“--”定义一个参数是有效的。这个想法是为参数获取一个唯一标识符。这是我当前的语法,但解析失败,我不知道为什么:

//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <map>
#include <string>
#include <vector>

// Structure stores the parsed command line information:
struct CmdData
{
    typedef std::string               Name;

    typedef std::string               ArgName;
    typedef std::string               Value;

    typedef std::vector<Value>        Values;  // Type defines a list of values:
    typedef std::map<ArgName, Values> Args;    // Type defines a map storing the relation between a argument and the corresponding values:

    Name cmd; // Stores the command name as a string.
    Args arg; // Stores the arguments and the corresponding values as strings.
};

BOOST_FUSION_ADAPT_STRUCT(CmdData, (CmdData::Name, cmd)(CmdData::Args, arg))

namespace Grammar
{
    namespace qi = boost::spirit::qi;

    // This class implements the grammar used to parse a command line.
    // The expected format is as follows:
    // - command
    // - command value0 ... valueN
    // - command -arg0 ... -argN
    // - command -arg0 value0 ... valueN ... -argN value0 ... valueN
    template <typename It>
    struct decode : qi::grammar<It, CmdData()>
    {
    decode() : decode::base_type(data)
    {
        using namespace qi;

        token  = +( ~char_( "\r\n -" ) );
        values = +( ~char_( "--" ) >> +token );

        //
        entry  = (lexeme[ "--" >> token ] >> -values | attr( "empty" ) >> values );
        args   = *entry;

        //
        data   = skip(qi::blank) [ token >> args ];

        BOOST_SPIRIT_DEBUG_NODES( (token)(values)(entry)(args)(data) )
    }

private:
    qi::rule<It, CmdData()> data;

    // The following variables define the rules used within this grammar:
    typedef std::pair<CmdData::ArgName, CmdData::Values> Entry;
    qi::rule<It, CmdData::Values(), qi::blank_type> values;
    qi::rule<It, Entry(),           qi::blank_type> entry;
    qi::rule<It, CmdData::Args(),   qi::blank_type> args;

    // lexemes
    qi::rule<It, std::string()> token;
    };

}   // namespace

bool parse(const std::string& in)
{
    CmdData data;

    // Create an instance of the used grammar:
    Grammar::decode<std::string::const_iterator> gr;

    // Try to parse the data stored within the stream according the grammar and store the result in the tag variable:
    bool b = boost::spirit::qi::parse(in.begin(), in.end(), gr, data);

    std::cout << "Parsing: '" << in << "' ok: " << std::boolalpha << b << "\n";
    if (b)
        std::cout << "Entries parsed: " << data.arg.size() << "\n";

    return b;
}

int main()
{
    parse("   cmd0");
    parse("   cmd0        value0  value1  value2 -23.0");
    parse("   cmd0  -23.0 value0  value1  value2");
    parse("   cmd0  --arg0  --arg1  123 --arg2 -23.0");
    parse("   cmd0  --arg0  value0  --arg1  value0  value1  --arg2  value0  value1  value2");
}

最佳答案

好的,我试过你的语法,我想我成功了。

声明一下,我不是 boost spirit 的专家,我只有中等水平的经验。

以下是我更改的内容:

  1. 我不知道 ~ 运算符的本质是什么,这里没有记录:http://www.boost.org/doc/libs/1_44_0/libs/spirit/doc/html/spirit/qi/reference/operator.html在我的版本中,我删除了它。

  2. 我认为您使用 ~ 试图表示“不是这些字符”。我这样做的方式通常是使用 - 运算符。也就是说,我创建了一个“通用”表达式,然后使用 - 从中排除了一些内容。

  3. 我去掉了你所有的跳过语法,只添加了一个空格规则。只要空白规则没有属性,就不会影响自动属性扣除,它会有qi::unused_type。这可能不是必需的/最佳的,但以这种方式做出有效的回答对我来说更快。

  4. 我认为我在您的语法中修复的两个主要问题是,使用 ~char_( "--") 而您应该使用 - "-- "- lit("--") 正如 cv_and_he 在评论中指出的那样,以及您解析参数类的部分 "--">> token 并且没有使用 lit,这肯定会混淆自动属性收集系统。

这是我最终得到的结果:

#define BOOST_SPIRIT_USE_PHOENIX_V3

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>

#include <string>
#include <vector>

// Structure stores the parsed command line information:
struct CmdData
{
    typedef std::string               Name;

    typedef std::string               ArgName;
    typedef std::string               Value;

    typedef std::vector<Value>        Values;  // Type defines a list of values:
    typedef std::map<ArgName, Values> Args;    // Type defines a map storing the relation between a argument and the corresponding values:

    Name cmd; // Stores the command name as a string.
    Args arg; // Stores the arguments and the corresponding values as strings.
};

BOOST_FUSION_ADAPT_STRUCT(CmdData, (CmdData::Name, cmd)(CmdData::Args, arg))

namespace Grammar
{
    namespace qi = boost::spirit::qi;

    // This class implements the grammar used to parse a command line.
    // The expected format is as follows:
    // - command
    // - command value0 ... valueN
    // - command -arg0 ... -argN
    // - command -arg0 value0 ... valueN ... -argN value0 ... valueN
    template <typename It>
    struct decode : qi::grammar<It, CmdData()>
    {
    decode() : decode::base_type(data)
    {
        using namespace qi;

        ws = char_("\r\n ");
        token  = +( char_ - ws - lit("--") );
        values = token % (+ws);

        //
        arg_label = lit("--") >> token;
        entry  = arg_label >> -(+ws >> values);
        args   = entry % (+ws);

        //
        data   = *ws >> token >> -(+ws >> args) >> *ws;

        BOOST_SPIRIT_DEBUG_NODES( (token)(values)(entry)(args)(data) )
    }

private:
    qi::rule<It, CmdData()> data;

    // The following variables define the rules used within this grammar:
    typedef std::pair<CmdData::ArgName, CmdData::Values> Entry;
    qi::rule<It, CmdData::Values()> values;
    qi::rule<It, Entry()> entry;
    qi::rule<It, CmdData::Args()> args;

    // lexemes
    qi::rule<It, std::string()> token;
    qi::rule<It, std::string()> arg_label;
    qi::rule<It> ws;
    };

}   // namespace

bool parse(const std::string& in)
{
    CmdData data;

    // Create an instance of the used grammar:
    Grammar::decode<std::string::const_iterator> gr;

    // Try to parse the data stored within the stream according the grammar and store the result in the tag variable:
    bool b = boost::spirit::qi::parse(in.begin(), in.end(), gr, data);

    std::cout << "Parsing: '" << in << "' ok: " << std::boolalpha << b << "\n";
    if (b) {
        std::cout << "Entries parsed: " << data.arg.size() << "\n";

        for (const auto & p : data.arg) {
            std::cout << "  " << p.first;
            bool first = true;
            for (const auto & v : p.second) {
                if (first) {
                    std::cout << " : ";
                    first = false;
                } else {
                    std::cout << " , ";
                }
                std::cout << v;
            }
        std::cout << std::endl;
        }
    }

    return b;
}

int main()
{
    parse("   cmd0");
    parse("   cmd0        value0  value1  value2 -23.0");
    parse("   cmd0  -23.0 value0  value1  value2");
    parse("   cmd0  --arg0  --arg1  123 --arg2 -23.0");
    parse("   cmd0  --arg0  value0  --arg1  value0  value1  --arg2  value0  value1  value2");
}

使用 gcc 版本 4.8.4 编译。这是我的输出:

$ g++ -std=c++11 main.cpp -o main
$ ./main 
Parsing: '   cmd0' ok: true
Entries parsed: 0
Parsing: '   cmd0        value0  value1  value2 -23.0' ok: true
Entries parsed: 0
Parsing: '   cmd0  -23.0 value0  value1  value2' ok: true
Entries parsed: 0
Parsing: '   cmd0  --arg0  --arg1  123 --arg2 -23.0' ok: true
Entries parsed: 3
  arg0
  arg1 : 123
  arg2 : -23.0
Parsing: '   cmd0  --arg0  value0  --arg1  value0  value1  --arg2  value0  value1  value2' ok: true
Entries parsed: 3
  arg0 : value0
  arg1 : value0 , value1
  arg2 : value0 , value1 , value2

编辑:

正如评论中指出的那样,我的第一个答案是不正确的,因为它不处理“空”参数类型。我现在看到第 1 部分的答案正确地完成了该部分。在这个版本中,我修复了这个问题,还修复了空格,以便处理得更干净/更像原始代码示例。

#define BOOST_SPIRIT_USE_PHOENIX_V3

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>

#include <string>
#include <vector>

// Structure stores the parsed command line information:
struct CmdData
{
    typedef std::string               Name;

    typedef std::string               ArgName;
    typedef std::string               Value;

    typedef std::vector<Value>        Values;  // Type defines a list of values:
    typedef std::map<ArgName, Values> Args;    // Type defines a map storing the relation between a argument and the corresponding values:

    Name cmd; // Stores the command name as a string.
    Args arg; // Stores the arguments and the corresponding values as strings.
};

BOOST_FUSION_ADAPT_STRUCT(CmdData, (CmdData::Name, cmd)(CmdData::Args, arg))

namespace Grammar
{
    namespace qi = boost::spirit::qi;

    // This class implements the grammar used to parse a command line.
    // The expected format is as follows:
    // - command
    // - command value0 ... valueN
    // - command -arg0 ... -argN
    // - command -arg0 value0 ... valueN ... -argN value0 ... valueN
    template <typename It>
    struct decode : qi::grammar<It, CmdData()>
    {
    decode() : decode::base_type(data)
    {
        using namespace qi;

        token  = +( char_ - blank - lit("--") );

        //
        arg_label = lit("--") >> token;
        entry  = skip(blank) [
                     (arg_label >> *token) | ( attr("empty") >> +token)
                 ];
        args   = *entry;

        //
        data   = skip(blank) [ token >> args ];

        BOOST_SPIRIT_DEBUG_NODES( (token)(entry)(args)(arg_label)(data) )
    }

private:
    qi::rule<It, CmdData()> data;

    // The following variables define the rules used within this grammar:
    typedef std::pair<CmdData::ArgName, CmdData::Values> Entry;
    qi::rule<It, Entry()> entry;
    qi::rule<It, CmdData::Args()> args;

    // lexemes
    qi::rule<It, std::string()> token;
    qi::rule<It, std::string()> arg_label;
    };

}   // namespace

bool parse(const std::string& in)
{
    CmdData data;

    // Create an instance of the used grammar:
    Grammar::decode<std::string::const_iterator> gr;

    // Try to parse the data stored within the stream according the grammar and store the result in the tag variable:
    bool b = boost::spirit::qi::parse(in.begin(), in.end(), gr, data);

    std::cout << "Parsing: '" << in << "' ok: " << std::boolalpha << b << "\n";
    if (b) {
        std::cout << "Entries parsed: " << data.arg.size() << "\n";

        for (const auto & p : data.arg) {
            std::cout << "  " << p.first;
            bool first = true;
            for (const auto & v : p.second) {
                if (first) {
                    std::cout << " : ";
                    first = false;
                } else {
                    std::cout << " , ";
                }
                std::cout << v;
            }
        std::cout << std::endl;
        }
    }

    return b;
}

int main()
{
    parse("   cmd0");
    parse("   cmd0        value0  value1  value2 -23.0");
    parse("   cmd0  -23.0 value0  value1  value2");
    parse("   cmd0  --arg0  --arg1  123 --arg2 -23.0");
    parse("   cmd0  --arg0  value0  --arg1  value0  value1  --arg2  value0  value1  value2");
}

我的输出现在是这样的:

$ ./main 
Parsing: '   cmd0' ok: true
Entries parsed: 0
Parsing: '   cmd0        value0  value1  value2 -23.0' ok: true
Entries parsed: 1
  empty : value0 , value1 , value2 , -23.0
Parsing: '   cmd0  -23.0 value0  value1  value2' ok: true
Entries parsed: 1
  empty : -23.0 , value0 , value1 , value2
Parsing: '   cmd0  --arg0  --arg1  123 --arg2 -23.0' ok: true
Entries parsed: 3
  arg0
  arg1 : 123
  arg2 : -23.0
Parsing: '   cmd0  --arg0  value0  --arg1  value0  value1  --arg2  value0  value1  value2' ok: true
Entries parsed: 3
  arg0 : value0
  arg1 : value0 , value1
  arg2 : value0 , value1 , value2

我不得不在那个版本中稍微改变一下,因为我得到了一个无限循环 *entry 然后是 attr("empty") >> *tokens。我认为这很可能是在仍然使用所有自动归因的同时让它发挥作用的最简单方法,我不确定。

关于c++ - boost::spirit::qi::parse 语法未按预期工作 - 第 2 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158007/

相关文章:

C++ 模板元编程 : constexpr function

c++ - 访问 boost::graph 中的特定顶点

sockets - boost asio SO_REUSEPORT

c++ - 交叉编译到 x86 时出现 libboost 系统链接器错误

boost - 有没有更好的方法可以在 boost spirit 中转换为大写?

C++ boost::spirit 解析嵌入式语言

c++ - 将 main 声明为被认为有害的 friend ?

c++ - 是否存在 stb_image simd 支持?

c++ - 空字符串和 '\0' 字符有什么区别? (从指针和数组的角度来看)

c++ - 使用 boost.spirit 时不推荐使用的警告