我正在分析一些科学文本,其格式类似于
Keyword
{ 1.0 22.2 59.6 'cm' 'yes' }
我是精神新手,在研究完文档后,我可以用精神来解决固定格式的关键字。
但是对于以下格式,我不知道如何构建语法。我的问题是: 在我遇到的科学关键字中,某些数据项可以默认为内置默认值。关键字说明指示何时可以应用默认值。将数量设置为默认值有两种方法。首先,通过斜杠'}'提前结束数据记录,将未指定的数量设置为其默认值。其次,通过输入n *可以默认位于'}'之前的选定数量,其中n是要默认的连续数量的数量。例如,3 *会使关键字数据中的后三个数量被赋予其默认值。
例如,
Person
{ 'Tom' 188 80 'male' 32 }
说“男性”和“32”是默认值,其等价物可以是:
Person
{ 'Tom' 188 88 2* }
或
Person
{ 'Tom' 188 88 'male' 1* }
或
Person
{ 'Tom' 188 88 }
我搜索过去的帖子,this给了我一些想法,但我怎样才能写出n *的规则?
答案 0 :(得分:4)
您要求的解析器非常复杂,因为它必须解决几个任务:
这里的诀窍是以不同的方式利用qi::attr
:
为缺少的元素提供默认值:
qi::int_ | qi::attr(180)
即。要么匹配整数,要么使用默认值180
提供“2 *”语法的所有剩余值(如@vines建议的那样):
"2*" >> qi::attr(attr2)
即。如果匹配2*
,请使用默认值attr2(fusion::vector
)。
总的来说,我提出了这个解决方案,它似乎解析并返回默认值就好了(即使看起来非常复杂):
#include <string>
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/vector.hpp>
int main()
{
namespace qi = boost::spirit::qi;
namespace fusion = boost::fusion;
// the attribute passed to the parser has to match (in structure) the
// parser, requiring to create nested fusion::vector's
typedef fusion::vector<std::string, int> attribute1_type;
typedef fusion::vector<int, attribute1_type> attribute2_type;
typedef fusion::vector<int, attribute2_type> attribute3_type;
// overall attribute type
typedef fusion::vector<std::string, attribute3_type> attribute_type;
// initialize attributes with default values
attribute1_type attr1("male", 32);
attribute2_type attr2(80, attr1);
attribute3_type attr3(180, attr2);
qi::rule<std::string::iterator, std::string()> quoted_string =
"'" >> *~qi::char_("'") >> "'";
qi::rule<std::string::iterator, attribute_type(), qi::space_type> data =
qi::lit("Person") >> "{"
>> quoted_string
>> -( ("4*" >> qi::attr(attr3))
| (qi::int_ | qi::attr(180))
>> -( ("3*" >> qi::attr(attr2))
| (qi::int_ | qi::attr(80))
>> -( ("2*" >> qi::attr(attr1))
| (quoted_string | qi::attr("male"))
>> -( "1*"
| qi::int_
| qi::attr(32)
)
)
)
)
>> "}";
std::string in1 = "Person\n{ 'Tom' 188 80 'male' 32 }";
attribute_type fullattr1;
if (qi::phrase_parse(in1.begin(), in1.end(), data, qi::space, fullattr1))
std::cout << fullattr1 << std::endl;
std::string in2 = "Person\n{ 'Tom' 188 80 'male' }";
attribute_type fullattr2;
if (qi::phrase_parse(in2.begin(), in2.end(), data, qi::space, fullattr2))
std::cout << fullattr2 << std::endl;
std::string in3 = "Person\n{ 'Tom' 188 3* }";
attribute_type fullattr3;
if (qi::phrase_parse(in3.begin(), in3.end(), data, qi::space, fullattr3))
std::cout << fullattr3 << std::endl;
return 0;
}
将规则拆分为单独的规则(如@vines建议的那样)将需要多次解析输入,这就是我使用这种嵌套结构的序列和替代方法的原因。
答案 1 :(得分:4)
我刚刚提出了广义解决方案,虽然它有点复杂=)
它处理“过早支撑”和多个任意跳过说明符。就是这样:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <iostream>
#include <string>
namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
struct numbers { int i1, i2, i3, i4; };
BOOST_FUSION_ADAPT_STRUCT
(numbers,
(int, i1)
(int, i2)
(int, i3)
(int, i4)
)
template <typename Iterator, typename Skipper>
struct Grammar : public qi::grammar <Iterator, numbers(), Skipper>
{
Grammar() : Grammar::base_type(start, "numbers")
{
using qi::int_;
// This rule resets the skip counter:
init_skip = qi::eps[ph::ref(skp) = 0];
// This rule parses the skip directive ("n*") and sets the skip counter:
skip_spec = qi::omit[ (qi::lexeme[ int_ >> "*" ])[ph::ref(skp) = qi::_1] ];
// This rule checks if we should skip the field, and if so, decrements
// the skip counter and returns the value given to it (the default one).
// If not, it tries to parse the int.
// If int fails to parse, the rule resorts the default value again,
// thus handling the "premature brace" case.
int_dflt %= qi::eps(ph::ref(skp) > 0)[--ph::ref(skp)] >> qi::attr(qi::_r1) | int_ | qi::attr(qi::_r1);
// And this is the grammar:
start %= init_skip >>
"{" >> -skip_spec >> int_dflt(-1)
>> -skip_spec >> int_dflt(-1)
>> -skip_spec >> int_dflt(-1)
>> -skip_spec >> int_dflt(-1)
>> "}";
}
// the skip counter itself:
int skp;
qi::rule <Iterator, numbers(), Skipper> start;
qi::rule <Iterator, Skipper> skip_spec, init_skip;
qi::rule <Iterator, int(int), Skipper> int_dflt;
};
int main (int argc, char **argv)
{
using std::cout;
using std::endl;
std::string s = argv[1];
numbers result;
std::string::iterator ib = s.begin();
std::string::iterator ie = s.end();
bool r = qi::phrase_parse(ib, ie, Grammar<std::string::iterator, qi::space_type>(), qi::space, result );
if (r && ib == ie)
{
cout << boost::fusion::tuple_open('[');
cout << boost::fusion::tuple_close(']');
cout << boost::fusion::tuple_delimiter(", ");
cout << "Parsing succeeded\n";
cout << "got: " << boost::fusion::as_vector(result) << endl;
}
else
{
cout << "Parsing failed\n";
cout << "err: " << std::string(ib, ie) << endl;
}
return 0;
}
PS:请注意,Skipper模板参数与字段跳过无关 - 它只是语法使用的空白跳过解析器的类型。
答案 2 :(得分:1)
首先我能想到:
如果您的结构没有太多成员,您可以将* n描述为某种语法,例如:
struct_full = "{" >> a >> b >> c >> "}";
struct_reduced_1 = "{" >> a >> b >> "1*" >> attr(c_default) >> "}"
struct_reduced_2 = "{" >> a >> "2*" >> attr(b_default) >> attr(c_default) >> "}";
struct_reduced_3 = "{" >> "3*" >> attr(a_default) >> attr(b_default) >> attr(c_default) >> "}";
当然,这不是最美丽的方式..