<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 8. Tips</title><link rel="stylesheet" href="fptools.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="Happy User Guide"><link rel="up" href="index.html" title="Happy User Guide"><link rel="prev" href="sec-info-files.html" title="Chapter 7. Info Files"><link rel="next" href="sec-compilation-time.html" title="8.2. Compilation-Time Tips"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 8. Tips</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="sec-info-files.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="sec-compilation-time.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="sec-tips"></a>Chapter 8. Tips</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="sec-tips.html#sec-performance-tips">8.1. Performance Tips</a></span></dt><dt><span class="sect1"><a href="sec-compilation-time.html">8.2. Compilation-Time Tips</a></span></dt><dt><span class="sect1"><a href="sec-finding-errors.html">8.3. Finding Type Errors</a></span></dt><dt><span class="sect1"><a href="sec-conflict-tips.html">8.4. Conflict Tips</a></span></dt><dd><dl><dt><span class="sect2"><a href="sec-conflict-tips.html#sec-lalr">8.4.1. LALR(1) parsers</a></span></dt></dl></dd><dt><span class="sect1"><a href="sec-happy-ghci.html">8.5. Using Happy with <span class="application">GHCi</span></a></span></dt></dl></div><p>This section contains a lot of accumulated lore about using <span class="application">Happy</span>.</p><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec-performance-tips"></a>8.1. Performance Tips</h2></div></div></div><p>How to make your parser go faster:</p><div class="itemizedlist"><ul type="disc"><li><a class="indexterm" name="id2551010"></a><p> If you are using GHC, generate parsers using the <code class="literal">-a -g -c</code> options, and compile them using GHC with the <code class="literal">-fglasgow-exts</code> option. This is worth a <span class="emphasis"><em>lot</em></span>, in terms of compile-time, execution speed and binary size <sup>[<a name="id2551039" href="#ftn.id2551039" class="footnote">4</a>]</sup>.</p></li><li><p> 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.</p></li><li><p> Simplify the grammar as much as possible, as this reduces the number of states and reduction rules that need to be applied.</p></li><li><a class="indexterm" name="id2551075"></a><p> 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.</p></li></ul></div></div><div class="footnotes"><br><hr width="100" align="left"><div class="footnote"><p><sup>[<a name="ftn.id2551039" href="#id2551039" class="para">4</a>] </sup>omitting the <code class="literal">-a</code> may generate slightly faster parsers, but they will be much bigger.</p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sec-info-files.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="sec-compilation-time.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 7. Info Files </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 8.2. Compilation-Time Tips</td></tr></table></div></body></html>