{"by":"renjipanicker","descendants":17,"id":49916997,"kids":[49917666,49917551,49918212,49918659,49928253,49918242,49919407,49917786],"score":33,"text":"Yantra is a C++ parser generator: lexer, parser, and AST walker all generated from one tool.\nIt 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&#x27;s action runs, you don&#x27;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&#x27;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;\n\n  expr := expr(a) PLUS expr(b)\n  %{\n      std::cout &lt;&lt; &quot;Adding&quot; &lt;&lt; std::endl;\n  %}\n\n  expr := NUMBER(N)\n  %{\n      std::cout &lt;&lt; &quot;Number: &quot; &lt;&lt; N.text &lt;&lt; std::endl;\n  %}\n\n  NUMBER := &quot;\\d+&quot;;\n  PLUS := &quot;\\+&quot;;\n  WS := &quot;\\s+&quot;!;\n</code></pre>\nRunning this on &quot;1 + 2 + 3&quot; prints:<p><pre><code>  Adding\n  Number: 1\n  Adding\n  Number: 2\n  Number: 3\n</code></pre>\nThe outer &quot;Adding&quot;, the root of the tree, prints first, before either of its children. That&#x27;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&#x27;s young (0.5.1, pre-1.0) and single-maintainer, so treat it as early.\nI&#x27;d rather know what breaks than have it look more finished than it is.<p>Known gaps are listed at\n<a href=\"https:&#x2F;&#x2F;github.com&#x2F;TantrixAuto&#x2F;yantra&#x2F;blob&#x2F;main&#x2F;docs&#x2F;known_limitations.md\" rel=\"nofollow\">https:&#x2F;&#x2F;github.com&#x2F;TantrixAuto&#x2F;yantra&#x2F;blob&#x2F;main&#x2F;docs&#x2F;known_l...</a><p>Repo: <a href=\"https:&#x2F;&#x2F;github.com&#x2F;TantrixAuto&#x2F;yantra\" rel=\"nofollow\">https:&#x2F;&#x2F;github.com&#x2F;TantrixAuto&#x2F;yantra</a><p>Feedback and questions are all welcome. I&#x27;ll be around.","time":1790821848,"title":"Show HN: Yantra – an LALR(1) parser generator for C++","type":"story","url":"https://github.com/TantrixAuto/yantra"}