boost::spirit::qi::parse 语法没有按预期工作

boost::spirit::qi::parse grammar not working as expected

我尝试编写一个语法来解析以下语法:

// - command
// - command value0 ... valueN
// - command -arg0 ... -argN
// - command -arg0 value0 ... valueN ... -argN value0 ... valueN

我在一个简短的示例中添加了我当前的语法 here

我的问题:

矢量包含的条目多于可用值,因为空白也被解释为值

不太清楚你希望语法如何运作¹,但从目标数据结构来看,我觉得事情可以通过

大大简化
  1. 使用船长(背景见Boost spirit skipper issues
  2. 使用自动属性传播而不是 phoenix(另请参阅 Boost Spirit: "Semantic actions are evil"?)。

    token  = +~char_("\r\n -");
    values = +token;
    
    //
    entry  = (lexeme['-' >> token] >> -values | attr("empty") >> values);
    args   = *entry;
    
    //
    data   = skip(qi::blank) [ token >> args ];
    

在下面的示例中,我使用 Fusion 适配来启用自动属性传播(它立即启用调试输出

#define BOOST_SPIRIT_DEBUG

Live On Coliru

//#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 = +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  -23.0 value0  value1  value2");
    parse("   cmd0  -arg0  -arg1  -arg2");
    parse("   cmd0  -arg0  value0  -arg1  value0  value1  -arg2  value0  value1  value2");
}

版画

Parsing: '   cmd0' ok: true
Entries parsed: 0
Parsing: '   cmd0  -23.0 value0  value1  value2' ok: true
Entries parsed: 1
Parsing: '   cmd0  -arg0  -arg1  -arg2' ok: true
Entries parsed: 3
Parsing: '   cmd0  -arg0  value0  -arg1  value0  value1  -arg2  value0  value1  value2' ok: true
Entries parsed: 3

(禁用调试输出)


¹(例如 -23.0 是否明确表示为一个选项)