Skip to content

Instantly share code, notes, and snippets.

@sehe sehe/CMakeLists.txt
Last active Dec 2, 2016

What would you like to do?
old fashioned grammar struct; using X3
#include "b.hpp"
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace parsers {
namespace x3 = boost::spirit::x3;
namespace internal {
namespace /*file-static*/ {
auto the_usual = x3::rule<struct tag, std::string> { "the_usual" }
= x3::lexeme [ +x3::graph ];
template <typename It, typename Attr>
bool subgrammar<It, Attr>::parse(It& f, It const& l, Attr& attr) const {
return x3::parse(f, l, internal::the_usual, attr);
// do the explicit instantiations that you wanted
namespace parsers {
template struct subgrammar<std::string::const_iterator, std::string>;
template struct subgrammar<char const*, std::string>;
#include <boost/spirit/home/x3/support/unused.hpp>
namespace parsers {
using boost::spirit::x3::unused_type;
namespace internal { }
template <typename It, typename Attr = unused_type> struct subgrammar {
bool parse(It& f, It const& l, Attr& attr) const;
#include <vector>
namespace parsers {
// forward declare/extern declare some template instantiations
extern template struct subgrammar<std::string::const_iterator, std::string>;
extern template struct subgrammar<char const*, std::string>;
ADD_EXECUTABLE(demo test.cpp b.cpp)
-std=c++1z \
-g0 -O3 -pthread -march=native")
#include "b.hpp"
#include <iostream>
int main() {
std::string const input("hello world");
parsers::subgrammar<std::string::const_iterator, std::string> g;
std::string word;
auto f = input.begin(), l = input.end();
if (g.parse(f, l, word))
std::cout << "Parsed: '" << word << "'\n";
std::cout << "Failed\n";
if (f != l)
std::cout << "remaining unparsed: '" << std::string(f,l) << "'\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.