Diff
checker
Texte
Texte
Images
Documents
Excel
Dossiers
Legal
Enterprise
Application de bureau
Prix
Se connecter
Télécharger Diffchecker Desktop
Comparer le texte
Trouver la différence entre deux fichiers texte
Outils
Historique
Éditeur live
Cacher identiques
Sans retour à la ligne
Vue
Divisé
Unifié
Niveau de précision
Intelligent
Mot
Caractère
Coloration syntaxique
Choisir la syntaxe
Ignorer
Transformer le texte
Aller au premier écart
Modifier l'entrée
Diffchecker Desktop
La façon la plus sécurisée d'utiliser Diffchecker. Obtenez l'application Diffchecker Desktop : vos diffs ne quittent jamais votre ordinateur !
Obtenir Desktop
Untitled diff
Créé
il y a 9 ans
Le diff n'expire jamais
Effacer
Exporter
Partager
Expliquer
25 suppressions
Lignes
Total
Supprimé
Caractères
Total
Supprimé
Pour continuer à utiliser cette fonctionnalité, passez à
Diff
checker
Pro
Voir les prix
307 lignes
Copier tout
4 ajouts
Lignes
Total
Ajouté
Caractères
Total
Ajouté
Pour continuer à utiliser cette fonctionnalité, passez à
Diff
checker
Pro
Voir les prix
301 lignes
Copier tout
/*=============================================================================
/*=============================================================================
Copyright (c) 2001-2014 Joel de Guzman
Copyright (c) 2001-2014 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
Distributed under 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)
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
//
// Same as Calc4, this time, we'll incorporate debugging support,
// Same as Calc4, this time, we'll incorporate debugging support,
// plus error handling and reporting.
// plus error handling and reporting.
//
//
// [ JDG April 28, 2008 ] For BoostCon 2008
// [ JDG April 28, 2008 ] For BoostCon 2008
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
// [ JDG April 9, 2014 ] Spirit X3
// [ JDG April 9, 2014 ] Spirit X3
//
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment this if you want to enable debugging
// Uncomment this if you want to enable debugging
//#define BOOST_SPIRIT_X3_DEBUG
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/config/warning_disable.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <iostream>
#include <iostream>
#include <string>
#include <string>
#include <list>
#include <list>
#include <numeric>
#include <numeric>
namespace x3 = boost::spirit::x3;
namespace x3 = boost::spirit::x3;
namespace client { namespace ast
namespace client { namespace ast
{
{
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// The AST
// The AST
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
struct nil {};
struct nil {};
struct signed_;
struct signed_;
struct program;
struct program;
struct operand : x3::variant<
struct operand : x3::variant<
nil
nil
, unsigned int
, unsigned int
, x3::forward_ast<signed_>
, x3::forward_ast<signed_>
, x3::forward_ast<program>
, x3::forward_ast<program>
>
>
{
{
using base_type::base_type;
using base_type::base_type;
using base_type::operator=;
using base_type::operator=;
};
};
struct signed_
struct signed_
{
{
char sign;
char sign;
operand operand_;
operand operand_;
};
};
struct operation
struct operation
{
{
char operator_;
char operator_;
operand operand_;
operand operand_;
};
};
struct program
struct program
{
{
operand first;
operand first;
std::list<operation> rest;
std::list<operation> rest;
};
};
// print function for debugging
// print function for debugging
inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; }
inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; }
}}
}}
Copier
Copié
Copier
Copié
BOOST_FUSION_ADAPT_STRUCT(
BOOST_FUSION_ADAPT_STRUCT(
client::ast::signed_,
client::ast::signed_,
sign
, operand_
(char,
sign
)
(client::ast::operand
, operand_
)
)
)
Copier
Copié
Copier
Copié
BOOST_FUSION_ADAPT_STRUCT(
BOOST_FUSION_ADAPT_STRUCT(
client::ast::operation,
client::ast::operation,
operator_
, operand_
(char,
operator_
)
(client::ast::operand
, operand_
)
)
)
Copier
Copié
Copier
Copié
BOOST_FUSION_ADAPT_STRUCT(
BOOST_FUSION_ADAPT_STRUCT(
client::ast::program,
client::ast::program,
first
, rest
(client::ast::operand,
first
)
(std::list<client::ast::operation>
, rest
)
)
)
namespace client { namespace ast
namespace client { namespace ast
{
{
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// The AST Printer
// The AST Printer
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
struct printer
struct printer
{
{
typedef void result_type;
typedef void result_type;
void operator()(nil) const {}
void operator()(nil) const {}
void operator()(unsigned int n) const { std::cout << n; }
void operator()(unsigned int n) const { std::cout << n; }
void operator()(operation const& x) const
void operator()(operation const& x) const
{
{
boost::apply_visitor(*this, x.operand_);
boost::apply_visitor(*this, x.operand_);
switch (x.operator_)
switch (x.operator_)
{
{
case '+': std::cout << " add"; break;
case '+': std::cout << " add"; break;
case '-': std::cout << " subt"; break;
case '-': std::cout << " subt"; break;
case '*': std::cout << " mult"; break;
case '*': std::cout << " mult"; break;
case '/': std::cout << " div"; break;
case '/': std::cout << " div"; break;
}
}
}
}
void operator()(signed_ const& x) const
void operator()(signed_ const& x) const
{
{
boost::apply_visitor(*this, x.operand_);
boost::apply_visitor(*this, x.operand_);
switch (x.sign)
switch (x.sign)
{
{
case '-': std::cout << " neg"; break;
case '-': std::cout << " neg"; break;
case '+': std::cout << " pos"; break;
case '+': std::cout << " pos"; break;
}
}
}
}
void operator()(program const& x) const
void operator()(program const& x) const
{
{
boost::apply_visitor(*this, x.first);
boost::apply_visitor(*this, x.first);
Copier
Copié
Copier
Copié
BOOST_FOREACH
(operation const& oper
,
x.rest)
for
(operation const& oper
:
x.rest)
{
{
std::cout << ' ';
std::cout << ' ';
(*this)(oper);
(*this)(oper);
}
}
}
}
};
};
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// The AST evaluator
// The AST evaluator
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
struct eval
struct eval
{
{
typedef int result_type;
typedef int result_type;
int operator()(nil) const { BOOST_ASSERT(0); return 0; }
int operator()(nil) const { BOOST_ASSERT(0); return 0; }
int operator()(unsigned int n) const { return n; }
int operator()(unsigned int n) const { return n; }
int operator()(operation const& x, int lhs) const
int operator()(operation const& x, int lhs) const
{
{
int rhs = boost::apply_visitor(*this, x.operand_);
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.operator_)
switch (x.operator_)
{
{
case '+': return lhs + rhs;
case '+': return lhs + rhs;
case '-': return lhs - rhs;
case '-': return lhs - rhs;
case '*': return lhs * rhs;
case '*': return lhs * rhs;
case '/': return lhs / rhs;
case '/': return lhs / rhs;
}
}
BOOST_ASSERT(0);
BOOST_ASSERT(0);
return 0;
return 0;
}
}
int operator()(signed_ const& x) const
int operator()(signed_ const& x) const
{
{
int rhs = boost::apply_visitor(*this, x.operand_);
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.sign)
switch (x.sign)
{
{
case '-': return -rhs;
case '-': return -rhs;
case '+': return +rhs;
case '+': return +rhs;
}
}
BOOST_ASSERT(0);
BOOST_ASSERT(0);
return 0;
return 0;
}
}
int operator()(program const& x) const
int operator()(program const& x) const
{
{
int state = boost::apply_visitor(*this, x.first);
int state = boost::apply_visitor(*this, x.first);
Copier
Copié
Copier
Copié
BOOST_FOREACH
(operation const& oper
,
x.rest)
for
(operation const& oper
:
x.rest)
{
{
state = (*this)(oper, state);
state = (*this)(oper, state);
}
}
return state;
return state;
}
}
};
};
}}
}}
namespace client
namespace client
{
{
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// The calculator grammar
// The calculator grammar
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
namespace calculator_grammar
namespace calculator_grammar
{
{
using x3::uint_;
using x3::uint_;
using x3::char_;
using x3::char_;
struct expression_class;
struct expression_class;
struct term_class;
struct term_class;
struct factor_class;
struct factor_class;
x3::rule<expression_class, ast::program> const expression("expression");
x3::rule<expression_class, ast::program> const expression("expression");
x3::rule<term_class, ast::program> const term("term");
x3::rule<term_class, ast::program> const term("term");
x3::rule<factor_class, ast::operand> const factor("factor");
x3::rule<factor_class, ast::operand> const factor("factor");
auto const expression_def =
auto const expression_def =
term
term
>> *( (char_('+') > term)
>> *( (char_('+') > term)
| (char_('-') > term)
| (char_('-') > term)
)
)
;
;
auto const term_def =
auto const term_def =
factor
factor
>> *( (char_('*') > factor)
>> *( (char_('*') > factor)
| (char_('/') > factor)
| (char_('/') > factor)
)
)
;
;
auto const factor_def =
auto const factor_def =
uint_
uint_
| '(' > expression > ')'
| '(' > expression > ')'
| (char_('-') > factor)
| (char_('-') > factor)
| (char_('+') > factor)
| (char_('+') > factor)
;
;
BOOST_SPIRIT_DEFINE(
BOOST_SPIRIT_DEFINE(
Copier
Copié
Copier
Copié
expression
= expression_def
expression
, term
= term_def
, term
, factor
= factor_def
, factor
);
);
struct expression_class
struct expression_class
{
{
// Our error handler
// Our error handler
template <typename Iterator, typename Exception, typename Context>
template <typename Iterator, typename Exception, typename Context>
x3::error_handler_result
x3::error_handler_result
on_error(Iterator&, Iterator const& last, Exception const& x, Context const& context)
on_error(Iterator&, Iterator const& last, Exception const& x, Context const& context)
{
{
std::cout
std::cout
<< "Error! Expecting: "
<< "Error! Expecting: "
<< x.which()
<< x.which()
<< " here: \""
<< " here: \""
<< std::string(x.where(), last)
<< std::string(x.where(), last)
<< "\""
<< "\""
<< std::endl
<< std::endl
;
;
return x3::error_handler_result::fail;
return x3::error_handler_result::fail;
}
}
};
};
auto calculator = expression;
auto calculator = expression;
}
}
using calculator_grammar::calculator;
using calculator_grammar::calculator;
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Main program
// Main program
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int
int
main()
main()
{
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef std::string::const_iterator iterator_type;
typedef client::ast::program ast_program;
typedef client::ast::program ast_program;
typedef client::ast::printer ast_print;
typedef client::ast::printer ast_print;
typedef client::ast::eval ast_eval;
typedef client::ast::eval ast_eval;
std::string str;
std::string str;
while (std::getline(std::cin, str))
while (std::getline(std::cin, str))
{
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
break;
auto& calc = client::calculator; // Our grammar
auto& calc = client::calculator; // Our grammar
ast_program program; // Our program (AST)
ast_program program; // Our program (AST)
ast_print print; // Prints the program
ast_print print; // Prints the program
ast_eval eval; // Evaluates the program
ast_eval eval; // Evaluates the program
iterator_type iter = str.begin();
iterator_type iter = str.begin();
iterator_type end = str.end();
iterator_type end = str.end();
boost::spirit::x3::ascii::space_type space;
boost::spirit::x3::ascii::space_type space;
bool r = phrase_parse(iter, end, calc, space, program);
bool r = phrase_parse(iter, end, calc, space, program);
if (r && iter == end)
if (r && iter == end)
{
{
std::cout << "-------------------------\n";
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "Parsing succeeded\n";
print(program);
print(program);
std::cout << "\nResult: " << eval(program) << std::endl;
std::cout << "\nResult: " << eval(program) << std::endl;
std::cout << "-------------------------\n";
std::cout << "-------------------------\n";
}
}
else
else
{
{
std::cout << "-------------------------\n";
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "Parsing failed\n";
std::cout << "-------------------------\n";
std::cout << "-------------------------\n";
}
}
}
}
std::cout << "Bye... :-) \n\n";
std::cout << "Bye... :-) \n\n";
return 0;
return 0;
}
}
Différences enregistrées
Texte d'origine
Ouvrir un fichier
/*============================================================================= Copyright (c) 2001-2014 Joel de Guzman Distributed under 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) =============================================================================*/ /////////////////////////////////////////////////////////////////////////////// // // Same as Calc4, this time, we'll incorporate debugging support, // plus error handling and reporting. // // [ JDG April 28, 2008 ] For BoostCon 2008 // [ JDG February 18, 2011 ] Pure attributes. No semantic actions. // [ JDG April 9, 2014 ] Spirit X3 // /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // Uncomment this if you want to enable debugging //#define BOOST_SPIRIT_X3_DEBUG #include <boost/config/warning_disable.hpp> #include <boost/spirit/home/x3.hpp> #include <boost/spirit/home/x3/support/ast/variant.hpp> #include <boost/fusion/include/adapt_struct.hpp> #include <iostream> #include <string> #include <list> #include <numeric> namespace x3 = boost::spirit::x3; namespace client { namespace ast { /////////////////////////////////////////////////////////////////////////// // The AST /////////////////////////////////////////////////////////////////////////// struct nil {}; struct signed_; struct program; struct operand : x3::variant< nil , unsigned int , x3::forward_ast<signed_> , x3::forward_ast<program> > { using base_type::base_type; using base_type::operator=; }; struct signed_ { char sign; operand operand_; }; struct operation { char operator_; operand operand_; }; struct program { operand first; std::list<operation> rest; }; // print function for debugging inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; } }} 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()(operation const& x, int lhs) 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 { int state = boost::apply_visitor(*this, x.first); BOOST_FOREACH(operation const& oper, x.rest) { state = (*this)(oper, state); } return state; } }; }} namespace client { /////////////////////////////////////////////////////////////////////////////// // The calculator grammar /////////////////////////////////////////////////////////////////////////////// namespace calculator_grammar { using x3::uint_; using x3::char_; struct expression_class; struct term_class; struct factor_class; x3::rule<expression_class, ast::program> const expression("expression"); x3::rule<term_class, ast::program> const term("term"); x3::rule<factor_class, 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) ; BOOST_SPIRIT_DEFINE( expression = expression_def , term = term_def , factor = factor_def ); struct expression_class { // Our error handler template <typename Iterator, typename Exception, typename Context> x3::error_handler_result on_error(Iterator&, Iterator const& last, Exception const& x, Context const& context) { std::cout << "Error! Expecting: " << x.which() << " here: \"" << std::string(x.where(), last) << "\"" << std::endl ; return x3::error_handler_result::fail; } }; auto calculator = expression; } using calculator_grammar::calculator; } /////////////////////////////////////////////////////////////////////////////// // 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::calculator; // Our grammar ast_program program; // Our program (AST) ast_print print; // Prints the program ast_eval eval; // Evaluates the program iterator_type iter = str.begin(); iterator_type end = str.end(); boost::spirit::x3::ascii::space_type space; bool r = phrase_parse(iter, end, calc, space, program); 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::cout << "-------------------------\n"; std::cout << "Parsing failed\n"; std::cout << "-------------------------\n"; } } std::cout << "Bye... :-) \n\n"; return 0; }
Texte modifié
Ouvrir un fichier
/*============================================================================= Copyright (c) 2001-2014 Joel de Guzman Distributed under 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) =============================================================================*/ /////////////////////////////////////////////////////////////////////////////// // // Same as Calc4, this time, we'll incorporate debugging support, // plus error handling and reporting. // // [ JDG April 28, 2008 ] For BoostCon 2008 // [ JDG February 18, 2011 ] Pure attributes. No semantic actions. // [ JDG April 9, 2014 ] Spirit X3 // /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // Uncomment this if you want to enable debugging //#define BOOST_SPIRIT_X3_DEBUG #include <boost/config/warning_disable.hpp> #include <boost/spirit/home/x3.hpp> #include <boost/spirit/home/x3/support/ast/variant.hpp> #include <boost/fusion/include/adapt_struct.hpp> #include <iostream> #include <string> #include <list> #include <numeric> namespace x3 = boost::spirit::x3; namespace client { namespace ast { /////////////////////////////////////////////////////////////////////////// // The AST /////////////////////////////////////////////////////////////////////////// struct nil {}; struct signed_; struct program; struct operand : x3::variant< nil , unsigned int , x3::forward_ast<signed_> , x3::forward_ast<program> > { using base_type::base_type; using base_type::operator=; }; struct signed_ { char sign; operand operand_; }; struct operation { char operator_; operand operand_; }; struct program { operand first; std::list<operation> rest; }; // print function for debugging inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; } }} BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, sign, operand_ ) BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, operator_, operand_ ) BOOST_FUSION_ADAPT_STRUCT(client::ast::program, first, 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); for (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()(operation const& x, int lhs) 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 { int state = boost::apply_visitor(*this, x.first); for (operation const& oper : x.rest) { state = (*this)(oper, state); } return state; } }; }} namespace client { /////////////////////////////////////////////////////////////////////////////// // The calculator grammar /////////////////////////////////////////////////////////////////////////////// namespace calculator_grammar { using x3::uint_; using x3::char_; struct expression_class; struct term_class; struct factor_class; x3::rule<expression_class, ast::program> const expression("expression"); x3::rule<term_class, ast::program> const term("term"); x3::rule<factor_class, 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) ; BOOST_SPIRIT_DEFINE( expression , term , factor ); struct expression_class { // Our error handler template <typename Iterator, typename Exception, typename Context> x3::error_handler_result on_error(Iterator&, Iterator const& last, Exception const& x, Context const& context) { std::cout << "Error! Expecting: " << x.which() << " here: \"" << std::string(x.where(), last) << "\"" << std::endl ; return x3::error_handler_result::fail; } }; auto calculator = expression; } using calculator_grammar::calculator; } /////////////////////////////////////////////////////////////////////////////// // 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::calculator; // Our grammar ast_program program; // Our program (AST) ast_print print; // Prints the program ast_eval eval; // Evaluates the program iterator_type iter = str.begin(); iterator_type end = str.end(); boost::spirit::x3::ascii::space_type space; bool r = phrase_parse(iter, end, calc, space, program); 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::cout << "-------------------------\n"; std::cout << "Parsing failed\n"; std::cout << "-------------------------\n"; } } std::cout << "Bye... :-) \n\n"; return 0; }
Trouver la différence