<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage 2.0">
<title>Geyacc: The Generated Parser</title>
</head>

<body bgcolor="#FFFFFF">

<table border="0" width="100%">
    <tr>
        <td><font size="6"><strong>The Generated Parser</strong></font></td>
        <td align="right"><a href="actions.html"><img
        src="image/previous.gif" alt="Previous" border="0"
        width="40" height="40"></a><a href="options.html"><img
        src="image/next.gif" alt="Next" border="0" width="40"
        height="40"></a></td>
    </tr>
</table>

<hr size="1">

<p>For each parser description file given as input, <em>geyacc</em>
will generate an Eiffel class as output. The deferred class <a
href="skeleton.html"><font color="#008080"><em><tt>YY_PARSER</tt></em></font></a>,
which is part of <em>Gobo Eiffel Parse Library</em>, provides an
abstraction for parsers. Every parser class generated by <em>geyacc</em>
will be a descendant of this class. The main feature of <font
color="#008080"><em><tt>YY_PARSER</tt></em></font> is routine <a
href="skeleton.html#parse" name="parse"><font color="#008080"><em><tt>parse</tt></em></font></a><font
color="#008080" size="2" face="Courier New"><em> </em></font>which,
when called, reads tokens, executes actions and ultimately
returns when it encounters the end of input or an unrecoverable
syntax error. <font color="#008080"><em><tt>parse</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>sets
<a href="skeleton.html#syntax_error" name="syntax_error"><font
color="#008080"><em><tt>syntax_error</tt></em></font></a><font
color="#008080" size="2" face="Courier New"><em> </em></font>to
false if the parsing was successful. Otherwise, if an
unrecoverable error is detected, <font color="#008080"><em><tt>syntax_error</tt></em></font>
is set to true and the error is reported by calling <a
href="actions.html#report_error"><font color="#008080"><em><tt>report_error</tt></em></font></a>.
By default this routine just prints a message on the screen, but
it can easily be redefined to suit your needs. Also of interest
is feature <a href="actions.html#error_count"><font
color="#008080"><em><tt>error_count</tt></em></font></a> which
keeps track of the number of syntax errors (recovered and fatal)
detected during the last parsing.</p>

<p>The <a name="lexical_analyzer"><em><strong>lexical analyzer</strong></em></a>
routine, <a href="actions.html#read_token"><font color="#008080"><em><tt>read_token</tt></em></font></a>,
recognizes tokens from the input stream and makes them available
to the parser in <a href="actions.html#last_token"><font
color="#008080"><em><tt>last_token</tt></em></font></a>. <em>Geyacc</em>
does not provide a default implementation for <font
color="#008080"><em><tt>read_token</tt></em></font> and <font
color="#008080"><em><tt>last_token</tt></em></font>, so you must
define these two deferred features. In simple programs, <font
color="#008080"><em><tt>read_token</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>is
often defined at the end of the <em>geyacc</em> grammar file, in
the <a href="description.html#user_code">user code section</a>.
If <font color="#008080"><em><tt>read_token</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>is
defined in a separate class, you need to arrange for the
token-type integer constant definitions to be available there. To
do this, use the <a href="options.html#-t">option <font
color="#800000"><tt>-t</tt></font></a> when you run <em>geyacc</em>,
so that it will write these integer constant definitions along
with the feature <a href="actions.html#token_name"><font
color="#008080"><em><tt>token_name</tt></em></font></a> into a
separate class. </p>

<p>The value that <font color="#008080"><em><tt>read_token</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>returns
in <font color="#008080"><em><tt>last_token</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>must
be the numeric code for the <a href="symbols.html">type of token</a>
it has just found, or 0 for end-of-input. When a token is
referred to in the grammar rules by a name, that name in the
parser file becomes an integer constant whose value is the proper
numeric code for that token type. So <font color="#008080"><em><tt>read_token</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>can
use the name to indicate that type. When a token is referred to
in the grammar rules by a character literal, the numeric code for
that character is also the code for the token type. So <font
color="#008080"><em><tt>read_token</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>can
simply return that character code. The null character must not be
used this way, because its code is zero and that is what
signifies end-of-input. Here is an example showing these things:</p>

<blockquote>
    <pre><font color="#008080"><em>read_token</em>
        -- Read a token from input stream.
        -- Make result available in <em>last_token</em>.
    <em><strong>local</strong></em>
        <em>c</em>: <em>CHARACTER</em>
    <em><strong>do</strong></em>
        ...
        <em><strong>if</strong></em> <em>c</em> = <em>EOF</em> <em><strong>then</strong></em>
                -- Detect end of file.
            <em>last_token</em> := <em>0</em>
        <em><strong>elseif</strong></em> <em>c</em> = <em>'+' </em><em><strong>or</strong></em><em> c</em> = <em>'-' </em><em><strong>then</strong></em>
                -- Assume token type for `+' is '+'.
            <em>last_token</em> := <em>c.code</em>
        <em><strong>elseif</strong></em> ... <em><strong>then</strong></em>
                 -- Return the type of the token.
             <em>last_token</em> := <em>INT</em>
        <em><strong>else</strong></em>
            ...
        <em><strong>end</strong></em>
    <em><strong>end</strong></em></font></pre>
</blockquote>

<p>This interface has been designed so that the output from the <a
href="http://www.gobosoft.com/eiffel/gobo/gelex/index.html"><em>gelex</em></a> utility can be used
without change as the definition of <font color="#008080"><em><tt>read_token</tt></em></font>.</p>

<p>When scanning the input stream for a
new token, <font color="#008080"><em><tt>read_token</tt></em></font>
must update the semantic value of the token being read in feature
<font
color="#008080"><em><tt>last_&lt;type&gt;_value</tt></em></font> which is an
attribute generated by <em>geyacc</em> next to the token-type integer
constant definitions.</p>

<p>Class <font color="#008080"><em><tt>YY_PARSER</tt></em></font>
is equipped with a procedure <a href="skeleton.html#make"
name="make"><font color="#008080"><em><tt>make</tt></em></font></a>
which initializes the parser. This routine should be used as
creation routine in descendant classes. Also available to
descendants of <font color="#008080"><em><tt>YY_PARSER</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>are
a set of features which can be called from the <a
href="actions.html">semantic actions</a>. An implementation for
most of these routines is provided in class <font color="#008080"><em><tt>YY_PARSER_SKELETON</tt></em></font>.</p>

<p><em>Geyacc</em> does not automatically generate the note,
class header, formal generics, obsolete, inheritance, creation
and invariant clauses. These have to be specified in <a
href="description.html#eiffel_declarations">Eiffel declarations</a>
in the first section and in the <a
href="description.html#user_code">user code section</a> of the
parser <a href="description.html">description file</a>. The
following example shows a typical parser description file:</p>

<blockquote>
    <pre><font color="#0000FF">%{</font>
<font color="#008080"><em><strong>class</strong></em><em> MY_PARSER

</em><em><strong>inherit</strong></em><em>

    YY_PARSER_SKELETON
        </em><em><strong>rename</strong></em><em>
            make <strong>as</strong> make_parser_skeleton
        </em><em><strong>redefine</strong></em><em>
            report_error
        </em><em><strong>end</strong></em><em>

    MY_SCANNER
        </em><em><strong>rename</strong></em><em>
            make <strong>as</strong> make_scanner
        </em><em><strong>export</strong></em><em>
            </em>{<em>NONE</em>}<em> </em><em><strong>all</strong></em><em>
        </em><em><strong>end</strong></em><em>

</em><em><strong>create</strong></em><em>

    make</em></font>
<font color="#0000FF">%}</font></pre>
    <pre><font color="#0000FF">%token</font> ...

<font color="#0000FF">%%</font>

<font color="#FF0000">...rules...</font>

<font color="#0000FF">%%</font>

<font color="#008080"><em><strong>feature</strong></em><em> </em>{<em>NONE</em>} -- Initialization<em>

    make
            </em>-- Create a new parser.<em>
        </em><em><strong>do</strong></em><em>
            make_scanner
            make_parser_skeleton
            </em><em><strong>create</strong> error_messages.make
        </em><em><strong>end</strong></em><em>

</em><em><strong>feature</strong></em><em> </em>-- Access<em>

    error_messages: LINKED_LIST </em>[<em>STRING</em>]<em>
            </em>-- Error messages reported so far<em>
</em><em><strong>
feature</strong></em><em> </em>{<em>NONE</em>} -- Error<em> </em>reporting<em>

    report_error </em>(<em>a_message</em>:<em> STRING</em>)
            -- S</em>tore error message in<em> error_messages.
        </em><em><strong>do</strong></em><em>
            error_messages.extend </em>(<em>a_message</em>)<em>
        </em><em><strong>end</strong></em><em>

</em><em><strong>invariant</strong></em><em>

    error_messages_not_void</em>:<em> error_messages </em>/=<em> Void

</em><em><strong>end</strong></em><em> </em></font></pre>
</blockquote>

<p>The generated parser class, named <font color="#008080"><em><tt>MY_PARSER</tt></em></font>,
inherits its lexical analyzer features (<font color="#008080"><em><tt>read_token</tt></em></font>
and <font color="#008080"><em><tt>last_token</tt></em></font>) from class <font
color="#008080"><em><tt>MY_SCANNER</tt></em></font> which has
probably been generated using <a href="http://www.gobosoft.com/eiffel/gobo/gelex/index.html"><em>gelex</em></a>.
The routine <font color="#008080"><em><tt>report_error</tt></em></font>,
inherited from <font color="#008080"><em><tt>YY_PARSER_SKELETON</tt></em></font>,
has been redefined to keep track of the error messages reported
so far. Finally, the creation routine <font
color="#008080"><em><tt>make</tt></em></font> initializes the
lexical analyzer and the parser and makes sure that the invariant
is preserved.</p>

<hr size="1">

<table border="0" width="100%">
    <tr>
        <td><address>
            <font size="2"><b>Copyright © 1998-2019</b></font><font
            size="1"><b>, </b></font><font size="2"><strong>Eric
            Bezault</strong></font><strong> </strong><font
            size="2"><br>
            <strong>mailto:</strong></font><a
            href="mailto:ericb@gobosoft.com"><font size="2">ericb@gobosoft.com</font></a><font
            size="2"><br>
            <strong>http:</strong></font><a
            href="http://www.gobosoft.com"><font size="2">//www.gobosoft.com</font></a><font
            size="2"><br>
            <strong>Last Updated:</strong> 25 September 2019</font><br>
            <!--webbot bot="PurpleText"
            preview="
$Date$
$Revision$"
            -->
        </address>
        </td>
        <td align="right" valign="top"><a
        href="http://www.gobosoft.com"><img
        src="image/home.gif" alt="Home" border="0" width="40"
        height="40"></a><a href="index.html"><img
        src="image/toc.gif" alt="Toc" border="0" width="40"
        height="40"></a><a href="actions.html"><img
        src="image/previous.gif" alt="Previous" border="0"
        width="40" height="40"></a><a href="options.html"><img
        src="image/next.gif" alt="Next" border="0" width="40"
        height="40"></a></td>
    </tr>
</table>
</body>
</html>
