1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <boost/spirit.hpp>
#include <boost/spirit/dynamic/lazy.hpp>
using namespace BOOST_SPIRIT_CLASSIC_NS;
using namespace phoenix;
struct ma_closure : boost::spirit::closure<ma_closure, size_t>
{
member1 NbrElts;
};
class MaGrammaire
:public grammar<MaGrammaire>
{
public:
template <typename ScannerT>
struct definition
{
template<typename TArg>
struct regle_lire_liste
{
template<class Tuple>
struct result
{
typedef rule<ScannerT> type;
};
explicit
regle_lire_liste(definition &P_def,TArg& P_arg):m_def(P_def),m_arg(P_arg)
{
}
rule<ScannerT> operator()()const
{
return repeat_p<size_t>(m_arg())[m_def.element];
}
protected:
definition &m_def;
TArg& m_arg;
rule<ScannerT> m_regle;
};
template<class T>
regle_lire_liste<T> regle_lire_liste_p(definition &P_def, T&P_arg)
{
return regle_lire_liste<T>(P_def,P_arg);
}
rule<ScannerT,ma_closure::context_t> liste;
rule<ScannerT> element;
rule<ScannerT> lire_liste;
definition(MaGrammaire const& self)
{
liste = uint_p[assign_a(liste.NbrElts)]>>":">>lire_liste;
lire_liste = lazy_p(regle_lire_liste_p(*this,liste.NbrElts));
element = uint_p>>",";
}
rule<ScannerT,ma_closure::context_t> const& start() const
{return liste;}
};
};
#include <iostream>
int main()
{
MaGrammaire MonParseur;
// Create a file iterator for this file
std::string L_strChaine("5:1,2,3,4,5,");
parse_info<char const*> info = parse(
L_strChaine.c_str(),
L_strChaine.c_str()+L_strChaine.length(),
MonParseur
);
if (info.full)
std::cout << "Parse succeeded!\n";
else
std::cout << "Parse failed!\n";
return std::getc(stdin);
} |
Partager