yapc++ is a very simple parser library for C++, which enables you to write a cpp embedded parser in BNF-like notation. yapc++ is a parser but it also may be a lexer because yapcpp++ analyzes data as an array of data uint that may be a C pointer, STL iterators and so on. Take example for char* which is data of an array of char data unit, it seems that char* is data for lexer but yapc++ thinks of it as for lexer, parser and other things. That is, yapc++ is a very usable, extendable and minimizable parser library without learning difficulties Boost::spirit has.
There is a full code below that parses CSV data by using yapc++.
#include <iostream>
#include <vector>
#include <iterator>
#include <yapcpp/yapcpp.h>
#include <yapcpp/stdrules.h>
using namespace std;
using namespace yapcpp;
int main() {
const char* csvdata = "1,2,3,4\n5,6,7,8\n9,10,11,12\n";
// you just define a parser
parser<char> p(csvdata, csvdata + strlen(csvdata));
// then define a rule that parses CSV in BNF-like notation
rule<char, vector<int> > csvline = sepby_p(int_p(), symbol_p(","));
rule<char, vector<vector<int> > > csv = *hold(csvline[_] >> newline_p());
// parse!
vector<vector<int> > data;
p.run(csv[&data]);
// output
for (vector<vector<int> >::iterator ite = data.begin();
ite != data.end(); ++ite) {
copy(ite->begin(), ite->end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
}
// 1 2 3 4
// 5 6 7 8
// 9 10 11 12
Latest: 0.0.1a
Coming soon...