C++ Boost Spirit解析器可选表达式求值
我正在尝试解析文本文件中的一行,其形式如下:C++ Boost Spirit解析器可选表达式求值,c++,parsing,boost-spirit,boost-spirit-qi,C++,Parsing,Boost Spirit,Boost Spirit Qi,我正在尝试解析文本文件中的一行,其形式如下: [int_:] [int_/int_] [(int_, string)] string [string:int_]... 其中,[]是可选参数,但将包含(”:“,”(“,”),“/”)等标记。 最后一种格式是重复格式“key:value”组合。e、 g: 10: 0x1/2 (8, INC) rd API:2 SI:100 当所有参数都可用时,我能够解析整行。 但如果缺少任何起始可选参数,则解析器将失败 如何忽略Boost Spirit库中的可选
[int_:] [int_/int_] [(int_, string)] string [string:int_]...
其中,[]
是可选参数,但将包含(”:“
,”(“
,”),“/”
)等标记。
最后一种格式是重复格式“key:value”
组合。e、 g:
10: 0x1/2 (8, INC) rd API:2 SI:100
当所有参数都可用时,我能够解析整行。
但如果缺少任何起始可选参数,则解析器将失败
如何忽略Boost Spirit库中的可选参数?(即跳过将可选变量指定为默认值。)
以下是qi语法规则:
quoted_string = lexeme[+(char_ -(lit(' ') | lit(')')))];
hex_num = ((lit("0x") | lit("0X")) >> hex) | uint_;
start = (hex_num >> lit(":"))
>> (hex_num >> lit("/") >> hex_num )
>> lit("(") >> hex_num >> lit(",") >> quoted_string >> lit(")")
>> quoted_string
>> quoted_string;
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
qi::rule<Iterator, uint32_t(), ascii::space_type> hex_num;
qi::rule<Iterator, employee(), ascii::space_type> start;
quoted_string=lexeme[+(char-(lit(“”)| lit('))];
十六进制数=((点亮(“0x”)|点亮(“0x”)>>十六进制);
开始=(十六进制数>>点亮(“:”)
>>(十六进制数>>亮(“/”>>十六进制数)
>>亮(“”>>十六进制数>>亮(“”,“”>>带引号的字符串>>亮(“”)
>>带引号的字符串
>>带引号的字符串;
qi::规则引用的字符串;
qi::规则十六进制数;
qi::规则开始;
为AST节点建模以反映解析器树:
struct ratio_t { uint32_t a,b; };
struct opcode_t { uint32_t id; std::string name; };
struct Node {
uint32_t label; // prefix:
boost::optional<ratio_t> ratio; // a/b
boost::optional<opcode_t> opcode; // (id, name)
std::string extra;
std::multimap<std::string, uint32_t> params;
};
您可以使用类似的解析树简单地解析到其中:
// lexemes
unquoted_string = +(graph - ')');
num = (no_case[ "0x" ] >> hex) | uint_;
param = +(graph - ':') >> ':' >> num;
// skipping productions
opcode = '(' >> num >> ',' >> unquoted_string >> ')';
ratio = num >> '/' >> num;
prefix = (num >> ':') | attr(0); // defaults to 0
start = prefix
>> -ratio
>> -opcode
>> unquoted_string
>> *param;
现在,当您分析这些测试用例时:
for (std::string const input : {
"10: 0x1/2 (8, INC) rd API:2 SI:100",
"10: 0x1/2 (8, INC) rd API:2",
"10: 0x1/2 (8, INC) rd",
"10: 0x1/2 rd API:2 SI:100",
"10: rd API:2 SI:100",
"0x1/2 rd API:2 SI:100",
"rd API:2 SI:100",
})
{
It f = input.begin(), l = input.end();
AST::Node data;
bool ok = qi::phrase_parse(f, l, p, qi::ascii::space, data);
if (ok) {
std::cout << "Parse success: " << data << "\n";
}
else {
std::cout << "Parse failure ('" << input << "')\n";
}
if (f!=l) {
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
}
完整演示
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/optional/optional_io.hpp>
#include <map>
namespace qi = boost::spirit::qi;
namespace AST {
struct ratio_t { uint32_t a,b; };
struct opcode_t { uint32_t id; std::string name; };
struct Node {
uint32_t label; // prefix:
boost::optional<ratio_t> ratio; // a/b
boost::optional<opcode_t> opcode; // (id, name)
std::string extra;
std::multimap<std::string, uint32_t> params;
};
std::ostream& operator<<(std::ostream& os, ratio_t const& v) {
return os << v.a << "/" << v.b;
}
std::ostream& operator<<(std::ostream& os, opcode_t const& v) {
return os << "(" << v.id << ", '" << v.name << "')";
}
std::ostream& operator<<(std::ostream& os, Node const& v) {
os << v.label << ": " << v.ratio << " " << v.opcode << " " << v.extra;
for (auto& p : v.params) os << " " << p.first << ":" << p.second;
return os;
}
}
BOOST_FUSION_ADAPT_STRUCT(AST::ratio_t, a, b)
BOOST_FUSION_ADAPT_STRUCT(AST::opcode_t, id, name)
BOOST_FUSION_ADAPT_STRUCT(AST::Node, label, ratio, opcode, extra, params)
template <typename It, typename Skipper = qi::ascii::space_type>
struct P : qi::grammar<It, AST::Node(), Skipper> {
P() : P::base_type(start)
{
using namespace qi;
// lexemes
unquoted_string = +(graph - ')');
num = (no_case[ "0x" ] >> hex) | uint_;
param = +(graph - ':') >> ':' >> num;
// skipping productions
opcode = '(' >> num >> ',' >> unquoted_string >> ')';
ratio = num >> '/' >> num;
prefix = (num >> ':') | attr(0); // defaults to 0
start = prefix
>> -ratio
>> -opcode
>> unquoted_string
>> *param;
BOOST_SPIRIT_DEBUG_NODES((start)(unquoted_string)(num)(prefix)(ratio)(opcode)(param))
}
private:
qi::rule<It, AST::ratio_t(), Skipper> ratio;
qi::rule<It, AST::opcode_t(), Skipper> opcode;
qi::rule<It, AST::Node(), Skipper> start;
qi::rule<It, uint32_t(), Skipper> prefix;
//lexemes
qi::rule<It, std::string()> unquoted_string;
qi::rule<It, uint32_t()> num;
qi::rule<It, std::pair<std::string, uint32_t>> param;
};
int main() {
using It = std::string::const_iterator;
P<It> const p;
for (std::string const input : {
"10: 0x1/2 (8, INC) rd API:2 SI:100",
"10: 0x1/2 (8, INC) rd API:2",
"10: 0x1/2 (8, INC) rd",
"10: 0x1/2 rd API:2 SI:100",
"10: rd API:2 SI:100",
"0x1/2 rd API:2 SI:100",
"rd API:2 SI:100",
})
{
It f = input.begin(), l = input.end();
AST::Node data;
bool ok = qi::phrase_parse(f, l, p, qi::ascii::space, data);
if (ok) {
std::cout << "Parse success: " << data << "\n";
}
else {
std::cout << "Parse failure ('" << input << "')\n";
}
if (f!=l) {
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
}
}
#包括
#包括
#包括
#包括
名称空间qi=boost::spirit::qi;
名称空间AST{
结构比率{uint32\uta,b;};
结构操作码{uint32\ut id;std::string name;};
结构节点{
uint32\u t标签;//前缀:
boost::可选比率;//a/b
boost::可选操作码;//(id、名称)
std::字符串额外;
std::多映射参数;
};
std::ostream&operator>unquoted_字符串
>>*param;
BOOST_SPIRIT_DEBUG_节点((开始)(无引号的字符串)(num)(前缀)(比率)(操作码)(参数))
}
私人:
齐:规则比率;
qi::规则操作码;
qi::规则开始;
qi::规则前缀;
//词素
qi::规则不带引号的字符串;
qi::规则数;
qi::规则参数;
};
int main(){
使用它=std::string::const_迭代器;
常数P;
对于(标准::字符串常量输入:{
“10:0x1/2(8,INC)rd API:2 SI:100”,
“10:0x1/2(8,INC)rd API:2”,
“10:0x1/2(8,INC)rd”,
“10:0x1/2第三方API:2 SI:100”,
“10:rd API:2 SI:100”,
“0x1/2 rd API:2 SI:100”,
“研发API:2 SI:100”,
})
{
它f=input.begin(),l=input.end();
AST::节点数据;
bool ok=qi::短语解析(f,l,p,qi::ascii::空间,数据);
如果(确定){
std::cout为AST节点建模以反映解析器树:
struct ratio_t { uint32_t a,b; };
struct opcode_t { uint32_t id; std::string name; };
struct Node {
uint32_t label; // prefix:
boost::optional<ratio_t> ratio; // a/b
boost::optional<opcode_t> opcode; // (id, name)
std::string extra;
std::multimap<std::string, uint32_t> params;
};
您可以使用类似的解析树简单地解析到其中:
// lexemes
unquoted_string = +(graph - ')');
num = (no_case[ "0x" ] >> hex) | uint_;
param = +(graph - ':') >> ':' >> num;
// skipping productions
opcode = '(' >> num >> ',' >> unquoted_string >> ')';
ratio = num >> '/' >> num;
prefix = (num >> ':') | attr(0); // defaults to 0
start = prefix
>> -ratio
>> -opcode
>> unquoted_string
>> *param;
现在,当您分析这些测试用例时:
for (std::string const input : {
"10: 0x1/2 (8, INC) rd API:2 SI:100",
"10: 0x1/2 (8, INC) rd API:2",
"10: 0x1/2 (8, INC) rd",
"10: 0x1/2 rd API:2 SI:100",
"10: rd API:2 SI:100",
"0x1/2 rd API:2 SI:100",
"rd API:2 SI:100",
})
{
It f = input.begin(), l = input.end();
AST::Node data;
bool ok = qi::phrase_parse(f, l, p, qi::ascii::space, data);
if (ok) {
std::cout << "Parse success: " << data << "\n";
}
else {
std::cout << "Parse failure ('" << input << "')\n";
}
if (f!=l) {
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
}
完整演示
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/optional/optional_io.hpp>
#include <map>
namespace qi = boost::spirit::qi;
namespace AST {
struct ratio_t { uint32_t a,b; };
struct opcode_t { uint32_t id; std::string name; };
struct Node {
uint32_t label; // prefix:
boost::optional<ratio_t> ratio; // a/b
boost::optional<opcode_t> opcode; // (id, name)
std::string extra;
std::multimap<std::string, uint32_t> params;
};
std::ostream& operator<<(std::ostream& os, ratio_t const& v) {
return os << v.a << "/" << v.b;
}
std::ostream& operator<<(std::ostream& os, opcode_t const& v) {
return os << "(" << v.id << ", '" << v.name << "')";
}
std::ostream& operator<<(std::ostream& os, Node const& v) {
os << v.label << ": " << v.ratio << " " << v.opcode << " " << v.extra;
for (auto& p : v.params) os << " " << p.first << ":" << p.second;
return os;
}
}
BOOST_FUSION_ADAPT_STRUCT(AST::ratio_t, a, b)
BOOST_FUSION_ADAPT_STRUCT(AST::opcode_t, id, name)
BOOST_FUSION_ADAPT_STRUCT(AST::Node, label, ratio, opcode, extra, params)
template <typename It, typename Skipper = qi::ascii::space_type>
struct P : qi::grammar<It, AST::Node(), Skipper> {
P() : P::base_type(start)
{
using namespace qi;
// lexemes
unquoted_string = +(graph - ')');
num = (no_case[ "0x" ] >> hex) | uint_;
param = +(graph - ':') >> ':' >> num;
// skipping productions
opcode = '(' >> num >> ',' >> unquoted_string >> ')';
ratio = num >> '/' >> num;
prefix = (num >> ':') | attr(0); // defaults to 0
start = prefix
>> -ratio
>> -opcode
>> unquoted_string
>> *param;
BOOST_SPIRIT_DEBUG_NODES((start)(unquoted_string)(num)(prefix)(ratio)(opcode)(param))
}
private:
qi::rule<It, AST::ratio_t(), Skipper> ratio;
qi::rule<It, AST::opcode_t(), Skipper> opcode;
qi::rule<It, AST::Node(), Skipper> start;
qi::rule<It, uint32_t(), Skipper> prefix;
//lexemes
qi::rule<It, std::string()> unquoted_string;
qi::rule<It, uint32_t()> num;
qi::rule<It, std::pair<std::string, uint32_t>> param;
};
int main() {
using It = std::string::const_iterator;
P<It> const p;
for (std::string const input : {
"10: 0x1/2 (8, INC) rd API:2 SI:100",
"10: 0x1/2 (8, INC) rd API:2",
"10: 0x1/2 (8, INC) rd",
"10: 0x1/2 rd API:2 SI:100",
"10: rd API:2 SI:100",
"0x1/2 rd API:2 SI:100",
"rd API:2 SI:100",
})
{
It f = input.begin(), l = input.end();
AST::Node data;
bool ok = qi::phrase_parse(f, l, p, qi::ascii::space, data);
if (ok) {
std::cout << "Parse success: " << data << "\n";
}
else {
std::cout << "Parse failure ('" << input << "')\n";
}
if (f!=l) {
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
}
}
#包括
#包括
#包括
#包括
名称空间qi=boost::spirit::qi;
名称空间AST{
结构比率{uint32\uta,b;};
结构操作码{uint32\ut id;std::string name;};
结构节点{
uint32\u t标签;//前缀:
boost::可选比率;//a/b
boost::可选操作码;//(id、名称)
std::字符串额外;
std::多映射参数;
};
std::ostream&operator>unquoted_字符串
>>*param;
BOOST_SPIRIT_DEBUG_节点((开始)(无引号的字符串)(num)(前缀)(比率)(操作码)(参数))
}
私人:
齐:规则比率;
qi::规则操作码;
qi::规则开始;
qi::规则前缀;
//词素
qi::规则不带引号的字符串;
qi::规则数;
qi::规则参数;
};
int main(){
使用它=std::string::const_迭代器;
常数P;
对于(标准::字符串常量输入:{
“10:0x1/2(8,INC)rd API:2 SI:100”,
“10:0x1/2(8,INC)rd API:2”,
“10:0x1/2(8,INC)rd”,
“10:0x1/2第三方API:2 SI:100”,
“10:rd API:2 SI:100”,
“0x1/2 rd API:2 SI:100”,
“研发API:2 SI:100”,
})
{
它f=input.begin(),l=input.end();
AST::节点数据;
bool ok=qi::短语解析(f,l,p,qi::ascii::空间,数据);
如果(确定){
std::我不能谢谢你@sehe它帮了我很多。我没有用正确的方式使用它。谢谢你的帮助。谢谢@sehe它帮了我很多。我没有用正确的方式使用它。谢谢你的帮助。