calc3.cpp

来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 112 行

CPP
112
字号
/*=============================================================================    Copyright (c) 2001-2007 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)=============================================================================*////////////////////////////////////////////////////////////////////////////////////  A calculator example demonstrating the grammar and semantic actions//  using phoenix to do the actual expression evaluation. The parser is//  essentially an "interpreter" that evaluates expressions on the fly.////  [ JDG June 29, 2002 ]   spirit1//  [ JDG March 5, 2007 ]   spirit2/////////////////////////////////////////////////////////////////////////////////#include <boost/config/warning_disable.hpp>#include <boost/spirit/include/qi.hpp>#include <boost/spirit/include/phoenix_operator.hpp>#include <iostream>#include <string>using namespace boost::spirit;using namespace boost::spirit::qi;using namespace boost::spirit::ascii;using namespace boost::spirit::arg_names;/////////////////////////////////////////////////////////////////////////////////  Our calculator grammar///////////////////////////////////////////////////////////////////////////////template <typename Iterator>struct calculator : grammar<Iterator, int(), space_type>{    calculator() : calculator::base_type(expression)    {        expression =            term                            [_val = _1]            >> *(   ('+' >> term            [_val += _1])                |   ('-' >> term            [_val -= _1])                )            ;        term =            factor                          [_val = _1]            >> *(   ('*' >> factor          [_val *= _1])                |   ('/' >> factor          [_val /= _1])                )            ;        factor =            uint_                           [_val = _1]            |   '(' >> expression           [_val = _1] >> ')'            |   ('-' >> factor              [_val = -_1])            |   ('+' >> factor              [_val = _1])            ;    }    rule<Iterator, int(), space_type> expression, term, factor;};/////////////////////////////////////////////////////////////////////////////////  Main program///////////////////////////////////////////////////////////////////////////////intmain(){    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 calculator<iterator_type> calculator;    calculator calc; // Our grammar    std::string str;    int result;    while (std::getline(std::cin, str))    {        if (str.empty() || str[0] == 'q' || str[0] == 'Q')            break;        std::string::const_iterator iter = str.begin();        std::string::const_iterator end = str.end();        bool r = phrase_parse(iter, end, calc, result, space);        if (r && iter == end)        {            std::cout << "-------------------------\n";            std::cout << "Parsing succeeded\n";            std::cout << "result = " << result << 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;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?