Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created September 20, 2016 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefftrull/983d783cf508249e5352de8e844a27cb to your computer and use it in GitHub Desktop.
Save jefftrull/983d783cf508249e5352de8e844a27cb to your computer and use it in GitHub Desktop.
Interesting (possibly wrong) synthesized attribute behavior
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/optional.hpp>
#include <boost/optional/optional_io.hpp>
int main() {
using namespace std;
// parsing parentheses that may or may not contain integers
string in("(10) () (20)");
vector<boost::optional<int>> result;
auto beg = in.begin();
auto end = in.end();
using namespace boost::spirit::qi;
phrase_parse(beg, end,
*('(' > ((int_|eps) ^ eps) > ')'), // <<<< second in sequence uninit instead of "not present"
ascii::space,
result);
copy(result.begin(), result.end(),
ostream_iterator<boost::optional<int>>(cout, " "));
cout << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment