[英]Boost Spirit Parser with a vector of three strings compiling into a struct, adapt not working
我是一名學生,需要使用Boost-Library用C ++編寫解析器。
因此我在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;
很好,學生的規則帶來了一個問題,我們有一個包含三個部分的元素。 Matnumber,點和標記。 您可以想象我的意思,這里是我們嘗試解析的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;
我們的項目可能有最后的問題...
我們不知道BOOST_ADAPT ... qi:rule需要哪種數據類型可以正常工作。 所有其他點都是字符串,但是不知道如何實現我們創建的自己的向量。
所有其他規則都工作正常,並且稍后在結構中,僅矢量產生了問題。
有人對此有想法嗎? 如果需要,我可以上傳更多文件和代碼片段,但我仍然認為這可能只是我看不到的一個小問題。 我到處尋找許多促進話題,但沒有找到正確的東西。
我必須添加我只是一個初學者的信息,所以也許我沒有解釋所有正確的信息,是的。 希望你能理解。 我的英語也不是最好的...
預先感謝您的幫助。
威廉
Spirit是解析器生成器。 您似乎並沒有真正解析任何內容(您只是“提取”字符序列,這更像是標記化)。
我會這樣做:
blank
跳過(不包含eol
) eol
期望放在正確的位置 date_t
為自己的類型 student_t
為自己的類型 std::vector<student_t>()
而不是fusion::vector<student_t>()
(這是一個錯誤) operator<<
進行打印 repeat(n) [ student >> eol ]
解析學生的預期行數 qi::locals
實際將預期的學生人數傳遞給repeat()
#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 success\n";
std::cout << k;
} else {
std::cout << "Parse failed\n";
}
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: Physics\nDozent</try>
<klausur_>
<try>Fach: Physics\nDozent</try>
<subject>
<try>Fach: Physics\nDozent</try>
<success>Dozent: Wayne\nDatum:</success>
<attributes>[[P, h, y, s, i, c, s]]</attributes>
</subject>
<dozent>
<try>Dozent: Wayne\nDatum:</try>
<success>Datum: 20-10-2014\nAn</success>
<attributes>[[W, a, y, n, e]]</attributes>
</dozent>
<date>
<try>Datum: 20-10-2014\nAn</try>
<success>Anzahl: 2\n729.888 33</success>
<attributes>[[20, 10, 2014]]</attributes>
</date>
<count>
<try>Anzahl: 2\n729.888 33</try>
<success>729.888 33 5\n185.36</success>
<attributes>[2]</attributes>
</count>
<student>
<try>729.888 33 5\n185.36</try>
<matnumber>
<try>729.888 33 5\n185.36</try>
<success> 33 5\n185.363 35 5</success>
<attributes>[[7, 2, 9, ., 8, 8, 8]]</attributes>
</matnumber>
<points>
<try> 33 5\n185.363 35 5</try>
<success> 5\n185.363 35 5</success>
<attributes>[33]</attributes>
</points>
<mark>
<try> 5\n185.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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.