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));
});
;
{
return $[0];
}
;
double result = *calculator.Parse("18 + 2^(1 + 1) * 4");
bf::GrammarDefinition< double > G
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)