summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/classic/example/fundamental/sum.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/boost/libs/spirit/classic/example/fundamental/sum.cpp')
-rw-r--r--src/boost/libs/spirit/classic/example/fundamental/sum.cpp92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/boost/libs/spirit/classic/example/fundamental/sum.cpp b/src/boost/libs/spirit/classic/example/fundamental/sum.cpp
new file mode 100644
index 00000000..d6dd9c9d
--- /dev/null
+++ b/src/boost/libs/spirit/classic/example/fundamental/sum.cpp
@@ -0,0 +1,92 @@
+/*=============================================================================
+ 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)
+=============================================================================*/
+///////////////////////////////////////////////////////////////////////////////
+//
+// A parser for summing a list of numbers. Demonstrating phoenix
+// This is discussed in the "Phoenix" chapter in the Spirit User's Guide.
+//
+// [ JDG 6/28/2002 ]
+//
+///////////////////////////////////////////////////////////////////////////////
+#include <boost/spirit/include/classic_core.hpp>
+#include <boost/spirit/include/phoenix1_primitives.hpp>
+#include <boost/spirit/include/phoenix1_operators.hpp>
+#include <iostream>
+#include <string>
+
+///////////////////////////////////////////////////////////////////////////////
+using namespace std;
+using namespace BOOST_SPIRIT_CLASSIC_NS;
+using namespace phoenix;
+
+///////////////////////////////////////////////////////////////////////////////
+//
+// Our adder
+//
+///////////////////////////////////////////////////////////////////////////////
+template <typename IteratorT>
+bool adder(IteratorT first, IteratorT last, double& n)
+{
+ return parse(first, last,
+
+ // Begin grammar
+ (
+ real_p[var(n) = arg1] >> *(',' >> real_p[var(n) += arg1])
+ )
+ ,
+ // End grammar
+
+ space_p).full;
+}
+
+////////////////////////////////////////////////////////////////////////////
+//
+// Main program
+//
+////////////////////////////////////////////////////////////////////////////
+int
+main()
+{
+ cout << "/////////////////////////////////////////////////////////\n\n";
+ cout << "\t\tA parser for summing a list of numbers...\n\n";
+ cout << "/////////////////////////////////////////////////////////\n\n";
+
+ cout << "Give me a comma separated list of numbers.\n";
+ cout << "The numbers are added using Phoenix.\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;
+
+ double n;
+ if (adder(str.begin(), str.end(), n))
+ {
+ cout << "-------------------------\n";
+ cout << "Parsing succeeded\n";
+ cout << str << " Parses OK: " << endl;
+
+ cout << "sum = " << n;
+ cout << "\n-------------------------\n";
+ }
+ else
+ {
+ cout << "-------------------------\n";
+ cout << "Parsing failed\n";
+ cout << "-------------------------\n";
+ }
+ }
+
+ cout << "Bye... :-) \n\n";
+ return 0;
+}
+
+