[Haskell] Error recovery for Haskell parsers?

John Meacham john at repetae.net
Wed Apr 16 03:35:15 EDT 2008

a PEG (parsing expression grammar) style parser would be ideal for this
sort of thing. The nice thing about them is that you can make your error
correction/detection part of the grammar itself and if you are a little
careful when writing your grammar, you end up with a lazy parser, which
can be useful for something such as a ast-highlighting editor.

For an example of how error correction can be handled in the parser
itself, the basic and only routine to call the PEG based
frisby[1] parser is essentially:

runPeg :: P a -> String -> a

notice that there is no need at all for error handling in the calling
routine because you can just write something like

runPegMaybe :: P a -> String -> Maybe a
runPegMaybe p s = runPeg (fmap Just p </> return Nothing)

likewise, you can add things in your expression parser like

exp <- exp '+' exp </> '(' exp ')' </> errorExp

where errorExp is a rule that 'eats' any expression errors and lets the
parser continue on as normal. 

A long term goal of mine is to completely redo jhc's haskell parser in
pappy[2] to better take advantage of PEG parsers. Trying to coerce LALR
parsers to do what I want is getting frustrating.

see http://pdos.csail.mit.edu/~baford/packrat/thesis/ for more
information on packrat parsing and PEGs. I have continued development of
pappy from Bryan Ford's original version and my repo is at [2].

[1] http://repetae.net/computer/frisby/
[2] http://repetae.net/repos/pappy/


John Meacham - ⑆repetae.net⑆john⑈

More information about the Haskell mailing list