- SignalDesk2小时前
Original Summary
Yantra is a C++ parser generator: lexer, parser, and AST walker all generated from one tool. It builds the whole AST first, then walks it.<p>Most LALR parser generators (Yacc, Bison, Lemon) run your semantic actions during parsing, as each rule reduces, bottom-up.<p>That means at the time a rule's action runs, you don't yet know what its parent looks like. This pushes a lot of grammars toward hand-built AST classes and a separate walking pass whenever you need to look ahead into siblings or defer a decision until more context is available.<p>On the other hand, Yantra always builds the whole AST first, then walks it top-down in a separate pass, calling your semantic actions as it goes. A parent rule's action can run before its children are visited.<p>A single grammar can define more than one walker. For example, one that emits C++, another that emits Java, from the same parse. The AST and the walker classes are both generated for you.<p>A small example (full version, with compile commands, in the README):<p><pre><code> start := expr; expr := expr(a) PLUS expr(b) %{ std::cout << "Adding" << std::endl; %} expr := NUMBER(N) %{ std::cout << "Number: " << N.text << std::endl; %} NUMBER := "\d+"; PLUS := "\+"; WS := "\s+"!; </code></pre> Running this on "1 + 2 + 3" prints:<p><pre><code> Adding Number: 1 Adding Number: 2 Number: 3 </code></pre> The outer "Adding", the root of the tree, prints first, before either of its children. That's only possible because the whole tree exists before any action runs.<p>Some other things about it: integrated lexer with mode support (for things like nested comments), an optional amalgamated single-file output mode with a generated main(), C++23, MIT licensed.<p>It's young (0.5.1, pre-1.0) and single-maintainer, so treat it as early. I'd rather know what breaks than have it look more finished than it is.<p>Known gaps are listed at <a href="https://github.com/TantrixAuto/yantra/blob/main/docs/known_limitations.md" rel="nofollow">https://github.com/TantrixAuto/yantra/blob/main/docs/known_l...</a><p>Repo: <a href="https://github.com/TantrixAuto/yantra" rel="nofollow">https://github.com/TantrixAuto/yantra</a><p>Feedback and questions are all welcome. I'll be around.
- 情报分类:技术学习与提效
- 分类依据:内容涉及技术、AI、软件工具或工程实践
- 信息来源:Hacker News 新项目
- 发布时间:2026/10/1 10:30:48
- 暂无回复