Re: X3 employee example with semantic actions

Larry Evans <[email protected]> Thu, 31 May 2018 19:07:52 -0500
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <[email protected]>
On 05/31/2018 02:40 PM, Maarten Verhage wrote:
> 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. 

IIUC, you'd do that like the example employee.cpp does here:

https://github.com/boostorg/spirit/blob/develop/example/x3/employee.cpp#L108

Or am I missing what you mean by "properly instantiate"?

> 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), 

But this is exactly what parsers do with the attribute argument; hence,
I'm a bit puzzled by your question.

> and then dealing with some ostream. But
> the ostream part I can figure out myself.

IOW, once the attribute is created by the parser, you then can "figure 
out" how to output it to an ostream after the parser is done?

> 
> 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.

Are the examples here:

https://github.com/boostorg/spirit/blob/develop/example/x3/actions.cpp

lacking in some way?

> 
> 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
> 



------------------------------------------------------------------------------
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