Yantra: an LALR(1) parser generator for C++23
Yantra: an LALR(1) parser generator for C++23 that builds the AST first, then walks it top-down
Yantra is a parser generator: lexer, parser, and AST walker generation, all in one tool, no separate lexer generator needed.
The main thing that's different from Yacc/Bison/Lemon: most LALR tools run your semantic actions during parsing, as each rule reduces, bottom-up, so by the time an action runs you don't know what its parent looks like. Yantra always builds the whole AST first, then walks it top-down in a separate pass, so a parent's action can run before its children are visited. It's still LALR(1) under the hood, bottom-up, table-driven, the top-down walk is layered on top of that, not a trade-off for a different parsing algorithm the way ANTLR's LL(*) gets it for free.
A small example:
expr := expr(a) PLUS expr(b)
%{
std::cout << "Adding" << std::endl;
%}
expr := NUMBER(N)
%{
std::cout << "Number: " << N.text << std::endl;
%}
Running `1 + 2 + 3` through it prints `Adding`, `Number: 1`, `Adding`, `Number: 2`, `Number: 3`, the outer `Adding` fires first, because the whole tree already exists before any action runs.
A single grammar can also define multiple walkers (e.g. one emitting C++, another emitting Java, from the same parse), with the AST and walker classes generated for you either way.
C++23, MIT licensed, no dependencies beyond the standard library. It's young (0.y.z, pre-1.0) and single-maintainer, so there are real gaps, listed honestly in known\_limitations.md rather than glossed over.
GitHub: https://github.com/TantrixAuto/yantra
https://redd.it/1x2kqq0
@r_cpp
Post #25833
10