/usr/share/doc/frown-doc/html/Manual003.html is in frown-doc 0.6.1-14.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="hevea 2.06">
<link rel="stylesheet" type="text/css" href="Manual.css">
<title>Quick start</title>
</head>
<body >
<a href="Manual002.html"><img src="previous_motif.gif" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
<a href="Manual004.html"><img src="next_motif.gif" alt="Next"></a>
<hr>
<h1 id="sec13" class="chapter">Chapter 2 Quick start</h1>
<p>First install <span style="font-family:sans-serif">Frown</span> as described in Sec. <a href="Manual002.html#sec%3Ainstall">1.1</a>. Then enter
the directory <span style="font-family:monospace">QuickStart</span>.
</p><table style="border:0;border-spacing:0" class="cellpadding0"><tr><td style="text-align:left;white-space:nowrap" ><span style="font-family:monospace"> ralf/Frown> cd QuickStart
</span></td></tr>
</table><p>
The file <span style="font-family:monospace">Tiger.lg</span>, listed in Fig. <a href="#fig%3Atiger">2.1</a>, contains a
medium-sized grammar for an imperative language.
</p><blockquote class="figure"><div class="center"><hr style="width:80%;height:2"></div>
<span style="font-size:small">A grammar file consists of two parts: the specification of the
grammar, enclosed in special curly braces, and Haskell source code.
The source file typically starts with a Haskell module header.
</span><span style="font-size:small">
<TABLE BORDER="2" BGCOLOR="#E0E0E0" WIDTH="100%"><TR><TD>
</span><table style="border:0;border-spacing:0" class="cellpadding0"><tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-weight:bold">module</span></span></span><span style="font-size:small"><span style="font-family:monospace"> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Tiger</span></span></span><span style="font-size:small"><span style="font-family:monospace"> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-weight:bold">where</span></span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-weight:bold">import</span></span></span><span style="font-size:small"><span style="font-family:monospace"> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Lexer</span></span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-weight:bold">import</span></span></span><span style="font-size:small"><span style="font-family:monospace"> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Syntax</span></span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-weight:bold">import</span></span></span><span style="font-size:small"><span style="font-family:monospace"> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Prelude</span></span></span><span style="font-size:small"><span style="font-family:monospace"> hiding (exp)</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> %{</span></span></td></tr>
</table><span style="font-size:small">
</span><span style="font-size:small">
</TD></TR></TABLE><P>
</span><span style="font-size:small">The grammar part begins here. A context-free grammar consists of sets
of terminal and nonterminal symbols, a set of start symbols, and set
of productions or grammar rules. The declaration below introduces the
terminal symbols. Each terminal is given by a Haskell pattern of type
</span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Terminal</span></span></span><span style="font-size:small">.
</span><span style="font-size:small">
<TABLE BORDER="2" BGCOLOR="#E0E0E0" WIDTH="100%"><TR><TD>
</span><table style="border:0;border-spacing:0" class="cellpadding0"><tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Terminal</span></span></span><span style="font-size:small"><span style="font-family:monospace"> = </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">DO</span></span></span><span style="font-size:small"><span style="font-family:monospace"> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ELSE</span></span></span><span style="font-size:small"><span style="font-family:monospace"> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">END</span></span></span><span style="font-size:small"><span style="font-family:monospace"> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">FUNCTION</span></span></span><span style="font-size:small"><span style="font-family:monospace"> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">IF</span></span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">IN</span></span></span><span style="font-size:small"><span style="font-family:monospace"> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">LET</span></span></span><span style="font-size:small"><span style="font-family:monospace"> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">THEN</span></span></span><span style="font-size:small"><span style="font-family:monospace"> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">VAR</span></span></span><span style="font-size:small"><span style="font-family:monospace"> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">WHILE</span></span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ASSIGN</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as ":=" | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">COLON</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as ":" | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">COMMA</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as "," | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">CPAREN</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as ")"</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">DIV</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as "/" | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">EQU</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as "=" | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">LST</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as "<=" | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">MINUS</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as "-"</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">NEG</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as "~" | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">OPAREN</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as "(" | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">PLUS</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as "+" | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">SEMI</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as ";"</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">TIMES</span></span></span><span style="font-size:small"><span style="font-family:monospace"> as "*"</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ID</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">String</span></span></span><span style="font-size:small"><span style="font-family:monospace">} | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">INT</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">String</span></span></span><span style="font-size:small"><span style="font-family:monospace">};
</span></span></td></tr>
</table><span style="font-size:small">
</span><span style="font-size:small">
</TD></TR></TABLE><P>
</span><span style="font-size:small">A terminal symbol may carry a semantic value or attribute. The Haskell
type of the semantic value is given in curly braces. As a rule,
Haskell source code is always enclosed in curly braces within the
grammar part. The as-clauses define shortcuts for terminals, which may
then be used in the productions.</span><p><span style="font-size:small">The declaration below introduces a nonterminal symbol called </span><span style="font-size:small"><span style="font-family:monospace">exp</span></span><span style="font-size:small">
followed by sixteen productions for that symbol. The asterix marks
</span><span style="font-size:small"><span style="font-family:monospace">exp</span></span><span style="font-size:small"> as a start symbol; </span><span style="font-size:small"><span style="font-family:monospace">exp</span></span><span style="font-size:small"> has a single attribute of type </span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Expr</span></span></span><span style="font-size:small">.
</span><span style="font-size:small">
<TABLE BORDER="2" BGCOLOR="#E0E0E0" WIDTH="100%"><TR><TD>
</span></p><table style="border:0;border-spacing:0" class="cellpadding0"><tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> *exp {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Expr</span></span></span><span style="font-size:small"><span style="font-family:monospace">};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> exp {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Var</span></span></span><span style="font-size:small"><span style="font-family:monospace"> v} : </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ID</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {v};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Block</span></span></span><span style="font-size:small"><span style="font-family:monospace"> es} | "(", sepBy exp ";" {es}, ")";</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Int</span></span></span><span style="font-size:small"><span style="font-family:monospace"> i } | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">INT</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {i};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Un</span></span></span><span style="font-size:small"><span style="font-family:monospace"> </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Neg</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e} | "-", exp {e}, prec "~";</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Call</span></span></span><span style="font-size:small"><span style="font-family:monospace"> f es} | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ID</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {f}, "(", sepBy exp "," {es}, ")";</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Bin</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e1 </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Eq</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e2} | exp {e1}, "=", exp {e2};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Bin</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e1 </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Leq</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e2} | exp {e1}, "<=", exp {e2};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Bin</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e1 </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Add</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e2} | exp {e1}, "+", exp {e2};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Bin</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e1 </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Sub</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e2} | exp {e1}, "-", exp {e2};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Bin</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e1 </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Mul</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e2} | exp {e1}, "*", exp {e2};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Bin</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e1 </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Div</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e2} | exp {e1}, "/", exp {e2};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Assign</span></span></span><span style="font-size:small"><span style="font-family:monospace"> v e} | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ID</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {v}, ":=", exp {e};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">IfThen</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e e1} | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">IF</span></span></span><span style="font-size:small"><span style="font-family:monospace">, exp {e}, </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">THEN</span></span></span><span style="font-size:small"><span style="font-family:monospace">, exp {e1};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">IfElse</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e e1 e2} | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">IF</span></span></span><span style="font-size:small"><span style="font-family:monospace">, exp {e}, </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">THEN</span></span></span><span style="font-size:small"><span style="font-family:monospace">, exp {e1}, </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ELSE</span></span></span><span style="font-size:small"><span style="font-family:monospace">, exp {e2};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">While</span></span></span><span style="font-size:small"><span style="font-family:monospace"> e e1} | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">WHILE</span></span></span><span style="font-size:small"><span style="font-family:monospace">, exp {e}, </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">DO</span></span></span><span style="font-size:small"><span style="font-family:monospace">, exp {e1};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Let</span></span></span><span style="font-size:small"><span style="font-family:monospace"> ds es} | </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">LET</span></span></span><span style="font-size:small"><span style="font-family:monospace">, many dec {ds}, </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">IN</span></span></span><span style="font-size:small"><span style="font-family:monospace">, sepBy exp ";" {es}, </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">END</span></span></span><span style="font-size:small"><span style="font-family:monospace">;
</span></span></td></tr>
</table><p><span style="font-size:small">
</span><span style="font-size:small">
</TD></TR></TABLE><P>
</span><span style="font-size:small">Left-hand and right-hand side of a production are separated by a
colon; symbols on the right are separated by commas and terminated by
a semicolon. Alternative right-hand sides are separated by a vertical
bar. </span></p><p><span style="font-size:small">The pieces in curly braces constitute Haskell source code. Each rule
can be seen as a function from the right-hand to the left-hand
side. On the right-hand side, Haskell variables are used to name the
values of attributes. The values of the attributes on the left-hand
side are given by Haskell expressions, in which the variables of the
right-hand side occur free.</span></p><p><span style="font-size:small">The last production makes use of two (predefined) rule schemes: </span><span style="font-size:small"><span style="font-family:monospace">many x</span></span><span style="font-size:small">
implements the repetition of the symbol </span><span style="font-size:small"><span style="font-family:monospace">x</span></span><span style="font-size:small">, and </span><span style="font-size:small"><span style="font-family:monospace">sepBy x sep</span></span><span style="font-size:small">
denotes a repetition of </span><span style="font-size:small"><span style="font-family:monospace">x</span></span><span style="font-size:small"> symbols separated by </span><span style="font-size:small"><span style="font-family:monospace">sep</span></span><span style="font-size:small"> symbols.</span></p><p><span style="font-size:small">The above productions are ambiguous as, for instance, </span><span style="font-size:small"><span style="font-family:monospace">1 + 2 * 3</span></span><span style="font-size:small"> has
two derivations. The ambiguity can be resolved by assigning
precedences to terminal symbols.
</span><span style="font-size:small">
<TABLE BORDER="2" BGCOLOR="#E0E0E0" WIDTH="100%"><TR><TD>
</span></p><table style="border:0;border-spacing:0" class="cellpadding0"><tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> left 7 "~"; left 6 "*"; left 6 "/"; left 5 "+"; left 5 "-";</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> right 0 </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">THEN</span></span></span><span style="font-size:small"><span style="font-family:monospace">; right 0 </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ELSE</span></span></span><span style="font-size:small"><span style="font-family:monospace">; </span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> nonassoc 4 "<="; nonassoc 4 "="; nonassoc 0 </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">DO</span></span></span><span style="font-size:small"><span style="font-family:monospace">; nonassoc 0 ":=";
</span></span></td></tr>
</table><p><span style="font-size:small">
</span><span style="font-size:small">
</TD></TR></TABLE><P>
</span><span style="font-size:small">The following declarations define the nonterminal </span><span style="font-size:small"><span style="font-family:monospace">dec</span></span><span style="font-size:small"> and three
further nonterminals.
</span><span style="font-size:small">
<TABLE BORDER="2" BGCOLOR="#E0E0E0" WIDTH="100%"><TR><TD>
</span></p><table style="border:0;border-spacing:0" class="cellpadding0"><tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> dec {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Decl</span></span></span><span style="font-size:small"><span style="font-family:monospace">};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> dec {d} : vardec {d};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> {d} | fundec {d};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> vardec {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Decl</span></span></span><span style="font-size:small"><span style="font-family:monospace">};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> vardec {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Variable</span></span></span><span style="font-size:small"><span style="font-family:monospace"> v e} : </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">VAR</span></span></span><span style="font-size:small"><span style="font-family:monospace">, </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ID</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {v}, ":=", exp {e};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> fundec {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Decl</span></span></span><span style="font-size:small"><span style="font-family:monospace">};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> fundec {</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Function</span></span></span><span style="font-size:small"><span style="font-family:monospace"> f xs e} : </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">FUNCTION</span></span></span><span style="font-size:small"><span style="font-family:monospace">, </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ID</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {f}, "(", sepBy (</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ID</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {}) "," {xs}, ")", "=", exp {e};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> formal {(</span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">Ident</span></span></span><span style="font-size:small"><span style="font-family:monospace">, </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">TyIdent</span></span></span><span style="font-size:small"><span style="font-family:monospace">)};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> formal {(v, t)} : </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ID</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {v}, ":", </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-style:italic">ID</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {t};</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> }%
</span></span></td></tr>
</table><p><span style="font-size:small">
</span><span style="font-size:small">
</TD></TR></TABLE><P>
</span><span style="font-size:small">The grammar part ends here. The source file typically includes a
couple of Haskell declarations. The user-defined function </span><span style="font-size:small"><span style="font-family:monospace">frown</span></span><span style="font-size:small"> is
the error routine invoked by the parser in case of a syntax error; its
definition is mandatory.
</span><span style="font-size:small">
<TABLE BORDER="2" BGCOLOR="#E0E0E0" WIDTH="100%"><TR><TD>
</span></p><table style="border:0;border-spacing:0" class="cellpadding0"><tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> frown _ = error "syntax error"</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> </span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> tc f = </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-weight:bold">do</span></span></span><span style="font-size:small"><span style="font-family:monospace"> { putStrLn "*** reading ..."; s <- readFile f; print s;</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> putStrLn "*** lexing ..."; </span></span><span style="font-size:small"><span style="font-family:monospace"><span style="font-weight:bold">let</span></span></span><span style="font-size:small"><span style="font-family:monospace"> {ts = lexer s}; print ts;</span></span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-size:small"><span style="font-family:monospace">> putStrLn "*** parsing ..."; e <- exp ts; print e }</span></span></td></tr>
</table><p><span style="font-size:small">
</span><span style="font-size:small">
</TD></TR></TABLE><P>
</span></p><div class="caption"><table style="border-spacing:6px;border-collapse:separate;" class="cellpading0"><tr><td style="vertical-align:top;text-align:left;" ><span style="font-size:small">Figure 2.1: </span><a id="fig:tiger"></a><span style="font-size:small">A sample </span><span style="font-size:small"><span style="font-family:sans-serif">Frown</span></span><span style="font-size:small"> grammar file.</span></td></tr>
</table></div><div class="center"><hr style="width:80%;height:2"></div></blockquote><p>
Now, type
</p><table style="border:0;border-spacing:0" class="cellpadding0"><tr><td style="text-align:left;white-space:nowrap" ><span style="font-family:monospace"> ralf/Frown/QuickStart> frown -v Tiger.lg</span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-family:monospace"> ralf/Frown/QuickStart> hugs Tiger.hs</span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-family:monospace"> ...</span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-family:monospace"> Tiger> exp [ID "a", PLUS, ID "b", TIMES, INT "2"] >>= print</span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-family:monospace"> Bin (Var "a") Add (Bin (Var "b") Mul (Int "2"))</span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-family:monospace"> Tiger> tc "fib.tig"</span></td></tr>
<tr><td style="text-align:left;white-space:nowrap" ><span style="font-family:monospace"> ...
</span></td></tr>
</table><p>
The call <span style="font-family:monospace">frown -v Tiger.lg</span> generates a Haskell parser which can then
be loaded into <span style="font-family:monospace">hugs</span> (or <span style="font-family:monospace">ghci</span>). The parser has type <span style="font-family:monospace">exp :: (</span><span style="font-family:monospace"><span style="font-style:italic">Monad</span></span><span style="font-family:monospace"> m) => [</span><span style="font-family:monospace"><span style="font-style:italic">Terminal</span></span><span style="font-family:monospace">] -> m </span><span style="font-family:monospace"><span style="font-style:italic">Expr</span></span>, that is, the parser is a computation that
takes a list of terminals as input and returns an expression.</p><p><br>
More examples can be found in the directory <span style="font-family:monospace">Manual/Examples</span>:
</p><dl class="description"><dt class="dt-description">
<span style="font-weight:bold"><span style="font-family:monospace">Paren1.lg</span></span></dt><dd class="dd-description"> <br>
well-balanced parentheses: a pure grammar (see Sec. <a href="Manual004.html#sec%3Apure">3.2.1</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Paren2.lg</span></span></dt><dd class="dd-description"> <br>
an extension of <span style="font-family:monospace">Paren1.lg</span> illustrating the definition of attributes
(see Sec. <a href="Manual004.html#sec%3Aattributes">3.2.2</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Calc.lg</span></span></dt><dd class="dd-description"> <br>
a simple evaluator for arithmetic expressions: a parser that
interfaces with a separate lexer (see Sec. <a href="Manual004.html#sec%3Alexer">3.2.3</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">MCalc.lg</span></span></dt><dd class="dd-description"> <br>
a variant of the desktop calculator (<span style="font-family:monospace">Calc.lg</span>) that prints all
intermediate results: illustrates monadic actions (see
Sec. <a href="Manual004.html#sec%3Amonadicactions">3.2.4</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Let1.lg</span></span></dt><dd class="dd-description"> <br>
an ambiguous expression grammar: illustrates backtracking parsers (see
Sec. <a href="Manual004.html#sec%3Abacktracking">3.2.5</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Let2.lg</span></span></dt><dd class="dd-description"> <br>
an expression grammar: illustrates the use of precedences and
associativity (see Sec. <a href="Manual004.html#sec%3Aprec">3.2.6</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Let3.lg</span></span></dt><dd class="dd-description"> <br>
a variant of the expression grammar: shows how to simulate inherited
attributes using a reader monad (see Sec. <a href="Manual004.html#sec%3Areader">3.2.8</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Let4.lg</span></span></dt><dd class="dd-description"> <br>
an expression grammar: illustrates a parser
that interfaces with a monadic lexer (see Sec. <a href="Manual004.html#sec%3Amlexer">3.3.1</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Let5.lg</span></span></dt><dd class="dd-description"> <br>
a variant of <span style="font-family:monospace">Let4.lg</span> with better error reporting (see
Sec. <a href="Manual004.html#sec%3Areporting">3.3.2</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Let6.lg</span></span></dt><dd class="dd-description"> <br>
a variant of <span style="font-family:monospace">Let5.lg</span> with even better error reporting: prints a
list of expected tokens upon error (see Sec. <a href="Manual004.html#sec%3Aexpected">3.3.3</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Let7.lg</span></span></dt><dd class="dd-description"> <br>
yet another variant of the expression grammar: illustrates a simple
form of error correction (see Sec. <a href="Manual004.html#sec%3Aerrorcorrection">3.3.4</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Let8.lg</span></span></dt><dd class="dd-description"> <br>
variant of <span style="font-family:monospace">Let7.lg</span> that notifies the user of corrections (see
Sec. <a href="Manual004.html#sec%3Aerrorcorrection">3.3.4</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">VarCalc.lg</span></span></dt><dd class="dd-description"> <br>
a variant of the desktop calculator (<span style="font-family:monospace">Calc.lg</span>) that works without a
separate lexer: illustrates guards (see Sec. <a href="Manual004.html#sec%3Aterminal2">3.4.2</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">Paren3.lg</span></span></dt><dd class="dd-description"> <br>
illustrates the tracing facilities (see Sec. <a href="Manual004.html#sec%3Atracing">3.4.4</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">VarParen.lg</span></span></dt><dd class="dd-description"> <br>
illustrates irrefutable patterns on the right-hand side of productions
(see Sec. <a href="Manual005.html#sec%3Airrefutable">4.1</a>);</dd><dt class="dt-description"><span style="font-weight:bold"><span style="font-family:monospace">RepMin.lg</span></span></dt><dd class="dd-description"> <br>
a solution to the rep-min problem: illustrates how to simulate
inherited attributes using functional attributes (see
Sec. <a href="Manual005.html#sec%3Ainherited">4.2</a>).
</dd></dl>
<hr>
<a href="Manual002.html"><img src="previous_motif.gif" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
<a href="Manual004.html"><img src="next_motif.gif" alt="Next"></a>
</body>
</html>
|