A C++ PEG parser generator I wrote a C++ PEG packrat parser generator and would love to get some feedback on the code and/or syntax. I currently use the &lt;&lt; operator for defining grammar expressions and linking the handler functions that are called when a rule is matched. These functions can then store a value of an arbitrary C++ type in the corresponding node of the parse tree using the value&lt;type&gt;() function.

For example, a very simple math parser can be generated through the following source code:

#include "IO.h"
#include "parserTools.h"
#include &lt;cmath&gt; //pow

using namespace llib;

std::map&lt;std::string, double&gt; variables;

void op(parser::info &amp;lhs,parser::info &amp;rhs,parser::info &amp;r,const std::string &amp;op){
  if(op[0]=='+')      r.value() =     lhs.value&lt;double&gt;() + rhs.value&lt;double&gt;() ;
  else if(op[0]=='-') r.value() =     lhs.value&lt;double&gt;() - rhs.value&lt;double&gt;() ;
  else if(op[0]=='*') r.value() =     lhs.value&lt;double&gt;() * rhs.value&lt;double&gt;() ;
  else if(op[0]=='/') r.value() =     lhs.value&lt;double&gt;() / rhs.value&lt;double&gt;() ;
  else if(op[0]=='^') r.value() = pow(lhs.value&lt;double&gt;() , rhs.value&lt;double&gt;());
}

void variable(parser::info &amp;s){ s.value&lt;double&gt;()=variables[s.string()];             }
void negate(parser::info &amp;s)  { s.value()=-s[0].value&lt;double&gt;();                     }
void set(parser::info &amp;s)     { variables[s[0].string()]=s[1].value&lt;double&gt;();       }

int main(){
  peg::grammar math("Math");

  math &lt;&lt; "Math"        &lt;&lt; "Set | Expression"                           &lt;&lt; parserTools::show("%v0\n");
  math &lt;&lt; "Set"         &lt;&lt; "Variable '=' Sum"                           &lt;&lt; set &lt;&lt; parserTools::pass(1);
  math &lt;&lt; "Expression"  &lt;&lt; "Sum"                                        &lt;&lt; parserTools::pass();
  math &lt;&lt; "Sum"         &lt;&lt; "Product  ([+-] Product )*"                  &lt;&lt; parserTools::leftbinary(op);
  math &lt;&lt; "Product"     &lt;&lt; "Exponent ([*/] Exponent)*"                  &lt;&lt; parserTools::leftbinary(op);
  math &lt;&lt; "Exponent"    &lt;&lt; "Atomic   ('^'  Atomic  )*"                  &lt;&lt; parserTools::leftbinary(op);
  math &lt;&lt; "Atomic"      &lt;&lt; "Parentheses | Number | Negate | Variable"   &lt;&lt; parserTools::pass();
  math &lt;&lt; "Parentheses" &lt;&lt; "'(' Expression ')'"                         &lt;&lt; parserTools::pass();
  math &lt;&lt; "Negate"      &lt;&lt; "'-' Atomic"                                 &lt;&lt; negate;
  math &lt;&lt; "Variable"    &lt;&lt; "[a-zA-Z] [a-zA-Z0-9]*"                      &lt;&lt; variable;
  math &lt;&lt; "Number"      &lt;&lt; parserTools::doubleGrammar()                 &lt;&lt; parserTools::pass();

  math.ignored=" \t";
  math.setStart("Math");

  parser p(math);

  p.error=parserTools::genericError;
  p.warning=parserTools::genericWarning;

  while (1) {
    std::string str;
    IO &lt;&lt; "&gt; " &gt;&gt; str &lt;&lt; str &lt;&lt; " -&gt; ";
    if(str=="q") break;
    else if(str!="") p.parse(str);
    else IO &lt;&lt; clearline &lt;&lt; endl;
  }

  return 0;
}


The full code and documentation is available on GitHub (though LParser is probably pretty boring name).

So any thoughts on the project? Is it something you would actually use?
void op(parser::info &amp;lhs,parser::info &amp;rhs,parser::info &amp;r,const std::string &amp;op){


I think I'd prefer a more descriptive name than r here. I'm guessing it's intended to stand for "result", in which case I think I'd prefer to just name it result.

  peg::grammar math("Math");

  math &lt;&lt; "Math" &lt;&lt; "Set | Expression" &lt;&lt; parserTools::show("%v0\n");


It's not immediately clear why you specify Math both when you create the grammar object and when you specify rules. I think the second is naming the top-level rule Math, but it's not clear what the parameter to the constructor accomplishes.

  math &lt;&lt; "Set" &lt;&lt; "Variable '=' Sum" &lt;&lt; set &lt;&lt; parserTools::pass(1);


At least without some documentation on your parserTools, the meaning of the pass(1) isn't immediately obvious. Presumably this is (at least roughly equivalent to) a semantic rule that's executed when the associated pattern is matched. I can think of a couple of different things pass(1) might be intended to mean, but lac, context to even guess at which is likely to be accurate. Sufficient study of your documentation would undoubtedly make the meaning clear, but it wasn't immediately obvious.

This is kind of a difficult situation: you're basically defining an EDSL, which means understanding the code pretty much requires learning the language you've defined. In other words, no matter how carefully or well written the code is, it's likely to be at least somewhat difficult to understand in isolation.

Summary: looking at the code in isolation, the grammar rules are highly readable, but the (presumed) associated actions much less so.

math &lt;&lt; "Exponent" &lt;&lt; "Atomic   ('^'  Atomic  )*" &lt;&lt; parserTools::leftbinary(op);


At first glance, I'd have assumed that leftbinary was intended to signify a left-associative binary operator--but exponentiation is normally right-associative. I'm left uncertain whether:


it's a bug,
you intentionally changed it from right- to left-associative, or 
I've completely misinterpreted what leftbinary really means.




  while (1) {
    std::string str;
    IO &lt;&lt; "&gt; " &gt;&gt; str &lt;&lt; str &lt;&lt; " -&gt; ";


I have mixed feelings about this mixture of input and output in the same expression. It does lead to relatively terse code, but requires unusual care in reading to keep track of which parts are input vs. output.

Ultimately, I think it would probably be better to review the parser generator and the IO library separately. Here I'm really concentrating on the parser generator, but the design if the I/O library might well be worth examination and review in its own right.