File: parser.html

package info (click to toggle)
gobo 1.5-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,728 kB
  • ctags: 13,070
  • sloc: ansic: 85,961; lex: 2,758; yacc: 2,298; sh: 1,464; makefile: 64
file content (272 lines) | stat: -rw-r--r-- 13,215 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!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 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> <em><strong>is</strong></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="../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><font color="#008080"><em><tt>YY_PARSER</tt></em></font> is
actually a generic class whose generic parameter specifies the <a
href="actions.html#value_types">type</a> of the <a
href="introduction.html#semantic_values">semantic values</a>
associated with each token. 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
<a href="actions.html#last_value"><font color="#008080"><em><tt>last_value</tt></em></font></a>.
As for <font color="#008080"><em><tt>read_token</tt></em></font>
and <font color="#008080"><em><tt>last_token</tt></em></font>, <font
color="#008080"><em><tt>last_value</tt></em></font> is a deferred
feature for which you must provide an implementation.</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 indexing,
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" size="3">%{</font><font size="3">
</font><font color="#008080" size="3"><em><strong>class</strong></em><em> MY_PARSER

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

    YY_PARSER_SKELETON </em>[<em>INTEGER</em>]<em>
        </em><em><strong>rename</strong></em><em>
            make as 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 as 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>creation</strong></em><em>

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

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

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

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

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

    make </em><em><strong>is</strong></em><em>
            </em>-- Create a new parser.<em>
        </em><em><strong>do</strong></em><em>
            make_scanner
            make_parser_skeleton
            </em>!!<em> 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>)<em> </em><em><strong>is</strong></em><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>-- class MY_PARSER</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>,
<font color="#008080"><em><tt>last_token</tt></em></font> and <font
color="#008080"><em><tt>last_value</tt></em></font>) from class <font
color="#008080"><em><tt>MY_SCANNER</tt></em></font> which has
probably been generated using <a href="../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. Since the generic parameter of class <font
color="#008080"><em><tt>YY_PARSER_SKELETON</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>is <font
color="#008080"><em><tt>INTEGER</tt></em></font>, the semantic
values of the tokens are integers. 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</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> 21 May 1998</font><br>
            <!--webbot bot="PurpleText"
            preview="
$Date: 1999/06/12 18:56:56 $ 
$Revision: 1.10 $"
            --> 
        </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>