Re: newbie here ask about qi::_val and semantic action
Yi Leong <[email protected]> Mon, 9 Jul 2018 15:17:46 +0800
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <CAMFq=pbX8DEfT87miKxW2yoW3Mb7yzNDCJ15PSxxhXTnr_ExtQ@mail.gmail.com> |
Thanks! I made a mistake about the placeholder, so the return value in
the following:
double print_action(double f)
{
return f; // this value will be assigned to the rule r's attribute d?
}
rule<std::string::const_iterator, double() > r =
qi::double_[&print_action];
will be assigned to the rule r's attribute d?
On Mon, Jul 9, 2018 at 3:03 PM, Henri Menke <[email protected]> wrote:
> On 09/07/18 17:38, Yi Leong wrote:
>
>> yeah, in the qi::double_[qi::_val = qi::_1] , the semantic action is a
>> lambda expression, but as the following:
>>
>> #include <iostream>
>> #include <string>
>>
>> #include <boost/spirit/include/phoenix.hpp>
>> #include <boost/spirit/include/qi.hpp>
>>
>> void print_action(double f)
>> {
>> qi::_val = qi::_1; // it does not work....
>> }
>>
>>
> print_action already has a callable type, so you can use it in the
> semantic action without placeholders.
>
> The placeholders are useful if you have a oneline semantic action for
> which you don't want to write an extra callable. See the docs for details:
> https://www.boost.org/libs/spirit/doc/html/spirit/qi/tutoria
> ls/semantic_actions.html
>
> #include <iostream>
> #include <string>
>
> #include <boost/spirit/include/phoenix.hpp>
> #include <boost/spirit/include/qi.hpp>
>
> double print_action(double f)
> {
> std::cout << "Attribute value: " << f << "\n";
> return f;
> }
>
>
> int main() {
> std::string s = "1234.56789";
> double d;
>
> std::string::iterator first = s.begin();
> std::string::iterator last = s.end();
>
> namespace qi = boost::spirit::qi;
> bool r = qi::phrase_parse(first, last, qi::double_ [&print_action],
> qi::space, d);
>
> if (!r || first != last) {
> std::cerr << "Error while parsing. Rest: " << std::string(first,
> last) << "\n";
> }
> std::cout << "Success! " << d << "\n";
> }
>
> int main() {
>> std::string s = "1234.56789";
>> double d;
>>
>> std::string::iterator first = s.begin();
>> std::string::iterator last = s.end();
>>
>> namespace qi = boost::spirit::qi;
>> bool r = qi::phrase_parse(first, last, qi::double_ [&print_action],
>> qi::space, d);
>>
>> if (!r || first != last) {
>> std::cerr << "Error while parsing. Rest: " <<
>> std::string(first, last) << "\n";
>> }
>> std::cout << "Success! " << d << "\n";
>>
>> }
>>
>> The above code gets compiling error, so is the placeholder qi::_val
>> usable only in the lambda expression, right?
>>
>> On Mon, Jul 9, 2018 at 10:06 AM, Henri Menke <[email protected]
>> <mailto:[email protected]>> wrote:
>>
>> Elaborate. The following works.
>>
>> #include <iostream>
>> #include <string>
>>
>> #include <boost/spirit/include/phoenix.hpp>
>> #include <boost/spirit/include/qi.hpp>
>>
>> int main() {
>> std::string s = "1234.56789";
>> double d;
>>
>> std::string::iterator first = s.begin();
>> std::string::iterator last = s.end();
>>
>> namespace qi = boost::spirit::qi;
>> bool r = qi::phrase_parse(first, last, qi::double_ [qi::_val =
>> qi::_1], qi::space, d);
>>
>> if (!r || first != last) {
>> std::cerr << "Error while parsing. Rest: " <<
>> std::string(first, last) << "\n";
>> }
>> std::cout << "Success! " << d << "\n";
>>
>> }
>>
>>
>> On 09/07/18 03:40, Yi Leong wrote:
>>
>> Hi,
>> I set qi::_val in semantic action function , but it does not work.
>> Is the placeholder _val only usable in lambda expression?
>>
>>
>>
>>
>>
>> ------------------------------------------------------------
>> ------------------
>> 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]
>> <mailto:[email protected]>
>> https://lists.sourceforge.net/lists/listinfo/spirit-general
>> <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]
>> <mailto:[email protected]>
>> https://lists.sourceforge.net/lists/listinfo/spirit-general
>> <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
>>
>>
> ------------------------------------------------------------
> ------------------
> 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