Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Boost Spirit: Parse an int into a string.

Boost Spirit: Parse an int into a string.

Scheduled Pinned Locked Moved C / C++ / MFC
comdata-structuresdebuggingjsonquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    I am trying to parse an expression like this:

    F( 1, 2, 3)

    and extract 1, 2 and 3 as a strings. My test grammar and program parse properly, but the output is not "nice", In the start, limit and increment variable contain strings like "\x1", "\x2" and "\x3" This is part of my grammar. (the pre tag does not work "good" here) I also tried to read the numerical value as an int and use a boost::variant to hold either an int or std:wstring, but that did not work. Any thoughts, hints or tips ? Thanks. More friendly code here http://pastebin.com/2mmK9gns

    struct RangeResult
    {
    std::wstring start;
    std::wstring limit;
    std::wstring increment;
    };

    BOOST_FUSION_ADAPT_STRUCT(
    RangeGrammar::RangeResult,
    (std::wstring, start)
    (std::wstring, limit)
    (std::wstring, increment)
    )

    template struct range_parser : boost::spirit::qi::grammar
    {
    range_parser() : range_parser::base_type(start)
    {
    using qi::lit;
    using qi::lexeme;
    using qi::int_;
    using iso8859_1::char_;
    using iso8859_1::digit;

    		variable\_string %= lexeme\["$(" >> +(char\_ - ")") >> ")"\];
    
    		node = (int\_ | variable\_string);
    
    		start %=
    			lit("\_RANGE")
    			>> '('
    			>> node >> ',' >> node >> ',' >> node
    			>> ")\_"
    			;
    
    		debug(variable\_string);
    		debug(start);
    
    	}
    
    	qi::rule variable\_string;
    	qi::rule node;
    	qi::rule start;
    

    };

    My test program:

    void TestRangeGrammar()
    {
    using boost::spirit::iso8859_1::space;
    typedef std::wstring::const_iterator iterator_type;
    typedef RangeGrammar::range_parser range_parser;

    range\_parser grammar; // Our grammar
    
    std::array< std::wstring, 1 > testStrings =
    {{
    		\_TEXT("\_RANGE(1, 2, 3 )\_")
    }};
    
    for (auto str : testStrings)
    {
    	std::wstring::const\_iterator iter = str.begin();
    	std::wstring::const\_iterator end = str.end();
    
    	RangeGrammar::RangeResult emp;
    
    	bool r = phrase\_parse(iter, end, grammar, space, emp);
    	if (r)
    	{
    		std::wcout << \_TEXT("-------------------------\\n");
    		std::wcout << \_TEXT("Range Parsing succeeded\\n");
    		std::wcout << \_TEXT("Start: ") << emp.start << std::endl;
    		std::wcout << \_TEXT("Start: ") << emp.limit << std::endl;
    		std::wcou
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups