AI News Feed
Market watch
Products & Applications

Yantra, a LALR(1) Parser Generator for C++, Debuts on Hacker News

Yantra, a LALR(1) parser generator for C++, was announced on Hacker News. It builds the entire AST before walking it top-down, allowing parent actions to run before children and multiple walkers from one grammar. It is pre-1.0, MIT-licensed, and available on GitHub.

According to the post, Yantra differs from most LALR parser generators such as Yacc, Bison, and Lemon in when it runs semantic actions. Those tools execute actions during parsing as each rule reduces, bottom-up, so a rule's action runs before its parent is known. That pushes many grammars toward hand-built AST classes and a separate walking pass when lookahead into siblings or deferred decisions are needed. Yantra instead builds the entire AST first and then walks it top-down in a separate pass, calling semantic actions as it goes. This allows a parent rule's action to run before its children are visited.

A single grammar can define more than one walker, for example one that emits C++ and another that emits Java from the same parse. The AST and walker classes are generated automatically. The post included a small example grammar with actions that print "Adding" and "Number: " messages. Running it on the input "1 + 2 + 3" prints the root "Adding" first, before either of its children, which the author said is only possible because the whole tree exists before any action runs.

Other features mentioned in the post include an integrated lexer with mode support for nested comments, an optional amalgamated single-file output mode with a generated main(), C++23, and an MIT license. The project is at version 0.5.1, pre-1.0, and maintained by a single person. The author described it as young and early, and said they would rather know what breaks than have it look more finished than it is. Known gaps are listed in the project's documentation.

The repository is at github.com/TantrixAuto/yantra, and the author invited feedback and questions. The Hacker News post had 4 points and no comments at the time of publication.