boost::spirit::qi 和乱序变量
我正在写一个词典分析器。它接受一个英文字符串,并将其转换为一组纬度/经度坐标。它有点像谷歌地球。
无论如何,我已经编写了符号表和语法,并且它正在愉快地解析格式化数据。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有几种可能性。最简单的方法是按照所需的顺序将您的结构调整为 Fusion 序列:(
是的,调整的顺序不必与原始结构中成员的顺序匹配,您甚至可以省略成员或复制它们)。如果您想要以一种特定的顺序解析成员,那么这种方法就很好用。如果
您在同一程序中需要不同的顺序,则可能需要利用类似的适应机制,但它还允许为适应的结构体指定名称:
其中
reversed_LatLongDegrees
是在 Spirit 语法中用作属性的数据类型:这允许同时为同一结构创建多个改编。
You have several possibilities. The easiest is to adapt your struct into a Fusion sequence in the required order:
(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:
where
reversed_LatLongDegrees
is the data type used as the attribute in your Spirit grammar:This allows to create several adaptations for the same struct at the same time.