‹ BackHN Continuity

Thread

Show HN: Yantra – an LALR(1) parser generator for C++

33 points · 17 comments · renjipanicker

  1. userbinator · · focus · HN ↗
    It&#x27;s a little surprising to see new parser generators being written, long after the vast majority of compilers have already settled on recursive descent &#x2F; precedence climbing (including <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49913192">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49913192 , which is currently nearby on the front page.)
    1. traes · · focus · HN ↗
      I recall reading in the past that the primary reason parser generators aren&#x27;t used for production compilers is the difficulty of making them produce useful error messages on malformed code. (Of course, the need for fine tuned optimizations also plays a role). Is this still true, or have parser generators caught up in this regard?
      1. renjipanicker · · focus · HN ↗
        Mostly still true, yeah. A few tools have made real progress on error tolerance, continuing past a mistake rather than just stopping, tree-sitter is probably the best example, it&#x27;s explicitly built to produce a best-effort tree from broken input, which is why it&#x27;s good for editors. ANTLR also has configurable recovery strategies, single-token insertion&#x2F;deletion heuristics and the like.

        But &quot;tolerant&quot; isn&#x27;t the same as &quot;as good as hand-written.&quot; A hand-rolled recursive descent parser can say something like &quot;missing semicolon after return statement&quot; because the code knows exactly what construct it&#x27;s in.

        A generated parser&#x27;s error is usually derived mechanically from the state machine, &quot;expected one of: X, Y, Z, got W&quot;, which is correct but generic. This is what Yantra does at the moment. Closing that specific gap would mostly require hand-authored, context-specific messages layered on top. But its a good problem to solve.

        For yantra specifically, it doesn&#x27;t have error recovery at all yet. A syntax or lexer error just stops parsing at that point, no resynchronization, no continuing to find more errors in one pass. It&#x27;s a known, documented gap, not something I&#x27;d claim is solved. For the kind of smaller or evolving DSLs this is aimed at, that&#x27;s probably an acceptable tradeoff, but it does exist as a limitation.

        1. Calavar · · focus · HN ↗
          &gt; A hand-rolled recursive descent parser can say something like &quot;missing semicolon after return statement&quot; because the code knows exactly what construct it&#x27;s in.

          &gt; A generated parser&#x27;s error is usually derived mechanically from the state machine, &quot;expected one of: X, Y, Z, got W&quot;, which is correct but generic.

          You are not describing the difference between hand-written and generated but rather top down vs. bottom up.

          There may be some confusion here because hand-written parsers are almost always recursive decent (a form of top down), while the most popular parser generators (yacc, bison) are bottom up. However hand-written bottom up parsers do exist (Pratt parsing, recursive ascent), as do top down parser generators (ANTLR, Coco&#x2F;R, Chumksy). Chumsky in particular gives pretty decent error messages out of the box.

          Bottom up parsers can parse a larger set of languages than top down parsers, but the tradeoff is you don&#x27;t know what you are parsing until you successfully reduce the rule (unlike top down parsers). This is why errors in bottom up parsers often lack comments on structure&#x2F;context like &quot;after return statement&quot; and can only give a list of which alternative symbols would have been valid.

          1. renjipanicker · · focus · HN ↗

            [dead]

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.