buffalo: to bewilder, baffle (also: bamboozle) [2024 Merriam-Webster Dictionary]
Bewilder, baffle (even bamboozle!) your grammars with buffalo
, a C++23 header-only SLR parser generator with yacc/bison
-like syntax.
It supports the definition of terminals, non-terminals and grammar structures all within the same C++ file.
Features
- Terminal definition with builtin scanning based on
compile-time-regular-expressions
(spex
).
- Grammar definition in pseudo BNF notation.
- Shift/Reduce conflict resolution through precedence (based on definition order) and associativity (left/right/none).
Compiler Support
buffalo
officially supports the following compilers:
Examples
Calculator
++
#include <buffalo/spex.h>
return std::stod(std::string(tok.raw));
});
=
bf::PR<G>(NUMBER)<=>[](
auto &$) {
return $[0]; }
| (PAR_OPEN + expression + PAR_CLOSE)<=>[](auto &$) { return $[1]; }
| (expression + OP_EXP + expression)<=>[](auto &$) { return std::pow($[0], $[2]); }
| (expression + OP_MUL + expression)<=>[](auto &$) { return $[0] * $[2]; }
| (expression + OP_DIV + expression)<=>[](auto &$) { return $[0] / $[2]; }
| (expression + OP_ADD + expression)<=>[](auto &$) { return $[0] + $[2]; }
| (expression + OP_SUB + expression)<=>[](auto &$) { return $[0] - $[2]; }
;
{
return $[0];
}
;
double result = *
calculator.Parse(
"18 + 2^(1 + 1) * 4");
static std::expected< SLRParser, Error > Build(NonTerminal< G > &start)
bf::DefineNonTerminal< CalculatorG > calculator
constexpr bool always_false_v