buffalo
Loading...
Searching...
No Matches
buffalo

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:

  • GCC 14
  • Clang 18
  • MSVC

Examples

Calculator

++
/* calculator.cpp */
#include <buffalo/spex.h>
/*
* Grammar Definition
*/
/*
* Terminals
*/
bf::DefineTerminal<G, R"(\d+(\.\d+)?)", double> NUMBER([](auto const &tok) {
return std::stod(std::string(tok.raw));
});
bf::DefineTerminal<G, R"(\‍()"> PAR_OPEN;
/*
* Non-Terminals
*/
= 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]; }
;
= bf::PR<G>(expression)<=>[](auto &$)
{
return $[0];
}
;
/*
* Calculations
*/
double result = *calculator.Parse("18 + 2^(1 + 1) * 4");
bf::GrammarDefinition< double > G
Definition calculator.cpp:8
bf::DefineNonTerminal< G > expression
bf::DefineTerminal< G, R"(\*)"> OP_MUL(bf::Left)
bf::DefineTerminal< G, R"(\‍()"> PAR_OPEN;bf::DefineTerminal< G, R"(\‍))"> PAR_CLOSE
bf::DefineTerminal< G, R"(\+)"> OP_ADD(bf::Left)
bf::DefineNonTerminal< G > statement
bf::DefineTerminal< G, R"(\^)"> OP_EXP(bf::Right)
bf::DefineTerminal< G, R"(\d+(\.\d+)?)", double > NUMBER([](auto const &tok) { return std::stod(std::string(tok.raw));})
bf::DefineTerminal< G, R"(\/)"> OP_DIV(bf::Left)
bf::DefineTerminal< G, R"(\-)"> OP_SUB(bf::Left)
static std::expected< SLRParser, Error > Build(NonTerminal< G > &start)
Definition buffalo.h:1147
@ Left
Definition buffalo.h:196
@ Right
Definition buffalo.h:197
ProductionRule< G > PR
Definition buffalo.h:438