提振精神:: ::气乱序列变量 [英] boost::spirit::qi and out-of-sequence variables

查看:112
本文介绍了提振精神:: ::气乱序列变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个lexigraphical分析仪。它需要一个英语字符串,并将其转换成一组纬度/经度坐标的。这是一个有点像谷歌地球。

I'm writing a lexigraphical analyser. It takes an English string, and converts it into a set of latitude/longitude co-ordinates. It's a bit like Google Earth.

不管怎样,我写我的符号表和语法,它高兴地解析格式的数据。

Anyway, I've written my symbol tables and grammar, and it's happily parsing formatted data.

struct LatLongDegrees
{
 std::string  dirLat_;
 double   degLat_;
 std::string     dirLong_;
 double   degLong_;
}

例如:{北,23.59,东方,-30.82}

For example: {"North", 23.59, "East", -30.82}

下面是我的语法:

 basic =(latitude >> ' ' >> double_ >> ' ' >> longitude >> ' ' >> double_);

在哪里,从速记指南针方向映射到字符串纬度和经度符号表(如E到东方)

Where latitude and longitude are symbol tables that map from shorthand compass directions to strings (eg "e" to "East")

所以,关于我的问题:

欲以下规则添加到我的语法,其中的经纬度符号是相反的位置:

I want to add the following rule to my grammar, where the latitude and longitude symbols are in the opposite positions:

reversed = (longitude  >> ' ' >> double_ >> ' ' >> latitude >> double_ )

这解析,但degLat_和degLong_值与字符串值一起逆转。他们只是直接解析成的结构,没有字符串标签方面。

This parses, BUT the degLat_ and degLong_ values are not reversed along with string values. They are simply parsed directly into the struct, without regard for the string labels.

如何建立一个结构(或升压::融合表达载体)时,要分析数据不连续?

推荐答案

您有几种可能性。最简单的是你的结构改编成融合序列中的要求的顺序:

You have several possibilities. The easiest is to adapt your struct into a Fusion sequence in the required order:

BOOST_FUSION_ADAPT_STRUCT(
    LatLongDegrees,
    (std::string, dirLong_)
    (double, degLong_)
    (std::string, dirLat_)
    (double, degLat_)
);

(是的,适应的顺序并不具有对成员的顺序在原结构匹配,你甚至可以离开了成员或复制它们)。如果您有想给您的成员分析在一个特定的顺序这工作得很好。

(yes, the order of adaptation does not have to match the order of the members in the original struct, you can even leave out members or duplicate them). This works fine if you have one particular order you want to parse your members in.

如果您在同一程序中需要不同的排序,你可能想利用类似的适应机制,但还允许给一个名称调整结构:

If you need different orderings in the same program, you might want to utilize a similar adaptation mechanism, but which additionally allows to give a name to the adapted struct:

BOOST_FUSION_ADAPT_STRUCT_NAME(
    LatLongDegrees, reversed_LatLongDegrees,
    (std::string, dirLong_)
    (double, degLong_)
    (std::string, dirLat_)
    (double, degLat_)
);

其中, reversed_LatLongDegrees 是用来作为你的精神语法属性的数据类型:

where reversed_LatLongDegrees is the data type used as the attribute in your Spirit grammar:

rule <Iterator, reversed_LatLongDegrees()> reversed;
reversed = longitude  >> ' ' >> double_ >> ' ' >> latitude >> double_;

LatLongDegrees data;
parse(begin, end, reversed, data);

这允许对在同一时间在同一结构创建若干修改。

This allows to create several adaptations for the same struct at the same time.

这篇关于提振精神:: ::气乱序列变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆