重复使用升压因缘解析的变量 [英] reuse parsed variable with boost karma

查看:180
本文介绍了重复使用升压因缘解析的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code碱基这是相当相当于低于code。
我尝试生成具有可变的两倍内容的文本文件。
我觉得答案是语义动作和_a和_val,但不能管理,甚至通过与文档就搞定了。

你会

怎么做才能有:
在海峡TOTO
输出:
TOTO一些东西TOTO

即如何在业力重用解析变量?

 结构数据
 {
  性病::字符串str;
 };BOOST_FUSION_ADAPT_STRUCT(
                      数据,
                      (标准::字符串,STR)
                      )模板< typename的迭代器>
结构数据:因缘::语法<迭代器,数据()>
{
    数据():数据:: base_type(开始)
    {
        开始=因果报应::字符串<<因果报应::亮起(一些东西)<<因果报应:: string的; //第二个字符串实际上是在第一个
    }
    因果报应::规则<迭代器,数据()>开始;
};

解决方案(根据以下职位:)

 的#include<&iostream的GT;
#包括LT&;串GT;
#包括LT&;升压/融合/有/ adapt_struct.hpp>
#包括LT&;升压/精神/有/ karma.hpp>
#包括LT&;升压/精神/有/ phoenix_core.hpp>
#包括LT&;升压/精神/有/ phoenix_operator.hpp>
#包括LT&;升压/精神/有/ phoenix_fusion.hpp>
#包括LT&;升压/精神/有/ phoenix_stl.hpp>
#包括LT&;升压/精神/有/ phoenix_object.hpp>
#包括LT&;升压/精神/有/ support_istream_iterator.hpp>
#包括LT&;升压/精神/有/ support_iso8859_1.hpp>命名空间AST
{
    结构数据
    {
        性病::字符串str;
    };
}BOOST_FUSION_ADAPT_STRUCT(
                          AST ::数据,
                          (标准::字符串,STR)
                          )命名空间因缘=的boost ::精神::人缘;命名空间的解析器
{    模板< typename的迭代器>
    结构数据:因缘::语法<迭代器,AST​​ ::数据()>
    {
        数据():数据:: base_type(开始)
        {
            开始=
                因果报应::字符串[人缘:: _ 1 =提振::凤:: at_c℃的>(因缘:: _ VAL)<<
                因果报应::亮起(一些东西)<<
                因果报应::字符串[人缘:: _ 1 =提振::凤:: at_c℃的>(因缘:: _ VAL)
                ;
        }
        因果报应::规则<迭代器,AST​​ ::数据()>开始;
    };
}主要()
{
    AST ::数据D;
    d.str =TOTO
    产生的std ::串;
    的typedef的std :: back_insert_iterator<标准::字符串> iterator_type;
    解析器::数据< iterator_type> D_P;
    iterator_type下沉(产生);
    因果报应::生成(水槽,D_P,D);
    性病::法院LT&;<生成<<的std :: ENDL;
}


解决方案

这应该做的伎俩:

 开始=因果报应::字符串[人缘:: _ 1 =因果报应:: _ VAL]
    <<因果报应::亮起(一些东西)
    <<因果报应::字符串[人缘:: _ 1 =因果报应:: _ VAL]。

I have a code base which is quite equivalent to the code below. I try to generate a text file with two times the content of a variable. I feel that the answer is in semantic actions and _a and _val but cannot manage to get through even with the documentation.

How will you do to have : "toto" in str and output : toto some stuff toto

i.e how to reuse a parsed variable in karma ?

struct data
 {
  std::string str;
 };

BOOST_FUSION_ADAPT_STRUCT(
                      data,
                      (std::string, str)
                      )

template <typename Iterator>
struct data: karma::grammar<Iterator, data() >
{
    data():data::base_type(start)
    {
        start = karma::string << karma::lit("some stuff") << karma::string; //Second string is in fact the first one 
    }
    karma::rule<Iterator, data()> start;
};

Solution (according to posts below :)

#include <iostream>
#include <string>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <boost/spirit/include/support_iso8859_1.hpp>

namespace ast
{
    struct data
    {
        std::string str;
    };
}

BOOST_FUSION_ADAPT_STRUCT(
                          ast::data,
                          (std::string, str)
                          )

namespace karma = boost::spirit::karma;

namespace parser
{

    template <typename Iterator>
    struct data: karma::grammar<Iterator, ast::data() >
    {
        data():data::base_type(start)
        {
            start =
                karma::string[karma::_1 =  boost::phoenix::at_c<0>(karma::_val)] <<
                karma::lit("some stuff") <<
                karma::string[karma::_1 =  boost::phoenix::at_c<0>(karma::_val)]
                ;
        }
        karma::rule<Iterator, ast::data()> start;
    };
}

main()
{
    ast::data d;
    d.str = "toto";
    std::string generated;
    typedef std::back_insert_iterator<std::string> iterator_type;
    parser::data<iterator_type> d_p;
    iterator_type sink(generated);
    karma::generate(sink, d_p, d);
    std::cout << generated << std::endl;
}

解决方案

This should do the trick:

start = karma::string[karma::_1 = karma::_val] 
    << karma::lit("some stuff") 
    << karma::string[karma::_1 = karma::_val]; 

这篇关于重复使用升压因缘解析的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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