提振精神2.4.2:不能提取字符串 [英] Boost Spirit 2.4.2: Cannot extract a string
问题描述
继解决问题的Boost精神:错误C2664,无法将'常量的boost ::凤::演员<评估和演示>'到字符,我还有一个问题:
为什么要使用下面的js_key和js_string的code,我无法捕捉的格式为STR或'海峡'打印字符串。那些总是返回空白!
例如:
输入: {一:AA,B:'C'}
实际输出:
{
STR:
STR:
STR:乙
STR:
成功解析的输入作为JSON!
期望的输出:
{
STR:一
STR:AA
STR:乙
STR:C
成功解析的输入作为JSON!
请请告知。谢谢!
的#include<地图和GT;
#包括LT&;串GT;
#包括LT&;矢量>
#包括LT&;&iostream的GT;#包括LT&;升压/配置/ warning_disable.hpp>
#包括LT&;升压/精神/有/ qi.hpp>
#包括LT&;升压/精神/有/ phoenix_core.hpp>
#包括LT&;升压/精神/有/ phoenix_operator.hpp>
#包括LT&;升压/精神/有/ phoenix_stl.hpp>
#包括LT&;升压/精神/有/ phoenix_object.hpp>
#包括LT&;升压/精神/有/ phoenix_container.hpp>
#包括LT&;升压/精神/有/ phoenix_function.hpp>
#包括LT&;升压/精神/有/ phoenix_fusion.hpp>
#包括LT&;升压/精神/有/ phoenix_bind.hpp>
#包括LT&;升压/融合/有/ adapt_assoc_struct.hpp>
#包括LT&;升压/融合/有/ io.hpp>
#包括LT&;升压/ bind.hpp>
#包括LT&;升压/ function.hpp>命名空间补气=的boost ::精神::补气;
命名空间ASCII =的boost ::精神:: ASCII;
命名空间凤=提振::凤;无效PRINT_CHAR(焦三)
{
性病::法院LT&;< C<<的std :: ENDL;
}无效print_str(性病::字符串str)
{
性病::法院LT&;< STR:<< STR<<的std :: ENDL;
}模板< typename的迭代器>
结构json_grammar:补气::语法<迭代器,ASCII ::空间类型>
{
json_grammar():json_grammar :: base_type(开始)
{
使用ASCII字母::;
使用ASCII :: alnum;
使用气:: long_long;
使用气:: long_double;
用气点燃::;
使用气:: char_;
使用气::词位; //
开始=
烧焦_('{')[凤::绑定(安培; PRINT_CHAR,补气:: _ 1)]
>> - (js_member%',')
>> char_(})
;
//
js_member =
js_key [凤凰::绑定(安培; print_str,补气:: _ 1)]
>> ':'>> js_value
;
//
js_key =(阿尔法>> * alnum)| js_string
;
//
js_string = js_single_quoted_str | js_double_quoted_str
;
//
js_array =亮起('[')GT;> - (js_value%',')GT;>点亮(']')
;
//
js_bool =点亮(真)|点亮(假)
;
//
js_null =点亮(空)
;
//
js_value = js_string [凤凰::绑定(安培; print_str,补气:: _ 1)]
| js_num | js_array |开始| js_bool | js_null;
//
js_single_quoted_str =(语义['>> *(char_ - ')>>中]);
//
js_double_quoted_str =(语义[''>> *(char_ - ')GT;>'']);
//
js_num = long_long | long_double;
} 齐::规则<迭代器,标准::字符串(),ASCII ::空间类型> js_key;
齐::规则<迭代器,标准::字符串(),ASCII ::空间类型> js_string; 齐::规则<迭代器,ASCII ::空间类型>开始;
齐::规则<迭代器,ASCII ::空间类型> js_member;
齐::规则<迭代器,ASCII ::空间类型> js_value;
齐::规则<迭代器,ASCII ::空间类型> js_single_quoted_str;
齐::规则<迭代器,ASCII ::空间类型> js_double_quoted_str;
齐::规则<迭代器,ASCII ::空间类型> js_array;
齐::规则<迭代器,ASCII ::空间类型> js_num;
齐::规则<迭代器,ASCII ::空间类型> js_bool;
齐::规则<迭代器,ASCII ::空间类型> js_null;
};诠释的main()
{
标准::字符串inputStr;
json_grammar<的std ::字符串::为const_iterator> jsonParser;
布尔parseOK = FALSE; 而(的std ::函数getline(给std :: cin,inputStr)){
如果(inputStr.empty()|| inputStr [0] =='Q'|| inputStr [0] =='Q')
打破; 标准::字符串::为const_iterator ITER = inputStr.begin();
标准::字符串::为const_iterator iterEnd = inputStr.end(); parseOK =齐:: phrase_parse(ITER,iterEnd,jsonParser,ASCII ::空间); 如果(parseOK&安培;&安培; ITER == iterEnd){
性病::法院LT&;< 成功解析了输入作为JSON! <<的std :: ENDL;
}其他{
性病::法院LT&;< 不能解析输入作为JSON! <<的std :: ENDL;
}
} 返回0;
}
答案很简单:2结构 js_single_quoted_str
和 js_double_quoted_str
必须被重新定义为以下,使解析器岩石:
齐::规则<迭代器,标准::字符串(),ASCII ::空间类型> js_single_quoted_str;
齐::规则<迭代器,标准::字符串(),ASCII ::空间类型> js_double_quoted_str;
Following the resolved question Boost Spirit: Error C2664, Cannot convert 'const boost::phoenix::actor<Eval>' to 'char' , I have another question:
Why using the code below for js_key and js_string, I cannot capture print the strings in the format "str" or 'str'. Those always return blank!
For example:
Input: {"a":"aa", b:'c'}
Actual output:
{
str:
str:
str: b
str:
Successfully parsed the input as JSON!
Expected output:
{
str: a
str: aa
str: b
str: c
Successfully parsed the input as JSON!
Please kindly advise. Thank you!
#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;
}
The answer is very simple: 2 structs js_single_quoted_str
and js_double_quoted_str
must be redefined as the following to make the parser rock:
qi::rule<Iterator, std::string(), ascii::space_type> js_single_quoted_str;
qi::rule<Iterator, std::string(), ascii::space_type> js_double_quoted_str;
这篇关于提振精神2.4.2:不能提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!