I am having some issues specifying and parsing a rather simple grammar.
vertex = char+
edge = vertex " -> " vertex
start = ((vertex | edge) eol)*
input = "a\nb\na -> b\n"
Spirit is doing the following:
"a" -> vertex
"\n" -> eol
-> start
"b" -> vertex
"\n" -> eol
-> start
and
"a" -> vertex
terminate
instead of identifying the edge in the end and parsing the entire input. That is, it could parse the entire input but it's not. Shouldn't it backtrack and attempt to parse using the alternate rule? And thus complete the start rule.
Is it because Spirit uses PEGs? (http://www.boost.org/doc/libs/1_57_0/libs/spirit/doc/html/spirit/abstracts/parsing_expression_grammar.html#spirit.abstracts.parsing_expression_grammar.alternatives)
Minimal working example:
#include <cstdio>
#include <string>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
void print(const std::string & str) {
printf("%s\n", str.c_str());
}
void print_eol() {
printf("eol\n");
}
int main() {
std::string str = "a\nb\na -> b\n";
std::string::iterator it = str.begin(), begin = str.begin(), end = str.end();
qi::rule<std::string::iterator, std::string()> vertex = +qi::alnum;
qi::rule<std::string::iterator, std::string()> edge = vertex >> " -> " >> vertex;
qi::rule<std::string::iterator> start = (vertex[&print] | edge[&print]) % qi::eol[&print_eol];
bool matched = parse(it, end, start);
if (matched) {
printf("matched\n");
}
if (it == end) {
printf("all\n");
} else if (it != begin) {
printf("some\n");
} else {
printf("none\n");
}
return 0;
}
Output:
$ ./a.out
a
eol
b
eol
a
matched
some
I'm using Boost 1.57.0 and Clang 3.5.1 on MSYS2.