You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/br-parser-tools/br-parser-tools-doc/parser-tools/doc/br-parser-tools/LALR_1__Parsers.html

91 lines
32 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>2&nbsp;LALR(1) Parsers</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="file:///Users/mb/git/racket/racket/doc/local-redirect/local-redirect.js"></script><script type="text/javascript" src="file:///Users/MB/Library/Racket/6.8.0.1/doc/local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_0&quot;);">&#9660;</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">Parser Tools:<span class="mywbr"> &nbsp;</span> <span class="stt">lex</span> and <span class="stt">yacc</span>-<wbr></wbr>style Parsing</a></td></tr></table></div><div class="tocviewsublistonly" style="display: block;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1&nbsp;</td><td><a href="Lexers.html" class="tocviewlink" data-pltdoc="x">Lexers</a></td></tr><tr><td align="right">2&nbsp;</td><td><a href="" class="tocviewselflink" data-pltdoc="x">LALR(1) Parsers</a></td></tr><tr><td align="right">3&nbsp;</td><td><a href="Context-Free_Parsers.html" class="tocviewlink" data-pltdoc="x">Context-<wbr></wbr>Free Parsers</a></td></tr><tr><td align="right">4&nbsp;</td><td><a href="Converting_yacc_or_bison_Grammars.html" class="tocviewlink" data-pltdoc="x">Converting <span class="stt">yacc</span> or <span class="stt">bison</span> Grammars</a></td></tr><tr><td align="right"></td><td><a href="doc-index.html" class="tocviewlink" data-pltdoc="x">Index</a></td></tr></table></div></div></div><div class="tocsub"><div class="tocsubtitle">On this page:</div><table class="tocsublist" cellspacing="0"><tr><td><a href="#%28form._%28%28lib._br-parser-tools%2Fyacc..rkt%29._parser%29%29" class="tocsublink" data-pltdoc="x"><span class="RktSym"><span class="RktStxLink">parser</span></span></a></td></tr></table></div></div><div class="maincolumn"><div class="main"><div class="navsettop"><span class="navleft"><form class="searchform"><input class="searchbox" style="color: #888;" type="text" value="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, &quot;6.8.0.1&quot;, &quot;../&quot;);" onfocus="this.style.color=&quot;black&quot;; this.style.textAlign=&quot;left&quot;; if (this.value == &quot;...search manuals...&quot;) this.value=&quot;&quot;;" onblur="if (this.value.match(/^ *$/)) { this.style.color=&quot;#888&quot;; this.style.textAlign=&quot;center&quot;; this.value=&quot;...search manuals...&quot;; }"/></form>&nbsp;&nbsp;<a href="../index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot(&quot;6.8.0.1&quot;);">top</a></span><span class="navright">&nbsp;&nbsp;<a href="Lexers.html" title="backward to &quot;1 Lexers&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="index.html" title="up to &quot;Parser Tools: lex and yacc-style Parsing&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="Context-Free_Parsers.html" title="forward to &quot;3 Context-Free Parsers&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div><h3 x-source-module="(lib &quot;parser-tools/br-parser-tools.scrbl&quot;)" x-source-pkg="br-parser-tools-doc" x-part-tag="&quot;LALR_1__Parsers&quot;">2<tt>&nbsp;</tt><a name="(part._.L.A.L.R_1__.Parsers)"></a><a name="(mod-path._br-parser-tools/yacc)"></a><a name="(idx._(gentag._3._(lib._parser-tools/br-parser-tools..scrbl)))"></a>LALR(1) Parsers</h3><p><table cellspacing="0" cellpadding="0" class="defmodule"><tr><td align="left" colspan="2"><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">require</a></span><span class="stt"> </span><a href="" class="RktModLink" data-pltdoc="x"><span class="RktSym">br-parser-tools/yacc</span></a><span class="RktPn">)</span></td></tr><tr><td align="left">&nbsp;</td><td align="right"><span class="RpackageSpec"><span class="Smaller">&nbsp;package:</span> <span class="stt">br-parser-tools-lib</span></span></td></tr></table></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>syntax</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(form._((lib._br-parser-tools/yacc..rkt)._parser))"></a><span title="Provided from: br-parser-tools/yacc | Package: br-parser-tools-lib"><span class="RktSym"><a href="#%28form._%28%28lib._br-parser-tools%2Fyacc..rkt%29._parser%29%29" class="RktStxDef RktStxLink" data-pltdoc="x">parser</a></span></span><span class="hspace">&nbsp;</span><span class="RktVar">clause</span><span class="hspace">&nbsp;</span><span class="RktMeta">...</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="stt">&nbsp;</span></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="specgrammar"><tr><td align="right" valign="baseline"><span class="RktVar">clause</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">=</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">grammar</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktVar">non-terminal-id</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktPn">(</span><span class="RktVar">grammar-id</span><span class="hspace">&nbsp;</span><span class="RktMeta">...</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVar">maybe-prec</span><span class="hspace">&nbsp;</span><span class="RktVar">expr</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktMeta">...</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktMeta">...</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">tokens</span><span class="hspace">&nbsp;</span><span class="RktVar">group-id</span><span class="hspace">&nbsp;</span><span class="RktMeta">...</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">start</span><span class="hspace">&nbsp;</span><span class="RktVar">non-terminal-id</span><span class="hspace">&nbsp;</span><span class="RktMeta">...</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">end</span><span class="hspace">&nbsp;</span><span class="RktVar">token-id</span><span class="hspace">&nbsp;</span><span class="RktMeta">...</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">error</span><span class="hspace">&nbsp;</span><span class="RktVar">expr</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">precs</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktVar">assoc</span><span class="hspace">&nbsp;</span><span class="RktVar">token-id</span><span class="hspace">&nbsp;</span><span class="RktMeta">...</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktMeta">...</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">src-pos</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">suppress</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">debug</span><span class="hspace">&nbsp;</span><span class="RktVar">filename</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">yacc-output</span><span class="hspace">&nbsp;</span><span class="RktVar">filename</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td></tr><tr><td align="right" valign="baseline"><span class="RktVar">maybe-prec</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">=</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td></td></tr></table></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktPn">(</span><span class="RktSym">prec</span><span class="hspace">&nbsp;</span><span class="RktVar">token-id</span><span class="RktPn">)</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td></tr><tr><td align="right" valign="baseline"><span class="RktVar">assoc</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">=</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktVar">left</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktVar">right</span></td></tr><tr><td align="right" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="center" valign="baseline">|</td><td align="left" valign="baseline"><span class="stt">&nbsp;</span></td><td align="left" valign="baseline"><span class="RktVar">nonassoc</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">Creates a parser. The clauses may be in any order, as long as there
are no duplicates and all non-<span style="font-style: italic">OPTIONAL</span> declarations are
present:</div></p><ul><li><p><table cellspacing="0" cellpadding="0" class="RktBlk" style="display: inline-table; vertical-align: text-top; margin-top: 0;"><tr><td><span class="RktPn">(</span><span class="RktSym">grammar</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktVar">non-terminal-id</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktPn">(</span><span class="RktVar">grammar-id</span><span class="hspace">&nbsp;</span><span class="RktMeta"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVar">maybe-prec</span><span class="hspace">&nbsp;</span><span class="RktVar">expr</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktMeta"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktMeta"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></p><p>Declares the grammar to be parsed. Each <span class="RktVar">grammar-id</span> can
be a <span class="RktVar">token-id</span> from a <span class="RktVar">group-id</span> named in a
<span class="RktSym">tokens</span> declaration, or it can be a
<span class="RktVar">non-terminal-id</span> declared in the <span class="RktSym">grammar</span>
declaration. The optional <span class="RktSym">prec</span> declaration works with
the <span class="RktSym">precs</span> declaration. The <span class="RktVar">expr</span> is a
&ldquo;semantic action,&rdquo; which is evaluated when the input is found
to match its corresponding production.</p><p>Each action is Racket code that has the same scope as its
parser&rsquo;s definition, except that the variables <span class="RktSym">$1</span>, ...,
<span class="RktSym">$</span><span style="font-style: italic">i</span> are bound, where <span style="font-style: italic">i</span> is the number
of <span class="RktVar">grammar-id</span>s in the corresponding production. Each
<span class="RktSym">$</span><span style="font-style: italic">k</span> is bound to the result of the action
for the <span style="font-style: italic">k</span><span style="vertical-align: super; font-size: 80%">th</span> grammar symbol on the right of
the production, if that grammar symbol is a non-terminal, or the
value stored in the token if the grammar symbol is a terminal.
If the <span class="RktSym">src-pos</span> option is present in the parser, then
variables <span class="RktSym">$1-start-pos</span>, ...,
<span class="RktSym">$</span><span style="font-style: italic">i</span><span class="RktSym">-start-pos</span> and
<span class="RktSym">$1-end-pos</span>, ...,
<span class="RktSym">$</span><span style="font-style: italic">i</span><span class="RktSym">-end-pos</span> and are also
available, and they refer to the position structures
corresponding to the start and end of the corresponding
<span class="RktSym">grammar-symbol</span>. Grammar symbols defined as empty-tokens
have no <span class="RktSym">$</span><span style="font-style: italic">k</span> associated, but do have
<span class="RktSym">$</span><span style="font-style: italic">k</span><span class="RktSym">-start-pos</span> and
<span class="RktSym">$</span><span style="font-style: italic">k</span><span class="RktSym">-end-pos</span>.
Also <span class="RktSym">$n-start-pos</span> and <span class="RktSym">$n-end-pos</span>
are bound to the largest start and end positions, (i.e.,
<span class="RktSym">$</span><span style="font-style: italic">i</span><span class="RktSym">-start-pos</span> and
<span class="RktSym">$</span><span style="font-style: italic">i</span><span class="RktSym">-end-pos</span>).</p><p>An <a name="(tech._error._production)"></a><span style="font-style: italic">error production</span> can be defined by providing
a production of the form <span class="RktPn">(</span><span class="RktSym"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=exns.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._error%2529%2529&amp;version=6.8.0.1" class="RktValLink Sq" data-pltdoc="x">error</a></span><span class="stt"> </span><span class="RktSym">&#945;</span><span class="RktPn">)</span>, where &#945; is a
string of grammar symbols, possibly empty.</p><p>All of the productions for a given non-terminal must be grouped
with it. That is, no <span class="RktVar">non-terminal-id</span> may appear twice
on the left hand side in a parser.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">tokens</span><span class="stt"> </span><span class="RktVar">group-id</span><span class="stt"> </span><span class="RktMeta"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></p><p>Declares that all of the tokens defined in each
<span class="RktVar">group-id</span>&#8212;<wbr></wbr>as bound by <span class="RktSym"><a href="Lexers.html#%28form._%28%28lib._br-parser-tools%2Flex..rkt%29._define-tokens%29%29" class="RktStxLink" data-pltdoc="x">define-tokens</a></span> or
<span class="RktSym"><a href="Lexers.html#%28form._%28%28lib._br-parser-tools%2Flex..rkt%29._define-empty-tokens%29%29" class="RktStxLink" data-pltdoc="x">define-empty-tokens</a></span>&#8212;<wbr></wbr>can be used by the parser in the
<span class="RktSym">grammar</span> declaration.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">start</span><span class="stt"> </span><span class="RktVar">non-terminal-id</span><span class="stt"> </span><span class="RktMeta"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></p><p>Declares a list of starting non-terminals for the grammar.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">end</span><span class="stt"> </span><span class="RktVar">token-id</span><span class="stt"> </span><span class="RktMeta"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span></p><p>Specifies a set of tokens from which some member must follow any
valid parse. For example, an EOF token would be specified for a
parser that parses entire files and a newline token for a parser
that parses entire lines individually.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">error</span><span class="stt"> </span><span class="RktVar">expr</span><span class="RktPn">)</span></p><p>The <span class="RktVar">expr</span> should evaluate to a function which will be
executed for its side-effect whenever the parser encounters an
error.</p><p>If the <span class="RktSym">src-pos</span> declaration is present, the function
should accept 5 arguments,:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tok-ok?</span><span class="hspace">&nbsp;</span><span class="RktSym">tok-name</span><span class="hspace">&nbsp;</span><span class="RktSym">tok-value</span><span class="hspace">&nbsp;</span><span class="RktVar">start-pos</span><span class="hspace">&nbsp;</span><span class="RktVar">end-pos</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym">....</span><span class="RktPn">)</span></td></tr></table></blockquote><p>Otherwise it should accept 3:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tok-ok?</span><span class="hspace">&nbsp;</span><span class="RktSym">tok-name</span><span class="hspace">&nbsp;</span><span class="RktSym">tok-value</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym">....</span><span class="RktPn">)</span></td></tr></table></blockquote><p>The first argument will be <span class="RktVal">#f</span> if and only if the error
is that an invalid token was received. The second and third
arguments will be the name and the value of the token at which
the error was detected. The fourth and fifth arguments, if
present, provide the source positions of that token.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">precs</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktVar">assoc</span><span class="stt"> </span><span class="RktVar">token-id</span><span class="stt"> </span><span class="RktMeta"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="stt"> </span><span class="RktMeta"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&amp;version=6.8.0.1" class="RktStxLink Sq" data-pltdoc="x">...</a></span><span class="RktPn">)</span>
<span style="font-style: italic">OPTIONAL</span></p><p>Precedence declarations to resolve shift/reduce and
reduce/reduce conflicts as in <span class="stt">yacc</span>/<span class="stt">bison</span>. An
<span class="RktVar">assoc</span> must be one of <span class="RktVar">left</span>, <span class="RktVar">right</span> or
<span class="RktVar">nonassoc</span>. States with multiple shift/reduce or
reduce/reduce conflicts (or some combination thereof) are not
resolved with precedence.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">src-pos</span><span class="RktPn">)</span> <span style="font-style: italic">OPTIONAL</span></p><p>Causes the generated parser to expect input in the form
<span class="RktPn">(</span><span class="RktSym"><a href="Lexers.html#%28def._%28%28lib._br-parser-tools%2Flex..rkt%29._make-position-token%29%29" class="RktValLink" data-pltdoc="x">make-position-token</a></span><span class="stt"> </span><span class="RktVar">token</span><span class="stt"> </span><span class="RktVar">start-pos</span><span class="stt"> </span><span class="RktVar">end-pos</span><span class="RktPn">)</span> instead
of simply <span class="RktVar">token</span>. Include this option when using the
parser with a lexer generated with <span class="RktSym"><a href="Lexers.html#%28form._%28%28lib._br-parser-tools%2Flex..rkt%29._lexer-src-pos%29%29" class="RktStxLink" data-pltdoc="x">lexer-src-pos</a></span>.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">debug</span><span class="stt"> </span><span class="RktVar">filename</span><span class="RktPn">)</span> <span style="font-style: italic">OPTIONAL</span></p><p>Causes the parser generator to write the LALR table to the file
named <span class="RktVar">filename</span> (unless the file exists), where
<span class="RktVar">filename</span> is a literal string. Additionally, if a debug
file is specified, when a running generated parser encounters a
parse error on some input file, after the user specified error
expression returns, the complete parse stack is printed to
assist in debugging the grammar of that particular parser. The
numbers in the stack printout correspond to the state numbers in
the LALR table file.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">yacc-output</span><span class="stt"> </span><span class="RktVar">filename</span><span class="RktPn">)</span> <span style="font-style: italic">OPTIONAL</span></p><p>Causes the parser generator to write a grammar file in
approximately the syntax of <span class="stt">yacc</span>/<span class="stt">bison</span>. The file
might not be a valid <span class="stt">yacc</span> file, because the Racket
grammar can use symbols that are invalid in C.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">suppress</span><span class="RktPn">)</span> <span style="font-style: italic">OPTIONAL</span></p><p>Causes the parser generator not to report shift/reduce or
reduce/reduce conflicts.</p></li></ul><p>The result of a <span class="RktSym"><a href="#%28form._%28%28lib._br-parser-tools%2Fyacc..rkt%29._parser%29%29" class="RktStxLink" data-pltdoc="x">parser</a></span> expression with one <span class="RktSym">start</span>
non-terminal is a function, <span class="RktVar">parse</span>, that takes one
argument. This argument must be a zero argument function,
<span class="RktVar">gen</span>, that produces successive tokens of the input each
time it is called. If desired, the <span class="RktVar">gen</span> may return
symbols instead of tokens, and the parser will treat symbols as
tokens of the corresponding name (with <span class="RktVal">#f</span> as a value, so
it is usual to return symbols only in the case of empty tokens).
The <span class="RktVar">parse</span> function returns the value associated with the
parse tree by the semantic actions. If the parser encounters an
error, after invoking the supplied error function, it will try to
use <a href="#%28tech._error._production%29" class="techoutside" data-pltdoc="x"><span class="techinside">error production</span></a>s to continue parsing. If it cannot, it
raises <span class="RktSym"><a href="http://docs.racket-lang.org/local-redirect/index.html?doc=reference&amp;rel=exns.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._exn%7E3afail%7E3aread%2529%2529&amp;version=6.8.0.1" class="RktValLink Sq" data-pltdoc="x">exn:fail:read</a></span>.</p><p>If multiple non-terminals are provided in <span class="RktSym">start</span>, the
<span class="RktSym"><a href="#%28form._%28%28lib._br-parser-tools%2Fyacc..rkt%29._parser%29%29" class="RktStxLink" data-pltdoc="x">parser</a></span> expression produces a list of parsing functions,
one for each non-terminal in the same order. Each parsing function
is like the result of a parser expression with only one
<span class="RktSym">start</span> non-terminal,</p><p>Each time the Racket code for a <span class="RktSym"><a href="#%28form._%28%28lib._br-parser-tools%2Fyacc..rkt%29._parser%29%29" class="RktStxLink" data-pltdoc="x">parser</a></span> is compiled
(e.g. when a <span class="stt">".rkt"</span> file containing a <span class="RktSym"><a href="#%28form._%28%28lib._br-parser-tools%2Fyacc..rkt%29._parser%29%29" class="RktStxLink" data-pltdoc="x">parser</a></span> form
is loaded), the parser generator is run. To avoid this overhead
place the parser into a module and compile the module to a
<span class="stt">".zo"</span> bytecode file.</p><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" style="color: #888;" type="text" value="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, &quot;6.8.0.1&quot;, &quot;../&quot;);" onfocus="this.style.color=&quot;black&quot;; this.style.textAlign=&quot;left&quot;; if (this.value == &quot;...search manuals...&quot;) this.value=&quot;&quot;;" onblur="if (this.value.match(/^ *$/)) { this.style.color=&quot;#888&quot;; this.style.textAlign=&quot;center&quot;; this.value=&quot;...search manuals...&quot;; }"/></form>&nbsp;&nbsp;<a href="../index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot(&quot;6.8.0.1&quot;);">top</a></span><span class="navright">&nbsp;&nbsp;<a href="Lexers.html" title="backward to &quot;1 Lexers&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="index.html" title="up to &quot;Parser Tools: lex and yacc-style Parsing&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="Context-Free_Parsers.html" title="forward to &quot;3 Context-Free Parsers&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div></div></div><div id="contextindicator">&nbsp;</div></body></html>