# Modern parser generator

**URL:** <https://discourse.haskell.org/t/modern-parser-generator/8451>\
**Category:** Learn\
**Created:** [January 2, 2024, 4:42pm UTC](https://discourse.haskell.org/t/modern-parser-generator/8451 "2024-01-02T16:42:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![deemp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/deemp/32/3983_2.png) [@deemp](https://discourse.haskell.org/u/deemp)\
**Post date:** [January 2, 2024, 4:42pm UTC](https://discourse.haskell.org/t/modern-parser-generator/8451/1 "2024-01-02T16:42:00Z")

</div>

Hi! Is there a tool that generates Haskell ISGLR parsers with automatic error recovery and support for attribute grammars?

- LR with error recovery like [grmtools](https://github.com/softdevteam/grmtools).
  - Probably with compile-time grammar evaluation support

- ISGLR like [this](https://eelcovisser.org/posters/2019/10/22/incremental-scannerless-generalized-lr-parsing/).
  - Maybe there’s a port of [SDF3](https://spoofax.dev/references/sdf3/) (Spoofax) to begin with?

- Attribute grammars like ones supported by [happy](https://haskell-happy.readthedocs.io/en/latest/attribute-grammars.html).
  - The evaluation should happen on a successful parse.

The target is Language Servers.

Is this combination of features an overkill?

---

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [January 2, 2024, 6:24pm UTC](https://discourse.haskell.org/t/modern-parser-generator/8451/2 "2024-01-02T18:24:36Z")

</div>

I don’t think there is one. I’ve been working on something in this direction. My idea was to build upon data-dependent grammars (a la [Iguana](https://iguana-parser.github.io/index.html)) and [fungll](https://github.com/ltbinsbe/fungll-combinators) in a parser combinator library, but it doesn’t quite work out yet. My latest experiment is [on Github](https://github.com/noughtmare/gpc/blob/0b237a966bdf7b0ace8494edc1d0c4679e0fe21c/experiments/Experiment4.hs). The main issue I’m getting stuck on is dealing with variable binding in the EDSL.

The reason I’m considering data-dependant grammars rather than SGLR parsers is because Jurgen Vinju gave quite a convincing talk at EVCS: [https://homepages.cwi.nl/~jurgenv/papers/EVCS.pdf](https://homepages.cwi.nl/~jurgenv/papers/EVCS.pdf). TL;DR: data-dependant grammars allow you to easily define disambiguation strategies in a generic framework instead of modifying the internals of the parsing algorithm for each new disambiguation strategy.

When I manage to crack that, I think it should not be hard to turn the parser combinator library into a parser generator by using some Template Haskell.

I don’t have plans to include full attribute grammars, because I think they are usually at the wrong level of abstraction. After parsing you generally want to think in terms of control flow and data flow, but attribute grammars just keep viewing the IR as a tree. And you either have to thread the attributes yourself or rely on syntactic flow like from left to right through the tree.

But if you want to use attribute grammars anyway, I’d recommend taking a look at [UUAGC](https://github.com/UU-ComputerScience/uuagc) which is a completely standalone attribute grammar system. You can combine that with any parser you want.

---

<div class="post-metadata">

**Author:** ![deemp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/deemp/32/3983_2.png) [@deemp](https://discourse.haskell.org/u/deemp)\
**Post date:** [January 14, 2024, 10:50pm UTC](https://discourse.haskell.org/t/modern-parser-generator/8451/3 "2024-01-14T22:50:38Z")

</div>

The Rascal site lists some more works ([link](https://www.rascal-mpl.org/docs/Bibliography/#2010s-2)), including a thesis about `Iguana` and `Meerkat` (`🎓 Ali Afroozeh and Anastasia Izmaylova, Practical General Top-down Parsers. (2019) Universiteit van Amsterdam.`).

> **[Thesis.pdf](https://pure.uva.nl/ws/files/36086100/Thesis.pdf)**

Unfortunately, error recovery is left for the future work.

> Error recovery  
> One of the important topics that we did not discuss in this thesis is error recovery. Top-down parsers have a good reputation for error reporting and error recovery, and we suspect it should be easier to provide good error recovery features for GLL, say compared to GLR. Currently, Iguana reports the parse error location, but does not provide any error recovery features. Providing error recovery should be the highest priority feature, especially for using Iguana in language workbenches and development tools.

---

<div class="post-metadata">

**Author:** ![sgraf](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/sgraf/32/392_2.png) [@sgraf](https://discourse.haskell.org/u/sgraf)\
**Post date:** [January 15, 2024, 12:52pm UTC](https://discourse.haskell.org/t/modern-parser-generator/8451/4 "2024-01-15T12:52:42Z")

</div>

A modern version of `happy` has been on my wish list for quite some time. We would be able to use it to great effect in GHC, which currently has abysmal parse error recovery; see [Fault-tolerant GHC compilation pipeline by michaelpj · Pull Request #63 · haskellfoundation/tech-proposals · GitHub](https://github.com/haskellfoundation/tech-proposals/pull/63).  
Furthermore, a staged Template Haskell frontend for `happy` based on combinators or some other declarative framework would go a long way toward raising excitement about LR parsing that is currently reserved to monadic LL parsing. (An [old idea](https://github.com/haskell/happy/issues/149) that hasn’t generated much interest since.)

Ultimately I think we lack someone dedicated who is willing to invest the work on these matters in the Haskell ecosystem. That someone would need to have a good idea of the state of the art and then perhaps write a tool (or improve `happy`) that can cope with a large parser such as GHC’s.

As far as disambiguation is concerned, the shortcomings that are often associated with LR but not with LL aren’t so prominent in GHC because of judicious use of `%shift` pragmas and a crazy hand-crafted disambiguation mechanism for the overlapping `exp` and `pat` fragments (one that would be necessary in LL as well). It would be interesting to see whether GHC’s parser could instead use happy’s GLR mode to disambiguate the parses.

I wouldn’t worry too much about performance as long as the asymptotics are no worse than established generalised parsing techniques.

---

<div class="post-metadata">

**Author:** ![Kleidukos](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/kleidukos/32/1213_2.png) [@Kleidukos](https://discourse.haskell.org/u/Kleidukos)\
**Post date:** [January 15, 2024, 1:04pm UTC](https://discourse.haskell.org/t/modern-parser-generator/8451/5 "2024-01-15T13:04:42Z")

</div>

> [@deemp](#):
>
> The target is Language Servers.

Oh you may have stumbled right into [tree-sitter](https://tree-sitter.github.io/tree-sitter/) territory!

I have been told by @alanz that the [Erlang Language Platform](https://github.com/WhatsApp/erlang-language-platform) uses it to power the LSP server. 🙂

---

<div class="post-metadata">

**Author:** ![deemp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/deemp/32/3983_2.png) [@deemp](https://discourse.haskell.org/u/deemp)\
**Post date:** [January 15, 2024, 2:39pm UTC](https://discourse.haskell.org/t/modern-parser-generator/8451/6 "2024-01-15T14:39:15Z")

</div>

Yep, I know about tree-sitter and [Lezer](https://github.com/lezer-parser/lr). They are GLR.  
After stuying these, I attempted to find something backed by recent research.

I probably formulated my initial question not completely clearly (or too specifically) and can’t edit it now.

Initially, I wanted generated parsers to be LR-something, but then @jaror linked ([Modern parser generator - #2 by jaror](http://discourse.haskell.org/t/modern-parser-generator/8451/2)) a paper by Jurgen Vinju who stated:

> We conclude that top-down is much easier to experiment with and extend. Bottom-up could be faster due to partial evaluation, but still, additional bookkeeping and less sharing are required to filter correctly. Rascal’s SGLL in Java is as fast as the SDF2’s SGLR in C.

> Second, it is arguably easier to design and implement (new) disambiguation constructs with GLL than with GLR. Third, data-dependent context-free grammars add the level of formality and generality that we were always searching for when inventing new disambiguation schemes (as exemplified by the Jakker and Iguana Parsing Architectures). We conclude that a top-down implementation of data-dependent context-free parsing is the way to go for Rascal as well as SDF3.

So, after years of research, the objective of the [Rascal team](https://www.rascal-mpl.org/team/) became SGLL + DDCFG parsing.

Now, I want:

- a tool written in Haskell
- that produces an SGLL parser for a DDCFG
  - the tool generates purely Haskell code
  - or, the tool reads a grammar and generates a parser in memory (nice for prototyping)

- and the produced parser is
  - fault-tolerant
  - optionally incremental

UPD: Maybe I want an even more powerful formalism the LLR-system ([paper](https://www.mdpi.com/2227-7390/11/7/1594)). Its [repo](https://github.com/slivnik/LLR-systems) has \< 3 KLOC in Java

---

<div class="post-metadata">

**Author:** ![deemp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/deemp/32/3983_2.png) [@deemp](https://discourse.haskell.org/u/deemp)\
**Post date:** [January 15, 2024, 5:03pm UTC](https://discourse.haskell.org/t/modern-parser-generator/8451/7 "2024-01-15T17:03:18Z")

</div>

I asked on the Rascal repo [(off topic) Modern parser generator for Haskell · Issue #1909 · usethesource/rascal · GitHub](https://github.com/usethesource/rascal/issues/1909) for correct requirements and for an estimated amount of work.

In short:

- 1 man-year for a good parser generator
- read the aforementioned thesis by Afroozeh and Izmaylova plus some other works

---

<div class="post-metadata">

**Author:** ![alanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/alanz/32/678_2.png) [@alanz](https://discourse.haskell.org/u/alanz)\
**Post date:** [January 15, 2024, 6:25pm UTC](https://discourse.haskell.org/t/modern-parser-generator/8451/8 "2024-01-15T18:25:29Z")

</div>

FWIW, I did some experiments on applying the techniques from the paper that inspired tree-sitter [to happy](https://github.com/alanz/incremental-play). It looked promising, but I never had the time to push it to anything real.
