/*=============================================================================
Copyright (c) 2001-2013 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://w...content-available-to-author-only...t.org/LICENSE_1_0.txt)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// A Calculator example demonstrating generation of AST. The AST,
// once created, is traversed, 1) To print its contents and
// 2) To evaluate the result.
//
// [ JDG April 28, 2008 ] For BoostCon 2008
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
// [ JDG January 9, 2013 ] Spirit X3
//
///////////////////////////////////////////////////////////////////////////////
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include <boost/config/warning_disable.hpp>
#include <boost/range/numeric.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <string>
#include <list>
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST
///////////////////////////////////////////////////////////////////////////
struct nil {};
struct signed_;
struct program;
typedef boost::variant<
nil
, unsigned int
, boost::recursive_wrapper<signed_>
, boost::recursive_wrapper<program>
>
operand;
struct signed_
{
char sign;
operand operand_;
};
struct operation
{
char operator_;
operand operand_;
};
struct program
{
operand first;
std::list<operation> rest;
};
}}
namespace client{ namespace ast{
struct ts{
//ts(){}
//ts(unsigned int) {}
unsigned int id;
};
struct fnc;
typedef boost::variant<ts, boost::recursive_wrapper<fnc> > node;
struct fnc{
std::vector<char> id;
std::vector<node> args;
};
}}
BOOST_FUSION_ADAPT_STRUCT(
client::ast::ts,
(unsigned int, id)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::fnc,
(std::vector<char>, id)
(std::vector<client::ast::node>, args)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::signed_,
(char, sign)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::operation,
(char, operator_)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::program,
(client::ast::operand, first)
(std::list<client::ast::operation>, rest)
)
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST Printer
///////////////////////////////////////////////////////////////////////////
struct printer
{
typedef void result_type;
void operator()(nil) const {}
void operator()(unsigned int n) const { std::cout << n; }
void operator()(operation const& x) const
{
boost::apply_visitor(*this, x.operand_);
switch (x.operator_)
{
case '+': std::cout << " add"; break;
case '-': std::cout << " subt"; break;
case '*': std::cout << " mult"; break;
case '/': std::cout << " div"; break;
}
}
void operator()(signed_ const& x) const
{
boost::apply_visitor(*this, x.operand_);
switch (x.sign)
{
case '-': std::cout << " neg"; break;
case '+': std::cout << " pos"; break;
}
}
void operator()(program const& x) const
{
boost::apply_visitor(*this, x.first);
BOOST_FOREACH(operation const& oper, x.rest)
{
std::cout << ' ';
(*this)(oper);
}
}
};
///////////////////////////////////////////////////////////////////////////
// The AST evaluator
///////////////////////////////////////////////////////////////////////////
struct eval
{
typedef int result_type;
int operator()(nil) const { BOOST_ASSERT(0); return 0; }
int operator()(unsigned int n) const { return n; }
int operator()(int lhs, operation const& x) const
{
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.operator_)
{
case '+': return lhs + rhs;
case '-': return lhs - rhs;
case '*': return lhs * rhs;
case '/': return lhs / rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(signed_ const& x) const
{
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.sign)
{
case '-': return -rhs;
case '+': return +rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(program const& x) const
{
return boost::accumulate( x.rest
, boost::apply_visitor(*this, x.first)
, *this);
}
};
}}
namespace client
{
namespace x3 = boost::spirit::x3;
namespace calc_grammar{
using x3::uint_;
using x3::alpha;
using x3::alnum;
using x3::lit;
using x3::char_;
x3::rule<class funct, ast::fnc> const funct("function");
x3::rule<class ts, ast::ts> const ts("timeseries");
x3::rule<class funct_name, std::vector<char>> const funct_name("function_name");
auto const funct_def = funct_name >> lit('(') >> -((ts|funct) % lit(',')) >> lit(')');
auto const ts_def = lit('#') >> uint_ >> lit('#');
auto const funct_name_def = lit('@') >> alpha >> *(alnum|char_('_'));
auto const calc = x3::grammar(
"calc",
funct = funct_def,
ts = ts_def,
funct_name = funct_name_def
);
}
///////////////////////////////////////////////////////////////////////////////
// The calculator grammar
///////////////////////////////////////////////////////////////////////////////
namespace calculator_grammar
{
using x3::uint_;
using x3::char_;
x3::rule<class expression, ast::program> const expression("expression");
x3::rule<class term, ast::program> const term("term");
x3::rule<class factor, ast::operand> const factor("factor");
auto const expression_def =
term
>> *( (char_('+') >> term)
| (char_('-') >> term)
)
;
auto const term_def =
factor
>> *( (char_('*') >> factor)
| (char_('/') >> factor)
)
;
auto const factor_def =
uint_
| '(' >> expression >> ')'
| (char_('-') >> factor)
| (char_('+') >> factor)
;
auto const calculator = x3::grammar(
"calculator"
, expression = expression_def
, term = term_def
, factor = factor_def
);
}
using calculator_grammar::calculator;
using calc_grammar::calc;
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef client::ast::program ast_program;
typedef client::ast::printer ast_print;
typedef client::ast::eval ast_eval;
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
auto& calc = client::calc; // Our grammar
client::ast::fnc fnc; // fnx
ast_program program; // Our program (AST)
ast_print print; // Prints the program
ast_eval eval; // Evaluates the program
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
boost::spirit::x3::ascii::space_type space;
bool r = phrase_parse(iter, end, calc, space, fnc);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
print(program);
std::cout << "\nResult: " << eval(program) << std::endl;
std::cout << "-------------------------\n";
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "stopped at: \"" << rest << "\"\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}