HeadlinesBriefing HeadlinesBriefing.com

Yantra: C++ Parser Generator with AST Walker

Hacker News •
×

Yantra is a C++ parser generator that builds the entire AST first, then walks it top-down in a separate pass, calling semantic actions as it goes. Unlike most LALR parser generators (Yacc, Bison, Lemon) which run semantic actions during parsing, Yantra allows a parent rule's action to run before its children are visited. This enables a single grammar to define multiple walkers, such as one emitting C++ and another emitting Java. The AST and walker classes are both generated automatically. Yantra features an integrated lexer with mode support for nested comments, optional amalgamated single-file output with generated main(), and requires only a C++23 compiler. It is MIT licensed and currently at version 0.5.1, pre-1.0, maintained by a single developer.

A small example grammar demonstrates the top-down walking behavior: parsing "1 + 2 + 3" prints "Adding" first (the root), followed by number outputs. This ordering is only possible because the full tree exists before any action executes.

Known limitations are documented on the project's GitHub page. The repository is available at https://github.com/TantrixAuto/yantra, with feedback and questions welcomed by the maintainer.

The tool uses CMake for building and produces the ycc executable for generating parsers from grammar files.

Source: Hacker News · Summarized by HeadlinesBriefing