4

I am currently playing with the happy parser generator.

Other parser generators can give nice messages like "unexpected endline, expected 'then'". With happy I just get the current Tokens and the position of the error.

Can you give me an example of how to get error messages like above?

2
  • You can add extra right-hand sides to each production to catch known errors. Make sure to always add them at the end, otherwise you will upset the precedence of the grammar. Mar 25, 2011 at 11:22
  • 2
    It's not generally possible to do this, due to the way Happy works - it's a traditional LALR(1) table-based parser, and the information you need to generate the error is not easily available at runtime. Mar 25, 2011 at 12:47

2 Answers 2

5

There is a Happy feature that I have authored for this purpose.

See my blog post: Toward better GHC syntax errors

It was merged in this pull request RFC: On parse error - show the next possible tokens.

3
  • 1
    This was finally released in Happy 1.19.7. Thanks!
    – Alec
    Sep 15, 2017 at 18:01
  • By the way, this is documented here. (My own build of the Happy documentation that's up to date)
    – Monolith
    Mar 21, 2021 at 21:01
  • 1
    Thanks for implementing this @Dan Aloni, this is amazing!
    – Martinsos
    Dec 8, 2021 at 14:55
0

Generally, from what I've heard, if you want nice parser errors, use Parsec instead of Happy.

1
  • I tried Parsec now but the definition of the grammer is very verbose compared to happy and also quite hard to read. But it does give the kind of error messages which I wanted. Mar 26, 2011 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.