spirit/classic/example/fundamental/number_list.cpp
2008-04-13 16:28:27 +00:00

94 lines
2.8 KiB
C++

/*=============================================================================
Copyright (c) 2002-2003 Joel de Guzman
http://spirit.sourceforge.net/
Use, modification and distribution is subject to the Boost Software
License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// This sample demontrates a parser for a comma separated list of numbers
// This is discussed in the "Quick Start" chapter in the Spirit User's Guide.
//
// [ JDG 5/10/2002 ]
//
///////////////////////////////////////////////////////////////////////////////
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_push_back_actor.hpp>
#include <iostream>
#include <vector>
#include <string>
///////////////////////////////////////////////////////////////////////////////
using namespace std;
using namespace BOOST_SPIRIT_CLASSIC_NS;
///////////////////////////////////////////////////////////////////////////////
//
// Our comma separated list parser
//
///////////////////////////////////////////////////////////////////////////////
bool
parse_numbers(char const* str, vector<double>& v)
{
return parse(str,
// Begin grammar
(
real_p[push_back_a(v)] >> *(',' >> real_p[push_back_a(v)])
)
,
// End grammar
space_p).full;
}
////////////////////////////////////////////////////////////////////////////
//
// Main program
//
////////////////////////////////////////////////////////////////////////////
int
main()
{
cout << "/////////////////////////////////////////////////////////\n\n";
cout << "\t\tA comma separated list parser for Spirit...\n\n";
cout << "/////////////////////////////////////////////////////////\n\n";
cout << "Give me a comma separated list of numbers.\n";
cout << "The numbers will be inserted in a vector of numbers\n";
cout << "Type [q or Q] to quit\n\n";
string str;
while (getline(cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
vector<double> v;
if (parse_numbers(str.c_str(), v))
{
cout << "-------------------------\n";
cout << "Parsing succeeded\n";
cout << str << " Parses OK: " << endl;
for (vector<double>::size_type i = 0; i < v.size(); ++i)
cout << i << ": " << v[i] << endl;
cout << "-------------------------\n";
}
else
{
cout << "-------------------------\n";
cout << "Parsing failed\n";
cout << "-------------------------\n";
}
}
cout << "Bye... :-) \n\n";
return 0;
}