C++?Boost?Spirit進階教程
一、行動
到目前為止,本章中的示例只檢測到兩件事:解析器是否成功以及解析器在哪里結(jié)束。但是,解析器通常以某種方式處理數(shù)據(jù),正如您將在下一個示例中看到的那樣。
Example11.9.Linking actions with parsers
#include <boost/spirit/include/qi.hpp> #include <string> #include <iostream> using namespace boost::spirit; int main() { std::string s; std::getline(std::cin, s); auto it = s.begin(); bool match = qi::phrase_parse(it, s.end(), qi::int_[([](int i){ std::cout << i << '\n'; })], ascii::space); std::cout << std::boolalpha << match << '\n'; if (it != s.end()) std::cout << std::string{it, s.end()} << '\n'; }
示例 11.9 使用 boost::spirit::qi::int_ 解析一個整數(shù),然后將該整數(shù)寫入標(biāo)準(zhǔn)輸出。這就是為什么一個動作與 boost::spirit::qi::int_ 相關(guān)聯(lián)。動作是應(yīng)用解析器時調(diào)用的函數(shù)或函數(shù)對象。鏈接是使用運算符 operator[] 完成的,它被 boost::spirit::qi::int_ 和其他解析器重載。示例 11.9 使用 lambda 函數(shù)作為操作,該操作需要一個 int 類型的唯一參數(shù)并將其寫入標(biāo)準(zhǔn)輸出。
如果您啟動示例 11.9 并輸入一個數(shù)字,則會顯示該數(shù)字。
傳遞給動作的參數(shù)類型取決于解析器。例如, boost::spirit::qi::int_ 轉(zhuǎn)發(fā)一個 int 值,而 boost::spirit::qi::float_ 傳遞一個 float 值。
Example11.10.Boost.Spirit with Boost.Phoenix
#define BOOST_SPIRIT_USE_PHOENIX_V3 #include <boost/spirit/include/qi.hpp> #include <boost/phoenix/phoenix.hpp> #include <string> #include <iostream> using namespace boost::spirit; using boost::phoenix::ref; int main() { std::string s; std::getline(std::cin, s); auto it = s.begin(); int i; bool match = qi::phrase_parse(it, s.end(), qi::int_[ref(i) = qi::_1], ascii::space); std::cout << std::boolalpha << match << '\n'; if (match) std::cout << i << '\n'; if (it != s.end()) std::cout << std::string{it, s.end()} << '\n'; }
示例 11.10 使用 Boost.Phoenix 將使用 boost::spirit::qi::int_ 解析的 int 值存儲在 i 中。如果 boost::spirit::qi::phrase_parse() 返回 true,則將 i 寫入標(biāo)準(zhǔn)輸出。
這個例子在包含來自 Boost.Spirit 的頭文件之前定義了宏 BOOST_SPIRIT_USE_PHOENIX_V3。此宏選擇 Boost.Phoenix 的第三個和當(dāng)前版本。這很重要,因為 Boost.Phoenix 是從 Boost.Spirit 派生的,而 Boost.Spirit 包含 Boost.Phoenix 的第二個版本。如果未定義 BOOST_SPIRIT_USE_PHOENIX_V3,則 Boost.Phoenix 的第二個版本將包含在 Boost.Spirit 頭文件中,第三個版本將包含在 boost/phoenix/phoenix.hpp 中。不同的版本會導(dǎo)致編譯器錯誤。
請注意如何詳細定義 lambda 函數(shù)。 boost::phoenix::ref() 創(chuàng)建對變量 i 的引用。但是,占位符 _1 不是來自 Boost.Phoenix,而是來自 Boost.Spirit。這很重要,因為 boost::spirit::qi::_1 提供了對具有通常預(yù)期類型的??解析值的訪問——示例 11.10 中的 int。如果 lambda 函數(shù)使用 boost::phoenix::placeholders::arg1,編譯器會報錯,因為 boost::phoenix::placeholders::arg1 不代表 int;它將基于 Boost.Spirit 中的另一種類型,并且需要提取 int 值。
二、屬性
操作是處理解析值的一種選擇。另一種選擇是將對象傳遞給 boost::spirit::qi::parse() 或 boost::spirit::qi::phrase_parse() 用于存儲解析值。這些對象稱為屬性。它們的類型必須與解析器的類型相匹配。
您已經(jīng)在上一節(jié)中使用過屬性。傳遞給動作的參數(shù)也是屬性。每個解析器都有一個屬性。例如,解析器 boost::spirit::qi::int_ 具有 int 類型的屬性。在以下示例中,屬性不會作為參數(shù)傳遞給函數(shù)。相反,解析后的值存儲在屬性中,可以在 boost::spirit::qi::parse() 或 boost::spirit::qi::phrase_parse() 返回后處理。
Example11.11.Storing anint
value in an attribute
#include <boost/spirit/include/qi.hpp> #include <string> #include <iostream> using namespace boost::spirit; int main() { std::string s; std::getline(std::cin, s); auto it = s.begin(); int i; if (qi::phrase_parse(it, s.end(), qi::int_, ascii::space, i)) std::cout << i << '\n'; }
示例 11.11 使用解析器 boost::spirit::qi::int_。解析后的 int 值存儲在變量 i 中。 i 作為另一個參數(shù)傳遞給 boost::spirit::qi::phrase_parse(),因此成為解析器 boost::spirit::qi::int_ 的一個屬性。 如果您啟動示例 11.11 并輸入一個數(shù)字,該數(shù)字將被寫入標(biāo)準(zhǔn)輸出流。
Example11.12.Storing severalint
values in an attribute
#include <boost/spirit/include/qi.hpp> #include <string> #include <vector> #include <iterator> #include <algorithm> #include <iostream> using namespace boost::spirit; int main() { std::string s; std::getline(std::cin, s); auto it = s.begin(); std::vector<int> v; if (qi::phrase_parse(it, s.end(), qi::int_ % ',', ascii::space, v)) { std::ostream_iterator<int> out{std::cout, ";"}; std::copy(v.begin(), v.end(), out); } }
示例 11.12 使用了一個用 qi::int_ % ',' 定義的解析器。解析器接受由逗號分隔的任意數(shù)量的整數(shù)。像往常一樣忽略空格。
因為解析器可以返回多個 int 值,所以屬性的類型必須支持存儲多個 int 值。該示例傳遞一個向量。如果您開始該示例并輸入多個以逗號分隔的整數(shù),則這些整數(shù)將寫入以分號分隔的標(biāo)準(zhǔn)輸出流。
除了向量,您還可以傳遞其他類型的容器,例如 std::list。Boost.Spirit 文檔描述了哪些屬性類型必須與哪些運算符一起使用。
到此這篇關(guān)于C++ Boost Spirit進階教程的文章就介紹到這了,更多相關(guān)C++ Boost Spirit內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Qt編寫地圖之實現(xiàn)經(jīng)緯度坐標(biāo)糾偏
地圖應(yīng)用中都涉及到一個問題就是坐標(biāo)糾偏的問題,這個問題的是因為根據(jù)地方規(guī)則保密性要求不允許地圖廠商使用標(biāo)準(zhǔn)的GPS坐標(biāo),而是要用國家定義的偏移標(biāo)準(zhǔn)。本文將詳細講解如何在Qt中實現(xiàn)經(jīng)緯度坐標(biāo)糾偏,需要的可以參考一下2022-03-03