Boost Spirit Parser用三个字符串的矢量编译成一个结构,自适应不工作

Boost Spirit Parser with a vector of three strings compiling into a struct, adapt not working

本文关键字:结构 一个 工作 自适应 编译 Parser Spirit 三个 Boost 字符串      更新时间:2023-10-16

我是一名学生,需要用C++和Boost库编写一个Parser。

因此,我在QI中写了一个语法,因为我需要解析成一个结构。到目前为止,一切都很好。

我将给你一些示例代码。我认为这比把整个程序写下来容易。

说明:因此,首先我们获取一个txt文件并读取它,然后解析器对其进行检查,说"解析可以!"并解析到结构中。我们的输出是控制台中的结构。

现在,对于一些代码示例来说,这很好。在这里你可以看到Boost Spirit QI:中的语法

subject %= lexeme[lit("Fach: ") >> +(char_("a-zA-Z"))   >> lit("n")]; //works!
dozent %= lexeme[lit("Dozent: ") >> +(char_("a-zA-Z")) >> lit("n")];
date %= lexeme[lit("Datum: ") >> digit >> digit >> lit("-") >> digit >> digit >> lit("-") >> digit >> digit >> digit >> digit >> lit("n")];
count %= lexeme[lit("Anzahl: ") >> +digit >> lit("n")];
points %= lexeme[+digit >> lit("t")];
mark %= lexeme[digit >> lit("n")];
matnumber %= lexeme[(digit >> digit >> digit >> punct >> digit >> digit >> digit) >> lit("t")];
student %= matnumber >> points >> mark;
start %=  subject >> dozent >> date >> count >> student;

这很好,学生的规则带来了我们有一个由三部分组成的元素的问题。匹配号、点数和标记。你可以想象我的意思,这里是我们试图解析的TXT文件:

Subject: Physics
Dozent: Wayne
Datum: 20-10-2014
Anzahl: 20
729.888 33  5
185.363 35  5

最后两行是规则学生。在txt文件中,我们有超过这两行的内容。

我们可以把这些行作为"学生",我们在结构中用typedef:写了一个向量

typedef boost::fusion::vector<string, string, string> student_t;

然后我们将在我们的结构中使用它:

struct klausur
{
string str_subject;
string str_dozent;
string str_date;
string count;
string matr_nr;
string points;
string mark;
string ende;
student_t student;
void ToString()
{
cout << "Struct.Fach: " << str_subject << endl;
cout << "Struct.Dozent: " << str_dozent << endl;
cout << "Struct.Datum: " << str_date << endl;
cout << "Struct.Anzahl: " << count << endl;
cout << "Struct.Mat_Nr: " << matr_nr << endl;
cout << "Struct.Punkte: " << points << endl;
cout << "Struct.Note: " << mark << endl;
cout << "Struct.Student<0>: " << vec::at_c<0>(student); 
cout << "Struct.Student<1>: " << vec::at_c<1>(student);
cout << "Struct.Student<2>: " << vec::at_c<2>(student);
}
};

然后我们的BOOST_ADAPT_STRUCT是这样的:

BOOST_FUSION_ADAPT_STRUCT(
client::klausur,
(string, str_subject)
(string, str_dozent)
(string, str_date)
(string, count)
(string, matr_nr)
(string, points)
(string, mark)
(student_t, student)

)

你看,我们下面有typedef。

然后我们在语法中有我们的规则。

qi::rule<Iterator, string(), ascii::space_type> subject;
qi::rule<Iterator, string(), ascii::space_type> dozent;
qi::rule<Iterator, string(), ascii::space_type> date;
qi::rule<Iterator, string(), ascii::space_type> count;
qi::rule<Iterator, string(), ascii::space_type> matnumber;
qi::rule<Iterator, string(), ascii::space_type> points;
qi::rule<Iterator, string(), ascii::space_type> mark;
qi::rule<Iterator, boost::fusion::vector<boost::fusion::vector<std::string, std::string, std::string> >()> student; 

希望我们的项目还有最后一个问题。。。

我们不知道qi:规则需要哪种数据类型BOOST_ADAPT。。。所有其他点都是字符串,但不知道如何实现我们创建的向量。

所有其他规则都运行良好,并且稍后在结构中,只有向量会产生问题。

有人知道吗?如果你需要,我可以上传更多的文件和代码片段,但我仍然认为这可能只是一个我看不到的小问题。我四处寻找许多助推话题,但没有找到合适的话题。

我必须补充一点信息,我只是一个初学者,所以也许我没有正确解释所有事情,并且。。。是 啊希望你能理解。而且我的英语不是最好的。。。

提前感谢您的帮助。

William

Spirit是一个语法分析器生成器。您似乎并不真正解析任何内容(您只是"提取"字符序列,这更像是标记化)。

我会这么做:

  • 使用正确的数据类型
  • 使用blank跳过(不包括eol)
  • eol的期望放在正确的位置
  • 把单词放在正确的位置
  • 使date_t成为自己的类型
  • 使student_t成为自己的类型
  • FIX使用std::vector<student_t>()而不是~fusion::vector<student_t>()的规则(这是一个错误)
  • 使用operator<<进行打印
  • 使用repeat(n) [ student >> eol ]解析预期的学生行数
  • 使用qi::locals将预期的学生人数实际传递给repeat()

Coliru

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <iostream>
#include <iomanip>
namespace qi = boost::spirit::qi;
namespace vec = boost::fusion;
struct student_t {
std::string matr_nr;
unsigned    points;
int         mark;
};
struct date_t {
unsigned dd, mm, yyyy;
friend std::ostream& operator<<(std::ostream& os, date_t const& d) {
std::ostream local(os.rdbuf());
local << std::setw(2) << std::setfill('0') << d.dd <<
"-" << std::setw(2) << std::setfill('0') << d.mm <<
"-" << std::setw(4) << std::setfill('0') << d.yyyy;
return os;
}
};
BOOST_FUSION_ADAPT_STRUCT(student_t,
(std::string,matr_nr)(unsigned,points)(int,mark))
BOOST_FUSION_ADAPT_STRUCT(date_t,
(unsigned,dd)(unsigned,mm)(unsigned,yyyy))
struct klausur {
std::string str_subject;
std::string str_dozent;
date_t date;
unsigned count;
std::vector<student_t>   students;
friend std::ostream& operator<<(std::ostream& os, klausur const& k)
{
os << "Fach: "   << k.str_subject << 'n';
os << "Dozent: " << k.str_dozent  << 'n';
os << "Datum: "  << k.date        << 'n';
os << "Anzahl: " << k.count       << 'n';
for (auto& s : k.students) {
os << "Mat_Nr: " << s.matr_nr << 'n';
os << "Punkte: " << s.points  << 'n';
os << "Note: "   << s.mark    << 'n';
}
return os;
}
};
BOOST_FUSION_ADAPT_STRUCT(klausur,
(std::string                     , str_subject)
(std::string                     , str_dozent)
(date_t                          , date)
(unsigned                        , count)
(std::vector<student_t>          , students)
)
template <typename Iterator, typename Skipper = qi::ascii::blank_type>
struct grammar : qi::grammar<Iterator, klausur(), Skipper> {
grammar() : grammar::base_type(start) {
using namespace qi;
subject   = "Fach:"   >> lexeme [ +~char_('n') ] >> eol;
dozent    = "Dozent:" >> lexeme [ +~char_('n') ] >> eol;
date      = "Datum:"  >> lexeme [uint_ >> '-' >> uint_ >> '-' >> uint_] >> eol;
count     = "Anzahl:" >> uint_ >> eol;
points    = uint_;
mark      = int_parser<int, 10, 1, 1>(); // single base-10 digit
// no clue about this format; what is it? Just a real number?
matnumber = lexeme[digit >> digit >> digit >> punct >> digit >> digit >> digit];
student   = matnumber >> points >> mark;
_a_type expected;
klausur_ %= subject
>> dozent
>> date
>> count            [ expected = _1 ]
>> repeat(expected) [ student >> (eol|eoi) ]
;
start     = klausur_;
BOOST_SPIRIT_DEBUG_NODES((start)(klausur_)(student)(matnumber)(mark)(points)(count)(date)(dozent)(subject))
}
private:
qi::rule<Iterator, klausur(), Skipper> start;
qi::rule<Iterator, klausur(), Skipper, qi::locals<unsigned> > klausur_;
qi::rule<Iterator, std::string()    , Skipper> subject;
qi::rule<Iterator, std::string()    , Skipper> dozent;
qi::rule<Iterator, date_t(),          Skipper> date;
qi::rule<Iterator, unsigned()       , Skipper> count;
qi::rule<Iterator, std::string()    , Skipper> matnumber;
qi::rule<Iterator, unsigned()       , Skipper> points;
qi::rule<Iterator, int()            , Skipper> mark;
qi::rule<Iterator, student_t()      , Skipper> student;
};
int main() {
using It = std::string::const_iterator;
std::string const input =
R"(Fach: Physics
Dozent: Wayne
Datum: 20-10-2014
Anzahl: 2
729.888 33  5
185.363 35  5)";
It f = input.begin(), l = input.end();
grammar<It> g;
klausur k;
bool ok = qi::phrase_parse(f, l, g, qi::ascii::blank, k);
if (ok) {
std::cout << "Parse successn";
std::cout << k;
} else {
std::cout << "Parse failedn";
}
if (f!=l) {
std::cout << "Remaining input: '" << std::string(f,l) << "'n";
}
}

输出:

Parse success
Fach: Physics
Dozent: Wayne
Datum: 20-10-2014
Anzahl: 2
Mat_Nr: 729.888
Punkte: 33
Note: 5
Mat_Nr: 185.363
Punkte: 35
Note: 5

以及72行调试输出:

<start>
<try>Fach: PhysicsnDozent</try>
<klausur_>
<try>Fach: PhysicsnDozent</try>
<subject>
<try>Fach: PhysicsnDozent</try>
<success>Dozent: WaynenDatum:</success>
<attributes>[[P, h, y, s, i, c, s]]</attributes>
</subject>
<dozent>
<try>Dozent: WaynenDatum:</try>
<success>Datum: 20-10-2014nAn</success>
<attributes>[[W, a, y, n, e]]</attributes>
</dozent>
<date>
<try>Datum: 20-10-2014nAn</try>
<success>Anzahl: 2n729.888 33</success>
<attributes>[[20, 10, 2014]]</attributes>
</date>
<count>
<try>Anzahl: 2n729.888 33</try>
<success>729.888 33  5n185.36</success>
<attributes>[2]</attributes>
</count>
<student>
<try>729.888 33  5n185.36</try>
<matnumber>
<try>729.888 33  5n185.36</try>
<success> 33  5n185.363 35  5</success>
<attributes>[[7, 2, 9, ., 8, 8, 8]]</attributes>
</matnumber>
<points>
<try> 33  5n185.363 35  5</try>
<success>  5n185.363 35  5</success>
<attributes>[33]</attributes>
</points>
<mark>
<try>  5n185.363 35  5</try>
<success>n185.363 35  5</success>
<attributes>[5]</attributes>
</mark>
<success>n185.363 35  5</success>
<attributes>[[[7, 2, 9, ., 8, 8, 8], 33, 5]]</attributes>
</student>
<student>
<try>185.363 35  5</try>
<matnumber>
<try>185.363 35  5</try>
<success> 35  5</success>
<attributes>[[1, 8, 5, ., 3, 6, 3]]</attributes>
</matnumber>
<points>
<try> 35  5</try>
<success>  5</success>
<attributes>[35]</attributes>
</points>
<mark>
<try>  5</try>
<success></success>
<attributes>[5]</attributes>
</mark>
<success></success>
<attributes>[[[1, 8, 5, ., 3, 6, 3], 35, 5]]</attributes>
</student>
<success></success>
<attributes>[[[P, h, y, s, i, c, s], [W, a, y, n, e], [20, 10, 2014], 2, [[[7, 2, 9, ., 8, 8, 8], 33, 5], [[1, 8, 5, ., 3, 6, 3], 35, 5]]]]</attributes><locals>(2)</locals>
</klausur_>
<success></success>
<attributes>[[[P, h, y, s, i, c, s], [W, a, y, n, e], [20, 10, 2014], 2, [[[7, 2, 9, ., 8, 8, 8], 33, 5], [[1, 8, 5, ., 3, 6, 3], 35, 5]]]]</attributes>
</start>