1

Concretely, using the grammar g, how do I parse the string s ? What arguments should I give ? I've tried many calls and always got errors.

Also, since I'm not sure yet which one I will use later, would there be any difference using phrase_parse instead ?

namespace qi = boost::spirit::qi;
int main() {
    My_grammar<std::string::const_iterator> g;
    std::string s = "a"; // string to parse
    if (qi::parse( /*...*/ )) {
        std::cout << "String parsed !";
    } else {
        std::cout << "String doesn't parse !";
    }
    return EXIT_SUCCESS;
}
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
LogicalKip
  • 514
  • 4
  • 13

2 Answers2

3

Basically, you should look in the tutorial, but part of the issue is, you more or less need to create a variable to hold the start iterator. Because, it is passed by reference to qi::parse, and where exactly it stops can be considered an output of the qi::parse function. If you try to pass it by s.begin() it won't work because then you are trying to bind a reference to a temporary.

namespace qi = boost::spirit::qi;
int main() {
    My_grammar<std::string::const_iterator> g;
    std::string s = "a"; // string to parse
    std::string::const_iterator it = s.begin(); // The type declaration here
    std::string::const_iterator end = s.end(); // and here needs to match template parameter
                                               // which you used to instantiate g

    if (qi::parse( it, end, g )) {
        std::cout << "String parsed !";
    } else {
        std::cout << "String doesn't parse !";
    }
    return EXIT_SUCCESS;
}

You use phrase_parse only when you want to explicitly specify a skip grammar.

Chris Beck
  • 15,614
  • 4
  • 51
  • 87
  • This gives me several errors, the first of which being : required from ‘bool boost::spirit::qi::parse(Iterator&, Iterator, Expr&) [with Iterator = __gnu_cxx::__normal_iterator >; Expr = My_grammar<__gnu_cxx::__normal_iterator > >]’ – LogicalKip Sep 10 '15 at 15:00
  • I changed the answer, its possible that `auto` is not deducing `std::string::const_iterator` exactly, which is needed for the above to work. Also you didn't mark it C++11 so I shouldn't have used `auto` :X – Chris Beck Sep 10 '15 at 15:02
2

Yes there will be a difference.

phrase_parse uses a skipper and might not consume all input and still return true.

In all other respects, the two are identical. You should really just hit the documentation:

 std::string::const_iterator f(s.begin(), l(s.end());

 if (qi::parse(f, l, g/*, optional, bound, attribute, references*/))
sehe
  • 374,641
  • 47
  • 450
  • 633
  • The examples were only with on-the-fly grammars and phrase_parse (therefore my second question), I wasn't sure if it changed anything. As for the commentless "Iterator" parameters, once I checked the full code of the examples, I saw it used s.begin() and s.end(), and thus so did I. It seems the problem was actually because I have to declare them as variables and not simply give the value (made sense to me at the moment). – LogicalKip Sep 10 '15 at 15:18
  • Hehe. You forgot to add these pieces of confusion to the question it seems. Did you talk to Chris about this question (or did he just guess)? – sehe Sep 10 '15 at 17:05
  • I just guessed, I think I made similar mistakes when I started – Chris Beck Sep 10 '15 at 18:05