Skip to content

Instantly share code, notes, and snippets.

@mhhollomon
Last active September 17, 2018 22:37
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 mhhollomon/475312dbdce7d574cbb3b6983b2a4b60 to your computer and use it in GitHub Desktop.
Save mhhollomon/475312dbdce7d574cbb3b6983b2a4b60 to your computer and use it in GitHub Desktop.
first attempt to parse identifiers
#include <iostream>
#include <string>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
//****************************
// The parser
//****************************
namespace x3 = boost::spirit::x3;
using x3::lexeme;
using x3::alnum;
using x3::alpha;
auto const kw_var = x3::lit("var");
auto const ident = lexeme[ alpha >> *alnum ];
auto const stmt = kw_var >> ident;
auto const program = +stmt;
//****************************
// Main
//****************************
int main(int argc, char**argv)
{
if (argc < 2) {
std::cout << "Need something to parse\n";
exit(1);
}
std::string input(argv[1]);
auto iter = input.cbegin();
auto end_iter = input.cend();
std::cout << "parsing : " << input << "\n";
bool r = x3::phrase_parse(iter, end_iter, program, x3::ascii::space);
if (iter != end_iter) {
auto distance = end_iter - iter;
std::cout << "Failed: didn't parse everything\n";
std::cout << "stopped " << distance << " characters from the end "
<< "( '" << *iter << "' )\n";
return 1;
} else if (r) {
std::cout << "Good input\n";
return 0;
} else {
std::cout << "Parse failed\n";
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment