Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:38:52+00:00 2026-06-17T05:38:52+00:00

When I define in lexical analyzer typedef boost::mpl::vector<std::string, unsigned int, bool> token_value_types; lex::token_def<unsigned int>

  • 0

When I define in lexical analyzer

typedef boost::mpl::vector<std::string, unsigned int, bool>
            token_value_types;
lex::token_def<unsigned int> lit_uint("[0-9]+", token_ids::lit_uint);

and then use it in some grammar as

primary_expr =
        lexer.lit_uint
    |   lexer.true_or_false
    |   identifier
    |   '(' > expr > ')'
    ;

so how the string is converted to the value of correct token value type (unsigned int in this case)? What happens if you specify a custom type or floating-point type as a token value type? Where is the presence of conversion routine (I think something like boost::iterator_range to double conversion)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:38:53+00:00Added an answer on June 17, 2026 at 5:38 am

    The way to accomplish what you want is specializing assign_to_attribute_from_iterators. You can find an example with a custom type here. If you use double as the attribute in your token definition, spirit internally uses qi::double_ to parse the value. (You can find here the specialization for double and the rest of the fundamental types).

    Silly example where I define the real token as anything that is not a , or a ; to show the parsing of doubles.

    #define BOOST_SPIRIT_DEBUG
    #include <boost/spirit/include/lex_lexertl.hpp>
    #include <boost/spirit/include/qi.hpp>
    
    namespace lex = boost::spirit::lex;
    namespace qi = boost::spirit::qi;
    namespace mpl = boost::mpl;
    
    
    template <typename Lexer>
    struct my_lexer : lex::lexer<Lexer>
    {
        my_lexer()
        {
            real = "[^,;]*"; //anything that is not a , or ; is a real number
    
            this->self=lex::token_def<lex::omit>(',')| ';';
    
            this->self.add(real);
        }
        lex::token_def<double> real;
    };
    
    
    int main()
    {
        // the token type needs to know the iterator type of the underlying
        // input and the set of used token value types
        typedef lex::lexertl::token<std::string::iterator,
            mpl::vector<double> > token_type;
    
        // use actor_lexer<> here if your token definitions have semantic
        // actions
        typedef lex::lexertl::lexer<token_type> lexer_type;
    
        // this is the iterator exposed by the lexer, we use this for parsing
        typedef lexer_type::iterator_type iterator_type;
    
        // create a lexer instance
        std::string input("3.4,2,.4,4.,infinity,NaN,-3.8,1e2,1.5E3;");
        std::string::iterator s = input.begin();
    
        my_lexer<lexer_type> lex;
        iterator_type b = lex.begin(s, input.end());
    
        // use the embedded token_def as a parser, it exposes its token value type
        // as its parser attribute type
        std::vector<double> result;
        qi::rule<iterator_type,double()> number= lex.real;
        qi::rule<iterator_type,std::vector<double>()> sequence= number >> *(',' >> number) >> ';';
        BOOST_SPIRIT_DEBUG_NODE(number);
        BOOST_SPIRIT_DEBUG_NODE(sequence);
        if (!qi::parse(b, lex.end(), sequence, result))
        {
            std::cerr << "Parsing failed!" << std::endl;
            return -1;
        }
    
        std::cout << "Parsing succeeded:"  << std::endl;
        for(auto& n : result)
            std::cout << n << std::endl;
        return 0;
    }
    

    Edit: I have very little experience with regular expressions, but I believe that the token definition equivalent to the grammar linked in the comment (that I believe should have fractional_constant >> -exponent_part instead of fractional_constant >> !exponent_part) would be:

    template <typename Lexer>
    struct my_lexer : lex::lexer<Lexer>
    {
        my_lexer()
        {
            this->self.add_pattern("SIGN","[\\+\\-]");
            this->self.add_pattern("NAN","(1\\.0#)?(?i:nan)(\\([^\\)]\\))?");
            this->self.add_pattern("INF","(?i:inf(inity)?)");
            this->self.add_pattern("DIGIT","[0-9]");
            this->self.add_pattern("FRACT_CONST","{DIGIT}*\\.{DIGIT}+|{DIGIT}+\\.?");
            this->self.add_pattern("EXP","[eE]{SIGN}?{DIGIT}+");
    
            real = "{SIGN}?({NAN}|{INF}|{FRACT_CONST}{EXP}?|{DIGIT}+{EXP})";
    
            this->self=lex::token_def<lex::omit>(',')| ';';
    
            this->self.add(real);
        }
        lex::token_def<double> real;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#define LOG(format,...) Logger::Log(format,__VA_ARGS__) #define STRIP(netIp) GeneralUtils::inet_ntop_(netIp) string GeneralUtils::inet_ntop_(unsigned int netIp){ char strIP[INET_ADDRSTRLEN]; in_addr sin_addr;
#define MAX 100 struct bs{ int ab; int ac; }be; struct s{ be b;
I'm trying to build a simple lexical analyzer to go along with a simple
#define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #include <thread> using namespace std; void Hello() {}
#define f(g,g2) g##g2 main() { int var12=100; printf(%d,f(var,12)); } The above program prints 100
#define MAXBUF 1000 int buf[MAXBUF]; int buffered = 0; int bufp = 0; int
This is my lexical analyzer code when I enter as an input the following
Define the following C# interface: public interface IShape { int NumberOfLineSegments {get;} int Area
I am building a lexical analyzer in Ruby and am about to start gathering
(define (subtract-1 n) (string-append Number is: (number->string n)) (cond [(= n 0) All done!]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.