Re: X3 employee example with semantic actions

Henri Menke <[email protected]> Thu, 31 May 2018 09:50:25 +1200
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <[email protected]>

On 31/05/18 03:16, Maarten Verhage wrote:
> Dear people,
> 
> 
> 
> What would help me in understanding Spirit better is when I can clearly see
> how the parsed attributes are stored into the data structure. So, I’m aiming
> for semantic actions to do the work.

It is much easier than that.  Just add

#define BOOST_SPIRIT_DEBUG

before including any Spirit headers.  Then in the grammar of the 
employee example you add

BOOST_SPIRIT_DEBUG_NODE(quoted_string);
BOOST_SPIRIT_DEBUG_NODE(start);

This will make Spirit print the AST in XML.  Here is a shortened version 
of the output I got (the first line is my input):

employee{50, "Simpson", "Homer", 1500.00}
<start>
   <try>employee{50, "Simpso</try>
   <quoted_string>
     <try> "Simpson", "Homer",</try>
     <success>, "Homer", 1500.00}</success>
     <attributes>[[S, i, m, p, s, o, n]]</attributes>
   </quoted_string>
   <quoted_string>
     <try> "Homer", 1500.00}</try>
     <success>, 1500.00}</success>
     <attributes>[[H, o, m, e, r]]</attributes>
   </quoted_string>
   <success></success>
   <attributes>[[50, [S, i, m, p, s, o, n], [H, o, m, e, r], 
1500]]</attributes>
</start>
-------------------------
Parsing succeeded
got: [50, Simpson, Homer, 1500]

> 
> 
> 
> I was dealing with the employee example code in Spirit 2. But I realized
> that the type of an compound attribute can grow pretty complex. For example
> if I want the quotes themselves stored as well. See:
> 
> 
> 
> 
> 
> quoted_string = lexeme[char_('"') >> +(char_ - '"') >> char_('"')]
> 
>    [phoenix::bind( &assign_quoted_str, phoenix::ref(qi::_val), ::_1 ) ];
> 
> 
> 
> 
> 
> And the function:
> 
> 
> 
> void assign_quoted_str(
> 
>    std::string& val,
> 
>    const boost::fusion::vector<
> 
>    boost::fusion::vector< char, std::vector<char>, char >>& fvc )
> 
> {
> 
>    val = boost::fusion::at_c<0>( boost::fusion::at_c<0>( fvc ) );
> 
> 
> 
>    const std::vector<char>& vc =
> 
>      boost::fusion::at_c<1>( boost::fusion::at_c<0>( fvc ) );
> 
>    val.append( vc.begin(), vc.end() );
> 
> 
> 
>    val += boost::fusion::at_c<2>( boost::fusion::at_c<0>( fvc ) );
> 
> }
> 
> 
> 
> 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.
> 
> 
> 
> I hope I did explain my situation clearly. I’m certainly willing to explain
> something differently if I made some parts ambiguous. Let me know in that
> case.
> 
> 
> 
> Best regards,
> 
> Maarten Verhage
> 
> ------------------------------------------------------------------------------
> 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