2012-05-02 31 views
7

मैं boost::spirit सीखने की कोशिश कर रहा हूं। उदाहरण के तौर पर, मैं vector<string> में शब्दों के अनुक्रम को पार्स करने का प्रयास कर रहा हूं।वेक्टर में शब्दों के अनुक्रम को पार्स करने के लिए boost :: spirit का उपयोग कैसे करें?

#include <boost/spirit/include/qi.hpp> 
#include <boost/foreach.hpp> 

namespace qi = boost::spirit::qi; 

int main() { 

    std::vector<std::string> words; 
    std::string input = "this is a test"; 

    bool result = qi::phrase_parse(
     input.begin(), input.end(), 
     +(+qi::char_), 
     qi::space, 
     words); 

    BOOST_FOREACH(std::string str, words) { 
    std::cout << "'" << str << "'" << std::endl; 
    } 
} 

जो मुझे इस उत्पादन देता है:

'thisisatest' 

लेकिन मैं निम्नलिखित उत्पादन है, जहां प्रत्येक शब्द अलग से मिलान किया जाता है चाहता था:

'this' 
'is' 
'a' 
'test' 

संभव हो तो, मैं मैं इस कोशिश की डी इस साधारण मामले के लिए अपने qi::grammar सबक्लास को परिभाषित करने से बचने के लिए पसंद है।

उत्तर

13

आप मौलिक (या कम से कम का दुरुपयोग) एक छोड़ पार्सर – qi::space, एक को छोड़ पार्सर के रूप में इस्तेमाल करने के उद्देश्य से गलत समझ रहे हैं, वहाँ a b और ab के बीच कोई अंतर नहीं है ऐसी है कि अपने पार्सर खाली स्थान के नास्तिक बनाने के लिए है।

आपके मामले में, व्हाइटस्पेस महत्वपूर्ण है, क्योंकि आप इसे शब्दों को सीमित करना चाहते हैं। नतीजतन, आप सफेद स्थान लंघन नहीं किया जाना चाहिए, और आप qi::phrase_parse बजाय qi::parse उपयोग करना चाहते हैं:

#include <vector> 
#include <string> 
#include <iostream> 
#include <boost/foreach.hpp> 
#include <boost/spirit/include/qi.hpp> 

int main() 
{ 
    namespace qi = boost::spirit::qi; 

    std::string const input = "this is a test"; 

    std::vector<std::string> words; 
    bool const result = qi::parse(
     input.begin(), input.end(), 
     +qi::alnum % +qi::space, 
     words 
    ); 

    BOOST_FOREACH(std::string const& str, words) 
    { 
     std::cout << '\'' << str << "'\n"; 
    } 
} 

(अब जी Civardi के ठीक साथ अद्यतन।)

+0

अच्छी तरह से समझाया गया, +1 – sehe

2

मेरा मानना ​​है कि यह कम से कम संस्करण है। qi :: qi सूची पार्सर विभाजक पर लागू ओमिट आवश्यक नहीं है - यह किसी आउटपुट विशेषता उत्पन्न नहीं करता है। जानकारी के लिए देखें: http://www.boost.org/doc/libs/1_48_0/libs/spirit/doc/html/spirit/qi/reference/operator/list.html

#include <string> 
#include <iostream> 
#include <boost/foreach.hpp> 
#include <boost/spirit/include/qi.hpp> 

int main() 
{ 
    namespace qi = boost::spirit::qi; 

    std::string const input = "this is a test"; 

    std::vector<std::string> words; 
    bool const result = qi::parse(
     input.begin(), input.end(), 
     +qi::alnum % +qi::space, 
     words 
); 

    BOOST_FOREACH(std::string const& str, words) 
    { 
     std::cout << '\'' << str << "'\n"; 
    } 
} 
1

शायद ज़रुरत पड़े किसी और प्रमुख स्थानों में से मेरी समस्या में पड़।

मैं ildjarn के समाधान का उपयोग कर रहा था, जब तक कि मैं कुछ रिक्त स्थान से शुरू होने वाली स्ट्रिंग में भाग नहीं लेता।

std::string const input = " this is a test"; 

मुझे यह पता लगाने में थोड़ी देर लग गई कि प्रमुख स्थान फ़ंक्शन qi :: parse (...) की विफलता की ओर जाता है। समाधान qi :: parse() को कॉल करने से पहले इनपुट रिक्त स्थान को ट्रिम करना है।

संबंधित मुद्दे

 संबंधित मुद्दे