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

  • Home
  • SEARCH
  • 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 8214327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:25:30+00:00 2026-06-07T11:25:30+00:00

I need to parse and generate some texts from and to c++ objects. The

  • 0

I need to parse and generate some texts from and to c++ objects.

The syntax is:

command #param #param #param

There is set of commands some of them have no params etc.
Params are mainly numbers.

The question is: Should I use Boost Spirit for this task? Or just simply tokenize each line evaluate function to call from string compare with command, read additional parameters and create cpp object from it?

If you suggest using Spirit or any other solution it would be nice if you could provide some examples similiar to my problem. I’ve read and tried all examples from Boost Spirit doc.

  • 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-07T11:25:32+00:00Added an answer on June 7, 2026 at 11:25 am

    I implemented more or less precisely this in a previous answer to the question ” Using boost::bind with boost::function: retrieve binded variable type “.

    The complete working sample program (which expects a very similar grammar) using Boost Spirit is here: https://gist.github.com/1314900. You’d just want to remove the /execute literals for your grammar, so edit Line 41 from

        if (!phrase_parse(f,l, "/execute" > (
    

    to

        if (!phrase_parse(f,l, (
    

    The example script

    WriteLine "bogus"
    Write     "here comes the answer: "
    Write     42
    Write     31415e-4
    Write     "that is the inverse of" 24 "and answers nothing"
    Shutdown  "Bye" 9
    Shutdown  "Test default value for retval"
    

    Now results in the following output after execution:

    WriteLine('bogus');
    Write(string: 'here comes the answer: ');
    Write(double: 42);
    Write(double: 3.1415);
    Write(string: 'that is the inverse of');
    Write(double: 24);
    Write(string: 'and answers nothing');
    Shutdown(reason: 'Bye', retval: 9)
    Shutdown(reason: 'Test default value for retval', retval: 0)
    

    Full Code

    For archival purposes:

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    #include <fstream>
    
    namespace qi  = boost::spirit::qi;
    namespace phx = boost::phoenix;
    
    ///////////////////////////////////
    // 'domain classes' (scriptables)
    struct Echo
    {
        void WriteLine(const std::string& s) { std::cout << "WriteLine('"     << s << "');" << std::endl; }
        void WriteStr (const std::string& s) { std::cout << "Write(string: '" << s << "');" << std::endl; }
        void WriteInt (int i)                { std::cout << "Write(int: "     << i <<  ");" << std::endl; }
        void WriteDbl (double d)             { std::cout << "Write(double: "  << d <<  ");" << std::endl; }
        void NewLine  ()                     { std::cout << "NewLine();"                    << std::endl; }
    } echoService;
    
    struct Admin
    {
        void Shutdown(const std::string& reason, int retval) 
        { 
            std::cout << "Shutdown(reason: '" << reason << "', retval: " << retval << ")" << std::endl;
            // exit(retval);
        }
    } adminService;
    
    void execute(const std::string& command)
    {
        typedef std::string::const_iterator It;
        It f(command.begin()), l(command.end());
    
        using namespace qi;
        using phx::bind;
        using phx::ref;
    
        rule<It, std::string(), space_type> stringlit = lexeme[ '"' >> *~char_('"') >> '"' ];
    
        try
        {
            if (!phrase_parse(f,l, /*"/execute" >*/ (
                    (lit("WriteLine")  
                        > stringlit  [ bind(&Echo::WriteLine, ref(echoService), _1) ])
                  | (lit("Write") >> +(
                          double_    [ bind(&Echo::WriteDbl,  ref(echoService), _1) ] // the order matters
                        | int_       [ bind(&Echo::WriteInt,  ref(echoService), _1) ]
                        | stringlit  [ bind(&Echo::WriteStr,  ref(echoService), _1) ]
                    ))
                  | (lit("NewLine")  [ bind(&Echo::NewLine,   ref(echoService)) ])
                  | (lit("Shutdown")  > (stringlit > (int_ | attr(0))) 
                                     [ bind(&Admin::Shutdown, ref(adminService), _1, _2) ])
                ), space))
            {
                if (f!=l) // allow whitespace only lines
                    std::cerr << "** (error interpreting command: " << command << ")" << std::endl;
            }
        }
        catch (const expectation_failure<It>& e)
        {
            std::cerr << "** (unexpected input '" << std::string(e.first, std::min(e.first+10, e.last)) << "') " << std::endl;
        }
    
        if (f!=l)
            std::cerr << "** (warning: skipping unhandled input '" << std::string(f,l) << "')" << std::endl;
    }
    
    int main()
    {
        std::ifstream ifs("input.txt");
    
        std::string command;
        while (std::getline(ifs/*std::cin*/, command))
            execute(command);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse C#, Ruby and Python source code to generate some reports.
Hi I need to generate some files on server side and return them to
I need parse through a file and do some processing into it. The file
I need to parse a bunch of incoming xml documents, they all have the
I need to parse the bytes from a file so that I only take
I have seen some examples of php scripts that read email headers and parse
Here's the problem I have, I need to generate a flat string file with
I am writing a client .NET app and need to generate some reports. I
I need to generate SVG from given DXF file. I try to archive that
Hi I need parse and deserialize pseudo JSON string. Input data: {aBubbleData[ 'jaja2581' ]={

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.