# Parsers with Error Recovery

**URL:** <https://discourse.elm-lang.org/t/parsers-with-error-recovery/6262>\
**Category:** Learn\
**Created:** [September 8, 2020, 3:12pm UTC](https://discourse.elm-lang.org/t/parsers-with-error-recovery/6262 "2020-09-08T15:12:52Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [September 30, 2020, 11:49am UTC](https://discourse.elm-lang.org/t/parsers-with-error-recovery/6262/20 "2020-09-30T11:49:38Z")

</div>

The recovery tactics I implemented so far require you to add them in to exising parsing code - which is not simple to do. The idea of having error recovery handled automatically, is appealing.

So the other thing I have been looking into is error recovery in LL grammars and parser generators. I don’t think `elm/parser` is really restricted to LL grammars, its just that the way we most commonly use it is to write recursive descent style parsers.

A recursive descent parser using a parsing combinator library, embeds _implicit knowledge_ about the grammar being parsed into code. An LL parser generator can generate recursive descent parsers, but can also make use of _explicit knowledge_ about the grammar it is parsing and has been giving as input to generate a parser for. When it comes to error recovery, this makes a big difference.

This paper gives details of how to calculate for a given token and parse state, how to figure out the set of things that could come next. That means that the parser can automatically figure out where it can continue from. There is also an algorithm for repairing the input, by inserting imaginary tokens not in the input, in order to get to the continuation token, and this algorithm also always terminates. The result is an LL parser generator that can recover and repair all errors automatically. Pretty nice:

> **[ell.pdf](https://www.cocolab.com/products/cocktail/doc.pdf/ell.pdf)**
>
> 52.09 KB

I did not realize it because I have never used Antlr (but have used StringTemplate, one of Terrence Parr’s other projects - all my StringTemplate code is now ported to Elm 🙂 ), but Antlr also uses this technique. Antlr is a popular LL(k) parser generator with auto error recovery. The (k) bit means it can look-ahead k tokens to figure out ambiguities in the input grammar - that is when 2 or more things start out the same way so both branches need to be explored in more depth, to figure out which one is correct.

> **[ANTLR](https://www.antlr.org/)**

And there is a port of it to Standard ML, which might provide some insights on how it could be written in Elm:

> **[sml-nj/ml-lpt](https://github.com/sml-nj/ml-lpt/tree/master/ml-antlr)**
>
> SML language processing tools - New versions of the scanner and parser generator tools for the Standard ML of New Jersey system. - sml-nj/ml-lpt

I think a parser generator could be written on top of `elm/parser`, essentially `elm/parser` would just do the tokenization, with the parser code generated on top of a `List` of `Token`s, rather than parsing direct from the input `String`. That is, in `elm/parser` we use the combinators to do both the lexing and parsing, in a single step, which is quite convenient for hand-written recursive descent parsers but for a parser generator, probably conceptually easier to be thinking at the level of tokens.

This also proves my point above that `elm/parser` is not restricted to LL grammars - you could do an LR parser on this token list too. It is clear though that error recovery in LR parsers is more difficult than for LL.

If anyone knows of any LL parser generator proejcts implemented in functional languages (seeing something in Haskell or OCaml might be nice), do post a link.

---

_[View the full topic](https://discourse.elm-lang.org/t/parsers-with-error-recovery/6262)._
