Chapter 8. Tips

Table of Contents

8.1. Performance Tips
8.2. Compilation-Time Tips
8.3. Finding Type Errors
8.4. Conflict Tips
8.4.1. LALR(1) parsers
8.5. Using Happy with GHCi
8.6. Basic monadic Happy use with Alex

This section contains a lot of accumulated lore about using Happy.

8.1. Performance Tips

How to make your parser go faster:

  • If you are using GHC, generate parsers using the -a -g -c options, and compile them using GHC with the -fglasgow-exts option. This is worth a lot, in terms of compile-time, execution speed and binary size [4].

  • The lexical analyser is usually the most performance critical part of a parser, so it's worth spending some time optimising this. Profiling tools are essential here. In really dire circumstances, resort to some of the hacks that are used in the Glasgow Haskell Compiler's interface-file lexer.

  • Simplify the grammar as much as possible, as this reduces the number of states and reduction rules that need to be applied.

  • Use left recursion rather than right recursion wherever possible. While not strictly a performance issue, this affects the size of the parser stack, which is kept on the heap and thus needs to be garbage collected.



[4] omitting the -a may generate slightly faster parsers, but they will be much bigger.