boost spirit - Retrieving the ID of a token_def -


Is it possible to retrieve the token ID defined in lexer like this: token_def & lt; & Gt; The meaning of a soul parser is from inside the verb.

What I am trying to do is to use the token id of every operator (like +, -, *, etc.) To do. And recieve the same name for that operator from within the parser semantic action (eg, add, subtract, times, etc.)

As far as I understand, in production:

toks.symbol> & Gt; Toks.plus & gt; & Gt; Toks.symbol;

If toks.plus is or type token_def & gt; Plus; , _1 will first refer to toks.symbol and _2 another toks.symbol . is it true? If so, how do I get to the middle token (to recover the ID)?

Thank you!

You can use the lazy placeholder you created, see the docs:

    < Li>

    I have optimized to print token IDs on the fly:

      #include & lt; Boost / config / warning_disable.hpp & gt; #include & lt; Boost / spirit / include / lex_lexertl.hpp & gt; # Include & lt; Boost / spirit / include / phoenix_operator.hpp & gt; #include & lt; Boost / spirit / include / phoenix_statement.hpp & gt; #include & lt; Boost / spirit / include / phoenix_algorithm.hpp & gt; # Include & lt; Boost / spirit / include / phoenix_core.hpp & gt; # Include & lt; Iostream & gt; #include & lt; String & gt; Namespace Lex = boost :: spirit :: lex; Namespace phx = boost :: phoenix; Struct distance_func {template & lt; Typename iterator1, typewriter iterator2 & gt; Structure Result: Promotion: Eterator_defrence & lt; Iterator1 & gt; {}; Template & lt; Typename iterator1, typewriter iterator2 & gt; The result of the typename & lt; Iterator1, Iterator2 & gt; :: type operator () (Iterator1 & start, Iterator2 and end) const {return std :: distance (start, end); }}; Promote: Phoenix :: The function's & lt; Distance_func & gt; Const distance = distance_func (); Template & lt; Typename Lexer & gt; Struct word_count_tokens: lex :: lexer & lt; Lexer & gt; {Word_count_tokens (): c (0), w (0), l (0), word ("[^ \ t \ n] +") // defined token, EOL ("\ n"), any (" ") {Boost :: spirit :: lex :: _ start; Promotion :: emotion :: lex :: _ end; Boost :: spirit :: lex :: _ tokenid; Boost: Phoenix :: Riff; Collaborative token with Lexer-gt; Self = word [++ re (w), ref (c) + = distance (_start, _end), phx :: ref (std :: cout) & lt; & Lt; _tokenid & lt; & Lt; ","] | EOL [++ referee (C), ++ referee (l), phx :: ref (STD :: cout) & lt; & Lt; _tokenid & lt; & Lt; ","] | Any [++ Ref (C), Phx :: Ref (Stud :: Ceigh) & lt; & Lt; _tokenid & lt; & Lt; ","]; } Std :: size_t c, w, l; Lex :: token_def & lt; & Gt; Words, EOL, Anyone; }; //////////////////////////////////////////////// ///////////////////////////// At the main (int argc, four * argv []) {typedef lex :: lexertl :: token & Lt; Four Conte *, Lex :: Blank, Boost :: Emple :: False_ & gt; Token_type; Typing Lex :: Lexterl :: Actor_Luxer & lt; Token_type & gt; Lexer_type; Word_count_tokens & lt; Lexer_type & gt; Word_count_lexer; Std :: string str ("lazy moon jumped on colorless mold"); Four const * first = str.c_str (); Char const * last = and first [str.size ()]; Lexer_type :: iterator_type iter = word_count_lexer.begin (first, last); Lexer_type :: iterator_type end = word_count_lexer.end (); While (itter! = End & token_is_widid (* etter)) ++ year; If (Iter == end) {std :: cout < & Lt; "\ Nlines:" & lt; & Lt; Word_count_lexer.l & lt; & Lt; ", Word:" & lt; & Lt; Word_count_lexer.w & lt; & Lt; ", Characters:" & lt; & Lt; Word_count_lexer.c & lt; & Lt; "\ N"; } Else {std :: string rest (first, last); Std :: cout & lt; & Lt; "Lexical analysis failed \ n" & lt; & Lt; "It was closed: \" "& lt;   

    Output:

      65536; 65536; 65538; 65536; 65538; 65536; 65538; 65536; 65538; 65536; 65538; 65536; 65,538; 65536; Lines: 0, word: 8, characters: 41    

Comments