c++ - Boost spirit 解析器的 Fortran 打印 double

标签 c++ boost-spirit

我正在使用 num_list3.cpp来自 Boost Spirit 示例。我正在测试它可以解析的各种 double 类型。我使用了以下列表:

1.2,0.2
.2,5.
1.e+23,.23E4
0e+10
1.3D+3

我注意到它无法解析最后一个数字 1.3D+3

如何将 D 设置为 double 的指数前缀?

最佳答案

您可以使用 Boost.Spirit 轻松做到这一点。你只需要实例化一个 real_parser使用处理“d|D”前缀的自定义策略。它可以像这样简单:

template <typename Type>
struct fortran_policy : qi::real_policies<Type>
{
    template <typename Iterator>
    static bool parse_exp(Iterator& first, const Iterator& last)
    {
        if (first == last || (*first != 'e' && *first != 'E' && *first != 'd' && *first != 'D'))
            return false;
        ++first;
        return true;
    }
};

那么你只需要使用:

qi::real_parser<double,fortran_policy<double>> double_;

无需更改任何其他内容(尽管语义操作似乎是不必要的)。

Live on ideone

关于c++ - Boost spirit 解析器的 Fortran 打印 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34339579/

相关文章:

c++ - 用灵气解析C风格的关系运算符

c++ - 给定一个整数数组,找到在给定位置之间的排序数组中的整数总和

C++类成员函数转c函数

c++ - 使用 boost::spirit::qi 解析带分隔符的数字

c++ - 支持具有固定数组的对象的 BOOST_FUSION_ADAPT_STRUCT?

c++ - Boost.Spirit语法问题

c++ - 在没有船长的情况下提升精神解析

c++ - Windows 中通配符搜索中的问号

c++ - 编译但失败的 C++ std::vector<std::auto_ptr<T>> 示例

c++ - C++ 中的字符串和整数连接