提升::精神.将(名称)(描述)文本解析到地图
boost::spirit. Parsing (name) (description) text to a map
>我目前正在以下一个格式解析文本文件中的数据库模式:
(table_name) (table_description)
元素之间的分隔符是双回车(nn
)我需要将其解析为地图,使用 boost::spirit
进行解析。
问题是table_description
也可以包含双重回车(nn
)。
table_name有严格的格式,这是*qi::char_("a-z0-9_")
.table_description
可以包含任何字符,但以后始终从大写字母开始。
任何想法如何为这个解析器创建语法?
这与Spirit
docs:使用Spirit.Qi解析键值对列表(2009年11月15日)中的文章非常相似。
我能想到的最直接的语法依赖于括号:
start = pair % "nn";
parenthesized = '(' > *(char_ - ')') > ')';
pair = parenthesized >> "nn" >> parenthesized;
您当然可以增强它以要求您需要的表名和描述(例如以大写字母开头)的确切语法,但以上是用于说明的。
唯一的/nifty/位是:
- 使用
char_ - ')'
_greedily匹配括号内的任何内容(请注意,这尚不支持嵌套的括号集) - 使用融合适配器对 std::p air 直接解析为 std::p air
- 使用
qi::blank
(不是qi::space
)船长以避免忽略换行
符
下面是一个完整的示例:
//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
namespace qi = boost::spirit::qi;
namespace karma = boost::spirit::karma;
typedef std::map<std::string, std::string> map_t;
template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, map_t(), Skipper>
{
parser() : parser::base_type(start)
{
using namespace qi;
// using phx::bind; using phx::ref; using phx::val;
start = pair % "nn";
pair = parenthesized >> "nn" >> parenthesized;
parenthesized = '(' > *(char_ - ')') > ')';
BOOST_SPIRIT_DEBUG_NODE(parenthesized);
BOOST_SPIRIT_DEBUG_NODE(pair);
BOOST_SPIRIT_DEBUG_NODE(start);
}
private:
qi::rule<It, std::string(), Skipper > parenthesized;
qi::rule<It, std::pair<std::string, std::string>(), Skipper> pair;
qi::rule<It, std::map <std::string, std::string>(), Skipper> start;
};
template <typename C, typename Skipper>
bool doParse(const C& input, const Skipper& skipper)
{
auto f(std::begin(input)), l(std::end(input));
parser<decltype(f), Skipper> p;
map_t data;
try
{
bool ok = qi::phrase_parse(f,l,p,skipper,data);
if (ok)
{
std::cout << "parse successn";
std::cout << "data: " << karma::format(
(karma::auto_ << ": "" << karma::auto_ << """) % karma::eol,
data) << "n";
}
else std::cerr << "parse failed: '" << std::string(f,l) << "'n";
if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'n";
return ok;
} catch(const qi::expectation_failure<decltype(f)>& e)
{
std::string frag(e.first, e.last);
std::cerr << e.what() << "'" << frag << "'n";
}
return false;
}
template <typename C>
bool doParse(const C& input)
{
return doParse(input, qi::blank);
}
int main()
{
const std::string input = "(table_name)nn(table_description)nn(other_table)nn(othernndescription)";
bool ok = doParse(input);
return ok? 0 : 255;
}
测试输出:
parse success
data: other_table: "other
description"
table_name: "table_description"
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 为什么不;名字在地图上是按顺序排列的吗
- 如何将内容数组写入文本文件?
- 基于多个条件处理地图中的所有元素
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 在C++中将矢量转换为嵌套地图
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何使用地图对文本进行编码?
- 提升::精神.将(名称)(描述)文本解析到地图