Offset 1, 1 lines modified | Offset 1, 1 lines modified | ||
1 | {"name":"hs-happy","origin":"devel/hs-happy","version":"1.19.5","comment":"Happy·is·a·parser·generator·for·Haskell","maintainer":"haskell@FreeBSD.org","www":"http://www.haskell.org/happy/","abi":"FreeBSD:11:amd64","arch":"freebsd:11:x86:64","prefix":"/usr/local","flatsize":1372462,"licenselogic":"single","licenses":["BSD3CLAUSE"],"desc":"Happy·is·a·parser·generator·system·for·Haskell.\n\nWWW:·http://www.haskell.org/happy/","deps":{"gmp":{"origin":"math/gmp","version":"5.1.3_3"},"ghc":{"origin":"lang/ghc","version":"7.10.2"},"hs-mtl":{"origin":"devel/hs-mtl","version":"2.2.1"},"gcc":{"origin":"lang/gcc","version":"4.8.5_2"}},"categories":["devel","haskell"],"shlibs_required":["libHSmtl-2.2.1-KMpng31YRYc5JfMWFZ3FCU-ghc7.10.2.so","libcharset.so.1","libgmp.so.10","libHSghc-prim-0.4.0.0-8TmvWUcS1U1IKHT0levwg3-ghc7.10.2.so","libHSbase-4.8.1.0-GDytRqRVSUX7zckgKqJjgw-ghc7.10.2.so","libHStransformers-0.4.2.0-3eG64VdP2vzGjP6wJiCp5X-ghc7.10.2.so","libHSdeepseq-1.4.1.1-LbCWUlehDDeLxurARKDH5o-ghc7.10.2.so","libffi.so.6","·✂ | 1 | {"name":"hs-happy","origin":"devel/hs-happy","version":"1.19.5","comment":"Happy·is·a·parser·generator·for·Haskell","maintainer":"haskell@FreeBSD.org","www":"http://www.haskell.org/happy/","abi":"FreeBSD:11:amd64","arch":"freebsd:11:x86:64","prefix":"/usr/local","flatsize":1372462,"licenselogic":"single","licenses":["BSD3CLAUSE"],"desc":"Happy·is·a·parser·generator·system·for·Haskell.\n\nWWW:·http://www.haskell.org/happy/","deps":{"gmp":{"origin":"math/gmp","version":"5.1.3_3"},"ghc":{"origin":"lang/ghc","version":"7.10.2"},"hs-mtl":{"origin":"devel/hs-mtl","version":"2.2.1"},"gcc":{"origin":"lang/gcc","version":"4.8.5_2"}},"categories":["devel","haskell"],"shlibs_required":["libHSmtl-2.2.1-KMpng31YRYc5JfMWFZ3FCU-ghc7.10.2.so","libcharset.so.1","libgmp.so.10","libHSghc-prim-0.4.0.0-8TmvWUcS1U1IKHT0levwg3-ghc7.10.2.so","libHSbase-4.8.1.0-GDytRqRVSUX7zckgKqJjgw-ghc7.10.2.so","libHStransformers-0.4.2.0-3eG64VdP2vzGjP6wJiCp5X-ghc7.10.2.so","libHSdeepseq-1.4.1.1-LbCWUlehDDeLxurARKDH5o-ghc7.10.2.so","libffi.so.6","·✂ |
Offset 1, 42 lines modified | Offset 1, 42 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Chapter 1. Introduction</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="index.html"·title="Happy·User·Guide"><link·rel="prev"·href="index.html"·title="Happy·User·Guide"><link·rel="next"·href="sec-reporting-bugs.html"·title="1.2. Reporting·Bugs"></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 1. Introduction</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="index.html">Prev</a> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-reporting-bugs.html">Next</a></td></tr></table><hr></div><div·class="chapter"·title="Chapter 1. Introduction"><div·class·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Chapter 1. Introduction</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="index.html"·title="Happy·User·Guide"><link·rel="prev"·href="index.html"·title="Happy·User·Guide"><link·rel="next"·href="sec-reporting-bugs.html"·title="1.2. Reporting·Bugs"></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 1. Introduction</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="index.html">Prev</a> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-reporting-bugs.html">Next</a></td></tr></table><hr></div><div·class="chapter"·title="Chapter 1. Introduction"><div·class·✂ |
2 | ····system·for·Haskell,·similar·to·the·tool | 2 | ····system·for·Haskell,·similar·to·the·tool |
3 | ····<span·class="application">yacc</span>·for·C.··Like | 3 | ····<span·class="application">yacc</span>·for·C.··Like |
4 | ····<span·class="application">yacc</span>,·it·takes·a·file·containing·an | 4 | ····<span·class="application">yacc</span>,·it·takes·a·file·containing·an |
5 | ····annotated·BNF·specification·of·a·grammar·and·produces·a·Haskell | 5 | ····annotated·BNF·specification·of·a·grammar·and·produces·a·Haskell |
6 | ····module·containing·a·parser·for·the·grammar.·</p><a·class="indexterm"·name="idp63 | 6 | ····module·containing·a·parser·for·the·grammar.·</p><a·class="indexterm"·name="idp63142328"></a><p>·<span·class="application">Happy</span>·is·flexible:·you·can·have·several |
7 | ····<span·class="application">Happy</span>·parsers·in·the·same·program,·and | 7 | ····<span·class="application">Happy</span>·parsers·in·the·same·program,·and |
8 | ····each·parser·may·have·multiple·entry·points. | 8 | ····each·parser·may·have·multiple·entry·points. |
9 | ····<span·class="application">Happy</span>·can·work·in·conjunction·with·a | 9 | ····<span·class="application">Happy</span>·can·work·in·conjunction·with·a |
10 | ····lexical·analyser·supplied·by·the·user·(either·hand-written·or | 10 | ····lexical·analyser·supplied·by·the·user·(either·hand-written·or |
11 | ····generated·by·another·program),·or·it·can·parse·a·stream·of | 11 | ····generated·by·another·program),·or·it·can·parse·a·stream·of |
12 | ····characters·directly·(but·this·isn't·practical·in·most·cases).··In | 12 | ····characters·directly·(but·this·isn't·practical·in·most·cases).··In |
13 | ····a·future·version·we·hope·to·include·a·lexical·analyser·generator | 13 | ····a·future·version·we·hope·to·include·a·lexical·analyser·generator |
14 | ····with·<span·class="application">Happy</span>·as·a·single·package.·</p><p>·Parsers·generated·by·<span·class="application">Happy</span>·are | 14 | ····with·<span·class="application">Happy</span>·as·a·single·package.·</p><p>·Parsers·generated·by·<span·class="application">Happy</span>·are |
15 | ····fast;·generally·faster·than·an·equivalent·parser·written·using | 15 | ····fast;·generally·faster·than·an·equivalent·parser·written·using |
16 | ····parsing·combinators·or·similar·tools.··Furthermore,·any·future | 16 | ····parsing·combinators·or·similar·tools.··Furthermore,·any·future |
17 | ····improvements·made·to·<span·class="application">Happy</span>·will·benefit | 17 | ····improvements·made·to·<span·class="application">Happy</span>·will·benefit |
18 | ····an·existing·grammar,·without·need·for·a·rewrite.·</p><p>·<span·class="application">Happy</span>·is·sufficiently·powerful | 18 | ····an·existing·grammar,·without·need·for·a·rewrite.·</p><p>·<span·class="application">Happy</span>·is·sufficiently·powerful |
19 | ····to·parse·full·Haskell | 19 | ····to·parse·full·Haskell |
20 | ····-·<a·class="ulink"·href="http://www.haskell.org/ghc"·target="_top">GHC</a>·itself·uses | 20 | ····-·<a·class="ulink"·href="http://www.haskell.org/ghc"·target="_top">GHC</a>·itself·uses |
21 | ····a·Happy·parser.</p><a·class="indexterm"·name="idp63 | 21 | ····a·Happy·parser.</p><a·class="indexterm"·name="idp63148216"></a><a·class="indexterm"·name="idp63149112"></a><p>·<span·class="application">Happy</span>·can·currently·generate |
22 | ····four·types·of·parser·from·a·given·grammar,·the·intention·being | 22 | ····four·types·of·parser·from·a·given·grammar,·the·intention·being |
23 | ····that·we·can·experiment·with·different·kinds·of·functional·code·to | 23 | ····that·we·can·experiment·with·different·kinds·of·functional·code·to |
24 | ····see·which·is·the·best,·and·compiler·writers·can·use·the·different | 24 | ····see·which·is·the·best,·and·compiler·writers·can·use·the·different |
25 | ····types·of·parser·to·tune·their·compilers.··The·types·of·parser | 25 | ····types·of·parser·to·tune·their·compilers.··The·types·of·parser |
26 | ····supported·are:·</p><div·class="orderedlist"><ol·class="orderedlist"·type="1"><li·class="listitem"><p><a·name="item-default-backend"></a>`standard'·Haskell·98·(should·work·with·any·compiler | 26 | ····supported·are:·</p><div·class="orderedlist"><ol·class="orderedlist"·type="1"><li·class="listitem"><p><a·name="item-default-backend"></a>`standard'·Haskell·98·(should·work·with·any·compiler |
27 | » that·compiles·Haskell·98).</p></li><li·class="listitem"><a·class="indexterm"·name="idp63 | 27 | » that·compiles·Haskell·98).</p></li><li·class="listitem"><a·class="indexterm"·name="idp63153208"></a><a·class="indexterm"·name="idp63153976"></a><p>standard·Haskell·using·arrays·(this·is·not·the·default |
28 | » because·we·have·found·this·generates·slower·parsers·than·<a·class="xref"·href="happy-introduction.html#item-default-backend">1</a>).</p></li><li·class="listitem"><a·class="indexterm"·name="idp63 | 28 | » because·we·have·found·this·generates·slower·parsers·than·<a·class="xref"·href="happy-introduction.html#item-default-backend">1</a>).</p></li><li·class="listitem"><a·class="indexterm"·name="idp63156280"></a><a·class="indexterm"·name="idp63156920"></a><p>Haskell·with·GHC·(Glasgow·Haskell)·extensions.·This·is·a |
29 | » slightly·faster·option·than·<a·class="xref"·href="happy-introduction.html#item-default-backend">1</a>·for·Glasgow·Haskell | 29 | » slightly·faster·option·than·<a·class="xref"·href="happy-introduction.html#item-default-backend">1</a>·for·Glasgow·Haskell |
30 | » users.</p></li><li·class="listitem"><p>GHC·Haskell·with·string-encoded·arrays.··This·is·the | 30 | » users.</p></li><li·class="listitem"><p>GHC·Haskell·with·string-encoded·arrays.··This·is·the |
31 | » fastest/smallest·option·for·GHC·users.··If·you're·using·GHC, | 31 | » fastest/smallest·option·for·GHC·users.··If·you're·using·GHC, |
32 | » the·optimum·flag·settings·are·<code·class="literal">-agc</code>·(see | 32 | » the·optimum·flag·settings·are·<code·class="literal">-agc</code>·(see |
33 | » <a·class="xref"·href="sec-invoking.html"·title="Chapter 5. Invoking·Happy">Chapter 5,·<i>Invoking·<span·class="application">Happy</span></i></a>).</p></li></ol></div><p>Happy·can·also·generate·parsers·which·will·dump·debugging | 33 | » <a·class="xref"·href="sec-invoking.html"·title="Chapter 5. Invoking·Happy">Chapter 5,·<i>Invoking·<span·class="application">Happy</span></i></a>).</p></li></ol></div><p>Happy·can·also·generate·parsers·which·will·dump·debugging |
34 | ····information·at·run·time,·showing·state·transitions·and·the·input | 34 | ····information·at·run·time,·showing·state·transitions·and·the·input |
35 | ····tokens·to·the·parser.</p><div·class="sect1"·title="1.1. Compatibility"><div·class="titlepage"><div><div><h2·class="title"·style="clear:·both"><a·name="sec-compatibility"></a>1.1. Compatibility</h2></div></div></div><p>·<span·class="application">Happy</span>·is·written·in·Glasgow·Haskell.··This | 35 | ····tokens·to·the·parser.</p><div·class="sect1"·title="1.1. Compatibility"><div·class="titlepage"><div><div><h2·class="title"·style="clear:·both"><a·name="sec-compatibility"></a>1.1. Compatibility</h2></div></div></div><p>·<span·class="application">Happy</span>·is·written·in·Glasgow·Haskell.··This |
36 | ······means·that·(for·the·time·being),·you·need·GHC·to·compile·it. | 36 | ······means·that·(for·the·time·being),·you·need·GHC·to·compile·it. |
37 | ······Any·version·of·GHC·>=·6.2·should·work.</p><p>·Remember:·parsers·produced·using | 37 | ······Any·version·of·GHC·>=·6.2·should·work.</p><p>·Remember:·parsers·produced·using |
38 | ······<span·class="application">Happy</span>·should·compile·without | 38 | ······<span·class="application">Happy</span>·should·compile·without |
39 | ······difficulty·under·any·Haskell·98·compiler·or·interpreter<sup>[<a·name="idp631 | 39 | ······difficulty·under·any·Haskell·98·compiler·or·interpreter<sup>[<a·name="idp63175608"·href="#ftn.idp63175608"·class="footnote">1</a>]</sup></p></div><div·class="footnotes"><br><hr·width="100"·align="left"><div·class="footnote"><p><sup>[<a·id="ftn.idp63175608"·href="#idp63175608"·class="para">1</a>]·</sup>With·one |
40 | » exception:·if·you·have·a·production·with·a·polymorphic·type·signature, | 40 | » exception:·if·you·have·a·production·with·a·polymorphic·type·signature, |
41 | » then·a·compiler·that·supports·local·universal·quantification·is | 41 | » then·a·compiler·that·supports·local·universal·quantification·is |
42 | » required.··See·<a·class="xref"·href="sec-type-signatures.html"·title="2.4. Type·Signatures">Section 2.4,·“Type·Signatures”</a>.</p></div></div></div><div·class="navfooter"><hr><table·width="100%"·summary="Navigation·footer"><tr><td·width="40%"·align="left"><a·accesskey="p"·href="index.html">Prev</a> </td><td·width="20%"·align="center"> </td><td·width="40%"·align="right"> <a·accesskey="n"·href="sec-reporting-bugs.html">Next</a></td></tr><tr><td·width="40%"·align="left"·valign="top">Happy·User·Guide </td><td·width="20%"·align="center"><a·accesskey="h"·href="index.html">Home</a></td><td·width="40%"·align="right"·valign="top"> 1.2. Reporting·Bugs</td></tr></table></div></body></html> | 42 | » required.··See·<a·class="xref"·href="sec-type-signatures.html"·title="2.4. Type·Signatures">Section 2.4,·“Type·Signatures”</a>.</p></div></div></div><div·class="navfooter"><hr><table·width="100%"·summary="Navigation·footer"><tr><td·width="40%"·align="left"><a·accesskey="p"·href="index.html">Prev</a> </td><td·width="20%"·align="center"> </td><td·width="40%"·align="right"> <a·accesskey="n"·href="sec-reporting-bugs.html">Next</a></td></tr><tr><td·width="40%"·align="left"·valign="top">Happy·User·Guide </td><td·width="20%"·align="center"><a·accesskey="h"·href="index.html">Home</a></td><td·width="40%"·align="right"·valign="top"> 1.2. Reporting·Bugs</td></tr></table></div></body></html> |
Offset 1, 2 lines modified | Offset 1, 2 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Happy·User·Guide</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><meta·name="description"·content="This·document·describes·Happy,·the·Haskell·Parser·Generator,·version·1.18."><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="next"·href="happy-introduction.html"·title="Chapter 1. Introduction"></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">Happy·User·Guide</th></tr><tr><td·width="20%"·align="left"> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> <a·accesskey="n"·href="happy-introduction.html">Next</a></td></tr></table><hr></div><div·class="book"·title="Happy·User·Guide"><div·class="titlepage"><div><div><h1·class="title"><a·name="happy"></a>Happy·User·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Happy·User·Guide</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><meta·name="description"·content="This·document·describes·Happy,·the·Haskell·Parser·Generator,·version·1.18."><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="next"·href="happy-introduction.html"·title="Chapter 1. Introduction"></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">Happy·User·Guide</th></tr><tr><td·width="20%"·align="left"> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> <a·accesskey="n"·href="happy-introduction.html">Next</a></td></tr></table><hr></div><div·class="book"·title="Happy·User·Guide"><div·class="titlepage"><div><div><h1·class="title"><a·name="happy"></a>Happy·User·✂ |
2 | » Generator,·version·1.18.</p></div></div></div><hr></div><div·class="toc"><p><b>Table·of·Contents</b></p><dl><dt><span·class="chapter"><a·href="happy-introduction.html">1.·Introduction</a></span></dt><dd><dl><dt><span·class="sect1"><a·href="happy-introduction.html#sec-compatibility">1.1.·Compatibility</a></span></dt><dt><span·class="sect1"><a·href="sec-reporting-bugs.html">1.2.·Reporting·Bugs</a></span></dt><dt><span·class="sect1"><a·href="sec-license.html">1.3.·License</a></span></dt><dt><span·class="sect1"><a·href="sec-obtaining.html">1.4.·Obtaining·<span·class="application">Happy</span></a></span></dt></dl></dd><dt><span·class="chapter"><a·href="sec-using.html">2.·Using·<span·class="application">Happy</span></a></span></dt><dd><dl><dt><span·class="sect1"><a·href="sec-using.html#sec-other-datatypes">2.1.·Returning·other·datatypes</a></span></dt><dt><span·class="sect1"><a·href="sec-sequences.html">2.2.·Parsing·sequences</a></span></dt><dd><dl><dt><span·class="sect2"><a·href="sec-sequences.html#sec-separators·✂ | 2 | » Generator,·version·1.18.</p></div></div></div><hr></div><div·class="toc"><p><b>Table·of·Contents</b></p><dl><dt><span·class="chapter"><a·href="happy-introduction.html">1.·Introduction</a></span></dt><dd><dl><dt><span·class="sect1"><a·href="happy-introduction.html#sec-compatibility">1.1.·Compatibility</a></span></dt><dt><span·class="sect1"><a·href="sec-reporting-bugs.html">1.2.·Reporting·Bugs</a></span></dt><dt><span·class="sect1"><a·href="sec-license.html">1.3.·License</a></span></dt><dt><span·class="sect1"><a·href="sec-obtaining.html">1.4.·Obtaining·<span·class="application">Happy</span></a></span></dt></dl></dd><dt><span·class="chapter"><a·href="sec-using.html">2.·Using·<span·class="application">Happy</span></a></span></dt><dd><dl><dt><span·class="sect1"><a·href="sec-using.html#sec-other-datatypes">2.1.·Returning·other·datatypes</a></span></dt><dt><span·class="sect1"><a·href="sec-sequences.html">2.2.·Parsing·sequences</a></span></dt><dd><dl><dt><span·class="sect2"><a·href="sec-sequences.html#sec-separators·✂ |
Offset 1, 1 lines modified | Offset 1, 1 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Index</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="index.html"·title="Happy·User·Guide"><link·rel="prev"·href="sec-monad-alex.html"·title="8.6. Basic·monadic·Happy·use·with·Alex"></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">Index</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-monad-alex.html">Prev</a> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> </td></tr></table><hr></div><div·class="index"·title="Index"><div·class="titlepage"><div><div><h2·class="title"><a·name="idp64 | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Index</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="index.html"·title="Happy·User·Guide"><link·rel="prev"·href="sec-monad-alex.html"·title="8.6. Basic·monadic·Happy·use·with·Alex"></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">Index</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-monad-alex.html">Prev</a> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> </td></tr></table><hr></div><div·class="index"·title="Index"><div·class="titlepage"><div><div><h2·class="title"><a·name="idp64145080"></a>Index</h2></div></div></div><div·class="index"><div·class="indexdiv"><h3>Symbols<·✂ |
Offset 40, 15 lines modified | Offset 40, 15 lines modified | ||
40 | ·······and·<code·class="literal">value</code>·would·be·the·default·attribute. | 40 | ·······and·<code·class="literal">value</code>·would·be·the·default·attribute. |
41 | ·······</p></div><div·class="sect2"·title="4.2.2. Semantic·Rules"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-semanticRules"></a>4.2.2. Semantic·Rules</h3></div></div></div><p>In·an·ordinary·Happy·grammar,·a·production·consists·of·a·list | 41 | ·······</p></div><div·class="sect2"·title="4.2.2. Semantic·Rules"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-semanticRules"></a>4.2.2. Semantic·Rules</h3></div></div></div><p>In·an·ordinary·Happy·grammar,·a·production·consists·of·a·list |
42 | ······of·terminals·and/or·non-terminals·followed·by·an·uninterpreted | 42 | ······of·terminals·and/or·non-terminals·followed·by·an·uninterpreted |
43 | ······code·fragment·enclosed·in·braces.··With·an·attribute·grammar,·the | 43 | ······code·fragment·enclosed·in·braces.··With·an·attribute·grammar,·the |
44 | ······format·is·very·similar,·but·the·braces·enclose·a·set·of·semantic·rules | 44 | ······format·is·very·similar,·but·the·braces·enclose·a·set·of·semantic·rules |
45 | ······rather·than·uninterpreted·Haskell·code.··Each·semantic·rule·is·either | 45 | ······rather·than·uninterpreted·Haskell·code.··Each·semantic·rule·is·either |
46 | ······an·attribute·calculation·or·a·conditional,·and·rules·are·separated·by | 46 | ······an·attribute·calculation·or·a·conditional,·and·rules·are·separated·by |
47 | ······semicolons<sup>[<a·name="idp63 | 47 | ······semicolons<sup>[<a·name="idp63807800"·href="#ftn.idp63807800"·class="footnote">3</a>]</sup>. |
48 | ······</p><p> | 48 | ······</p><p> |
49 | ······Both·attribute·calculations·and·conditionals·may·contain·attribute·references | 49 | ······Both·attribute·calculations·and·conditionals·may·contain·attribute·references |
50 | ······and/or·terminal·references.··Just·like·regular·Happy·grammars,·the·tokens | 50 | ······and/or·terminal·references.··Just·like·regular·Happy·grammars,·the·tokens |
51 | ······<code·class="literal">$1</code>·through·<code·class="literal">$<n></code>,·where | 51 | ······<code·class="literal">$1</code>·through·<code·class="literal">$<n></code>,·where |
52 | ······<code·class="literal">n</code>·is·the·number·of·symbols·in·the·production,·refer·to | 52 | ······<code·class="literal">n</code>·is·the·number·of·symbols·in·the·production,·refer·to |
53 | ······subtrees·of·the·parse.··If·the·referenced·symbol·is·a·terminal,·then·the | 53 | ······subtrees·of·the·parse.··If·the·referenced·symbol·is·a·terminal,·then·the |
54 | ······value·of·the·reference·is·just·the·value·of·the·terminal,·the·same·way·as | 54 | ······value·of·the·reference·is·just·the·value·of·the·terminal,·the·same·way·as |
Offset 101, 10 lines modified | Offset 101, 10 lines modified | ||
101 | ······if·the·inherited·length·attribute·is·not·0. | 101 | ······if·the·inherited·length·attribute·is·not·0. |
102 | ······</p><pre·class="programlisting"> | 102 | ······</p><pre·class="programlisting"> |
103 | where·if·$$.length·==·0·then·()·else·error·"length·not·equal·to·0" | 103 | where·if·$$.length·==·0·then·()·else·error·"length·not·equal·to·0" |
104 | </pre><p> | 104 | </pre><p> |
105 | ······This·conditional·is·the·monadic·equivalent: | 105 | ······This·conditional·is·the·monadic·equivalent: |
106 | ······</p><pre·class="programlisting"> | 106 | ······</p><pre·class="programlisting"> |
107 | where·unless·($$.length·==·0)·(fail·"length·not·equal·to·0") | 107 | where·unless·($$.length·==·0)·(fail·"length·not·equal·to·0") |
108 | </pre></div><div·class="footnotes"><br><hr·width="100"·align="left"><div·class="footnote"><p><sup>[<a·id="ftn.idp63 | 108 | </pre></div><div·class="footnotes"><br><hr·width="100"·align="left"><div·class="footnote"><p><sup>[<a·id="ftn.idp63807800"·href="#idp63807800"·class="para">3</a>]·</sup>Note·that·semantic·rules·must·not·rely·on |
109 | ······layout,·because·whitespace·alignment·is·not·guaranteed·to·be | 109 | ······layout,·because·whitespace·alignment·is·not·guaranteed·to·be |
110 | ······preserved</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-AttributeGrammar.html">Prev</a> </td><td·width="20%"·align="center"><a·accesskey="u"·href="sec-AttributeGrammar.html">Up</a></td><td·width="40%"·align="right"> <a·accesskey="n"·href="sec-AttrGrammarLimits.html">Next</a></td></tr><tr><td·width="40%"·align="left"·valign="top">Chapter 4. Attribute·Grammars </td><td·width="20%"·align="center"><a·accesskey="h"·href="index.html">Home</a></td><td·width="40%"·align="right"·valign="top"> 4.3. Limits·of·Happy·Attribute·Grammars</td></tr></table></div></body></html> | 110 | ······preserved</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-AttributeGrammar.html">Prev</a> </td><td·width="20%"·align="center"><a·accesskey="u"·href="sec-AttributeGrammar.html">Up</a></td><td·width="40%"·align="right"> <a·accesskey="n"·href="sec-AttrGrammarLimits.html">Next</a></td></tr><tr><td·width="40%"·align="left"·valign="top">Chapter 4. Attribute·Grammars </td><td·width="20%"·align="center"><a·accesskey="h"·href="index.html">Home</a></td><td·width="40%"·align="right"·valign="top"> 4.3. Limits·of·Happy·Attribute·Grammars</td></tr></table></div></body></html> |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.3. Using·Precedences</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-sequences.html"·title="2.2. Parsing·sequences"><link·rel="next"·href="sec-type-signatures.html"·title="2.4. Type·Signatures"></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">2.3. Using·Precedences</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-sequences.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-type-signatures.html">Next</a></td·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.3. Using·Precedences</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-sequences.html"·title="2.2. Parsing·sequences"><link·rel="next"·href="sec-type-signatures.html"·title="2.4. Type·Signatures"></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">2.3. Using·Precedences</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-sequences.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-type-signatures.html">Next</a></td·✂ |
2 | ······wouldn't·it·be·nicer·if·we·didn't·have·to·explicitly·separate | 2 | ······wouldn't·it·be·nicer·if·we·didn't·have·to·explicitly·separate |
3 | ······the·expressions·into·terms·and·factors,·merely·to·make·it | 3 | ······the·expressions·into·terms·and·factors,·merely·to·make·it |
4 | ······clear·that·<code·class="literal">'*'</code>·and·<code·class="literal">'/'</code> | 4 | ······clear·that·<code·class="literal">'*'</code>·and·<code·class="literal">'/'</code> |
5 | ······operators·bind·more·tightly·than·<code·class="literal">'+'</code>·and | 5 | ······operators·bind·more·tightly·than·<code·class="literal">'+'</code>·and |
6 | ······<code·class="literal">'-'</code>?</p><p>We·could·just·change·the·grammar·as·follows·(making·the | 6 | ······<code·class="literal">'-'</code>?</p><p>We·could·just·change·the·grammar·as·follows·(making·the |
7 | ······appropriate·changes·to·the·expression·datatype·too):</p><pre·class="programlisting"> | 7 | ······appropriate·changes·to·the·expression·datatype·too):</p><pre·class="programlisting"> |
8 | Exp···:·let·var·'='·Exp·in·Exp··{·Let·$2·$4·$6·} | 8 | Exp···:·let·var·'='·Exp·in·Exp··{·Let·$2·$4·$6·} |
Offset 16, 22 lines modified | Offset 16, 22 lines modified | ||
16 | </pre><p>but·now·Happy·will·complain·that·there·are·shift/reduce | 16 | </pre><p>but·now·Happy·will·complain·that·there·are·shift/reduce |
17 | ······conflicts·because·the·grammar·is·ambiguous·-·we·haven't | 17 | ······conflicts·because·the·grammar·is·ambiguous·-·we·haven't |
18 | ······specified·whether·e.g.·<code·class="literal">1·+·2·*·3</code>·is·to·be | 18 | ······specified·whether·e.g.·<code·class="literal">1·+·2·*·3</code>·is·to·be |
19 | ······parsed·as·<code·class="literal">1·+·(2·*·3)</code>·or·<code·class="literal">(1·+·2)·* | 19 | ······parsed·as·<code·class="literal">1·+·(2·*·3)</code>·or·<code·class="literal">(1·+·2)·* |
20 | ······3</code>.··Happy·allows·these·ambiguities·to·be·resolved·by | 20 | ······3</code>.··Happy·allows·these·ambiguities·to·be·resolved·by |
21 | ······specifying·the·<em·class="firstterm">precedences</em>·of·the | 21 | ······specifying·the·<em·class="firstterm">precedences</em>·of·the |
22 | ······operators·involved·using·directives·in·the | 22 | ······operators·involved·using·directives·in·the |
23 | ······header<sup>[<a·name="idp63 | 23 | ······header<sup>[<a·name="idp63341752"·href="#ftn.idp63341752"·class="footnote">2</a>]</sup>:</p><pre·class="programlisting"> |
24 | ... | 24 | ... |
25 | %right·in | 25 | %right·in |
26 | %left·'+'·'-' | 26 | %left·'+'·'-' |
27 | %left·'*'·'/' | 27 | %left·'*'·'/' |
28 | %% | 28 | %% |
29 | ... | 29 | ... |
30 | </pre><a·class="indexterm"·name="idp63 | 30 | </pre><a·class="indexterm"·name="idp63343416"></a><a·class="indexterm"·name="idp63344440"></a><a·class="indexterm"·name="idp63345464"></a><p>The·<code·class="literal">%left</code>·or·<code·class="literal">%right</code> |
31 | ······directive·is·followed·by·a·list·of·terminals,·and·declares·all | 31 | ······directive·is·followed·by·a·list·of·terminals,·and·declares·all |
32 | ······these·tokens·to·be·left·or·right-associative·respectively.··The | 32 | ······these·tokens·to·be·left·or·right-associative·respectively.··The |
33 | ······precedence·of·these·tokens·with·respect·to·other·tokens·is | 33 | ······precedence·of·these·tokens·with·respect·to·other·tokens·is |
34 | ······established·by·the·order·of·the·<code·class="literal">%left</code>·and | 34 | ······established·by·the·order·of·the·<code·class="literal">%left</code>·and |
35 | ······<code·class="literal">%right</code>·directives:·earlier·means·lower | 35 | ······<code·class="literal">%right</code>·directives:·earlier·means·lower |
36 | ······precedence.··A·higher·precedence·causes·an·operator·to·bind·more | 36 | ······precedence.··A·higher·precedence·causes·an·operator·to·bind·more |
37 | ······tightly;·in·our·example·above,·because·<code·class="literal">'*'</code> | 37 | ······tightly;·in·our·example·above,·because·<code·class="literal">'*'</code> |
Offset 86, 17 lines modified | Offset 86, 17 lines modified | ||
86 | ······|·Exp·'+'·Exp·············{·Plus·$1·$3·} | 86 | ······|·Exp·'+'·Exp·············{·Plus·$1·$3·} |
87 | ······|·Exp·'-'·Exp·············{·Minus·$1·$3·} | 87 | ······|·Exp·'-'·Exp·············{·Minus·$1·$3·} |
88 | ······|·Exp·'*'·Exp·············{·Times·$1·$3·} | 88 | ······|·Exp·'*'·Exp·············{·Times·$1·$3·} |
89 | ······|·Exp·'/'·Exp·············{·Div·$1·$3·} | 89 | ······|·Exp·'/'·Exp·············{·Div·$1·$3·} |
90 | ······|·'('·Exp·')'·············{·Brack·$2·} | 90 | ······|·'('·Exp·')'·············{·Brack·$2·} |
91 | ······|·'-'·Exp·%prec·NEG·······{·Negate·$2·} | 91 | ······|·'-'·Exp·%prec·NEG·······{·Negate·$2·} |
92 | ······|·int·····················{·Int·$1·} | 92 | ······|·int·····················{·Int·$1·} |
93 | ······|·var·····················{·Var·$1·}</pre><a·class="indexterm"·name="idp633 | 93 | ······|·var·····················{·Var·$1·}</pre><a·class="indexterm"·name="idp63381304"></a><p>We·invent·a·new·token·<code·class="literal">NEG</code>·as·a |
94 | » placeholder·for·the·precedence·of·our·prefix·negation·rule. | 94 | » placeholder·for·the·precedence·of·our·prefix·negation·rule. |
95 | » The·<code·class="literal">NEG</code>·token·doesn't·need·to·appear·in | 95 | » The·<code·class="literal">NEG</code>·token·doesn't·need·to·appear·in |
96 | » a·<code·class="literal">%token</code>·directive.··The·prefix·negation | 96 | » a·<code·class="literal">%token</code>·directive.··The·prefix·negation |
97 | » rule·has·a·<code·class="literal">%prec·NEG</code>·directive·attached, | 97 | » rule·has·a·<code·class="literal">%prec·NEG</code>·directive·attached, |
98 | » which·overrides·the·default·precedence·for·the·rule·(which | 98 | » which·overrides·the·default·precedence·for·the·rule·(which |
99 | » would·normally·be·the·precedence·of·'-')·with·the·precedence | 99 | » would·normally·be·the·precedence·of·'-')·with·the·precedence |
100 | » of·<code·class="literal">NEG</code>.</p></div><div·class="footnotes"><br><hr·width="100"·align="left"><div·class="footnote"><p><sup>[<a·id="ftn.idp63 | 100 | » of·<code·class="literal">NEG</code>.</p></div><div·class="footnotes"><br><hr·width="100"·align="left"><div·class="footnote"><p><sup>[<a·id="ftn.idp63341752"·href="#idp63341752"·class="para">2</a>]·</sup>Users·of·<code·class="literal">yacc</code>·will·find |
101 | ······this·familiar,·Happy's·precedence·scheme·works·in·exactly·the | 101 | ······this·familiar,·Happy's·precedence·scheme·works·in·exactly·the |
102 | ······same·way.</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-sequences.html">Prev</a> </td><td·width="20%"·align="center"><a·accesskey="u"·href="sec-using.html">Up</a></td><td·width="40%"·align="right"> <a·accesskey="n"·href="sec-type-signatures.html">Next</a></td></tr><tr><td·width="40%"·align="left"·valign="top">2.2. Parsing·sequences </td><td·width="20%"·align="center"><a·accesskey="h"·href="index.html">Home</a></td><td·width="40%"·align="right"·valign="top"> 2.4. Type·Signatures</td></tr></table></div></body></html> | 102 | ······same·way.</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-sequences.html">Prev</a> </td><td·width="20%"·align="center"><a·accesskey="u"·href="sec-using.html">Up</a></td><td·width="40%"·align="right"> <a·accesskey="n"·href="sec-type-signatures.html">Next</a></td></tr><tr><td·width="40%"·align="left"·valign="top">2.2. Parsing·sequences </td><td·width="20%"·align="center"><a·accesskey="h"·href="index.html">Home</a></td><td·width="40%"·align="right"·valign="top"> 2.4. Type·Signatures</td></tr></table></div></body></html> |
Offset 1, 13 lines modified | Offset 1, 13 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.2. Compilation-Time·Tips</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="next"·href="sec-finding-errors.html"·title="8.3. Finding·Type·Errors"></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">8.2. Compilation-Time·Tips</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-tips.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-finding-errors.html">Next</a></td></tr></table><hr></div><div·class="sect1"·title="8.2. ·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.2. Compilation-Time·Tips</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="next"·href="sec-finding-errors.html"·title="8.3. Finding·Type·Errors"></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">8.2. Compilation-Time·Tips</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-tips.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-finding-errors.html">Next</a></td></tr></table><hr></div><div·class="sect1"·title="8.2. ·✂ |
2 | ······<span·class="application">Happy</span>·can·take·a·large·amount·of·time/memory,·so | 2 | ······<span·class="application">Happy</span>·can·take·a·large·amount·of·time/memory,·so |
3 | ······here's·some·tips·on·making·things·more·sensible:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>·Include·as·little·code·as·possible·in·the·module | 3 | ······here's·some·tips·on·making·things·more·sensible:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>·Include·as·little·code·as·possible·in·the·module |
4 | ··········trailer.··This·code·is·included·verbatim·in·the·generated | 4 | ··········trailer.··This·code·is·included·verbatim·in·the·generated |
5 | ··········parser,·so·if·any·of·it·can·go·in·a·separate·module,·do | 5 | ··········parser,·so·if·any·of·it·can·go·in·a·separate·module,·do |
6 | ··········so.</p></li><li·class="listitem"><a·class="indexterm"·name="idp6 | 6 | ··········so.</p></li><li·class="listitem"><a·class="indexterm"·name="idp64057016"></a><p>·Give·type·signatures·for·everything·(see·<a·class="xref"·href="sec-type-signatures.html"·title="2.4. Type·Signatures">Section 2.4,·“Type·Signatures”</a>.··This·is·reported·to·improve |
7 | ··········things·by·about·50%.··If·there·is·a·type·signature·for·every | 7 | ··········things·by·about·50%.··If·there·is·a·type·signature·for·every |
8 | ··········single·non-terminal·in·the·grammar,·then·<span·class="application">Happy</span> | 8 | ··········single·non-terminal·in·the·grammar,·then·<span·class="application">Happy</span> |
9 | ··········automatically·generates·type·signatures·for·most·functions | 9 | ··········automatically·generates·type·signatures·for·most·functions |
10 | ··········in·the·parser.</p></li><li·class="listitem"><p>·Simplify·the·grammar·as·much·as·possible·(applies·to | 10 | ··········in·the·parser.</p></li><li·class="listitem"><p>·Simplify·the·grammar·as·much·as·possible·(applies·to |
11 | ··········everything,·this·one).</p></li><li·class="listitem"><p>·Use·a·recent·version·of·GHC.··Versions·from·4.04 | 11 | ··········everything,·this·one).</p></li><li·class="listitem"><p>·Use·a·recent·version·of·GHC.··Versions·from·4.04 |
12 | » ··onwards·have·lower·memory·requirements·for·compiling | 12 | » ··onwards·have·lower·memory·requirements·for·compiling |
13 | » ··<span·class="application">Happy</span>-generated·parsers.</p></li><li·class="listitem"><p>·Using·<span·class="application">Happy</span>'s·<code·class="literal">-g·-a·-c</code> | 13 | » ··<span·class="application">Happy</span>-generated·parsers.</p></li><li·class="listitem"><p>·Using·<span·class="application">Happy</span>'s·<code·class="literal">-g·-a·-c</code> |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.4. Conflict·Tips</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-finding-errors.html"·title="8.3. Finding·Type·Errors"><link·rel="next"·href="sec-happy-ghci.html"·title="8.5. Using·Happy·with·GHCi"></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">8.4. Conflict·Tips</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-finding-errors.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-happy-ghci.html">Next</a></td></tr></table><hr></div><div·class="sect1"·title·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.4. Conflict·Tips</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-finding-errors.html"·title="8.3. Finding·Type·Errors"><link·rel="next"·href="sec-happy-ghci.html"·title="8.5. Using·Happy·with·GHCi"></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">8.4. Conflict·Tips</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-finding-errors.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-happy-ghci.html">Next</a></td></tr></table><hr></div><div·class="sect1"·title·✂ |
2 | ······some·input·sequences·may·possess·more·than·one·parse. | 2 | ······some·input·sequences·may·possess·more·than·one·parse. |
3 | ······Shift/reduce·conflicts·are·benign·in·the·sense·that·they·are | 3 | ······Shift/reduce·conflicts·are·benign·in·the·sense·that·they·are |
4 | ······easily·resolved·(<span·class="application">Happy</span>·automatically·selects·the | 4 | ······easily·resolved·(<span·class="application">Happy</span>·automatically·selects·the |
5 | ······shift·action,·as·this·is·usually·the·intended·one). | 5 | ······shift·action,·as·this·is·usually·the·intended·one). |
6 | ······Reduce/reduce·conflicts·are·more·serious.··A·reduce/reduce | 6 | ······Reduce/reduce·conflicts·are·more·serious.··A·reduce/reduce |
7 | ······conflict·implies·that·a·certain·sequence·of·tokens·on·the·input | 7 | ······conflict·implies·that·a·certain·sequence·of·tokens·on·the·input |
8 | ······can·represent·more·than·one·non-terminal,·and·the·parser·is | 8 | ······can·represent·more·than·one·non-terminal,·and·the·parser·is |
Offset 1, 21 lines modified | Offset 1, 21 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>6.3. Directives</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="prev"·href="sec-module-header.html"·title="6.2. Module·Header"><link·rel="next"·href="sec-grammar.html"·title="6.4. Grammar"></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">6.3. Directives</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-module-header.html">Prev</a> </td><th·width="60%"·align="center">Chapter 6. Syntax·of·Grammar·Files</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-grammar.html">Next</a></td></tr></table><hr></div><div·class=·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>6.3. Directives</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="prev"·href="sec-module-header.html"·title="6.2. Module·Header"><link·rel="next"·href="sec-grammar.html"·title="6.4. Grammar"></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">6.3. Directives</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-module-header.html">Prev</a> </td><th·width="60%"·align="center">Chapter 6. Syntax·of·Grammar·Files</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-grammar.html">Next</a></td></tr></table><hr></div><div·class=·✂ |
2 | %<directive·name>·<argument>·... | 2 | %<directive·name>·<argument>·... |
3 | </pre><p>The·statements·here·are·all·annotations·to·help | 3 | </pre><p>The·statements·here·are·all·annotations·to·help |
4 | ······<span·class="application">Happy</span>·generate·the·Haskell·code·for·the·grammar. | 4 | ······<span·class="application">Happy</span>·generate·the·Haskell·code·for·the·grammar. |
5 | ······Some·of·them·are·optional,·and·some·of·them·are·required.</p><div·class="sect2"·title="6.3.1. Token·Type"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-token-type"></a>6.3.1. Token·Type</h3></div></div></div><pre·class="programlisting"> | 5 | ······Some·of·them·are·optional,·and·some·of·them·are·required.</p><div·class="sect2"·title="6.3.1. Token·Type"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-token-type"></a>6.3.1. Token·Type</h3></div></div></div><pre·class="programlisting"> |
6 | %tokentype···{·<valid·Haskell·type>·} | 6 | %tokentype···{·<valid·Haskell·type>·} |
7 | </pre><a·class="indexterm"·name="idp63 | 7 | </pre><a·class="indexterm"·name="idp63918392"></a><p>(mandatory)·The·<code·class="literal">%tokentype</code>·directive·gives·the |
8 | ········type·of·the·tokens·passed·from·the·lexical·analyser·to·the | 8 | ········type·of·the·tokens·passed·from·the·lexical·analyser·to·the |
9 | ········parser·(in·order·that·<span·class="application">Happy</span>·can·supply·types·for | 9 | ········parser·(in·order·that·<span·class="application">Happy</span>·can·supply·types·for |
10 | ········functions·and·data·in·the·generated·parser).</p></div><div·class="sect2"·title="6.3.2. Tokens"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-tokens"></a>6.3.2. Tokens</h3></div></div></div><pre·class="programlisting"> | 10 | ········functions·and·data·in·the·generated·parser).</p></div><div·class="sect2"·title="6.3.2. Tokens"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-tokens"></a>6.3.2. Tokens</h3></div></div></div><pre·class="programlisting"> |
11 | %token·<name>·{·<Haskell·pattern>·} | 11 | %token·<name>·{·<Haskell·pattern>·} |
12 | ·······<name>·{·<Haskell·pattern>·} | 12 | ·······<name>·{·<Haskell·pattern>·} |
13 | ·······... | 13 | ·······... |
14 | </pre><a·class="indexterm"·name="idp63 | 14 | </pre><a·class="indexterm"·name="idp63922488"></a><p>(mandatory)·The·<code·class="literal">%token</code>·directive·is·used·to |
15 | ········tell·<span·class="application">Happy</span>·about·all·the·terminal·symbols·used | 15 | ········tell·<span·class="application">Happy</span>·about·all·the·terminal·symbols·used |
16 | ········in·the·grammar.··Each·terminal·has·a·name,·by·which·it·is | 16 | ········in·the·grammar.··Each·terminal·has·a·name,·by·which·it·is |
17 | ········referred·to·in·the·grammar·itself,·and·a·Haskell | 17 | ········referred·to·in·the·grammar·itself,·and·a·Haskell |
18 | ········representation·enclosed·in·braces.··Each·of·the·patterns·must | 18 | ········representation·enclosed·in·braces.··Each·of·the·patterns·must |
19 | ········be·of·the·same·type,·given·by·the·<code·class="literal">%tokentype</code> | 19 | ········be·of·the·same·type,·given·by·the·<code·class="literal">%tokentype</code> |
20 | ········directive.</p><p>The·name·of·each·terminal·follows·the·lexical·rules·for | 20 | ········directive.</p><p>The·name·of·each·terminal·follows·the·lexical·rules·for |
21 | ········<span·class="application">Happy</span>·identifiers·given·above.··There·are·no | 21 | ········<span·class="application">Happy</span>·identifiers·given·above.··There·are·no |
Offset 24, 76 lines modified | Offset 24, 76 lines modified | ||
24 | ········for·example·using·upper·case·letters·for·terminals·and·lower | 24 | ········for·example·using·upper·case·letters·for·terminals·and·lower |
25 | ········case·for·non-terminals,·or·vice-versa.</p><p><span·class="application">Happy</span>·will·give·you·a·warning·if·you·try | 25 | ········case·for·non-terminals,·or·vice-versa.</p><p><span·class="application">Happy</span>·will·give·you·a·warning·if·you·try |
26 | ········to·use·the·same·identifier·both·as·a·non-terminal·and·a | 26 | ········to·use·the·same·identifier·both·as·a·non-terminal·and·a |
27 | ········terminal,·or·introduce·an·identifier·which·is·declared·as | 27 | ········terminal,·or·introduce·an·identifier·which·is·declared·as |
28 | ········neither.</p><p>To·save·writing·lots·of·projection·functions·that·map | 28 | ········neither.</p><p>To·save·writing·lots·of·projection·functions·that·map |
29 | ········tokens·to·their·components,·you·can·include | 29 | ········tokens·to·their·components,·you·can·include |
30 | ········<code·class="literal">$$</code>·in·your·Haskell·pattern.·For | 30 | ········<code·class="literal">$$</code>·in·your·Haskell·pattern.·For |
31 | ········example:</p><a·class="indexterm"·name="idp63 | 31 | ········example:</p><a·class="indexterm"·name="idp63928248"></a><pre·class="programlisting"> |
32 | %token·INT·{·TokenInt·$$·} | 32 | %token·INT·{·TokenInt·$$·} |
33 | ·······... | 33 | ·······... |
34 | </pre><p>This·makes·the·semantic·value·of·<code·class="literal">INT</code>·refer·to·the·first·argument | 34 | </pre><p>This·makes·the·semantic·value·of·<code·class="literal">INT</code>·refer·to·the·first·argument |
35 | of·<code·class="literal">TokenInt</code>·rather·than·the·whole·token,·eliminating·the·need·for | 35 | of·<code·class="literal">TokenInt</code>·rather·than·the·whole·token,·eliminating·the·need·for |
36 | any·projection·function.</p></div><div·class="sect2"·title="6.3.3. Parser·Name"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-parser-name"></a>6.3.3. Parser·Name</h3></div></div></div><pre·class="programlisting"> | 36 | any·projection·function.</p></div><div·class="sect2"·title="6.3.3. Parser·Name"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-parser-name"></a>6.3.3. Parser·Name</h3></div></div></div><pre·class="programlisting"> |
37 | %name·<Haskell·identifier>·[·<non-terminal>·] | 37 | %name·<Haskell·identifier>·[·<non-terminal>·] |
38 | ... | 38 | ... |
39 | </pre><a·class="indexterm"·name="idp63 | 39 | </pre><a·class="indexterm"·name="idp63932728"></a><p>(optional)·The·<code·class="literal">%name</code>·directive·is·followed·by |
40 | ········a·valid·Haskell·identifier,·and·gives·the·name·of·the | 40 | ········a·valid·Haskell·identifier,·and·gives·the·name·of·the |
41 | ········top-level·parsing·function·in·the·generated·parser.··This·is | 41 | ········top-level·parsing·function·in·the·generated·parser.··This·is |
42 | ········the·only·function·that·needs·to·be·exported·from·a·parser | 42 | ········the·only·function·that·needs·to·be·exported·from·a·parser |
43 | ········module.</p><p>If·the·<code·class="literal">%name</code>·directive·is·omitted,·it | 43 | ········module.</p><p>If·the·<code·class="literal">%name</code>·directive·is·omitted,·it |
44 | ········defaults·to·<code·class="literal">happyParse</code>.</p><a·class="indexterm"·name="idp63 | 44 | ········defaults·to·<code·class="literal">happyParse</code>.</p><a·class="indexterm"·name="idp63936184"></a><p>The·<code·class="literal">%name</code>·directive·takes·an·optional |
45 | » second·parameter·which·specifies·the·top-level·non-terminal | 45 | » second·parameter·which·specifies·the·top-level·non-terminal |
46 | » which·is·to·be·parsed.··If·this·parameter·is·omitted,·it | 46 | » which·is·to·be·parsed.··If·this·parameter·is·omitted,·it |
47 | » defaults·to·the·first·non-terminal·defined·in·the | 47 | » defaults·to·the·first·non-terminal·defined·in·the |
48 | » grammar.</p><p>Multiple·<code·class="literal">%name</code>·directives·may·be | 48 | » grammar.</p><p>Multiple·<code·class="literal">%name</code>·directives·may·be |
49 | » given,·specifying·multiple·parser·entry·points·for·this | 49 | » given,·specifying·multiple·parser·entry·points·for·this |
50 | » grammar·(see·<a·class="xref"·href="sec-multiple-parsers.html"·title="2.7. Generating·Multiple·Parsers·From·a·Single·Grammar">Section 2.7,·“Generating·Multiple·Parsers·From·a·Single·Grammar”</a>).··When | 50 | » grammar·(see·<a·class="xref"·href="sec-multiple-parsers.html"·title="2.7. Generating·Multiple·Parsers·From·a·Single·Grammar">Section 2.7,·“Generating·Multiple·Parsers·From·a·Single·Grammar”</a>).··When |
51 | » multiple·<code·class="literal">%name</code>·directives·are·given,·they | 51 | » multiple·<code·class="literal">%name</code>·directives·are·given,·they |
52 | » must·all·specify·explicit·non-terminals.</p></div><div·class="sect2"·title="6.3.4. Partial·Parsers"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-partial-parsers"></a>6.3.4. Partial·Parsers</h3></div></div></div><pre·class="programlisting"> | 52 | » must·all·specify·explicit·non-terminals.</p></div><div·class="sect2"·title="6.3.4. Partial·Parsers"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-partial-parsers"></a>6.3.4. Partial·Parsers</h3></div></div></div><pre·class="programlisting"> |
53 | %partial·<Haskell·identifier>·[·<non-terminal>·] | 53 | %partial·<Haskell·identifier>·[·<non-terminal>·] |
54 | ... | 54 | ... |
55 | </pre><a·class="indexterm"·name="idp63 | 55 | </pre><a·class="indexterm"·name="idp63941432"></a><p>The·<code·class="literal">%partial</code>·directive·can·be·used·instead·of |
56 | » ··<code·class="literal">%name</code>.··It·indicates·that·the·generated·parser | 56 | » ··<code·class="literal">%name</code>.··It·indicates·that·the·generated·parser |
57 | » ··should·be·able·to·parse·an·initial·portion·of·the·input.··In | 57 | » ··should·be·able·to·parse·an·initial·portion·of·the·input.··In |
58 | » ··contrast,·a·parser·specified·with·<code·class="literal">%name</code>·will·only | 58 | » ··contrast,·a·parser·specified·with·<code·class="literal">%name</code>·will·only |
59 | » ··parse·the·entire·input.</p><p>A·parser·specified·with·<code·class="literal">%partial</code>·will·stop | 59 | » ··parse·the·entire·input.</p><p>A·parser·specified·with·<code·class="literal">%partial</code>·will·stop |
60 | » ··parsing·and·return·a·result·as·soon·as·there·exists·a·complete·parse, | 60 | » ··parsing·and·return·a·result·as·soon·as·there·exists·a·complete·parse, |
61 | » ··and·no·more·of·the·input·can·be·parsed.··It·does·this·by·accepting | 61 | » ··and·no·more·of·the·input·can·be·parsed.··It·does·this·by·accepting |
62 | » ··the·parse·if·it·is·followed·by·the·<code·class="literal">error</code>·token, | 62 | » ··the·parse·if·it·is·followed·by·the·<code·class="literal">error</code>·token, |
63 | » ··rather·than·insisting·that·the·parse·is·followed·by·the | 63 | » ··rather·than·insisting·that·the·parse·is·followed·by·the |
64 | » ··end·of·the·token·stream·(or·the·<code·class="literal">eof</code>·token·in·the | 64 | » ··end·of·the·token·stream·(or·the·<code·class="literal">eof</code>·token·in·the |
65 | » ··case·of·a·<code·class="literal">%lexer</code>·parser).</p></div><div·class="sect2"·title="6.3.5. Monad·Directive"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-monad-decl"></a>6.3.5. Monad·Directive</h3></div></div></div><pre·class="programlisting"> | 65 | » ··case·of·a·<code·class="literal">%lexer</code>·parser).</p></div><div·class="sect2"·title="6.3.5. Monad·Directive"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-monad-decl"></a>6.3.5. Monad·Directive</h3></div></div></div><pre·class="programlisting"> |
66 | %monad·{·<type>·}·{·<then>·}·{·<return>·} | 66 | %monad·{·<type>·}·{·<then>·}·{·<return>·} |
67 | </pre><a·class="indexterm"·name="idp63 | 67 | </pre><a·class="indexterm"·name="idp63948344"></a><p>(optional)·The·<code·class="literal">%monad</code>·directive·takes·three |
68 | ········arguments:·the·type·constructor·of·the·monad,·the | 68 | ········arguments:·the·type·constructor·of·the·monad,·the |
69 | ········<code·class="literal">then</code>·(or·<code·class="literal">bind</code>)·operation,·and·the | 69 | ········<code·class="literal">then</code>·(or·<code·class="literal">bind</code>)·operation,·and·the |
70 | ········<code·class="literal">return</code>·(or·<code·class="literal">unit</code>)·operation.··The·type | 70 | ········<code·class="literal">return</code>·(or·<code·class="literal">unit</code>)·operation.··The·type |
71 | ········constructor·can·be·any·type·with·kind·<code·class="literal">*·->·*</code>.</p><p>Monad·declarations·are·described·in·more·detail·in·<a·class="xref"·href="sec-monads.html"·title="2.5. Monadic·Parsers">Section 2.5,·“Monadic·Parsers”</a>.</p></div><div·class="sect2"·title="6.3.6. Lexical·Analyser"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-lexer-decl"></a>6.3.6. Lexical·Analyser</h3></div></div></div><pre·class="programlisting"> | 71 | ········constructor·can·be·any·type·with·kind·<code·class="literal">*·->·*</code>.</p><p>Monad·declarations·are·described·in·more·detail·in·<a·class="xref"·href="sec-monads.html"·title="2.5. Monadic·Parsers">Section 2.5,·“Monadic·Parsers”</a>.</p></div><div·class="sect2"·title="6.3.6. Lexical·Analyser"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-lexer-decl"></a>6.3.6. Lexical·Analyser</h3></div></div></div><pre·class="programlisting"> |
72 | %lexer·{·<lexer>·}·{·<eof>·} | 72 | %lexer·{·<lexer>·}·{·<eof>·} |
73 | </pre><a·class="indexterm"·name="idp63 | 73 | </pre><a·class="indexterm"·name="idp63967416"></a><p>(optional)·The·<code·class="literal">%lexer</code>·directive·takes·two |
74 | ········arguments:·<code·class="literal"><lexer></code>·is·the·name·of·the·lexical | 74 | ········arguments:·<code·class="literal"><lexer></code>·is·the·name·of·the·lexical |
75 | ········analyser·function,·and·<code·class="literal"><eof></code>·is·a·token·that | 75 | ········analyser·function,·and·<code·class="literal"><eof></code>·is·a·token·that |
76 | ········is·to·be·treated·as·the·end·of·file.</p><p>Lexer·declarations·are·described·in·more·detail·in·<a·class="xref"·href="sec-monads.html#sec-lexers"·title="2.5.2. Threaded·Lexers">Section 2.5.2,·“Threaded·Lexers”</a>.</p></div><div·class="sect2"·title="6.3.7. Precedence·declarations"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-prec-decls"></a>6.3.7. Precedence·declarations</h3></div></div></div><pre·class="programlisting"> | 76 | ········is·to·be·treated·as·the·end·of·file.</p><p>Lexer·declarations·are·described·in·more·detail·in·<a·class="xref"·href="sec-monads.html#sec-lexers"·title="2.5.2. Threaded·Lexers">Section 2.5.2,·“Threaded·Lexers”</a>.</p></div><div·class="sect2"·title="6.3.7. Precedence·declarations"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-prec-decls"></a>6.3.7. Precedence·declarations</h3></div></div></div><pre·class="programlisting"> |
77 | %left·····<name>·... | 77 | %left·····<name>·... |
78 | %right····<name>·... | 78 | %right····<name>·... |
79 | %nonassoc·<name>·... | 79 | %nonassoc·<name>·... |
80 | </pre><a·class="indexterm"·name="idp639 | 80 | </pre><a·class="indexterm"·name="idp63972664"></a><a·class="indexterm"·name="idp63973944"></a><a·class="indexterm"·name="idp63979320"></a><p>These·declarations·are·used·to·specify·the·precedences |
81 | » and·associativity·of·tokens.··The·precedence·assigned·by·a | 81 | » and·associativity·of·tokens.··The·precedence·assigned·by·a |
82 | » <code·class="literal">%left</code>,·<code·class="literal">%right</code>·or | 82 | » <code·class="literal">%left</code>,·<code·class="literal">%right</code>·or |
83 | » <code·class="literal">%nonassoc</code>·declaration·is·defined·to·be | 83 | » <code·class="literal">%nonassoc</code>·declaration·is·defined·to·be |
84 | » higher·than·the·precedence·assigned·by·all·declarations | 84 | » higher·than·the·precedence·assigned·by·all·declarations |
85 | » earlier·in·the·file,·and·lower·than·the·precedence·assigned·by | 85 | » earlier·in·the·file,·and·lower·than·the·precedence·assigned·by |
86 | » all·declarations·later·in·the·file.</p><p>The·associativity·of·a·token·relative·to·tokens·in·the | 86 | » all·declarations·later·in·the·file.</p><p>The·associativity·of·a·token·relative·to·tokens·in·the |
87 | » same·<code·class="literal">%left</code>,·<code·class="literal">%right</code>,·or | 87 | » same·<code·class="literal">%left</code>,·<code·class="literal">%right</code>,·or |
88 | » <code·class="literal">%nonassoc</code>·declaration·is·to·the·left,·to | 88 | » <code·class="literal">%nonassoc</code>·declaration·is·to·the·left,·to |
89 | » the·right,·or·non-associative·respectively.</p><p>Precedence·declarations·are·described·in·more·detail·in | 89 | » the·right,·or·non-associative·respectively.</p><p>Precedence·declarations·are·described·in·more·detail·in |
90 | » <a·class="xref"·href="sec-Precedences.html"·title="2.3. Using·Precedences">Section 2.3,·“Using·Precedences”</a>.</p></div><div·class="sect2"·title="6.3.8. Expect·declarations"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-expect"></a>6.3.8. Expect·declarations</h3></div></div></div><pre·class="programlisting"> | 90 | » <a·class="xref"·href="sec-Precedences.html"·title="2.3. Using·Precedences">Section 2.3,·“Using·Precedences”</a>.</p></div><div·class="sect2"·title="6.3.8. Expect·declarations"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-expect"></a>6.3.8. Expect·declarations</h3></div></div></div><pre·class="programlisting"> |
91 | %expect·<number> | 91 | %expect·<number> |
92 | </pre><a·class="indexterm"·name="idp639 | 92 | </pre><a·class="indexterm"·name="idp63986744"></a><p>(optional)·More·often·than·not·the·grammar·you·write |
93 | » will·have·conflicts.·These·conflicts·generate·warnings.·But | 93 | » will·have·conflicts.·These·conflicts·generate·warnings.·But |
94 | » when·you·have·checked·the·warnings·and·made·sure·that·Happy | 94 | » when·you·have·checked·the·warnings·and·made·sure·that·Happy |
95 | » handles·them·correctly·these·warnings·are·just·annoying.·The | 95 | » handles·them·correctly·these·warnings·are·just·annoying.·The |
96 | » <code·class="literal">%expect</code>·directive·gives·a·way·of·avoiding | 96 | » <code·class="literal">%expect</code>·directive·gives·a·way·of·avoiding |
97 | » them.·Declaring·<code·class="literal">%expect | 97 | » them.·Declaring·<code·class="literal">%expect |
98 | » <em·class="replaceable"><code>n</code></em></code>·is·a·way·of·telling | 98 | » <em·class="replaceable"><code>n</code></em></code>·is·a·way·of·telling |
99 | » Happy·“There·are·exactly·<em·class="replaceable"><code>n</code></em> | 99 | » Happy·“There·are·exactly·<em·class="replaceable"><code>n</code></em> |
Offset 101, 28 lines modified | Offset 101, 28 lines modified | ||
101 | » this·grammar.·I·promise·I·have·checked·them·and·they·are | 101 | » this·grammar.·I·promise·I·have·checked·them·and·they·are |
102 | » resolved·correctly”.··When·processing·the·grammar,·Happy | 102 | » resolved·correctly”.··When·processing·the·grammar,·Happy |
103 | » will·check·the·actual·number·of·conflicts·against·the | 103 | » will·check·the·actual·number·of·conflicts·against·the |
104 | » <code·class="literal">%expect</code>·declaration·if·any,·and·if·there·is | 104 | » <code·class="literal">%expect</code>·declaration·if·any,·and·if·there·is |
105 | » a·discrepancy·then·an·error·will·be·reported.</p><p>Happy's·<code·class="literal">%expect</code>·directive·works | 105 | » a·discrepancy·then·an·error·will·be·reported.</p><p>Happy's·<code·class="literal">%expect</code>·directive·works |
106 | » exactly·like·that·of·yacc.</p></div><div·class="sect2"·title="6.3.9. Error·declaration"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-error-directive"></a>6.3.9. Error·declaration</h3></div></div></div><pre·class="programlisting"> | 106 | » exactly·like·that·of·yacc.</p></div><div·class="sect2"·title="6.3.9. Error·declaration"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-error-directive"></a>6.3.9. Error·declaration</h3></div></div></div><pre·class="programlisting"> |
107 | %error·{·<identifier>·} | 107 | %error·{·<identifier>·} |
108 | </pre><a·class="indexterm"·name="idp639 | 108 | </pre><a·class="indexterm"·name="idp63993144"></a><p>Specifies·the·function·to·be·called·in·the·event·of·a |
109 | » parse·error.··The·type·of·<code·class="literal"><f></code>·varies | 109 | » parse·error.··The·type·of·<code·class="literal"><f></code>·varies |
110 | » depending·on·the·presence·of·<code·class="literal">%lexer</code>·(see | 110 | » depending·on·the·presence·of·<code·class="literal">%lexer</code>·(see |
111 | » <a·class="xref"·href="sec-monads.html#sec-monad-summary"·title="2.5.4. Summary">Section 2.5.4,·“Summary”</a>).</p></div><div·class="sect2"·title="6.3.10. Attribute·Type·Declaration"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-attributes"></a>6.3.10. Attribute·Type·Declaration</h3></div></div></div><pre·class="programlisting"> | 111 | » <a·class="xref"·href="sec-monads.html#sec-monad-summary"·title="2.5.4. Summary">Section 2.5.4,·“Summary”</a>).</p></div><div·class="sect2"·title="6.3.10. Attribute·Type·Declaration"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-attributes"></a>6.3.10. Attribute·Type·Declaration</h3></div></div></div><pre·class="programlisting"> |
112 | %attributetype·{·<valid·Haskell·type·declaration>·} | 112 | %attributetype·{·<valid·Haskell·type·declaration>·} |
113 | </pre><a·class="indexterm"·name="idp639 | 113 | </pre><a·class="indexterm"·name="idp63997496"></a><p>(optional)·This·directive·allows·you·to·declare·the·type·of·the |
114 | » attributes·record·when·defining·an·attribute·grammar.··If·this·declaration | 114 | » attributes·record·when·defining·an·attribute·grammar.··If·this·declaration |
115 | » is·not·given,·Happy·will·choose·a·default.··This·declaration·may·only | 115 | » is·not·given,·Happy·will·choose·a·default.··This·declaration·may·only |
116 | » appear·once·in·a·grammar. | 116 | » appear·once·in·a·grammar. |
117 | » </p><p> | 117 | » </p><p> |
118 | » ··Attribute·grammars·are·explained·in·<a·class="xref"·href="sec-AttributeGrammar.html"·title="Chapter 4. Attribute·Grammars">Chapter 4,·<i>Attribute·Grammars</i></a>. | 118 | » ··Attribute·grammars·are·explained·in·<a·class="xref"·href="sec-AttributeGrammar.html"·title="Chapter 4. Attribute·Grammars">Chapter 4,·<i>Attribute·Grammars</i></a>. |
119 | » </p></div><div·class="sect2"·title="6.3.11. Attribute·declaration"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-attribute"></a>6.3.11. Attribute·declaration</h3></div></div></div><pre·class="programlisting"> | 119 | » </p></div><div·class="sect2"·title="6.3.11. Attribute·declaration"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-attribute"></a>6.3.11. Attribute·declaration</h3></div></div></div><pre·class="programlisting"> |
120 | %attribute·<Haskell·identifier>·{·<valid·Haskell·type>·} | 120 | %attribute·<Haskell·identifier>·{·<valid·Haskell·type>·} |
121 | </pre><a·class="indexterm"·name="idp6 | 121 | </pre><a·class="indexterm"·name="idp64005432"></a><p>The·presence·of·one·or·more·of·these·directives·declares·that·the |
122 | ·······grammar·is·an·attribute·grammar.··The·first·attribute·listed·becomes·the | 122 | ·······grammar·is·an·attribute·grammar.··The·first·attribute·listed·becomes·the |
123 | ·······default·attribute.··Each·<code·class="literal">%attribute</code>·directive·generates·a | 123 | ·······default·attribute.··Each·<code·class="literal">%attribute</code>·directive·generates·a |
124 | ·······field·in·the·attributes·record·with·the·given·label·and·type.··If·there | 124 | ·······field·in·the·attributes·record·with·the·given·label·and·type.··If·there |
125 | ·······is·an·<code·class="literal">%attributetype</code>·declaration·in·the·grammar·which | 125 | ·······is·an·<code·class="literal">%attributetype</code>·declaration·in·the·grammar·which |
126 | ·······introduces·type·variables,·then·the·type·of·an·attribute·may·mention·any | 126 | ·······introduces·type·variables,·then·the·type·of·an·attribute·may·mention·any |
127 | ·······such·type·variables. | 127 | ·······such·type·variables. |
128 | ·······</p><p> | 128 | ·······</p><p> |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.6. The·Error·Token</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-monads.html"·title="2.5. Monadic·Parsers"><link·rel="next"·href="sec-multiple-parsers.html"·title="2.7. Generating·Multiple·Parsers·From·a·Single·Grammar"></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">2.6. The·Error·Token</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-monads.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-multiple-pa·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.6. The·Error·Token</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-monads.html"·title="2.5. Monadic·Parsers"><link·rel="next"·href="sec-multiple-parsers.html"·title="2.7. Generating·Multiple·Parsers·From·a·Single·Grammar"></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">2.6. The·Error·Token</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-monads.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-multiple-pa·✂ |
2 | ······recovery,·using·the·special·symbol·<code·class="literal">error</code>·in·a·grammar | 2 | ······recovery,·using·the·special·symbol·<code·class="literal">error</code>·in·a·grammar |
3 | ······file.··When·<span·class="application">Happy</span>·finds·a·parse·error·during | 3 | ······file.··When·<span·class="application">Happy</span>·finds·a·parse·error·during |
4 | ······parsing,·it·automatically·inserts·the·<code·class="literal">error</code>·symbol;·if | 4 | ······parsing,·it·automatically·inserts·the·<code·class="literal">error</code>·symbol;·if |
5 | ······your·grammar·deals·with·<code·class="literal">error</code>·explicitly,·then·it·can | 5 | ······your·grammar·deals·with·<code·class="literal">error</code>·explicitly,·then·it·can |
6 | ······detect·the·error·and·carry·on.</p><p>For·example,·the·<span·class="application">Happy</span>·grammar·for·Haskell | 6 | ······detect·the·error·and·carry·on.</p><p>For·example,·the·<span·class="application">Happy</span>·grammar·for·Haskell |
7 | ······uses·error·recovery·to·implement·Haskell·layout.··The·grammar | 7 | ······uses·error·recovery·to·implement·Haskell·layout.··The·grammar |
8 | ······has·a·rule·that·looks·like·this:</p><pre·class="programlisting"> | 8 | ······has·a·rule·that·looks·like·this:</p><pre·class="programlisting"> |
Offset 10, 15 lines modified | Offset 10, 15 lines modified | ||
10 | ······|·error» » ·····{·()·} | 10 | ······|·error» » ·····{·()·} |
11 | </pre><p>This·says·that·a·close·brace·in·a·layout-indented·context | 11 | </pre><p>This·says·that·a·close·brace·in·a·layout-indented·context |
12 | ······may·be·either·a·curly·brace·(inserted·by·the·lexical·analyser), | 12 | ······may·be·either·a·curly·brace·(inserted·by·the·lexical·analyser), |
13 | ······or·a·parse·error.··</p><p>This·rule·is·used·to·parse·expressions·like·<code·class="literal">let·x | 13 | ······or·a·parse·error.··</p><p>This·rule·is·used·to·parse·expressions·like·<code·class="literal">let·x |
14 | ······=·e·in·e'</code>:·the·layout·system·inserts·an·open·brace·before | 14 | ······=·e·in·e'</code>:·the·layout·system·inserts·an·open·brace·before |
15 | ······<code·class="literal">x</code>,·and·the·occurrence·of·the·<code·class="literal">in</code>·symbol | 15 | ······<code·class="literal">x</code>,·and·the·occurrence·of·the·<code·class="literal">in</code>·symbol |
16 | ······generates·a·parse·error,·which·is·interpreted·as·a·close·brace | 16 | ······generates·a·parse·error,·which·is·interpreted·as·a·close·brace |
17 | ······by·the·above·rule.</p><a·class="indexterm"·name="idp63 | 17 | ······by·the·above·rule.</p><a·class="indexterm"·name="idp63562552"></a><p>Note·for·<code·class="literal">yacc</code>·users:·this·form·of·error·recovery |
18 | ······is·strictly·more·limited·than·that·provided·by·<code·class="literal">yacc</code>. | 18 | ······is·strictly·more·limited·than·that·provided·by·<code·class="literal">yacc</code>. |
19 | ······During·a·parse·error·condition,·<code·class="literal">yacc</code>·attempts·to | 19 | ······During·a·parse·error·condition,·<code·class="literal">yacc</code>·attempts·to |
20 | ······discard·states·and·tokens·in·order·to·get·back·into·a·state | 20 | ······discard·states·and·tokens·in·order·to·get·back·into·a·state |
21 | ······where·parsing·may·continue;·<span·class="application">Happy</span>·doesn't·do·this. | 21 | ······where·parsing·may·continue;·<span·class="application">Happy</span>·doesn't·do·this. |
22 | ······The·reason·is·that·normal·<code·class="literal">yacc</code>·error·recovery·is | 22 | ······The·reason·is·that·normal·<code·class="literal">yacc</code>·error·recovery·is |
23 | ······notoriously·hard·to·describe,·and·the·semantics·depend·heavily | 23 | ······notoriously·hard·to·describe,·and·the·semantics·depend·heavily |
24 | ······on·the·workings·of·a·shift-reduce·parser.··Furthermore, | 24 | ······on·the·workings·of·a·shift-reduce·parser.··Furthermore, |
Offset 1, 20 lines modified | Offset 1, 20 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.3. Finding·Type·Errors</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-compilation-time.html"·title="8.2. Compilation-Time·Tips"><link·rel="next"·href="sec-conflict-tips.html"·title="8.4. Conflict·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">8.3. Finding·Type·Errors</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-compilation-time.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-conflict-tips.html">Next</a></td></tr></table><hr></div><div·cla·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.3. Finding·Type·Errors</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-compilation-time.html"·title="8.2. Compilation-Time·Tips"><link·rel="next"·href="sec-conflict-tips.html"·title="8.4. Conflict·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">8.3. Finding·Type·Errors</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-compilation-time.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-conflict-tips.html">Next</a></td></tr></table><hr></div><div·cla·✂ |
2 | ······difficult·because·the·code·for·reductions·is·moved·around·before | 2 | ······difficult·because·the·code·for·reductions·is·moved·around·before |
3 | ······being·placed·in·the·parser.··We·currently·have·no·way·of·passing | 3 | ······being·placed·in·the·parser.··We·currently·have·no·way·of·passing |
4 | ······the·original·filename·and·line·numbers·to·the·Haskell·compiler, | 4 | ······the·original·filename·and·line·numbers·to·the·Haskell·compiler, |
5 | ······so·there·is·no·alternative·but·to·look·at·the·parser·and·match | 5 | ······so·there·is·no·alternative·but·to·look·at·the·parser·and·match |
6 | ······the·code·to·the·grammar·file.··An·info·file·(generated·by·the | 6 | ······the·code·to·the·grammar·file.··An·info·file·(generated·by·the |
7 | ······<code·class="literal">-i</code>·option)·can·be·helpful·here.</p><a·class="indexterm"·name="idp6400 | 7 | ······<code·class="literal">-i</code>·option)·can·be·helpful·here.</p><a·class="indexterm"·name="idp64070840"></a><p>Type·signature·sometimes·help·by·pinning·down·the |
8 | ······particular·error·to·the·place·where·the·mistake·is·made,·not | 8 | ······particular·error·to·the·place·where·the·mistake·is·made,·not |
9 | ······half·way·down·the·file.··For·each·production·in·the·grammar, | 9 | ······half·way·down·the·file.··For·each·production·in·the·grammar, |
10 | ······there's·a·bit·of·code·in·the·generated·file·that·looks·like | 10 | ······there's·a·bit·of·code·in·the·generated·file·that·looks·like |
11 | ······this:</p><pre·class="programlisting"> | 11 | ······this:</p><pre·class="programlisting"> |
12 | HappyAbsSyn<n>·(·E·) | 12 | HappyAbsSyn<n>·(·E·) |
13 | </pre><a·class="indexterm"·name="idp640 | 13 | </pre><a·class="indexterm"·name="idp64085304"></a><p>where·<code·class="literal">E</code>·is·the·Haskell·expression·from·the |
14 | ······grammar·file·(with·<code·class="literal">$n</code>·replaced·by | 14 | ······grammar·file·(with·<code·class="literal">$n</code>·replaced·by |
15 | ······<code·class="literal">happy_var_n</code>).··If·there·is·a·type·signature·for·this | 15 | ······<code·class="literal">happy_var_n</code>).··If·there·is·a·type·signature·for·this |
16 | ······production,·then·<span·class="application">Happy</span>·will·have·taken·it·into | 16 | ······production,·then·<span·class="application">Happy</span>·will·have·taken·it·into |
17 | ······account·when·declaring·the·HappyAbsSyn·datatype,·and·errors·in | 17 | ······account·when·declaring·the·HappyAbsSyn·datatype,·and·errors·in |
18 | ······<code·class="literal">E</code>·will·be·caught·right·here.··Of·course,·the·error·may | 18 | ······<code·class="literal">E</code>·will·be·caught·right·here.··Of·course,·the·error·may |
19 | ······be·really·caused·by·incorrect·use·of·one·of·the | 19 | ······be·really·caused·by·incorrect·use·of·one·of·the |
20 | ······<code·class="literal">happy_var_n</code>·variables.</p><p>(this·section·will·contain·more·info·as·we·gain·experience | 20 | ······<code·class="literal">happy_var_n</code>·variables.</p><p>(this·section·will·contain·more·info·as·we·gain·experience |
Offset 4, 15 lines modified | Offset 4, 15 lines modified | ||
4 | ····module·that·can·be·included·in·your·program.··This·section·gives | 4 | ····module·that·can·be·included·in·your·program.··This·section·gives |
5 | ····the·exact·syntax·of·grammar·files.·</p><p>The·overall·format·of·the·grammar·file·is·given·below:</p><pre·class="programlisting"> | 5 | ····the·exact·syntax·of·grammar·files.·</p><p>The·overall·format·of·the·grammar·file·is·given·below:</p><pre·class="programlisting"> |
6 | <optional·module·header> | 6 | <optional·module·header> |
7 | <directives> | 7 | <directives> |
8 | %% | 8 | %% |
9 | <grammar> | 9 | <grammar> |
10 | <optional·module·trailer> | 10 | <optional·module·trailer> |
11 | </pre><a·class="indexterm"·name="idp63 | 11 | </pre><a·class="indexterm"·name="idp63902264"></a><a·class="indexterm"·name="idp63903544"></a><p>If·the·name·of·the·grammar·file·ends·in·<code·class="literal">.ly</code>,·then |
12 | ····it·is·assumed·to·be·a·literate·script.··All·lines·except·those | 12 | ····it·is·assumed·to·be·a·literate·script.··All·lines·except·those |
13 | ····beginning·with·a·<code·class="literal">></code>·will·be·ignored,·and·the | 13 | ····beginning·with·a·<code·class="literal">></code>·will·be·ignored,·and·the |
14 | ····<code·class="literal">></code>·will·be·stripped·from·the·beginning·of·all·the·code | 14 | ····<code·class="literal">></code>·will·be·stripped·from·the·beginning·of·all·the·code |
15 | ····lines.··There·must·be·a·blank·line·between·each·code·section | 15 | ····lines.··There·must·be·a·blank·line·between·each·code·section |
16 | ····(lines·beginning·with·<code·class="literal">></code>)·and·comment·section. | 16 | ····(lines·beginning·with·<code·class="literal">></code>)·and·comment·section. |
17 | ····Grammars·not·using·the·literate·notation·must·be·in·a·file·with | 17 | ····Grammars·not·using·the·literate·notation·must·be·in·a·file·with |
18 | ····the·<code·class="literal">.y</code>·suffix.</p><div·class="sect1"·title="6.1. Lexical·Rules"><div·class="titlepage"><div><div><h2·class="title"·style="clear:·both"><a·name="sec-lexical-rules"></a>6.1. Lexical·Rules</h2></div></div></div><p>Identifiers·in·<span·class="application">Happy</span>·grammar·files·must·take·the·following·form·(using | 18 | ····the·<code·class="literal">.y</code>·suffix.</p><div·class="sect1"·title="6.1. Lexical·Rules"><div·class="titlepage"><div><div><h2·class="title"·style="clear:·both"><a·name="sec-lexical-rules"></a>6.1. Lexical·Rules</h2></div></div></div><p>Identifiers·in·<span·class="application">Happy</span>·grammar·files·must·take·the·following·form·(using |
Offset 1, 12 lines modified | Offset 1, 12 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>6.4. Grammar</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="prev"·href="sec-directives.html"·title="6.3. Directives"><link·rel="next"·href="sec-module-trailer.html"·title="6.5. Module·Trailer"></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">6.4. Grammar</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-directives.html">Prev</a> </td><th·width="60%"·align="center">Chapter 6. Syntax·of·Grammar·Files</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-module-trailer.html">Next</a></td></tr></table><hr></div><div··✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>6.4. Grammar</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="prev"·href="sec-directives.html"·title="6.3. Directives"><link·rel="next"·href="sec-module-trailer.html"·title="6.5. Module·Trailer"></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">6.4. Grammar</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-directives.html">Prev</a> </td><th·width="60%"·align="center">Chapter 6. Syntax·of·Grammar·Files</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-module-trailer.html">Next</a></td></tr></table><hr></div><div··✂ |
2 | ······from·them·by·a·double-percent·(<code·class="literal">%%</code>)·symbol. | 2 | ······from·them·by·a·double-percent·(<code·class="literal">%%</code>)·symbol. |
3 | ······This·section·contains·a·number·of | 3 | ······This·section·contains·a·number·of |
4 | ······<span·class="emphasis"><em>productions</em></span>,·each·of·which·defines·a·single | 4 | ······<span·class="emphasis"><em>productions</em></span>,·each·of·which·defines·a·single |
5 | ······non-terminal.··Each·production·has·the·following·syntax:</p><a·class="indexterm"·name="idp6 | 5 | ······non-terminal.··Each·production·has·the·following·syntax:</p><a·class="indexterm"·name="idp64011320"></a><pre·class="programlisting"> |
6 | <non-terminal>·[·::·{·<type>·}·] | 6 | <non-terminal>·[·::·{·<type>·}·] |
7 | ········:··<id>·...·{[%]·<expression>·} | 7 | ········:··<id>·...·{[%]·<expression>·} |
8 | ······[·|··<id>·...·{[%]·<expression>·} | 8 | ······[·|··<id>·...·{[%]·<expression>·} |
9 | ········...·] | 9 | ········...·] |
10 | </pre><p>The·first·line·gives·the·non-terminal·to·be·defined·by·the | 10 | </pre><p>The·first·line·gives·the·non-terminal·to·be·defined·by·the |
11 | ······production·and·optionally·its·type·(type·signatures·for | 11 | ······production·and·optionally·its·type·(type·signatures·for |
12 | ······productions·are·discussed·in·<a·class="xref"·href="sec-type-signatures.html"·title="2.4. Type·Signatures">Section 2.4,·“Type·Signatures”</a>).</p><p>Each·production·has·at·least·one,·and·possibly·many | 12 | ······productions·are·discussed·in·<a·class="xref"·href="sec-type-signatures.html"·title="2.4. Type·Signatures">Section 2.4,·“Type·Signatures”</a>).</p><p>Each·production·has·at·least·one,·and·possibly·many |
Offset 21, 15 lines modified | Offset 21, 15 lines modified | ||
21 | ······the·current·rule.·The·symbol·<code·class="literal">$</code>·may·be | 21 | ······the·current·rule.·The·symbol·<code·class="literal">$</code>·may·be |
22 | ······inserted·literally·in·the·Haskell·expression·using·the·sequence | 22 | ······inserted·literally·in·the·Haskell·expression·using·the·sequence |
23 | ······<code·class="literal">\$</code>·(this·isn't·necessary·inside·a | 23 | ······<code·class="literal">\$</code>·(this·isn't·necessary·inside·a |
24 | ······string·or·character·literal).</p><p>Additionally,·the·sequence·<code·class="literal">$></code> | 24 | ······string·or·character·literal).</p><p>Additionally,·the·sequence·<code·class="literal">$></code> |
25 | ······can·be·used·to·represent·the·value·of·the·rightmost·symbol.</p><p>A·semantic·value·of·the·form·<code·class="literal">{%·...·}</code>·is·a | 25 | ······can·be·used·to·represent·the·value·of·the·rightmost·symbol.</p><p>A·semantic·value·of·the·form·<code·class="literal">{%·...·}</code>·is·a |
26 | ······<span·class="emphasis"><em>monadic·action</em></span>,·and·is·only·valid·when·the·grammar | 26 | ······<span·class="emphasis"><em>monadic·action</em></span>,·and·is·only·valid·when·the·grammar |
27 | ······file·contains·a·<code·class="literal">%monad</code>·directive·(<a·class="xref"·href="sec-directives.html#sec-monad-decl"·title="6.3.5. Monad·Directive">Section 6.3.5,·“Monad·Directive”</a>).··Monadic·actions·are·discussed·in | 27 | ······file·contains·a·<code·class="literal">%monad</code>·directive·(<a·class="xref"·href="sec-directives.html#sec-monad-decl"·title="6.3.5. Monad·Directive">Section 6.3.5,·“Monad·Directive”</a>).··Monadic·actions·are·discussed·in |
28 | ······<a·class="xref"·href="sec-monads.html"·title="2.5. Monadic·Parsers">Section 2.5,·“Monadic·Parsers”</a>.</p><a·class="indexterm"·name="idp6 | 28 | ······<a·class="xref"·href="sec-monads.html"·title="2.5. Monadic·Parsers">Section 2.5,·“Monadic·Parsers”</a>.</p><a·class="indexterm"·name="idp64020536"></a><p>Remember·that·all·the·expressions·for·a·production·must |
29 | ······have·the·same·type.</p><div·class="sect2"·title="6.4.1. Parameterized·Productions"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-param-prods"></a>6.4.1. Parameterized·Productions</h3></div></div></div> | 29 | ······have·the·same·type.</p><div·class="sect2"·title="6.4.1. Parameterized·Productions"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-param-prods"></a>6.4.1. Parameterized·Productions</h3></div></div></div> |
30 | ········Starting·from·version·1.17.1,·<span·class="application">Happy</span>·supports | 30 | ········Starting·from·version·1.17.1,·<span·class="application">Happy</span>·supports |
31 | ········<span·class="emphasis"><em>parameterized·productions</em></span>·which·provide·a | 31 | ········<span·class="emphasis"><em>parameterized·productions</em></span>·which·provide·a |
32 | ········convenient·notation·for·capturing·recurring·patterns·in·context·free | 32 | ········convenient·notation·for·capturing·recurring·patterns·in·context·free |
33 | ········grammars.·This·gives·the·benefits·of·something·similar·to·parsing | 33 | ········grammars.·This·gives·the·benefits·of·something·similar·to·parsing |
34 | ········combinators·in·the·context·of·<span·class="application">Happy</span> | 34 | ········combinators·in·the·context·of·<span·class="application">Happy</span> |
35 | ········grammars. | 35 | ········grammars. |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.5. Using·Happy·with·GHCi</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-conflict-tips.html"·title="8.4. Conflict·Tips"><link·rel="next"·href="sec-monad-alex.html"·title="8.6. Basic·monadic·Happy·use·with·Alex"></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">8.5. Using·Happy·with·<span·class="application">GHCi</span></th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-conflict-tips.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-monad-alex.html">Next</a·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.5. Using·Happy·with·GHCi</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-conflict-tips.html"·title="8.4. Conflict·Tips"><link·rel="next"·href="sec-monad-alex.html"·title="8.6. Basic·monadic·Happy·use·with·Alex"></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">8.5. Using·Happy·with·<span·class="application">GHCi</span></th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-conflict-tips.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-monad-alex.html">Next</a·✂ |
2 | ······doesn't·understand·Happy·grammars,·but·with·some·creative·use·of | 2 | ······doesn't·understand·Happy·grammars,·but·with·some·creative·use·of |
3 | ······macros·and·makefiles·we·can·give·the·impression·that | 3 | ······macros·and·makefiles·we·can·give·the·impression·that |
4 | ······<span·class="application">GHCi</span>·is·invoking·Happy | 4 | ······<span·class="application">GHCi</span>·is·invoking·Happy |
5 | ······automatically:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>Create·a·simple·makefile,·called | 5 | ······automatically:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>Create·a·simple·makefile,·called |
6 | » ··<code·class="filename">Makefile_happysrcs</code>:</p><pre·class="programlisting">HAPPY·=·happy | 6 | » ··<code·class="filename">Makefile_happysrcs</code>:</p><pre·class="programlisting">HAPPY·=·happy |
7 | HAPPY_OPTS·= | 7 | HAPPY_OPTS·= |
Offset 1, 1 lines modified | Offset 1, 1 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Chapter 7. Info·Files</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="index.html"·title="Happy·User·Guide"><link·rel="prev"·href="sec-module-trailer.html"·title="6.5. Module·Trailer"><link·rel="next"·href="sec-tips.html"·title="Chapter 8. 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 7. Info·Files</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-module-trailer.html">Prev</a> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-tips.html">Next</a></td></tr></table><hr></div><div·class="chapter"·title="Chapter 7. Info·Files"><div·class=·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Chapter 7. Info·Files</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="index.html"·title="Happy·User·Guide"><link·rel="prev"·href="sec-module-trailer.html"·title="6.5. Module·Trailer"><link·rel="next"·href="sec-tips.html"·title="Chapter 8. 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 7. Info·Files</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-module-trailer.html">Prev</a> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-tips.html">Next</a></td></tr></table><hr></div><div·class="chapter"·title="Chapter 7. Info·Files"><div·class=·✂ |
Offset 4, 28 lines modified | Offset 4, 28 lines modified | ||
4 | ····will·be·the·value·used.</p><p>There·are·two·types·of·grammar·files, | 4 | ····will·be·the·value·used.</p><p>There·are·two·types·of·grammar·files, |
5 | ····<code·class="filename">file.y</code>·and·<code·class="filename">file.ly</code>,·with | 5 | ····<code·class="filename">file.y</code>·and·<code·class="filename">file.ly</code>,·with |
6 | ····the·latter·observing·the·reverse·comment·(or·literate)·convention | 6 | ····the·latter·observing·the·reverse·comment·(or·literate)·convention |
7 | ····(i.e.·each·code·line·must·begin·with·the·character | 7 | ····(i.e.·each·code·line·must·begin·with·the·character |
8 | ····<code·class="literal">></code>,·lines·which·don't·begin·with | 8 | ····<code·class="literal">></code>,·lines·which·don't·begin·with |
9 | ····<code·class="literal">></code>·are·treated·as·comments).··The·examples | 9 | ····<code·class="literal">></code>·are·treated·as·comments).··The·examples |
10 | ····distributed·with·<span·class="application">Happy</span>·are·all·of·the | 10 | ····distributed·with·<span·class="application">Happy</span>·are·all·of·the |
11 | ····.ly·form.</p><a·class="indexterm"·name="idp63 | 11 | ····.ly·form.</p><a·class="indexterm"·name="idp63845688"></a><p>The·flags·accepted·by·<span·class="application">Happy</span>·are·as·follows:</p><div·class="variablelist"><dl><dt><span·class="term"><code·class="option">-o</code>·<em·class="replaceable"><code>file</code></em>,·</span><span·class="term"><code·class="option">--outfile</code>=<em·class="replaceable"><code>file</code></em></span></dt><dd><p>Specifies·the·destination·of·the·generated·parser·module. |
12 | » ··If·omitted,·the·parser·will·be·placed·in | 12 | » ··If·omitted,·the·parser·will·be·placed·in |
13 | ··········<em·class="replaceable"><code>file</code></em><code·class="literal">.hs</code>, | 13 | ··········<em·class="replaceable"><code>file</code></em><code·class="literal">.hs</code>, |
14 | » ··where·<em·class="replaceable"><code>file</code></em>·is·the·name·of·the·input | 14 | » ··where·<em·class="replaceable"><code>file</code></em>·is·the·name·of·the·input |
15 | ··········file·with·any·extension·removed.</p></dd><dt><span·class="term"><code·class="option">-i</code>[<span·class="optional"><em·class="replaceable"><code>file</code></em></span>],·</span><span·class="term"><code·class="option">--info</code>[<span·class="optional">=<em·class="replaceable"><code>file</code></em></span>]</span></dt><dd><a·class="indexterm"·name="idp63 | 15 | ··········file·with·any·extension·removed.</p></dd><dt><span·class="term"><code·class="option">-i</code>[<span·class="optional"><em·class="replaceable"><code>file</code></em></span>],·</span><span·class="term"><code·class="option">--info</code>[<span·class="optional">=<em·class="replaceable"><code>file</code></em></span>]</span></dt><dd><a·class="indexterm"·name="idp63854392"></a><p>·Directs·<span·class="application">Happy</span>·to·produce·an·info·file |
16 | ··········containing·detailed·information·about·the·grammar,·parser | 16 | ··········containing·detailed·information·about·the·grammar,·parser |
17 | ··········states,·parser·actions,·and·conflicts.··Info·files·are·vital | 17 | ··········states,·parser·actions,·and·conflicts.··Info·files·are·vital |
18 | ··········during·the·debugging·of·grammars.··The·filename·argument·is | 18 | ··········during·the·debugging·of·grammars.··The·filename·argument·is |
19 | ··········optional·(note·that·there's·no·space·between | 19 | ··········optional·(note·that·there's·no·space·between |
20 | ··········<code·class="literal">-i</code>·and·the·filename·in·the·short | 20 | ··········<code·class="literal">-i</code>·and·the·filename·in·the·short |
21 | ··········version),·and·if·omitted·the·info·file·will·be·written·to | 21 | ··········version),·and·if·omitted·the·info·file·will·be·written·to |
22 | ··········<em·class="replaceable"><code>file</code></em><code·class="literal">.info</code>·(where | 22 | ··········<em·class="replaceable"><code>file</code></em><code·class="literal">.info</code>·(where |
23 | ··········<em·class="replaceable"><code>file</code></em>·is·the·input·file·name·with·any | 23 | ··········<em·class="replaceable"><code>file</code></em>·is·the·input·file·name·with·any |
24 | ··········extension·removed).</p></dd><dt><span·class="term"><code·class="option">-t</code>·<em·class="replaceable"><code>dir</code></em>,·</span><span·class="term"><code·class="option">--template</code>=<em·class="replaceable"><code>dir</code></em></span></dt><dd><a·class="indexterm"·name="idp63 | 24 | ··········extension·removed).</p></dd><dt><span·class="term"><code·class="option">-t</code>·<em·class="replaceable"><code>dir</code></em>,·</span><span·class="term"><code·class="option">--template</code>=<em·class="replaceable"><code>dir</code></em></span></dt><dd><a·class="indexterm"·name="idp63860408"></a><p>Instructs·<span·class="application">Happy</span>·to·use·this·directory |
25 | ··········when·looking·for·template·files:·these·files·contain·the | 25 | ··········when·looking·for·template·files:·these·files·contain·the |
26 | ··········static·code·that·<span·class="application">Happy</span>·includes·in·every | 26 | ··········static·code·that·<span·class="application">Happy</span>·includes·in·every |
27 | ··········generated·parser.··You·shouldn't·need·to·use·this·option·if | 27 | ··········generated·parser.··You·shouldn't·need·to·use·this·option·if |
28 | ··········<span·class="application">Happy</span>·is·properly·configured·for·your | 28 | ··········<span·class="application">Happy</span>·is·properly·configured·for·your |
29 | ··········computer.</p></dd><dt><span·class="term"><code·class="option">-m</code>·<em·class="replaceable"><code>name</code></em>,·</span><span·class="term"><code·class="option">--magic-name</code>=<em·class="replaceable"><code>name</code></em></span></dt><dd><p>·<span·class="application">Happy</span>·prefixes·all·the·symbols·it·uses·internally | 29 | ··········computer.</p></dd><dt><span·class="term"><code·class="option">-m</code>·<em·class="replaceable"><code>name</code></em>,·</span><span·class="term"><code·class="option">--magic-name</code>=<em·class="replaceable"><code>name</code></em></span></dt><dd><p>·<span·class="application">Happy</span>·prefixes·all·the·symbols·it·uses·internally |
30 | ··········with·either·<code·class="literal">happy</code>·or·<code·class="literal">Happy</code>.··To·use·a | 30 | ··········with·either·<code·class="literal">happy</code>·or·<code·class="literal">Happy</code>.··To·use·a |
31 | ··········different·string,·for·example·if·the·use·of·<code·class="literal">happy</code> | 31 | ··········different·string,·for·example·if·the·use·of·<code·class="literal">happy</code> |
Offset 34, 24 lines modified | Offset 34, 24 lines modified | ||
34 | » ··experimental·and·may·cause·unpredictable·results.</p><p>This·option·causes·the·right·hand·side·of·each | 34 | » ··experimental·and·may·cause·unpredictable·results.</p><p>This·option·causes·the·right·hand·side·of·each |
35 | » ··production·(the·semantic·value)·to·be·evaluated·eagerly·at | 35 | » ··production·(the·semantic·value)·to·be·evaluated·eagerly·at |
36 | » ··the·moment·the·production·is·reduced.··If·the·lazy·behaviour | 36 | » ··the·moment·the·production·is·reduced.··If·the·lazy·behaviour |
37 | » ··is·not·required,·then·using·this·option·will·improve | 37 | » ··is·not·required,·then·using·this·option·will·improve |
38 | » ··performance·and·may·reduce·space·leaks.··Note·that·the | 38 | » ··performance·and·may·reduce·space·leaks.··Note·that·the |
39 | » ··parser·as·a·whole·is·never·lazy·-·the·whole·input·will | 39 | » ··parser·as·a·whole·is·never·lazy·-·the·whole·input·will |
40 | » ··always·be·consumed·before·any·input·is·produced,·regardless | 40 | » ··always·be·consumed·before·any·input·is·produced,·regardless |
41 | » ··of·the·setting·of·the·<code·class="option">--strict</code>·flag.</p></dd><dt><span·class="term"><code·class="option">-g</code>,·</span><span·class="term"><code·class="option">--ghc</code></span></dt><dd><a·class="indexterm"·name="idp638 | 41 | » ··of·the·setting·of·the·<code·class="option">--strict</code>·flag.</p></dd><dt><span·class="term"><code·class="option">-g</code>,·</span><span·class="term"><code·class="option">--ghc</code></span></dt><dd><a·class="indexterm"·name="idp63873848"></a><a·class="indexterm"·name="idp63874744"></a><p>Instructs·<span·class="application">Happy</span>·to·generate·a·parser |
42 | » ··that·uses·GHC-specific·extensions·to·obtain·faster·code.</p></dd><dt><span·class="term"><code·class="option">-c</code>,·</span><span·class="term"><code·class="option">--coerce</code></span></dt><dd><a·class="indexterm"·name="idp638 | 42 | » ··that·uses·GHC-specific·extensions·to·obtain·faster·code.</p></dd><dt><span·class="term"><code·class="option">-c</code>,·</span><span·class="term"><code·class="option">--coerce</code></span></dt><dd><a·class="indexterm"·name="idp63878712"></a><a·class="indexterm"·name="idp63879608"></a><p>·Use·GHC's·<code·class="literal">unsafeCoerce#</code>·extension·to |
43 | ··········generate·smaller·faster·parsers.··Type-safety·isn't | 43 | ··········generate·smaller·faster·parsers.··Type-safety·isn't |
44 | ··········compromised.</p><p>This·option·may·only·be·used·in·conjuction·with | 44 | ··········compromised.</p><p>This·option·may·only·be·used·in·conjuction·with |
45 | ··········<code·class="option">-g</code>.</p></dd><dt><span·class="term"><code·class="option">-a</code>,·</span><span·class="term"><code·class="option">--arrays</code></span></dt><dd><a·class="indexterm"·name="idp638 | 45 | ··········<code·class="option">-g</code>.</p></dd><dt><span·class="term"><code·class="option">-a</code>,·</span><span·class="term"><code·class="option">--arrays</code></span></dt><dd><a·class="indexterm"·name="idp63884344"></a><a·class="indexterm"·name="idp63885240"></a><p>·Instructs·<span·class="application">Happy</span>·to·generate·a·parser |
46 | ··········using·an·array-based·shift·reduce·parser.··When·used·in | 46 | ··········using·an·array-based·shift·reduce·parser.··When·used·in |
47 | ··········conjunction·with·<code·class="option">-g</code>,·the·arrays·will·be | 47 | ··········conjunction·with·<code·class="option">-g</code>,·the·arrays·will·be |
48 | ··········encoded·as·strings,·resulting·in·faster·parsers.··Without | 48 | ··········encoded·as·strings,·resulting·in·faster·parsers.··Without |
49 | ··········<code·class="option">-g</code>,·standard·Haskell·arrays·will·be | 49 | ··········<code·class="option">-g</code>,·standard·Haskell·arrays·will·be |
50 | ··········used.</p></dd><dt><span·class="term"><code·class="option">-d</code>,·</span><span·class="term"><code·class="option">--debug</code></span></dt><dd><a·class="indexterm"·name="idp638 | 50 | ··········used.</p></dd><dt><span·class="term"><code·class="option">-d</code>,·</span><span·class="term"><code·class="option">--debug</code></span></dt><dd><a·class="indexterm"·name="idp63890104"></a><a·class="indexterm"·name="idp63891000"></a><p>Generate·a·parser·that·will·print·debugging |
51 | » ··information·to·<code·class="literal">stderr</code>·at·run-time, | 51 | » ··information·to·<code·class="literal">stderr</code>·at·run-time, |
52 | » ··including·all·the·shifts,·reductions,·state·transitions·and | 52 | » ··including·all·the·shifts,·reductions,·state·transitions·and |
53 | » ··token·inputs·performed·by·the·parser.</p><p>This·option·can·only·be·used·in·conjunction·with | 53 | » ··token·inputs·performed·by·the·parser.</p><p>This·option·can·only·be·used·in·conjunction·with |
54 | » ··<code·class="option">-a</code>.</p></dd><dt><span·class="term"><code·class="option">-?</code>,·</span><span·class="term"><code·class="option">--help</code></span></dt><dd><p>Print·usage·information·on·standard·output·then·exit | 54 | » ··<code·class="option">-a</code>.</p></dd><dt><span·class="term"><code·class="option">-?</code>,·</span><span·class="term"><code·class="option">--help</code></span></dt><dd><p>Print·usage·information·on·standard·output·then·exit |
55 | » ··successfully.</p></dd><dt><span·class="term"><code·class="option">-V</code>,·</span><span·class="term"><code·class="option">--version</code></span></dt><dd><p>Print·version·information·on·standard·output·then·exit | 55 | » ··successfully.</p></dd><dt><span·class="term"><code·class="option">-V</code>,·</span><span·class="term"><code·class="option">--version</code></span></dt><dd><p>Print·version·information·on·standard·output·then·exit |
56 | » ··successfully.·Note·that·for·legacy·reasons·<code·class="option">-v</code> | 56 | » ··successfully.·Note·that·for·legacy·reasons·<code·class="option">-v</code> |
57 | » ··is·supported,·too,·but·the·use·of·it·is·deprecated. | 57 | » ··is·supported,·too,·but·the·use·of·it·is·deprecated. |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>1.3. License</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="happy-introduction.html"·title="Chapter 1. Introduction"><link·rel="prev"·href="sec-reporting-bugs.html"·title="1.2. Reporting·Bugs"><link·rel="next"·href="sec-obtaining.html"·title="1.4. Obtaining·Happy"></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">1.3. License</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-reporting-bugs.html">Prev</a> </td><th·width="60%"·align="center">Chapter 1. Introduction</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-obtaining.html">Next</a></td></tr></table><hr></div><div·class="sect1"·titl·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>1.3. License</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="happy-introduction.html"·title="Chapter 1. Introduction"><link·rel="prev"·href="sec-reporting-bugs.html"·title="1.2. Reporting·Bugs"><link·rel="next"·href="sec-obtaining.html"·title="1.4. Obtaining·Happy"></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">1.3. License</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-reporting-bugs.html">Prev</a> </td><th·width="60%"·align="center">Chapter 1. Introduction</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-obtaining.html">Next</a></td></tr></table><hr></div><div·class="sect1"·titl·✂ |
2 | ······were·covered·by·the·GNU·general·public·license.··We're·now | 2 | ······were·covered·by·the·GNU·general·public·license.··We're·now |
3 | ······distributing·<span·class="application">Happy</span>·with·a·less | 3 | ······distributing·<span·class="application">Happy</span>·with·a·less |
4 | ······restrictive·BSD-style·license.··If·this·license·doesn't·work·for | 4 | ······restrictive·BSD-style·license.··If·this·license·doesn't·work·for |
5 | ······you,·please·get·in·touch.</p><div·class="blockquote"><blockquote·class="blockquote"><p>·Copyright·2009,·Simon·Marlow·and·Andy·Gill.··All·rights | 5 | ······you,·please·get·in·touch.</p><div·class="blockquote"><blockquote·class="blockquote"><p>·Copyright·2009,·Simon·Marlow·and·Andy·Gill.··All·rights |
6 | » reserved.·</p><p>·Redistribution·and·use·in·source·and·binary·forms,·with | 6 | » reserved.·</p><p>·Redistribution·and·use·in·source·and·binary·forms,·with |
7 | » or·without·modification,·are·permitted·provided·that·the | 7 | » or·without·modification,·are·permitted·provided·that·the |
8 | » following·conditions·are·met:·</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>Redistributions·of·source·code·must·retain·the·above | 8 | » following·conditions·are·met:·</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>Redistributions·of·source·code·must·retain·the·above |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>6.2. Module·Header</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="prev"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="next"·href="sec-directives.html"·title="6.3. Directives"></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">6.2. Module·Header</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-grammar-files.html">Prev</a> </td><th·width="60%"·align="center">Chapter 6. Syntax·of·Grammar·Files</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-directives.html">Next</a></td></t·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>6.2. Module·Header</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="prev"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="next"·href="sec-directives.html"·title="6.3. Directives"></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">6.2. Module·Header</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-grammar-files.html">Prev</a> </td><th·width="60%"·align="center">Chapter 6. Syntax·of·Grammar·Files</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-directives.html">Next</a></td></t·✂ |
2 | ······following·form:</p><pre·class="programlisting"> | 2 | ······following·form:</p><pre·class="programlisting"> |
3 | { | 3 | { |
4 | <Haskell·module·header> | 4 | <Haskell·module·header> |
5 | } | 5 | } |
6 | </pre><p>The·Haskell·module·header·contains·the·module·name, | 6 | </pre><p>The·Haskell·module·header·contains·the·module·name, |
7 | ······exports,·and·imports.··No·other·code·is·allowed·in·the | 7 | ······exports,·and·imports.··No·other·code·is·allowed·in·the |
8 | ······header---this·is·because·<span·class="application">Happy</span>·may·need·to·include | 8 | ······header---this·is·because·<span·class="application">Happy</span>·may·need·to·include |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>6.5. Module·Trailer</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="prev"·href="sec-grammar.html"·title="6.4. Grammar"><link·rel="next"·href="sec-info-files.html"·title="Chapter 7. Info·Files"></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">6.5. Module·Trailer</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-grammar.html">Prev</a> </td><th·width="60%"·align="center">Chapter 6. Syntax·of·Grammar·Files</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-info-files.html">Next</a></td></tr></table><hr></div><div·c·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>6.5. Module·Trailer</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-grammar-files.html"·title="Chapter 6. Syntax·of·Grammar·Files"><link·rel="prev"·href="sec-grammar.html"·title="6.4. Grammar"><link·rel="next"·href="sec-info-files.html"·title="Chapter 7. Info·Files"></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">6.5. Module·Trailer</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-grammar.html">Prev</a> </td><th·width="60%"·align="center">Chapter 6. Syntax·of·Grammar·Files</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-info-files.html">Next</a></td></tr></table><hr></div><div·c·✂ |
2 | ······the·grammar·file,·and·takes·the·same·form·as·the·module | 2 | ······the·grammar·file,·and·takes·the·same·form·as·the·module |
3 | ······header:</p><pre·class="programlisting"> | 3 | ······header:</p><pre·class="programlisting"> |
4 | { | 4 | { |
5 | <Haskell·code> | 5 | <Haskell·code> |
6 | } | 6 | } |
7 | </pre><p>This·section·is·used·for·placing·auxiliary·definitions | 7 | </pre><p>This·section·is·used·for·placing·auxiliary·definitions |
8 | ······that·need·to·be·in·the·same·module·as·the·parser.··In·small | 8 | ······that·need·to·be·in·the·same·module·as·the·parser.··In·small |
Offset 1, 37 lines modified | Offset 1, 37 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.6. Basic·monadic·Happy·use·with·Alex</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-happy-ghci.html"·title="8.5. Using·Happy·with·GHCi"><link·rel="next"·href="ix01.html"·title="Index"></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">8.6. Basic·monadic·Happy·use·with·Alex</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-happy-ghci.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="ix01.html">Next</a></td></tr></table><hr></div><div·class="sect1"·title="8.6. ·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>8.6. Basic·monadic·Happy·use·with·Alex</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-tips.html"·title="Chapter 8. Tips"><link·rel="prev"·href="sec-happy-ghci.html"·title="8.5. Using·Happy·with·GHCi"><link·rel="next"·href="ix01.html"·title="Index"></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">8.6. Basic·monadic·Happy·use·with·Alex</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-happy-ghci.html">Prev</a> </td><th·width="60%"·align="center">Chapter 8. Tips</th><td·width="20%"·align="right"> <a·accesskey="n"·href="ix01.html">Next</a></td></tr></table><hr></div><div·class="sect1"·title="8.6. ·✂ |
2 | ········<span·class="application">Alex</span>·lexers·are·often·used·by | 2 | ········<span·class="application">Alex</span>·lexers·are·often·used·by |
3 | ········<span·class="application">Happy</span>·parsers,·for·example·in | 3 | ········<span·class="application">Happy</span>·parsers,·for·example·in |
4 | ········GHC.·While·many·of·these·applications·are·quite·sophisticated, | 4 | ········GHC.·While·many·of·these·applications·are·quite·sophisticated, |
5 | ········it·is·still·quite·useful·to·combine·the·basic | 5 | ········it·is·still·quite·useful·to·combine·the·basic |
6 | ········<span·class="application">Happy</span>·<code·class="literal">%monad</code> | 6 | ········<span·class="application">Happy</span>·<code·class="literal">%monad</code> |
7 | ········directive·with·the·<span·class="application">Alex</span> | 7 | ········directive·with·the·<span·class="application">Alex</span> |
8 | ········<code·class="literal">monad</code>·wrapper.·By·using·monads·for·both, | 8 | ········<code·class="literal">monad</code>·wrapper.·By·using·monads·for·both, |
9 | ········the·resulting·parser·and·lexer·can·handle·errors·far·more | 9 | ········the·resulting·parser·and·lexer·can·handle·errors·far·more |
10 | ········gracefully·than·by·throwing·an·exception. | 10 | ········gracefully·than·by·throwing·an·exception. |
11 | ······</p><p> | 11 | ······</p><p> |
12 | ········The·most·straightforward·way·to·use·a·monadic | 12 | ········The·most·straightforward·way·to·use·a·monadic |
13 | ········<span·class="application">Alex</span>·lexer·is·to·simply·use·the | 13 | ········<span·class="application">Alex</span>·lexer·is·to·simply·use·the |
14 | ········<code·class="literal">Alex</code>·monad·as·the | 14 | ········<code·class="literal">Alex</code>·monad·as·the |
15 | ········<span·class="application">Happy</span>·monad: | 15 | ········<span·class="application">Happy</span>·monad: |
16 | ······</p><div·class="example"><a·name="idp64 | 16 | ······</p><div·class="example"><a·name="idp64140344"></a><p·class="title"><b>Example 8.1. Lexer.x</b></p><div·class="example-contents"><pre·class="programlisting">{ |
17 | module·Lexer·where | 17 | module·Lexer·where |
18 | } | 18 | } |
19 | %wrapper·"monad" | 19 | %wrapper·"monad" |
20 | tokens·:- | 20 | tokens·:- |
21 | ··... | 21 | ··... |
22 | { | 22 | { |
23 | data·Token·=·...·|·EOF | 23 | data·Token·=·...·|·EOF |
24 | ··deriving·(Eq,·Show) | 24 | ··deriving·(Eq,·Show) |
25 | alexEOF·=·return·EOF | 25 | alexEOF·=·return·EOF |
26 | }</pre></div></div><br·class="example-break"><div·class="example"><a·name="idp64 | 26 | }</pre></div></div><br·class="example-break"><div·class="example"><a·name="idp64141368"></a><p·class="title"><b>Example 8.2. Parser.y</b></p><div·class="example-contents"><pre·class="programlisting">{ |
27 | module·Parser·where | 27 | module·Parser·where |
28 | import·Lexer | 28 | import·Lexer |
29 | } | 29 | } |
30 | %name·pFoo | 30 | %name·pFoo |
31 | %tokentype·{·Token·} | 31 | %tokentype·{·Token·} |
Offset 1, 17 lines modified | Offset 1, 17 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.5. Monadic·Parsers</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-type-signatures.html"·title="2.4. Type·Signatures"><link·rel="next"·href="sec-error.html"·title="2.6. The·Error·Token"></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">2.5. Monadic·Parsers</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-type-signatures.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-error.html">Next</a></td></tr></table>·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.5. Monadic·Parsers</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-type-signatures.html"·title="2.4. Type·Signatures"><link·rel="next"·href="sec-error.html"·title="2.6. The·Error·Token"></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">2.5. Monadic·Parsers</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-type-signatures.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-error.html">Next</a></td></tr></table>·✂ |
2 | ······through·the·generated·parser.··This·might·be·useful·for·several | 2 | ······through·the·generated·parser.··This·might·be·useful·for·several |
3 | ······reasons:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><a·class="indexterm"·name="idp633 | 3 | ······reasons:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><a·class="indexterm"·name="idp63398968"></a><p>·Handling·parse·errors·by·using·an·exception·monad |
4 | ··········(see·<a·class="xref"·href="sec-monads.html#sec-exception"·title="2.5.1. Handling·Parse·Errors">Section 2.5.1,·“Handling·Parse·Errors”</a>).</p></li><li·class="listitem"><a·class="indexterm"·name="idp63 | 4 | ··········(see·<a·class="xref"·href="sec-monads.html#sec-exception"·title="2.5.1. Handling·Parse·Errors">Section 2.5.1,·“Handling·Parse·Errors”</a>).</p></li><li·class="listitem"><a·class="indexterm"·name="idp63401784"></a><p>·Keeping·track·of·line·numbers·in·the·input·file,·for |
5 | ··········example·for·use·in·error·messages·(see·<a·class="xref"·href="sec-monads.html#sec-line-numbers"·title="2.5.3. Line·Numbers">Section 2.5.3,·“Line·Numbers”</a>).</p></li><li·class="listitem"><p>·Performing·IO·operations·during·parsing.</p></li><li·class="listitem"><p>·Parsing·languages·with·context-dependencies·(such·as | 5 | ··········example·for·use·in·error·messages·(see·<a·class="xref"·href="sec-monads.html#sec-line-numbers"·title="2.5.3. Line·Numbers">Section 2.5.3,·“Line·Numbers”</a>).</p></li><li·class="listitem"><p>·Performing·IO·operations·during·parsing.</p></li><li·class="listitem"><p>·Parsing·languages·with·context-dependencies·(such·as |
6 | ··········C)·require·some·state·in·the·parser.</p></li></ul></div><p>Adding·monadic·support·to·your·parser·couldn't·be·simpler. | 6 | ··········C)·require·some·state·in·the·parser.</p></li></ul></div><p>Adding·monadic·support·to·your·parser·couldn't·be·simpler. |
7 | ······Just·add·the·following·directive·to·the·declaration·section·of | 7 | ······Just·add·the·following·directive·to·the·declaration·section·of |
8 | ······the·grammar·file:</p><pre·class="programlisting"> | 8 | ······the·grammar·file:</p><pre·class="programlisting"> |
9 | %monad·{·<type>·}·[·{·<then>·}·{·<return>·}·] | 9 | %monad·{·<type>·}·[·{·<then>·}·{·<return>·}·] |
10 | </pre><a·class="indexterm"·name="idp6 | 10 | </pre><a·class="indexterm"·name="idp63406136"></a><p>where·<code·class="literal"><type></code>·is·the·type·constructor·for |
11 | ······the·monad,·<code·class="literal"><then></code>·is·the·bind·operation·of·the | 11 | ······the·monad,·<code·class="literal"><then></code>·is·the·bind·operation·of·the |
12 | ······monad,·and·<code·class="literal"><return></code>·is·the·return·operation.·If | 12 | ······monad,·and·<code·class="literal"><return></code>·is·the·return·operation.·If |
13 | ······you·leave·out·the·names·for·the·bind·and·return·operations, | 13 | ······you·leave·out·the·names·for·the·bind·and·return·operations, |
14 | ······<span·class="application">Happy</span>·assumes·that·<code·class="literal"><type></code>·is·an | 14 | ······<span·class="application">Happy</span>·assumes·that·<code·class="literal"><type></code>·is·an |
15 | ······instance·of·the·standard·Haskell·type·class·<code·class="literal">Monad</code>·and | 15 | ······instance·of·the·standard·Haskell·type·class·<code·class="literal">Monad</code>·and |
16 | ······uses·the·overloaded·names·for·the·bind·and·return | 16 | ······uses·the·overloaded·names·for·the·bind·and·return |
17 | ······operations.</p><p>When·this·declaration·is·included·in·the·grammar, | 17 | ······operations.</p><p>When·this·declaration·is·included·in·the·grammar, |
Offset 30, 15 lines modified | Offset 30, 15 lines modified | ||
30 | ······specify·that·the·productions·can·also·have·type·<code·class="literal">P·a</code>? | 30 | ······specify·that·the·productions·can·also·have·type·<code·class="literal">P·a</code>? |
31 | ······Most·of·the·time,·you·don't·want·a·production·to·have·this·type: | 31 | ······Most·of·the·time,·you·don't·want·a·production·to·have·this·type: |
32 | ······you'd·have·to·write·explicit·<code·class="literal">returnP</code>s·everywhere. | 32 | ······you'd·have·to·write·explicit·<code·class="literal">returnP</code>s·everywhere. |
33 | ······However,·there·may·be·a·few·rules·in·a·grammar·that·need·to·get | 33 | ······However,·there·may·be·a·few·rules·in·a·grammar·that·need·to·get |
34 | ······at·the·monad,·so·<span·class="application">Happy</span>·has·a·special·syntax·for | 34 | ······at·the·monad,·so·<span·class="application">Happy</span>·has·a·special·syntax·for |
35 | ······monadic·actions:</p><pre·class="programlisting"> | 35 | ······monadic·actions:</p><pre·class="programlisting"> |
36 | n··:··t_1·...·t_n··········{%·<expr>·} | 36 | n··:··t_1·...·t_n··········{%·<expr>·} |
37 | </pre><a·class="indexterm"·name="idp63 | 37 | </pre><a·class="indexterm"·name="idp63451448"></a><p>The·<code·class="literal">%</code>·in·the·action·indicates·that·this·is·a |
38 | ······monadic·action,·with·type·<code·class="literal">P·a</code>,·where·<code·class="literal">a</code>·is | 38 | ······monadic·action,·with·type·<code·class="literal">P·a</code>,·where·<code·class="literal">a</code>·is |
39 | ······the·real·return·type·of·the·production.··When | 39 | ······the·real·return·type·of·the·production.··When |
40 | ······<span·class="application">Happy</span>·reduces·one·of·these·rules,·it·evaluates·the | 40 | ······<span·class="application">Happy</span>·reduces·one·of·these·rules,·it·evaluates·the |
41 | ······expression·</p><pre·class="programlisting"> | 41 | ······expression·</p><pre·class="programlisting"> |
42 | <expr>·`then`·\result·->·<continue·parsing> | 42 | <expr>·`then`·\result·->·<continue·parsing> |
43 | </pre><p><span·class="application">Happy</span>·uses·<code·class="literal">result</code>·as·the·real | 43 | </pre><p><span·class="application">Happy</span>·uses·<code·class="literal">result</code>·as·the·real |
44 | ······semantic·value·of·the·production.··During·parsing,·several | 44 | ······semantic·value·of·the·production.··During·parsing,·several |
Offset 55, 15 lines modified | Offset 55, 15 lines modified | ||
55 | ······doesn't·impose·any·performance·overhead·for·normal·reductions·- | 55 | ······doesn't·impose·any·performance·overhead·for·normal·reductions·- |
56 | ······only·the·monadic·ones·are·translated·like·this.</p><p>Take·a·look·at·the·Haskell·parser·for·a·good·illustration | 56 | ······only·the·monadic·ones·are·translated·like·this.</p><p>Take·a·look·at·the·Haskell·parser·for·a·good·illustration |
57 | ······of·how·to·use·a·monad·in·your·parser:·it·contains·examples·of | 57 | ······of·how·to·use·a·monad·in·your·parser:·it·contains·examples·of |
58 | ······all·the·principles·discussed·in·this·section,·namely·parse | 58 | ······all·the·principles·discussed·in·this·section,·namely·parse |
59 | ······errors,·a·threaded·lexer,·line/column·numbers,·and·state | 59 | ······errors,·a·threaded·lexer,·line/column·numbers,·and·state |
60 | ······communication·between·the·parser·and·lexer.</p><p>The·following·sections·consider·a·couple·of·uses·for | 60 | ······communication·between·the·parser·and·lexer.</p><p>The·following·sections·consider·a·couple·of·uses·for |
61 | ······monadic·parsers,·and·describe·how·to·also·thread·the·monad | 61 | ······monadic·parsers,·and·describe·how·to·also·thread·the·monad |
62 | ······through·the·lexical·analyser.</p><div·class="sect2"·title="2.5.1. Handling·Parse·Errors"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-exception"></a>2.5.1. Handling·Parse·Errors</h3></div></div></div><a·class="indexterm"·name="idp63 | 62 | ······through·the·lexical·analyser.</p><div·class="sect2"·title="2.5.1. Handling·Parse·Errors"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-exception"></a>2.5.1. Handling·Parse·Errors</h3></div></div></div><a·class="indexterm"·name="idp63459768"></a><p>It's·not·very·convenient·to·just·call·<code·class="literal">error</code>·when |
63 | ······a·parse·error·is·detected:·in·a·robust·setting,·you'd·like·the | 63 | ······a·parse·error·is·detected:·in·a·robust·setting,·you'd·like·the |
64 | ······program·to·recover·gracefully·and·report·a·useful·error·message | 64 | ······program·to·recover·gracefully·and·report·a·useful·error·message |
65 | ······to·the·user.··Exceptions·(of·which·errors·are·a·special·case) | 65 | ······to·the·user.··Exceptions·(of·which·errors·are·a·special·case) |
66 | ······are·normally·implemented·in·Haskell·by·using·an·exception·monad, | 66 | ······are·normally·implemented·in·Haskell·by·using·an·exception·monad, |
67 | ······something·like:</p><pre·class="programlisting"> | 67 | ······something·like:</p><pre·class="programlisting"> |
68 | data·E·a·=·Ok·a·|·Failed·String | 68 | data·E·a·=·Ok·a·|·Failed·String |
Offset 100, 28 lines modified | Offset 100, 28 lines modified | ||
100 | ········language·than·that·which·is·actually·intended,·and·check·it | 100 | ········language·than·that·which·is·actually·intended,·and·check·it |
101 | ········later.··An·example·comes·from·Haskell,·where·the·precedence | 101 | ········later.··An·example·comes·from·Haskell,·where·the·precedence |
102 | ········values·in·infix·declarations·must·be·between·0·and·9:</p><pre·class="programlisting">prec·::·{·Int·} | 102 | ········values·in·infix·declarations·must·be·between·0·and·9:</p><pre·class="programlisting">prec·::·{·Int·} |
103 | ······:·int····{%·if·$1·<·0·||·$1·>·9 | 103 | ······:·int····{%·if·$1·<·0·||·$1·>·9 |
104 | » ················then·failE·"Precedence·out·of·range" | 104 | » ················then·failE·"Precedence·out·of·range" |
105 | » » ········else·returnE·$1 | 105 | » » ········else·returnE·$1 |
106 | » » }</pre><p>The·monadic·action·allows·the·check·to·be·placed·in·the | 106 | » » }</pre><p>The·monadic·action·allows·the·check·to·be·placed·in·the |
107 | » parser·itself,·where·it·belongs.</p></div><div·class="sect2"·title="2.5.2. Threaded·Lexers"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-lexers"></a>2.5.2. Threaded·Lexers</h3></div></div></div><a·class="indexterm"·name="idp63 | 107 | » parser·itself,·where·it·belongs.</p></div><div·class="sect2"·title="2.5.2. Threaded·Lexers"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-lexers"></a>2.5.2. Threaded·Lexers</h3></div></div></div><a·class="indexterm"·name="idp63469752"></a><a·class="indexterm"·name="idp63470648"></a><p><span·class="application">Happy</span>·allows·the·monad·concept·to·be |
108 | » extended·to·the·lexical·analyser,·too.··This·has·several | 108 | » extended·to·the·lexical·analyser,·too.··This·has·several |
109 | » useful·consequences:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>·Lexical·errors·can·be·treated·in·the·same·way·as | 109 | » useful·consequences:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>·Lexical·errors·can·be·treated·in·the·same·way·as |
110 | ············parse·errors,·using·an·exception·monad.</p><a·class="indexterm"·name="idp63 | 110 | ············parse·errors,·using·an·exception·monad.</p><a·class="indexterm"·name="idp63473592"></a></li><li·class="listitem"><p>·Information·such·as·the·current·file·and·line |
111 | ············number·can·be·communicated·between·the·lexer·and | 111 | ············number·can·be·communicated·between·the·lexer·and |
112 | ············parser.·</p></li><li·class="listitem"><p>·General·state·communication·between·the·parser·and | 112 | ············parser.·</p></li><li·class="listitem"><p>·General·state·communication·between·the·parser·and |
113 | ············lexer·-·for·example,·implementation·of·the·Haskell·layout | 113 | ············lexer·-·for·example,·implementation·of·the·Haskell·layout |
114 | ············rule·requires·this·kind·of·interaction. | 114 | ············rule·requires·this·kind·of·interaction. |
115 | ············</p></li><li·class="listitem"><p>·IO·operations·can·be·performed·in·the·lexer·-·this | 115 | ············</p></li><li·class="listitem"><p>·IO·operations·can·be·performed·in·the·lexer·-·this |
116 | ············could·be·useful·for·following·import/include·declarations | 116 | ············could·be·useful·for·following·import/include·declarations |
117 | ············for·instance.</p></li></ul></div><p>A·monadic·lexer·is·requested·by·adding·the·following | 117 | ············for·instance.</p></li></ul></div><p>A·monadic·lexer·is·requested·by·adding·the·following |
118 | » declaration·to·the·grammar·file:</p><pre·class="programlisting"> | 118 | » declaration·to·the·grammar·file:</p><pre·class="programlisting"> |
119 | %lexer·{·<lexer>·}·{·<eof>·} | 119 | %lexer·{·<lexer>·}·{·<eof>·} |
120 | </pre><a·class="indexterm"·name="idp63 | 120 | </pre><a·class="indexterm"·name="idp63478328"></a><p>where·<code·class="literal"><lexer></code>·is·the·name·of·the·lexical |
121 | ········analyser·function,·and·<code·class="literal"><eof></code>·is·a·token·that | 121 | ········analyser·function,·and·<code·class="literal"><eof></code>·is·a·token·that |
122 | ········is·to·be·treated·as·the·end·of·file.</p><p>When·using·a·monadic·lexer,·the·parser·no·longer·reads·a | 122 | ········is·to·be·treated·as·the·end·of·file.</p><p>When·using·a·monadic·lexer,·the·parser·no·longer·reads·a |
123 | ········list·of·tokens.··Instead,·it·calls·the·lexical·analysis | 123 | ········list·of·tokens.··Instead,·it·calls·the·lexical·analysis |
124 | ········function·for·each·new·token·to·be·read.··This·has·the·side | 124 | ········function·for·each·new·token·to·be·read.··This·has·the·side |
125 | ········effect·of·eliminating·the·intermediate·list·of·tokens,·which | 125 | ········effect·of·eliminating·the·intermediate·list·of·tokens,·which |
126 | ········is·a·slight·performance·win.</p><p>The·type·of·the·main·parser·function·is·now·just | 126 | ········is·a·slight·performance·win.</p><p>The·type·of·the·main·parser·function·is·now·just |
127 | ········<code·class="literal">P·a</code>·-·the·input·is·being·handled·completely | 127 | ········<code·class="literal">P·a</code>·-·the·input·is·being·handled·completely |
Offset 185, 30 lines modified | Offset 185, 30 lines modified | ||
185 | ········still·holds·(or·indeed·ever·held),·and·it's·entirely·possible | 185 | ········still·holds·(or·indeed·ever·held),·and·it's·entirely·possible |
186 | ········that·the·use·of·a·continuation·here·is·just·a | 186 | ········that·the·use·of·a·continuation·here·is·just·a |
187 | ········misfeature.</p><p>If·you·want·a·lexer·of·type·<code·class="literal">P·Token</code>, | 187 | ········misfeature.</p><p>If·you·want·a·lexer·of·type·<code·class="literal">P·Token</code>, |
188 | ········then·just·define·a·wrapper·to·deal·with·the | 188 | ········then·just·define·a·wrapper·to·deal·with·the |
189 | ········continuation:</p><pre·class="programlisting"> | 189 | ········continuation:</p><pre·class="programlisting"> |
190 | lexwrap·::·(Token·->·P·a)·->·P·a | 190 | lexwrap·::·(Token·->·P·a)·->·P·a |
191 | lexwrap·cont·=·real_lexer·`thenP`·\token·->·cont·token | 191 | lexwrap·cont·=·real_lexer·`thenP`·\token·->·cont·token |
192 | </pre><div·class="sect3"·title="2.5.2.1. Monadic·productions·with·%lexer"><div·class="titlepage"><div><div><h4·class="title"><a·name="idp63 | 192 | </pre><div·class="sect3"·title="2.5.2.1. Monadic·productions·with·%lexer"><div·class="titlepage"><div><div><h4·class="title"><a·name="idp63494072"></a>2.5.2.1. Monadic·productions·with·%lexer</h4></div></div></div><p>The·<code·class="literal">{%·...·}</code>·actions·work·fine·with |
193 | » <code·class="literal">%lexer</code>,·but·additionally·there·are·two·more | 193 | » <code·class="literal">%lexer</code>,·but·additionally·there·are·two·more |
194 | » forms·which·are·useful·in·certain·cases.··Firstly:</p><pre·class="programlisting"> | 194 | » forms·which·are·useful·in·certain·cases.··Firstly:</p><pre·class="programlisting"> |
195 | n··:··t_1·...·t_n··········{%^·<expr>·} | 195 | n··:··t_1·...·t_n··········{%^·<expr>·} |
196 | </pre><p>In·this·case,·<code·class="literal"><expr></code>·has·type | 196 | </pre><p>In·this·case,·<code·class="literal"><expr></code>·has·type |
197 | » <code·class="literal">Token·->·P·a</code>.··That·is,·Happy·passes·the | 197 | » <code·class="literal">Token·->·P·a</code>.··That·is,·Happy·passes·the |
198 | » current·lookahead·token·to·the·monadic·action | 198 | » current·lookahead·token·to·the·monadic·action |
199 | » <code·class="literal"><expr></code>.··This·is·a·useful·way·to·get | 199 | » <code·class="literal"><expr></code>.··This·is·a·useful·way·to·get |
200 | » hold·of·Happy's·current·lookahead·token·without·having·to | 200 | » hold·of·Happy's·current·lookahead·token·without·having·to |
201 | » store·it·in·the·monad.</p><pre·class="programlisting"> | 201 | » store·it·in·the·monad.</p><pre·class="programlisting"> |
202 | n··:··t_1·...·t_n··········{%%·<expr>·} | 202 | n··:··t_1·...·t_n··········{%%·<expr>·} |
203 | </pre><p>This·is·a·slight·variant·on·the·previous·form.··The·type | 203 | </pre><p>This·is·a·slight·variant·on·the·previous·form.··The·type |
204 | » of·<code·class="literal"><expr></code>·is·the·same,·but·in·this | 204 | » of·<code·class="literal"><expr></code>·is·the·same,·but·in·this |
205 | » case·the·lookahead·token·is·actually·discarded·and·a·new·token | 205 | » case·the·lookahead·token·is·actually·discarded·and·a·new·token |
206 | » is·read·from·the·input.··This·can·be·useful·when·you·want·to | 206 | » is·read·from·the·input.··This·can·be·useful·when·you·want·to |
207 | » change·the·next·token·and·continue·parsing.</p></div></div><div·class="sect2"·title="2.5.3. Line·Numbers"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-line-numbers"></a>2.5.3. Line·Numbers</h3></div></div></div><a·class="indexterm"·name="idp63 | 207 | » change·the·next·token·and·continue·parsing.</p></div></div><div·class="sect2"·title="2.5.3. Line·Numbers"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-line-numbers"></a>2.5.3. Line·Numbers</h3></div></div></div><a·class="indexterm"·name="idp63501112"></a><a·class="indexterm"·name="idp63502008"></a><p>Previous·versions·of·<span·class="application">Happy</span>·had·a |
208 | ········<code·class="literal">%newline</code>·directive·that·enabled·simple·line·numbers | 208 | ········<code·class="literal">%newline</code>·directive·that·enabled·simple·line·numbers |
209 | ········to·be·counted·by·the·parser·and·referenced·in·the·actions.··We | 209 | ········to·be·counted·by·the·parser·and·referenced·in·the·actions.··We |
210 | ········warned·you·that·this·facility·may·go·away·and·be·replaced·by | 210 | ········warned·you·that·this·facility·may·go·away·and·be·replaced·by |
211 | ········something·more·general,·well·guess·what?·:-)</p><p>Line·numbers·can·now·be·dealt·with·quite | 211 | ········something·more·general,·well·guess·what?·:-)</p><p>Line·numbers·can·now·be·dealt·with·quite |
212 | ········straightforwardly·using·a·monadic·parser/lexer·combination. | 212 | ········straightforwardly·using·a·monadic·parser/lexer·combination. |
213 | ········Ok,·we·have·to·extend·the·monad·a·bit·more:</p><pre·class="programlisting"> | 213 | ········Ok,·we·have·to·extend·the·monad·a·bit·more:</p><pre·class="programlisting"> |
214 | type·LineNumber·=·Int | 214 | type·LineNumber·=·Int |
Offset 244, 15 lines modified | Offset 244, 15 lines modified | ||
244 | ········since·<span·class="application">Happy</span>·always·keeps·one·lookahead·token·in | 244 | ········since·<span·class="application">Happy</span>·always·keeps·one·lookahead·token·in |
245 | ········reserve.</p></div><div·class="sect2"·title="2.5.4. Summary"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-monad-summary"></a>2.5.4. Summary</h3></div></div></div><p>The·types·of·various·functions·related·to·the·parser·are | 245 | ········reserve.</p></div><div·class="sect2"·title="2.5.4. Summary"><div·class="titlepage"><div><div><h3·class="title"><a·name="sec-monad-summary"></a>2.5.4. Summary</h3></div></div></div><p>The·types·of·various·functions·related·to·the·parser·are |
246 | ········dependent·on·what·combination·of·<code·class="literal">%monad</code>·and | 246 | ········dependent·on·what·combination·of·<code·class="literal">%monad</code>·and |
247 | ········<code·class="literal">%lexer</code>·directives·are·present·in·the·grammar.··For | 247 | ········<code·class="literal">%lexer</code>·directives·are·present·in·the·grammar.··For |
248 | ········reference,·we·list·those·types·here.··In·the·following·types, | 248 | ········reference,·we·list·those·types·here.··In·the·following·types, |
249 | ········<span·class="emphasis"><em>t</em></span>·is·the·return·type·of·the | 249 | ········<span·class="emphasis"><em>t</em></span>·is·the·return·type·of·the |
250 | ········parser.··A·type·containing·a·type·variable·indicates·that·the | 250 | ········parser.··A·type·containing·a·type·variable·indicates·that·the |
251 | ········specified·function·must·be·polymorphic.</p><a·class="indexterm"·name="idp63 | 251 | ········specified·function·must·be·polymorphic.</p><a·class="indexterm"·name="idp63527480"></a><a·class="indexterm"·name="idp63533240"></a><a·class="indexterm"·name="idp63534520"></a><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p·title="No·%monad·or·%lexer"><b>·No·<code·class="literal">%monad</code>·or |
252 | » ······<code·class="literal">%lexer</code>·. </b> | 252 | » ······<code·class="literal">%lexer</code>·. </b> |
253 | </p><pre·class="programlisting"> | 253 | </p><pre·class="programlisting"> |
254 | parse······::·[Token]·->·<span·class="emphasis"><em>t</em></span> | 254 | parse······::·[Token]·->·<span·class="emphasis"><em>t</em></span> |
255 | parseError·::·[Token]·->·a | 255 | parseError·::·[Token]·->·a |
256 | </pre><p·title="No·%monad·or·%lexer"> | 256 | </pre><p·title="No·%monad·or·%lexer"> |
257 | </p></li><li·class="listitem"><p·title="with·%monad"><b>·with·<code·class="literal">%monad</code>·. </b> | 257 | </p></li><li·class="listitem"><p·title="with·%monad"><b>·with·<code·class="literal">%monad</code>·. </b> |
258 | </p><pre·class="programlisting"> | 258 | </p><pre·class="programlisting"> |
Offset 1, 20 lines modified | Offset 1, 20 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.7. Generating·Multiple·Parsers·From·a·Single·Grammar</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-error.html"·title="2.6. The·Error·Token"><link·rel="next"·href="sec-glr.html"·title="Chapter 3. Generalized·LR·Parsing"></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">2.7. Generating·Multiple·Parsers·From·a·Single·Grammar</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-error.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·acc·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.7. Generating·Multiple·Parsers·From·a·Single·Grammar</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-error.html"·title="2.6. The·Error·Token"><link·rel="next"·href="sec-glr.html"·title="Chapter 3. Generalized·LR·Parsing"></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">2.7. Generating·Multiple·Parsers·From·a·Single·Grammar</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-error.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·acc·✂ |
2 | ······multiple·parsers,·where·each·parser·has·a·different·top-level | 2 | ······multiple·parsers,·where·each·parser·has·a·different·top-level |
3 | ······non-terminal,·but·parts·of·the·grammar·are·shared·between | 3 | ······non-terminal,·but·parts·of·the·grammar·are·shared·between |
4 | ······parsers.··A·classic·example·of·this·is·an·interpreter,·which | 4 | ······parsers.··A·classic·example·of·this·is·an·interpreter,·which |
5 | ······needs·to·be·able·to·parse·both·entire·files·and·single | 5 | ······needs·to·be·able·to·parse·both·entire·files·and·single |
6 | ······expressions:·the·expression·grammar·is·likely·to·be·identical | 6 | ······expressions:·the·expression·grammar·is·likely·to·be·identical |
7 | ······for·the·two·parsers,·so·we·would·like·to·use·a·single·grammar | 7 | ······for·the·two·parsers,·so·we·would·like·to·use·a·single·grammar |
8 | ······but·have·two·entry·points.</p><p><span·class="application">Happy</span>·lets·you·do·this·by | 8 | ······but·have·two·entry·points.</p><p><span·class="application">Happy</span>·lets·you·do·this·by |
9 | ······allowing·multiple·<code·class="literal">%name</code>·directives·in·the | 9 | ······allowing·multiple·<code·class="literal">%name</code>·directives·in·the |
10 | ······grammar·file.··The·<code·class="literal">%name</code>·directive·takes·an | 10 | ······grammar·file.··The·<code·class="literal">%name</code>·directive·takes·an |
11 | ······optional·second·parameter·specifying·the·top-level | 11 | ······optional·second·parameter·specifying·the·top-level |
12 | ······non-terminal·for·this·parser,·so·we·may·specify·multiple·parsers | 12 | ······non-terminal·for·this·parser,·so·we·may·specify·multiple·parsers |
13 | ······like·so:</p><a·class="indexterm"·name="idp635 | 13 | ······like·so:</p><a·class="indexterm"·name="idp63571768"></a><pre·class="programlisting"> |
14 | %name·parse1·non-terminal1 | 14 | %name·parse1·non-terminal1 |
15 | %name·parse2·non-terminal2 | 15 | %name·parse2·non-terminal2 |
16 | </pre><p><span·class="application">Happy</span>·will·generate·from·this·a | 16 | </pre><p><span·class="application">Happy</span>·will·generate·from·this·a |
17 | ······module·which·defines·two·functions·<code·class="function">parse1</code> | 17 | ······module·which·defines·two·functions·<code·class="function">parse1</code> |
18 | ······and·<code·class="function">parse2</code>,·which·parse·the·grammars·given | 18 | ······and·<code·class="function">parse2</code>,·which·parse·the·grammars·given |
19 | ······by·<code·class="literal">non-terminal1</code>·and | 19 | ······by·<code·class="literal">non-terminal1</code>·and |
20 | ······<code·class="literal">non-terminal2</code>·respectively.··Each·parsing | 20 | ······<code·class="literal">non-terminal2</code>·respectively.··Each·parsing |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>1.2. Reporting·Bugs</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="happy-introduction.html"·title="Chapter 1. Introduction"><link·rel="prev"·href="happy-introduction.html"·title="Chapter 1. Introduction"><link·rel="next"·href="sec-license.html"·title="1.3. License"></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">1.2. Reporting·Bugs</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="happy-introduction.html">Prev</a> </td><th·width="60%"·align="center">Chapter 1. Introduction</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-license.html">Next</a></td></tr></table><hr></div><div·class="sect1·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>1.2. Reporting·Bugs</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="happy-introduction.html"·title="Chapter 1. Introduction"><link·rel="prev"·href="happy-introduction.html"·title="Chapter 1. Introduction"><link·rel="next"·href="sec-license.html"·title="1.3. License"></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">1.2. Reporting·Bugs</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="happy-introduction.html">Prev</a> </td><th·width="60%"·align="center">Chapter 1. Introduction</th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-license.html">Next</a></td></tr></table><hr></div><div·class="sect1·✂ |
2 | ······be·reported·to·me:·Simon·Marlow | 2 | ······be·reported·to·me:·Simon·Marlow |
3 | ······<code·class="email"><<a·class="email"·href="mailto:marlowsd@gmail.com">marlowsd@gmail.com</a>></code>·including·all·the·relevant | 3 | ······<code·class="email"><<a·class="email"·href="mailto:marlowsd@gmail.com">marlowsd@gmail.com</a>></code>·including·all·the·relevant |
4 | ······information:·the·compiler·used·to·compile | 4 | ······information:·the·compiler·used·to·compile |
5 | ······<span·class="application">Happy</span>,·the·command-line·options·used, | 5 | ······<span·class="application">Happy</span>,·the·command-line·options·used, |
6 | ······your·grammar·file·or·preferably·a·cut-down·example·showing·the | 6 | ······your·grammar·file·or·preferably·a·cut-down·example·showing·the |
7 | ······problem,·and·a·description·of·what·goes·wrong.··A·patch·to·fix | 7 | ······problem,·and·a·description·of·what·goes·wrong.··A·patch·to·fix |
8 | ······the·problem·would·also·be·greatly·appreciated.·</p><p>·Requests·for·new·features·should·also·be·sent·to·the | 8 | ······the·problem·would·also·be·greatly·appreciated.·</p><p>·Requests·for·new·features·should·also·be·sent·to·the |
Offset 9, 15 lines modified | Offset 9, 15 lines modified | ||
9 | prods·:·prod···················{·[$1]·} | 9 | prods·:·prod···················{·[$1]·} |
10 | ······|·prods·prod·············{·$2·:·$1·} | 10 | ······|·prods·prod·············{·$2·:·$1·} |
11 | </pre><p>In·other·words,·a·sequence·of·productions·is·either·a | 11 | </pre><p>In·other·words,·a·sequence·of·productions·is·either·a |
12 | ······single·production,·or·a·sequence·of·productions·followed·by·a | 12 | ······single·production,·or·a·sequence·of·productions·followed·by·a |
13 | ······single·production.··This·recursive·rule·defines·a·sequence·of | 13 | ······single·production.··This·recursive·rule·defines·a·sequence·of |
14 | ······one·or·more·productions.</p><p>One·thing·to·note·about·this·rule·is·that·we·used | 14 | ······one·or·more·productions.</p><p>One·thing·to·note·about·this·rule·is·that·we·used |
15 | ······<span·class="emphasis"><em>left·recursion</em></span>·to·define·it·-·we·could·have·written | 15 | ······<span·class="emphasis"><em>left·recursion</em></span>·to·define·it·-·we·could·have·written |
16 | ······it·like·this:</p><a·class="indexterm"·name="idp63 | 16 | ······it·like·this:</p><a·class="indexterm"·name="idp63317048"></a><pre·class="programlisting"> |
17 | prods·:·prod··················{·[$1]·} | 17 | prods·:·prod··················{·[$1]·} |
18 | ······|·prod·prods············{·$1·:·$2·} | 18 | ······|·prod·prods············{·$1·:·$2·} |
19 | </pre><p>The·only·reason·we·used·left·recursion·is·that | 19 | </pre><p>The·only·reason·we·used·left·recursion·is·that |
20 | ······<span·class="application">Happy</span>·is·more·efficient·at·parsing·left-recursive | 20 | ······<span·class="application">Happy</span>·is·more·efficient·at·parsing·left-recursive |
21 | ······rules;·they·result·in·a·constant·stack-space·parser,·whereas | 21 | ······rules;·they·result·in·a·constant·stack-space·parser,·whereas |
22 | ······right-recursive·rules·require·stack·space·proportional·to·the | 22 | ······right-recursive·rules·require·stack·space·proportional·to·the |
23 | ······length·of·the·list·being·parsed.··This·can·be·extremely | 23 | ······length·of·the·list·being·parsed.··This·can·be·extremely |
Offset 1, 18 lines modified | Offset 1, 18 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Chapter 8. Tips</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·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"·title="Chapter 8. Tips">·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Chapter 8. Tips</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·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"·title="Chapter 8. Tips">·✂ |
2 | ····<span·class="application">Happy</span>.</p><div·class="sect1"·title="8.1. Performance·Tips"><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·class="itemizedlist"·type="disc"><li·class="listitem"><a·class="indexterm"·name="idp6 | 2 | ····<span·class="application">Happy</span>.</p><div·class="sect1"·title="8.1. Performance·Tips"><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·class="itemizedlist"·type="disc"><li·class="listitem"><a·class="indexterm"·name="idp64042680"></a><p>·If·you·are·using·GHC,·generate·parsers·using·the |
3 | ··········<code·class="literal">-a·-g·-c</code>·options,·and·compile·them·using·GHC·with | 3 | ··········<code·class="literal">-a·-g·-c</code>·options,·and·compile·them·using·GHC·with |
4 | ··········the·<code·class="literal">-fglasgow-exts</code>·option.··This·is·worth·a | 4 | ··········the·<code·class="literal">-fglasgow-exts</code>·option.··This·is·worth·a |
5 | ··········<span·class="emphasis"><em>lot</em></span>,·in·terms·of·compile-time, | 5 | ··········<span·class="emphasis"><em>lot</em></span>,·in·terms·of·compile-time, |
6 | ··········execution·speed·and·binary·size·<sup>[<a·name="idp6 | 6 | ··········execution·speed·and·binary·size·<sup>[<a·name="idp64049336"·href="#ftn.idp64049336"·class="footnote">4</a>]</sup>.</p></li><li·class="listitem"><p>·The·lexical·analyser·is·usually·the·most·performance |
7 | ··········critical·part·of·a·parser,·so·it's·worth·spending·some·time | 7 | ··········critical·part·of·a·parser,·so·it's·worth·spending·some·time |
8 | ··········optimising·this.··Profiling·tools·are·essential·here.··In | 8 | ··········optimising·this.··Profiling·tools·are·essential·here.··In |
9 | ··········really·dire·circumstances,·resort·to·some·of·the·hacks·that | 9 | ··········really·dire·circumstances,·resort·to·some·of·the·hacks·that |
10 | ··········are·used·in·the·Glasgow·Haskell·Compiler's·interface-file | 10 | ··········are·used·in·the·Glasgow·Haskell·Compiler's·interface-file |
11 | ··········lexer.</p></li><li·class="listitem"><p>·Simplify·the·grammar·as·much·as·possible,·as·this | 11 | ··········lexer.</p></li><li·class="listitem"><p>·Simplify·the·grammar·as·much·as·possible,·as·this |
12 | ··········reduces·the·number·of·states·and·reduction·rules·that·need | 12 | ··········reduces·the·number·of·states·and·reduction·rules·that·need |
13 | ··········to·be·applied.</p></li><li·class="listitem"><a·class="indexterm"·name="idp6 | 13 | ··········to·be·applied.</p></li><li·class="listitem"><a·class="indexterm"·name="idp64052408"></a><p>·Use·left·recursion·rather·than·right·recursion |
14 | ··········wherever·possible.··While·not·strictly·a·performance·issue, | 14 | ··········wherever·possible.··While·not·strictly·a·performance·issue, |
15 | ··········this·affects·the·size·of·the·parser·stack,·which·is·kept·on | 15 | ··········this·affects·the·size·of·the·parser·stack,·which·is·kept·on |
16 | ··········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·id="ftn.idp6 | 16 | ··········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·id="ftn.idp64049336"·href="#idp64049336"·class="para">4</a>]·</sup>omitting·the |
17 | ··········<code·class="literal">-a</code>·may·generate·slightly·faster·parsers, | 17 | ··········<code·class="literal">-a</code>·may·generate·slightly·faster·parsers, |
18 | ··········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> | 18 | ··········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> |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.4. Type·Signatures</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-Precedences.html"·title="2.3. Using·Precedences"><link·rel="next"·href="sec-monads.html"·title="2.5. Monadic·Parsers"></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">2.4. Type·Signatures</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-Precedences.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-monads.html">Next</a></td></tr></table><hr>·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>2.4. Type·Signatures</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="sec-using.html"·title="Chapter 2. Using·Happy"><link·rel="prev"·href="sec-Precedences.html"·title="2.3. Using·Precedences"><link·rel="next"·href="sec-monads.html"·title="2.5. Monadic·Parsers"></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">2.4. Type·Signatures</th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-Precedences.html">Prev</a> </td><th·width="60%"·align="center">Chapter 2. Using·<span·class="application">Happy</span></th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-monads.html">Next</a></td></tr></table><hr>·✂ |
2 | ······in·the·grammar·file·itself,·to·indicate·the·type·of·each | 2 | ······in·the·grammar·file·itself,·to·indicate·the·type·of·each |
3 | ······production.··This·has·several·benefits:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>·Documentation:·including·types·in·the·grammar·helps | 3 | ······production.··This·has·several·benefits:</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>·Documentation:·including·types·in·the·grammar·helps |
4 | ··········to·document·the·grammar·for·someone·else·(and·indeed | 4 | ··········to·document·the·grammar·for·someone·else·(and·indeed |
5 | ··········yourself)·reading·the·code.</p></li><li·class="listitem"><p>·Fixing·type·errors·in·the·generated·module·can·become | 5 | ··········yourself)·reading·the·code.</p></li><li·class="listitem"><p>·Fixing·type·errors·in·the·generated·module·can·become |
6 | ··········slightly·easier·if·<span·class="application">Happy</span>·has·inserted·type | 6 | ··········slightly·easier·if·<span·class="application">Happy</span>·has·inserted·type |
7 | ··········signatures·for·you.··This·is·a·slightly·dubious·benefit, | 7 | ··········signatures·for·you.··This·is·a·slightly·dubious·benefit, |
8 | ··········since·type·errors·in·the·generated·module·are·still·somewhat | 8 | ··········since·type·errors·in·the·generated·module·are·still·somewhat |
Offset 1, 8 lines modified | Offset 1, 8 lines modified | ||
1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Chapter 2. Using·Happy</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="index.html"·title="Happy·User·Guide"><link·rel="prev"·href="sec-obtaining.html"·title="1.4. Obtaining·Happy"><link·rel="next"·href="sec-sequences.html"·title="2.2. Parsing·sequences"></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 2. Using·<span·class="application">Happy</span></th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-obtaining.html">Prev</a> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-sequences.html">Next</a></td></tr></table><hr></div><div·class="chapter·✂ | 1 | <html><head><meta·http-equiv="Content-Type"·content="text/html;·charset=ISO-8859-1"><title>Chapter 2. Using·Happy</title><link·rel="stylesheet"·type="text/css"·href="fptools.css"><meta·name="generator"·content="DocBook·XSL·Stylesheets·V1.76.1"><link·rel="home"·href="index.html"·title="Happy·User·Guide"><link·rel="up"·href="index.html"·title="Happy·User·Guide"><link·rel="prev"·href="sec-obtaining.html"·title="1.4. Obtaining·Happy"><link·rel="next"·href="sec-sequences.html"·title="2.2. Parsing·sequences"></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 2. Using·<span·class="application">Happy</span></th></tr><tr><td·width="20%"·align="left"><a·accesskey="p"·href="sec-obtaining.html">Prev</a> </td><th·width="60%"·align="center"> </th><td·width="20%"·align="right"> <a·accesskey="n"·href="sec-sequences.html">Next</a></td></tr></table><hr></div><div·class="chapter·✂ |
2 | ··<span·class="application">Happy</span>·quite·familiar.··The·basic·idea·is | 2 | ··<span·class="application">Happy</span>·quite·familiar.··The·basic·idea·is |
3 | ··as·follows:·</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>Define·the·grammar·you·want·to·parse·in·a | 3 | ··as·follows:·</p><div·class="itemizedlist"><ul·class="itemizedlist"·type="disc"><li·class="listitem"><p>Define·the·grammar·you·want·to·parse·in·a |
4 | ······<span·class="application">Happy</span>·grammar·file.·</p></li><li·class="listitem"><p>·Run·the·grammar·through·<span·class="application">Happy</span>,·to·generate | 4 | ······<span·class="application">Happy</span>·grammar·file.·</p></li><li·class="listitem"><p>·Run·the·grammar·through·<span·class="application">Happy</span>,·to·generate |
5 | ······a·compilable·Haskell·module.</p></li><li·class="listitem"><p>·Use·this·module·as·part·of·your·Haskell·program,·usually | 5 | ······a·compilable·Haskell·module.</p></li><li·class="listitem"><p>·Use·this·module·as·part·of·your·Haskell·program,·usually |
6 | ······in·conjunction·with·a·lexical·analyser·(a·function·that·splits | 6 | ······in·conjunction·with·a·lexical·analyser·(a·function·that·splits |
7 | ······the·input·into·``tokens'',·the·basic·unit·of·parsing).</p></li></ul></div><p>·Let's·run·through·an·example.··We'll·implement·a·parser·for·a | 7 | ······the·input·into·``tokens'',·the·basic·unit·of·parsing).</p></li></ul></div><p>·Let's·run·through·an·example.··We'll·implement·a·parser·for·a |
8 | ··simple·expression·syntax,·consisting·of·integers,·variables,·the | 8 | ··simple·expression·syntax,·consisting·of·integers,·variables,·the |
Offset 10, 25 lines modified | Offset 10, 25 lines modified | ||
10 | ··<code·class="literal">/</code>,·and·the·form·<code·class="literal">let·var·=·exp·in·exp</code>. | 10 | ··<code·class="literal">/</code>,·and·the·form·<code·class="literal">let·var·=·exp·in·exp</code>. |
11 | ··The·grammar·file·starts·off·like·this:</p><pre·class="programlisting"> | 11 | ··The·grammar·file·starts·off·like·this:</p><pre·class="programlisting"> |
12 | { | 12 | { |
13 | module·Main·where | 13 | module·Main·where |
14 | } | 14 | } |
15 | </pre><p>At·the·top·of·the·file·is·an·optional·<em·class="firstterm">module | 15 | </pre><p>At·the·top·of·the·file·is·an·optional·<em·class="firstterm">module |
16 | ····header</em>, | 16 | ····header</em>, |
17 | ······<a·class="indexterm"·name="idp63 | 17 | ······<a·class="indexterm"·name="idp63254840"></a> |
18 | ····which·is·just·a·Haskell·module·header·enclosed·in·braces.··This | 18 | ····which·is·just·a·Haskell·module·header·enclosed·in·braces.··This |
19 | ····code·is·emitted·verbatim·into·the·generated·module,·so·you·can·put | 19 | ····code·is·emitted·verbatim·into·the·generated·module,·so·you·can·put |
20 | ····any·Haskell·code·here·at·all.··In·a·grammar·file,·Haskell·code·is | 20 | ····any·Haskell·code·here·at·all.··In·a·grammar·file,·Haskell·code·is |
21 | ····always·contained·between·curly·braces·to·distinguish·it·from·the | 21 | ····always·contained·between·curly·braces·to·distinguish·it·from·the |
22 | ····grammar.</p><p>In·this·case,·the·parser·will·be·a·standalone·program·so | 22 | ····grammar.</p><p>In·this·case,·the·parser·will·be·a·standalone·program·so |
23 | ····we'll·call·the·module·<code·class="literal">Main</code>.</p><p>Next·comes·a·couple·of·declarations:</p><pre·class="programlisting"> | 23 | ····we'll·call·the·module·<code·class="literal">Main</code>.</p><p>Next·comes·a·couple·of·declarations:</p><pre·class="programlisting"> |
24 | %name·calc | 24 | %name·calc |
25 | %tokentype·{·Token·} | 25 | %tokentype·{·Token·} |
26 | %error·{·parseError·} | 26 | %error·{·parseError·} |
27 | </pre><a·class="indexterm"·name="idp63 | 27 | </pre><a·class="indexterm"·name="idp63258040"></a><a·class="indexterm"·name="idp63259192"></a><a·class="indexterm"·name="idp63260344"></a><p>The·first·line·declares·the·name·of·the·parsing·function |
28 | ····that·<span·class="application">Happy</span>·will·generate,·in·this·case | 28 | ····that·<span·class="application">Happy</span>·will·generate,·in·this·case |
29 | ····<code·class="literal">calc</code>.··In·many·cases,·this·is·the·only·symbol·you·need | 29 | ····<code·class="literal">calc</code>.··In·many·cases,·this·is·the·only·symbol·you·need |
30 | ····to·export·from·the·module.</p><p>The·second·line·declares·the·type·of·tokens·that·the·parser | 30 | ····to·export·from·the·module.</p><p>The·second·line·declares·the·type·of·tokens·that·the·parser |
31 | ····will·accept.··The·parser·(i.e.·the·function | 31 | ····will·accept.··The·parser·(i.e.·the·function |
32 | ····<code·class="function">calc</code>)·will·be·of·type·<code·class="literal">[Token]·-> | 32 | ····<code·class="function">calc</code>)·will·be·of·type·<code·class="literal">[Token]·-> |
33 | ····T</code>,·where·<code·class="literal">T</code>·is·the·return·type·of·the | 33 | ····T</code>,·where·<code·class="literal">T</code>·is·the·return·type·of·the |
34 | ····parser,·determined·by·the·production·rules·below.</p><p>The·<code·class="literal">%error</code>·directive·tells·Happy·the·name | 34 | ····parser,·determined·by·the·production·rules·below.</p><p>The·<code·class="literal">%error</code>·directive·tells·Happy·the·name |
Offset 42, 24 lines modified | Offset 42, 24 lines modified | ||
42 | ······'='·············{·TokenEq·} | 42 | ······'='·············{·TokenEq·} |
43 | ······'+'·············{·TokenPlus·} | 43 | ······'+'·············{·TokenPlus·} |
44 | ······'-'·············{·TokenMinus·} | 44 | ······'-'·············{·TokenMinus·} |
45 | ······'*'·············{·TokenTimes·} | 45 | ······'*'·············{·TokenTimes·} |
46 | ······'/'·············{·TokenDiv·} | 46 | ······'/'·············{·TokenDiv·} |
47 | ······'('·············{·TokenOB·} | 47 | ······'('·············{·TokenOB·} |
48 | ······')'·············{·TokenCB·} | 48 | ······')'·············{·TokenCB·} |
49 | </pre><a·class="indexterm"·name="idp632 | 49 | </pre><a·class="indexterm"·name="idp63266872"></a><p>The·symbols·on·the·left·are·the·tokens·as·they·will·be |
50 | ····referred·to·in·the·rest·of·the·grammar,·and·to·the·right·of·each | 50 | ····referred·to·in·the·rest·of·the·grammar,·and·to·the·right·of·each |
51 | ····token·enclosed·in·braces·is·a·Haskell·pattern·that·matches·the | 51 | ····token·enclosed·in·braces·is·a·Haskell·pattern·that·matches·the |
52 | ····token.··The·parser·will·expect·to·receive·a·stream·of·tokens,·each | 52 | ····token.··The·parser·will·expect·to·receive·a·stream·of·tokens,·each |
53 | ····of·which·will·match·one·of·the·given·patterns·(the·definition·of | 53 | ····of·which·will·match·one·of·the·given·patterns·(the·definition·of |
54 | ····the·<code·class="literal">Token</code>·datatype·is·given·later).</p><p>The·<code·class="literal">$$</code>·symbol·is·a·placeholder·that | 54 | ····the·<code·class="literal">Token</code>·datatype·is·given·later).</p><p>The·<code·class="literal">$$</code>·symbol·is·a·placeholder·that |
55 | ····represents·the·<span·class="emphasis"><em>value</em></span>·of·this·token.··Normally·the·value | 55 | ····represents·the·<span·class="emphasis"><em>value</em></span>·of·this·token.··Normally·the·value |
56 | ····of·a·token·is·the·token·itself,·but·by·using·the | 56 | ····of·a·token·is·the·token·itself,·but·by·using·the |
57 | ····<code·class="literal">$$</code>·symbol·you·can·specify·some·component | 57 | ····<code·class="literal">$$</code>·symbol·you·can·specify·some·component |
58 | ····of·the·token·object·to·be·the·value.·</p><a·class="indexterm"·name="idp6320 | 58 | ····of·the·token·object·to·be·the·value.·</p><a·class="indexterm"·name="idp63270840"></a><p>Like·yacc,·we·include·<code·class="literal">%%</code>·here,·for·no·real |
59 | ····reason.</p><pre·class="programlisting"> | 59 | ····reason.</p><pre·class="programlisting"> |
60 | %% | 60 | %% |
61 | </pre><p>Now·we·have·the·production·rules·for·the·grammar.</p><pre·class="programlisting"> | 61 | </pre><p>Now·we·have·the·production·rules·for·the·grammar.</p><pre·class="programlisting"> |
62 | Exp···:·let·var·'='·Exp·in·Exp··{·Let·$2·$4·$6·} | 62 | Exp···:·let·var·'='·Exp·in·Exp··{·Let·$2·$4·$6·} |
63 | ······|·Exp1····················{·Exp1·$1·} | 63 | ······|·Exp1····················{·Exp1·$1·} |
64 | Exp1··:·Exp1·'+'·Term···········{·Plus·$1·$3·} | 64 | Exp1··:·Exp1·'+'·Term···········{·Plus·$1·$3·} |
Offset 70, 15 lines modified | Offset 70, 15 lines modified | ||
70 | ······|·Term·'/'·Factor·········{·Div·$1·$3·} | 70 | ······|·Term·'/'·Factor·········{·Div·$1·$3·} |
71 | ······|·Factor··················{·Factor·$1·} | 71 | ······|·Factor··················{·Factor·$1·} |
72 | Factor | 72 | Factor |
73 | ······:·int·····················{·Int·$1·} | 73 | ······:·int·····················{·Int·$1·} |
74 | ······|·var·····················{·Var·$1·} | 74 | ······|·var·····················{·Var·$1·} |
75 | ······|·'('·Exp·')'·············{·Brack·$2·} | 75 | ······|·'('·Exp·')'·············{·Brack·$2·} |
76 | </pre><a·class="indexterm"·name="idp632 | 76 | </pre><a·class="indexterm"·name="idp63278392"></a><p>Each·production·consists·of·a·<em·class="firstterm">non-terminal</em> |
77 | ····symbol·on·the·left,·followed·by·a·colon,·followed·by·one·or·more | 77 | ····symbol·on·the·left,·followed·by·a·colon,·followed·by·one·or·more |
78 | ····expansions·on·the·right,·separated·by·<code·class="literal">|</code>.··Each·expansion | 78 | ····expansions·on·the·right,·separated·by·<code·class="literal">|</code>.··Each·expansion |
79 | ····has·some·Haskell·code·associated·with·it,·enclosed·in·braces·as | 79 | ····has·some·Haskell·code·associated·with·it,·enclosed·in·braces·as |
80 | ····usual.</p><p>The·way·to·think·about·a·parser·is·with·each·symbol·having·a | 80 | ····usual.</p><p>The·way·to·think·about·a·parser·is·with·each·symbol·having·a |
81 | ····`value':·we·defined·the·values·of·the·tokens·above,·and·the | 81 | ····`value':·we·defined·the·values·of·the·tokens·above,·and·the |
82 | ····grammar·defines·the·values·of·non-terminal·symbols·in·terms·of | 82 | ····grammar·defines·the·values·of·non-terminal·symbols·in·terms·of |
83 | ····sequences·of·other·symbols·(either·tokens·or·non-terminals).··In·a | 83 | ····sequences·of·other·symbols·(either·tokens·or·non-terminals).··In·a |
Offset 169, 15 lines modified | Offset 169, 15 lines modified | ||
169 | </pre><p>And·finally·a·top-level·function·to·take·some·input,·parse | 169 | </pre><p>And·finally·a·top-level·function·to·take·some·input,·parse |
170 | ····it,·and·print·out·the·result.</p><pre·class="programlisting"> | 170 | ····it,·and·print·out·the·result.</p><pre·class="programlisting"> |
171 | main·=·getContents·>>=·print·.·calc·.·lexer | 171 | main·=·getContents·>>=·print·.·calc·.·lexer |
172 | } | 172 | } |
173 | </pre><p>And·that's·it!·A·whole·lexer,·parser·and·grammar·in·a·few | 173 | </pre><p>And·that's·it!·A·whole·lexer,·parser·and·grammar·in·a·few |
174 | ····dozen·lines.··Another·good·example·is·<span·class="application">Happy</span>'s·own | 174 | ····dozen·lines.··Another·good·example·is·<span·class="application">Happy</span>'s·own |
175 | ····parser.·Several·features·in·<span·class="application">Happy</span>·were·developed | 175 | ····parser.·Several·features·in·<span·class="application">Happy</span>·were·developed |
176 | ····using·this·as·an·example.</p><a·class="indexterm"·name="idp63 | 176 | ····using·this·as·an·example.</p><a·class="indexterm"·name="idp63300024"></a><p>To·generate·the·Haskell·module·for·this·parser,·type·the |
177 | ····command·<span·class="command"><strong>happy·example.y</strong></span>·(where | 177 | ····command·<span·class="command"><strong>happy·example.y</strong></span>·(where |
178 | ····<code·class="filename">example.y</code>·is·the·name·of·the·grammar·file). | 178 | ····<code·class="filename">example.y</code>·is·the·name·of·the·grammar·file). |
179 | ····The·Haskell·module·will·be·placed·in·a·file·named | 179 | ····The·Haskell·module·will·be·placed·in·a·file·named |
180 | ····<code·class="filename">example.hs</code>.··Additionally,·invoking·the | 180 | ····<code·class="filename">example.hs</code>.··Additionally,·invoking·the |
181 | ····command·<span·class="command"><strong>happy·example.y·-i</strong></span>·will·produce·the | 181 | ····command·<span·class="command"><strong>happy·example.y·-i</strong></span>·will·produce·the |
182 | ····file·<code·class="filename">example.info</code>·which·contains·detailed·information | 182 | ····file·<code·class="filename">example.info</code>·which·contains·detailed·information |
183 | ····about·the·parser,·including·states·and·reduction·rules·(see·<a·class="xref"·href="sec-info-files.html"·title="Chapter 7. Info·Files">Chapter 7,·<i>Info·Files</i></a>).··This·can·be·invaluable·for·debugging | 183 | ····about·the·parser,·including·states·and·reduction·rules·(see·<a·class="xref"·href="sec-info-files.html"·title="Chapter 7. Info·Files">Chapter 7,·<i>Info·Files</i></a>).··This·can·be·invaluable·for·debugging |