Re: X3 employee example with semantic actions

Maarten Verhage <[email protected]> Thu, 31 May 2018 19:40:52 +0000
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <AM5P192MB0131AEE9A9D3C120A3403D55BA630@AM5P192MB0131.EURP192.PROD.OUTLOOK.COM>
As top posting is not aloud, my message is way down here,
Maarten

----- Original Message ----- 
From: "Larry Evans" <[email protected]>
To: <[email protected]>
Sent: Thursday, May 31, 2018 20:37
Subject: Re: [Spirit-general] X3 employee example with semantic actions


> On 05/30/2018 12:00 PM, Larry Evans wrote:
> > On 05/30/2018 10:16 AM, Maarten Verhage wrote:
> [snip]
>
> >> So, I thought how nice it would have been to get a pair
> >> of iterators into the “parsed” result once Spirit
> >> determined the rule to match. But I couldn’t found this
> >> facility in Spirit 2. But Spirit X3 has the Context
> >> object to be able to call the _where member function to
> >> get this right? And then just copy it to a memory area
> >> that will become my new file. For the file contents I do
> >> not want to modify this would be a pretty efficient way
> >> right?
>
> >> The obstacle I’m facing right now (and hope a list member
> >> here would be able to help me on) is this: As I do want
> >> manually store stuff using semantic actions how do I need
> >> to modify the Spirit X3 employee example code to 1)
> >> Prevent spirit to automatically store it. And 2) How do I
> >> need to provide the empty employee object to this
> >> client::parser namespace area as this is not a class
> >> anymore.
>
> Maarten, I apologize for not reading the above more
> carefully before posting my reply below.  My mind was
> focused on your previous post:
>
>   https://sourceforge.net/p/spirit/mailman/message/36329960/
>
> where you say:
>
>   It was pretty printing I was aiming for. According to
>   Seth’s suggestion I plan to do it without karma. Once I’ve
>   a parsed data structure I can manually print it out to a
>   ostream. And indent  based on what I encounter in this
>   data structure.
>
> However, now that I've more carefully read your above post,
> it appears, based on what you say here:
>
>   I need to modify the Spirit X3 employee example code to 1)
>   Prevent spirit to automatically store it. And 2) How do I need to 
> provide
>   the empty employee object to this client::parser namespace area as
>   this is not a class anymore.
>
> That:
>
>   1) You **don't** want x3 parser to store into any
>      attribute; hence, you'd pass unused as opposed to emp
>      as the attribute argument to:
>
> https://github.com/boostorg/spirit/blob/develop/example/x3/employee.cpp#L111
>
>   2) Instead of relying on x3 to construct the attributes,
>      you would define semantic actions which would, **as the
>      parsing progresses**, write out to some ostream, the
>      result of the parse.  IOW, there would be some
>      **global** declaration:
>
>   std::fostream* out_ptr=0;
>
>      which might be initialized in main with something like:
>
>   int main
>   (
>     .
>     .
>     .
>       std::ofstream
>     stream_out("some_output_file_name", std::ios_base::out);
>     std::fostream out_str=&stream_out;
>     .
>     .
>     .
>     return 0;
>   }
>
>      and there would be semantic actions between out_ptr and
>      main which write to out_ptr and are used in the x3
>      grammar somehow.
>
>      Is that about what you were aiming for?
> >
> > Maarten, why couldn't you define:
> >
> >    std::ostream&
> > operator<<
> >    ( std::ostream& sout
> >    , ast_from_parse_t const& ast
> >    )
> >    {
> >      //then print the ast to sout here using whatever
> >      //method.
> >    }
> >
> > That way you wouldn't have to wrestle with semantic actions at all,
> > unless I'm missing something (happens occasionally).
> >
> > -regards,
> > Larry
> >
> >
> >
> ------------------------------------------------------------------------------
>
> >
> > Check out the vibrant tech community on one of the world's most
> > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> > _______________________________________________
> > Spirit-general mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/spirit-general
>
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Spirit-general mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/spirit-general
>
Hi Larry and the people who read along,

Thanks that was much more helpful. Thanks for the effort to dive deeper into 
my parser application goal. I do realize I've to be more clear.

The issue is not about the ostream or ofstream. It is about where I do need 
to properly instantiate the employee object. And in the semantec actions how 
I can gain access to that object. Maybe it turns out to be very simple. But 
as Spirit shows so many advanced C++ stuff I'm questioning many things to 
check whether that is a recommended way to build upon the examples.

My intention is to first store the file contents into a data structure (for 
this example the employee object), and then dealing with some ostream. But 
the ostream part I can figure out myself.

Apart for the documentation examples Are there more good Spirit x3 
application examples. Preferably with semantic actions to teach me to 
utilize most of this API facilities. For my understanding **I have** to see 
real examples in which something is used to understand it's intent.

Regards, Maarten 

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general