boost::spirit::qi 和乱序变量

发布于 2024-10-13 19:15:31 字数 804 浏览 3 评论 0原文

我正在写一个词典分析器。它接受一个英文字符串,并将其转换为一组纬度/经度坐标。它有点像谷歌地球。

无论如何,我已经编写了符号表和语法,并且它正在愉快地解析格式化数据。

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

例如: {"North", 23.59, "East", -30.82}

这是我的语法:

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

其中纬度和经度是从速记罗盘方向映射到字符串的符号表(例如“e”到“East”)

所以,对于我的问题:

我想在我的语法中添加以下规则,其中纬度和经度符号位于相反的位置:

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

此解析,但 degLat_ 和 degLong_ 值不会与字符串值一起反转。它们只是直接解析到结构中,而不考虑字符串标签。

当要解析的数据不连续时,如何构建结构体(或 boost::fusion 向量)?

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_;
}

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

Here is my grammar:

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

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

So, on to my question:

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_ )

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.

How do I build a struct (or boost::fusion vector) when the data to be parsed is not sequential?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪荡不羁 2024-10-20 19:15:32

你有几种可能性。最简单的方法是按照所需的顺序将您的结构调整为 Fusion 序列:(

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

是的,调整的顺序不必与原始结构中成员的顺序匹配,您甚至可以省略成员或复制它们)。如果您想要以一种特定的顺序解析成员,那么这种方法就很好用。如果

您在同一程序中需要不同的顺序,则可能需要利用类似的适应机制,但它还允许为适应的结构体指定名称:

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

其中 reversed_LatLongDegrees 是在 Spirit 语法中用作属性的数据类型:

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

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

这允许同时为同一结构创建多个改编。

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_)
);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文