正如您可能在直播中看到的一樣,我已逐漸修正了語法。
那裏有很多「你喝醉了」的時刻,所以我不能確定我能在這裏捕捉到散文的基本步驟。無論如何,有a look at the recorded stream¹。
一些實質性的筆記(忽略隨機拼寫錯誤和清楚地混淆代碼):
可選:
- 使用
as_vector
破解一個快速的方法來打印結果使用BOOST_SPIRIT_DEBUG*
調試規則
- 我已經展示了該技術使用inherited arguments這樣你就可以重複使用
entry
規則爲entry1
和entry2
未顯示:
- 使用船長爲空白不敏感
- 使用
no_case
爲...不區分大小寫
- 與船長:的
lexeme
提防避免匹配的關鍵字跨跳過輸入字符
工作碼,作爲一個SSCCE:
Live On Coliru
#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <vector>
namespace std {
template <typename T>
static ostream& operator<<(ostream& os, vector<T> const& v) {
os << "vector{ ";
for(auto& e : v)
os << "'" << e << "', ";
return os << "}";
}
template <typename T, typename U>
static ostream& operator<<(ostream& os, pair<T,U> const& p) {
return os << "pair{ '" << p.first << "', '" << p.second << "' }";
}
}
#include <boost/fusion/adapted/struct.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
struct Entry
{
uint32_t id;
std::vector<std::string> entry1;
std::vector<std::string> entry2;
bool useFactor;
std::pair<int, int> factor;
};
BOOST_FUSION_ADAPT_STRUCT(Entry, id, entry1, entry2, useFactor, factor)
template<typename It>
struct testParser : qi::grammar<It, Entry()>
{
testParser() : testParser::base_type(start)
{
using namespace qi;
id = "id=" >> int_;
entry = lit(_r1) >> ('[' >> +~char_("],") % ',' >> ']');
factor = "factor=" >> ('[' >> int_ >> ',' >> int_ >> ']');
start =
id >> ';'
>> entry(+"entry1=") >> ';'
>> entry(+"entry2=") >> ';'
>> attr(true)
>> (factor | attr(std::pair<int,int>{1,1}))
>> '!';
BOOST_SPIRIT_DEBUG_NODES((start)(entry)(id)(factor))
#if 0
#endif
}
private:
qi::rule<It, Entry()> start;
qi::rule<It, int()> id;
qi::rule<It, std::vector<std::string>(std::string)> entry;
qi::rule<It, std::pair<int,int>()> factor;
};
int main() {
std::string const input = "id=1;entry1=[A,B,D];entry2=[bla,blubb];factor=[1,5]!";
using It = std::string::const_iterator;
testParser<It> g;
It f = input.begin(), l = input.end();
Entry entry;
bool ok = qi::parse(f, l, g, entry);
std::cout << std::boolalpha;
if (ok) {
std::cout << "Parsed: " << boost::fusion::as_vector(entry) << "\n";
} else {
std::cout << "Parse failed\n";
}
if (f!=l)
std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}
打印:
<start>
<try>id=1;entry1=[A,B,D];</try>
<id>
<try>id=1;entry1=[A,B,D];</try>
<success>;entry1=[A,B,D];entr</success>
<attributes>[1]</attributes>
</id>
<entry>
<try>entry1=[A,B,D];entry</try>
<success>;entry2=[bla,blubb];</success>
<attributes>[[[A], [B], [D]], [e, n, t, r, y, 1, =]]</attributes>
</entry>
<entry>
<try>entry2=[bla,blubb];f</try>
<success>;factor=[1,5]!</success>
<attributes>[[[b, l, a], [b, l, u, b, b]], [e, n, t, r, y, 2, =]]</attributes>
</entry>
<factor>
<try>factor=[1,5]!</try>
<success>!</success>
<attributes>[[1, 5]]</attributes>
</factor>
<success></success>
<attributes>[[1, [[A], [B], [D]], [[b, l, a], [b, l, u, b, b]], 1, [1, 5]]]</attributes>
</start>
Parsed: (1 vector{ 'A', 'B', 'D', } vector{ 'bla', 'blubb', } true pair{ '1', '5' })
¹(我錯過了第一部分,我在那裏固定頭,平衡括號並添加括號來構造等等o.O)。
你能開始發佈有效的代碼嗎?對樣品進行癱瘓確實沒有意義。您可以複製/粘貼。請參閱http://sscce.org。現在:https://www.livecoding.tv/sehe/ – sehe