2012-11-13 16 views
5

Cerco di imparare ad usare boost :: spirit. Per fare ciò, volevo creare un semplice lexer, combinarli e iniziare l'analisi usando lo spirito. Ho provato a modificare l'esempio, ma non funziona come previsto (il risultato r non è vero).Problemi con boost :: spirit :: lex & whitespace

Ecco il lexer:

#include <boost/spirit/include/lex_lexertl.hpp> 

namespace lex = boost::spirit::lex; 

template <typename Lexer> 
struct lexer_identifier : lex::lexer<Lexer> 
{ 
    lexer_identifier() 
     : identifier("[a-zA-Z_][a-zA-Z0-9_]*") 
     , white_space("[ \\t\\n]+") 
    { 
     using boost::spirit::lex::_start; 
     using boost::spirit::lex::_end; 

     this->self = identifier; 
     this->self("WS") = white_space; 
    } 
    lex::token_def<> identifier; 
    lex::token_def<> white_space; 
    std::string identifier_name; 
}; 

e questo è l'esempio che sto cercando di eseguire:

#include "stdafx.h" 

#include <boost/spirit/include/lex_lexertl.hpp> 
#include "my_Lexer.h" 

namespace lex = boost::spirit::lex; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    typedef lex::lexertl::token<char const*,lex::omit, boost::mpl::false_> token_type; 
    typedef lex::lexertl::lexer<token_type> lexer_type; 

    typedef lexer_identifier<lexer_type>::iterator_type iterator_type; 

    lexer_identifier<lexer_type> my_lexer; 

    std::string test("adedvied das934adf dfklj_03245"); 

    char const* first = test.c_str(); 
    char const* last = &first[test.size()]; 

    lexer_type::iterator_type iter = my_lexer.begin(first, last); 
    lexer_type::iterator_type end = my_lexer.end(); 

    while (iter != end && token_is_valid(*iter)) 
    { 
     ++iter; 
    } 

    bool r = (iter == end); 

    return 0; 
} 

r è vero finché c'è solo una pedina all'interno della stringa. Perché è così?

Regards Tobias

risposta

10

È stato creato un secondo stato lexer, ma mai invocato.

Semplificare e profitto:


Per la maggior parte dei casi, il modo più semplice per avere l'effetto desiderato potrebbe essere quella di utilizzare un solo Stato Lexing con una bandiera pass_ignore sui gettoni skippable:

this->self += identifier 
       | white_space [ lex::_pass = lex::pass_flags::pass_ignore ]; 

Nota che è necessario un actor_lexer per consentire l'azione semantica:

typedef lex::lexertl::actor_lexer<token_type> lexer_type; 

campione intero:

#include <boost/spirit/include/lex_lexertl.hpp> 
#include <boost/spirit/include/lex_lexertl.hpp> 
namespace lex = boost::spirit::lex; 

template <typename Lexer> 
struct lexer_identifier : lex::lexer<Lexer> 
{ 
    lexer_identifier() 
     : identifier("[a-zA-Z_][a-zA-Z0-9_]*") 
     , white_space("[ \\t\\n]+") 
    { 
     using boost::spirit::lex::_start; 
     using boost::spirit::lex::_end; 

     this->self += identifier 
        | white_space [ lex::_pass = lex::pass_flags::pass_ignore ]; 
    } 
    lex::token_def<> identifier; 
    lex::token_def<> white_space; 
    std::string identifier_name; 
}; 

int main(int argc, const char *argv[]) 
{ 
    typedef lex::lexertl::token<char const*,lex::omit, boost::mpl::false_> token_type; 
    typedef lex::lexertl::actor_lexer<token_type> lexer_type; 

    typedef lexer_identifier<lexer_type>::iterator_type iterator_type; 

    lexer_identifier<lexer_type> my_lexer; 

    std::string test("adedvied das934adf dfklj_03245"); 

    char const* first = test.c_str(); 
    char const* last = &first[test.size()]; 

    lexer_type::iterator_type iter = my_lexer.begin(first, last); 
    lexer_type::iterator_type end = my_lexer.end(); 

    while (iter != end && token_is_valid(*iter)) 
    { 
     ++iter; 
    } 

    bool r = (iter == end); 
    std::cout << std::boolalpha << r << "\n"; 
} 

Stampe

true 

"WS" come uno stato Skipper


E 'anche possibile che si è imbattuto in un esempio che utilizza il secondo stato parser per il comandante (lex::tokenize_and_phrase_parse). Lasciami prendere un minuto o 10 per creare un campione funzionante per quello.

Aggiornamento ho messo un po 'più di 10 minuti (Waaaah) :) Ecco un test comparativo, che mostra come gli stati lexer interagiscono, e come utilizzare lo Spirito Skipper analisi per richiamare il secondo stato parser:

#include <boost/spirit/include/qi.hpp> 
#include <boost/spirit/include/lex_lexertl.hpp> 
namespace lex = boost::spirit::lex; 
namespace qi = boost::spirit::qi; 

template <typename Lexer> 
struct lexer_identifier : lex::lexer<Lexer> 
{ 
    lexer_identifier() 
     : identifier("[a-zA-Z_][a-zA-Z0-9_]*") 
     , white_space("[ \\t\\n]+") 
    { 
     this->self  = identifier; 
     this->self("WS") = white_space; 
    } 
    lex::token_def<> identifier; 
    lex::token_def<lex::omit> white_space; 
}; 

int main() 
{ 
    typedef lex::lexertl::token<char const*, lex::omit, boost::mpl::true_> token_type; 
    typedef lex::lexertl::lexer<token_type> lexer_type; 

    typedef lexer_identifier<lexer_type>::iterator_type iterator_type; 

    lexer_identifier<lexer_type> my_lexer; 

    std::string test("adedvied das934adf dfklj_03245"); 

    { 
     char const* first = test.c_str(); 
     char const* last = &first[test.size()]; 

     // cannot lex in just default WS state: 
     bool ok = lex::tokenize(first, last, my_lexer, "WS"); 
     std::cout << "Starting state WS:\t" << std::boolalpha << ok << "\n"; 
    } 

    { 
     char const* first = test.c_str(); 
     char const* last = &first[test.size()]; 

     // cannot lex in just default state either: 
     bool ok = lex::tokenize(first, last, my_lexer, "INITIAL"); 
     std::cout << "Starting state INITIAL:\t" << std::boolalpha << ok << "\n"; 
    } 

    { 
     char const* first = test.c_str(); 
     char const* last = &first[test.size()]; 

     bool ok = lex::tokenize_and_phrase_parse(first, last, my_lexer, *my_lexer.self, qi::in_state("WS")[my_lexer.self]); 
     ok = ok && (first == last); // verify full input consumed 
     std::cout << std::boolalpha << ok << "\n"; 
    } 
} 

l'uscita è

Starting state WS: false 
Starting state INITIAL: false 
true 
+0

Aggiunta la "WS" approccio stato con demo sotto ** ' "WS" come state' Skipper **. Cheers – sehe

+0

Oops. Ho copiato la dichiarazione token_type sbagliata. Ha bisogno di 'mpl :: true_' per [' HasState'] (http://www.boost.org/doc/libs/1_49_0/libs/spirit/doc/html/spirit/lex/abstracts/lexer_primitives/lexer_token_values.html # spirit.lex.abstracts.lexer_primitives.lexer_token_values.the_anatomy_of_a_token), quando si ha a che fare con i lexer di stato - ovviamente! *** Risolto *** – sehe

+0

- grazie per il tuo ampio esempio. Ho ancora alcune domande: cosa fa lex :: omit? E riguardo alla chiamata tokenize_and_parse: cosa è my_lexer.self e qi :: in_state ("WS") [my_lexer.self]? –

Problemi correlati