[英]Boost::Spirit : Basic “logical and” expression parsing
我正在嘗試學習Boost :: Spirit的基礎知識,但進展不太順利。 我正在嘗試解析一個用c ++語法編寫的簡單“邏輯和”表達式。 由於某種原因,我無法跳過該空間。
到目前為止,這是我的代碼
template <typename Iterator>
struct boolGrammar : public qi::grammar<Iterator, bool>
{
public:
boolGrammar() : boolGrammar::base_type(expression)
{
andExpr = (qi::lit(L"1") >> qi::lit(L"&&") >> qi::lit(L"1"))[qi::_val = true];
}
qi::rule<Iterator, bool> andExpr;
};
bool conditionEvalAndParse(std::wstring condition)
{
boolGrammar<std::wstring::iterator> g;
bool result = false;
std::wstring::iterator it = condition.begin();
bool parseResult = qi::phrase_parse(it, condition.end(), g, boost::spirit::standard_wide::space , result);
if (parseResult) {
return result;
}
else
{
std::wcout << L"Failed to parse condition " << condition << L". The following wasn't parsed : " << std::wstring(condition, it - condition.begin(), std::wstring::npos) << std::endl;
return false;
}
}
在測試代碼中,我致電:
conditionEvalAndParse(L"1&&1");
conditionEvalAndParse(L"1 && 1");
確實,我得到了一個可愛的控制台輸出:
"Failed to parse condition 1 && 1. The following wasn't parsed : 1 && 1"
有人在乎指出新手的錯誤嗎? :)
通過將船長添加為模板參數來解決,如@Richard Hodges先前的問題所示:
template <typename Iterator, typename Skipper = boost::spirit::standard_wide::space_type>
struct boolGrammar : public qi::grammar<Iterator, bool, Skipper>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.