<
HTML>
<
HEAD>
<
TITLE>Using the ACCENT Compiler Compiler</
TITLE>
</
HEAD>
<
BODY bgcolor=
"white">
<
TABLE cellspacing=20>
<
TR>
<
TD valign=
"top">
<
img src=
"logo.gif">
</
TD>
<
TD valign=
"bottom" align=
"left">
<a href=
"index.html">The Accent Compiler Compiler</a>
<
h1>Using the ACCENT Compiler Compiler</
h1>
</
TD>
</
TR>
<
TR>
<
TD align=
"right" valign=
"top">
<!-- MENU -->
<
font face=
"helvetica">
<a href=
"index.html">Accent</a><
br>
<a href=
"overview.html">Overview</a><
br>
<a href=
"tutorial.html">Tutorial</a><
br>
<a href=
"language.html">Language</a><
br>
<a href=
"installation.html">Installation</a><
br>
Usage<
br>
<a href=
"lex.html">Lex</a><
br>
<a href=
"algorithms.html">Algorithms</a><
br>
<a href=
"distribution.html">Distribution</a><
br>
</
font>
</
TD>
<
TD valign=
"top">
<!--- begin main content -->
<h3>How To Generate a Language Processor</h3>
<
table>
<
tr>
<
td bgcolor=
"#e9e9d2">
<
pre>
spec.acc spec.lex auxil.c art.o
| | | |
| | | |
V V | |
+------+ +---+ | |
|ACCENT| |LEX| | |
+------+ +---+ | |
| | | |
| | | |
V V | |
yygrammar.h lex.yy.c | |
yygrammar.c | | |
| | | |
+------------+--+------+--------+
|
V
+--+
|CC|
+--+
|
|
V
calculator
</
pre>
</
td>
</
tr>
</
table>
<h3>Accent</h3>
<i>Accent</i> is invoked with the
command
<
pre>
accent file
</
pre>
This reads the grammar in <
tt>file</
tt>
and generates the
output files
<
tt>yygrammar.h</
tt> and
<
tt>yygrammar.c</
tt>.
<p>
<
tt>yygrammar.h</
tt> contains definitions of token codes
and a definition of the type <
tt>YYSTYPE</
tt> (which is only
effective if there is no previous definition specified by the user).
<p>
<
tt>yygrammar.c</
tt> contains the grammar encoding and a generated
tree walker that performs the semantic actions specified by the user.
<p>
Example:
<
pre>
accent spec.acc
</
pre>
The file
<a href=
"spec.acc">spec.acc</a>
contains the <i>Accent</i> specification of a simple desk calculator.
<h3>Lex</h3>
The scanner should be generated with <i>Lex</i>.
<i>Lex</i> is invoked by the
command
<
pre>
lex file
</
pre>
which reads the specification in <
tt>file</
tt>,
a
table of regular expressions and corresponding actions,
and generates a file <
tt>yy.lex.c</
tt>.
This file contains the function <
tt>yylex()</
tt>,
the lexical analyzer which is invoked by the parser.
<p>
Example:
<
pre>
lex spec.lex
</
pre>
The file
<a href=
"spec.lex">spec.lex</a>
contains the <i>Lex</i> specification
for the desk calculator.
<h3>Auxiliary Functions</h3>
The user has to prepare some auxiliary functions:
<p>
There should be a <
tt>main()</
tt> function that invokes
the parser function <
tt>yyparse()</
tt>.
<p>
<
tt>yyerror(char *msg)</
tt>
is invoked by the parser when an error is detected. It should print
the text <
tt>msg</
tt> and perhaps the current line number.
<p>
The scanner needs a function <
tt>yywrap()</
tt>
(it can be used to switch to a further
input file).
In its simplest
form it just returns <
tt>1</
tt>
(indicating no further
input file).
<p>
In our example, the file
<a href=
"auxil.c"><
tt>auxil.c</
tt></a>
defines these functions.
<h3>Accent Runtime</h3>
To create a functioning parser one also has to supply
the <i>Accent Runtime</i>.
This is contained in the file <
tt>art.o</
tt> in the
directory <
tt>art</
tt> of the distribution.
<h3>Compiling and Linking</h3>
The <i>C</i> compiler is used
to compile the sources and create the
object program.
<p>
Example:
<
pre>
cc -o calculator yygrammar.c lex.yy.c auxil.c $ART
</
pre>
(where <
tt>$ART</
tt> refers to the Accent runtime).
<p>
The file
<a href=
"build"><
tt>build</
tt></a>
contains a shell
script to produce and run the desk calculator.
<p>
Invoking the optimizer during compilation increases parser speed
by a factor of two.
For the GNU C compiler you should specify <
tt>-O3</
tt> as an
option
when compiling the Accent runtime and the generated program.
<!--- end main content -->
<
br>
<
br>
<
font face=
"helvetica" size=
"1">
<a href=
"http://accent.compilertools.net">accent.compilertools.net</a>
</
font>
</
TD>
</
TR>
</
TABLE>
</
BODY>
</
HTML>