gpt4 book ai didi

c++ - 获取 boost spirit 语法中的当前行

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:07:45 27 4
gpt4 key购买 nike

我正在尝试使用 boost spirit 获取我正在解析的文件的当前行。我创建了一个语法类和我的结构来解析我的命令。我还想跟踪在哪一行找到命令并将其解析到我的结构中。我已将我的 istream 文件迭代器包装在 multi_pass 迭代器中,然后将其包装在 boost::spirit::classic::position_iterator2 中。在我的语法规则中,我将如何获得迭代器的当前位置,或者这是不可能的?

更新:它与那个问题类似,但我只需要能够对所有已处理的行进行计数。我不需要执行解决方案中完成的所有额外缓冲。

最佳答案

Update: It is similar to that problem but I just need to be able to keep a count of all the lines processed. I don't need to do all of the extra buffering that was done in the solution.

记录所有已处理行的数量与“获取当前行”并不完全相同。

简单的拿

如果这是您需要的,只需在解析后检查它:

Live On Wandbox

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_line_pos_iterator.hpp>
#include <fstream>
#include <set>
namespace qi = boost::spirit::qi;

int main() {
using It = boost::spirit::istream_iterator;

std::ifstream ifs("main.cpp");
boost::spirit::line_pos_iterator<It> f(It(ifs >> std::noskipws)), l;

std::set<std::string> words;
if (qi::phrase_parse(f, l, *qi::lexeme[+qi::graph], qi::space, words)) {
std::cout << "Parsed " << words.size() << " words";
if (!words.empty())
std::cout << " (from '" << *words.begin() << "' to '" << *words.rbegin() << "')";
std::cout << "\nLast line processed: " << boost::spirit::get_line(f) << "\n";
}
}

打印

Parsed 50 words (from '"' to '}')
Last line processed: 22

稍微复杂一点

如果您说“不,等等,我真的确实想在/解析/时获取当前行”。真正的全月在这里:

这是使用 iter_pos 完全精简的版本:

Live On Wandbox

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/support_line_pos_iterator.hpp>
#include <boost/spirit/repository/include/qi_iter_pos.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <fstream>
#include <map>

namespace qi = boost::spirit::qi;
namespace qr = boost::spirit::repository::qi;

using LineNum = size_t;

struct line_number_f {
template <typename It> LineNum operator()(It it) const { return get_line(it); }
};

static boost::phoenix::function<line_number_f> line_number_;

int main() {
using Underlying = boost::spirit::istream_iterator;
using It = boost::spirit::line_pos_iterator<Underlying>;
qi::rule<It, LineNum()> line_no = qr::iter_pos [ qi::_val = line_number_(qi::_1) ];

std::ifstream ifs("main.cpp");
It f(Underlying{ifs >> std::noskipws}), l;

std::multimap<LineNum, std::string> words;

if (qi::phrase_parse(f, l, +(line_no >> qi::lexeme[+qi::graph]), qi::space, words)) {
std::cout << "Parsed " << words.size() << " words.\n";

if (!words.empty()) {
auto& first = *words.begin();
std::cout << "First word: '" << first.second << "' (in line " << first.first << ")\n";
auto& last = *words.rbegin();
std::cout << "Last word: '" << last.second << "' (in line " << last.first << ")\n";
}

std::cout << "Line 20 contains:\n";
auto p = words.equal_range(20);
for (auto it = p.first; it != p.second; ++it)
std::cout << " - '" << it->second << "'\n";

}
}

打印:

Parsed 166 words.
First word: '#include' (in line 1)
Last word: '}' (in line 46)
Line 20 contains:
- 'int'
- 'main()'
- '{'

关于c++ - 获取 boost spirit 语法中的当前行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46981589/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com