如何使用带有boost.Spirit的嵌套括号来解析表达式?

| 我需要解析包含键/值对和键/子表达式对的1行表达式,例如:
123=a 456=b 789=(a b c) 111=((1=a 2=b 3=c) (1=x 2=y 3=z) (123=(x y z))) 666=evil
为了简化解析器,我愿意分几个步骤进行解析,分离出第一级标记(这里是123、456、789、111和666),然后在另一步骤中解析其内容。 此处789 \的值为
\"a b c\"
,111 \的值为
(1=a 2=b 3=c) (1=x 2=y 3=z) (123=(x y z))
。 但是语法在这一点上击败了我,因此我可以找出一种方法来获取匹配括号之间的表达式。我得到111的全部是
(1=a 2=b 3=c
,结尾处是第一个闭合括号。 我找到了这个方便的示例并尝试使用它,但没有成功:
#include <map>
#include <string>
#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace qi = boost::spirit::qi;

void main()
{
    auto                                                                   value = +qi::char_(\"a-zA-Z_0-9\");
    auto                                                                   key   =  qi::char_(\"a-zA-Z_\") >> *qi::char_(\"a-zA-Z_0-9\");
    qi::rule<std::string::iterator, std::pair<std::string, std::string>()> pair  =  key >> -(\'=\' >> value);
    qi::rule<std::string::iterator, std::map<std::string, std::string>()>  query =  pair >> *((qi::lit(\';\') | \'&\') >> pair);

    std::string input(\"key1=value1;key2;key3=value3\");  // input to parse
    std::string::iterator begin = input.begin();
    std::string::iterator end = input.end();

    std::map<std::string, std::string> m;        // map to receive results
    bool result = qi::parse(begin, end, query, m);   // returns true if successful
}
我该怎么做? 编辑:我在http://boost-spirit.com/home/articles/qi-example/parsing-a-list-of-key-value-pairs-using-spirit-qi/找到了示例     
已邀请:
您可以将其编写为:
qi::rule<std::string::iterator, std::pair<std::string, std::string>()> pair = 
        key >> -(
           \'=\' >> ( \'(\' >> raw[query] >> \')\' | value )
        )
    ;
它将所有嵌入式查询存储为与键关联的值(字符串)。但是,这将从存储的值中删除括号。如果您仍然希望将括号存储在返回的属性中,请使用以下命令:
qi::rule<std::string::iterator, std::pair<std::string, std::string>()> pair = 
        key >> -(
           \'=\' >> ( raw[\'(\' >> query >> \')\'] | value )
        )
    ;
    

要回复问题请先登录注册