boost::spirit解析字符串引號等對稱符號

-----------------------spirit\spirit\MExpression2.h------------------------------

#pragma once
#include <string>
#include <iostream>
#include <cassert>


#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_confix.hpp>
#include <boost/spirit/include/classic_chset.hpp>


using namespace std;
using namespace BOOST_SPIRIT_CLASSIC_NS;


namespace MExpr{
///////////////////////////////////////////////////////////////////////////////
// actor called after successfully matching a single character
class actor_string
{
public:
actor_string(std::string &rstr) :
 matched(rstr)
 {
 }


 void operator() (const char *pbegin, const char *pend) const
 {
 matched += std::string(pbegin, pend-pbegin);
 }


private:
std::string &matched;
};


void eval3(const char* expr);

}

-------------------------------------------------------------------------------------------

------------------------spirit\MExpression2.cpp----------------------------------

#include "MExpression2.h"


void MExpr::eval3(const char* expr) {
string body;
rule<> open_tag, html_tag, close_tag, body_text;
open_tag = str_p("<b>");
body_text = anychar_p;
close_tag = str_p("</b>");


html_tag = confix_p(open_tag, (*body_text)[actor_string(body)], close_tag);


char const* pTag = "<b>Body text</b>";


parse_info<> result = parse (pTag, html_tag);
if (result.hit)
    {
        cout << "Parsed HTML snippet \"<b>Body text</b>\" successfully "
            "(with re-attached actor)!" << endl;
        cout << "Found body (" << (int)body.size() << " characters): ";
        cout << "\"" << body << "\"" << endl;
    }
    else
    {
        cout << "Failed to parse HTML snippet (with re-attached actor)!"
            << endl;
    }


string str;
rule<> strRule;
strRule = confix_p(str_p("\""), (*anychar_p)[actor_string(str)], str_p("\""));
string s="\"abcdefg\"";
cout << s << endl;
if(parse (s.c_str(), strRule).hit)
{
cout << str << endl;
}
else
{
cout << "不是字符串" << endl;
}


}

-------------------------------------------------------------------------------------------

發佈了37 篇原創文章 · 獲贊 32 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章