⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 right_recursion.cpp

📁 C++的一个好库。。。现在很流行
💻 CPP
字号:
/*=============================================================================
    Copyright (c) 2005 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)
=============================================================================*/
#include <iostream>

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/core.hpp>

using namespace boost::spirit;

struct non_greedy_kleene : public grammar<non_greedy_kleene>
{
    template <typename ScannerT>
    struct definition
    {
        typedef rule<ScannerT> rule_t;
        rule_t r;

        definition(non_greedy_kleene const& self)
        {
            r = (alnum_p >> r) | digit_p;
            BOOST_SPIRIT_DEBUG_RULE(r);
        }

        rule_t const&
        start() const
        {
            return r;
        }
    };
};

struct non_greedy_plus : public grammar<non_greedy_plus>
{
    template <typename ScannerT>
    struct definition
    {
        typedef rule<ScannerT> rule_t;
        rule_t r;

        definition(non_greedy_plus const& self)
        {
            r = alnum_p >> (r | digit_p);
            BOOST_SPIRIT_DEBUG_RULE(r);
        }

        rule_t const&
        start() const
        {
            return r;
        }
    };
};
int
main()
{
    bool success;
    {
        non_greedy_kleene k;
        success = parse("3", k).full;
        assert(success);
        success = parse("abcdef3", k).full;
        assert(success);
        success = parse("abc2def3", k).full;
        assert(success);
        success = parse("abc", k).full;
        assert(!success);
    }
    
    {
        non_greedy_plus p;
        success = parse("3", p).full;
        assert(!success);
        success = parse("abcdef3", p).full;
        assert(success);
        success = parse("abc2def3", p).full;
        assert(success);
        success = parse("abc", p).full;
        assert(!success);
    }

    std::cout << "SUCCESS!!!\n";
    return 0;
}

⌨️ 快捷键说明

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