我有以下語法,它按預期工作。Boost中的複合語法:: Spirit
struct query_term {
std::string term;
bool is_tag;
query_term(const std::string &a, bool tag = false): term(a), is_tag(tag) { } };
template<typename Iterator> struct query_grammar: grammar<Iterator, std::vector<query_term>(), space_type> {
query_grammar():
query_grammar::base_type(query) {
word %= +alnum;
tag = (omit[word >> ':'] >> word[_val = phoenix::construct<query_term>(_1, true)]);
non_tag = word[_val = phoenix::construct<query_term>(_1, false)];
query = (
(omit[word >> ':'] >> word[push_back(_val, phoenix::construct<query_term>(_1, true))])
|
word[push_back(_val,
phoenix::construct<query_term>(_1))
]
) % space;
};
qi::rule<Iterator, std::string(), space_type> word;
qi::rule<Iterator, query_term, space_type> tag;
qi::rule<Iterator, query_term, space_type> non_tag;
qi::rule<Iterator, std::vector<query_term>(), space_type> query; };
但是,當我與
query = (
tag[phoenix::push_back(_val, _1)]
|
word[push_back(_val,
phoenix::construct<query_term>(_1))
]
) % space;
代碼不編譯替代查詢。基本上我試圖將語法分解成可以在更大的語法中重用的組件。當解析單詞或標籤時,創建一個帶有適當標誌的query_term對象,其標籤爲標籤和單詞規則。在查詢規則中重用這些屬性。
在以前的版本中,標記和單詞規則在查詢語法中內聯。
我不知道我在這裏錯過了什麼。任何幫助將不勝感激。
供參考:這不是最終的代碼。在生產代碼中使用它之前,我正在嘗試使用這些規則。
感謝名單, - baliga
IMO,代碼不漂亮。嘗試整理它,你可能會看到問題出在哪裏。 – 2012-03-24 09:57:44
代碼本身很漂亮,Boost :: Spirit有這個運算符超負荷的語法,你必須忍受。 – buc 2012-03-24 11:23:12
@AshBurlaczenko Thanx建議。有些提示會對此有所幫助。 – 2012-03-24 19:42:59