C Compiler
Abstract
This compiler realizes the translation of a large subset of ANSI
C (I estimate more than 80%) to the assembly language of the
Motorola 68000 family. It recognizes all control instructions
such as if, while, etc.; all expressions elements except +=, *=
and other similar assignement operators; functions, struct, typedef etc. A preprocessor
is not yet implemented, float and double are also not implemented.
The general structure is derived from a tutorial by
J.Crenshaw
on compiler design. I started developing my
compiler from his original work that included a tiny compiler
of an hybrid language.
I chose to start from Crenshaw's work because of the quality of his software. The
internal organization of the code is divided in three main parts: parser, scanner and
code generation. Each section is composed by a large number of functions (in the order
of 50). This fact makes it very comprehensible so that it was much easier to develop
new parts or to modify existing ones. Throughout my effort I strove to maintain the
style of the original work.
The parser reads input file, discards comments and breaks input lines into tokens. I've
inserted also a few tools for output. The parser is quite independent from the
recognized language (C) and from the target system.
The scanner recognizes C syntax and semantics.To describe it I use the BNF formalism
(an equvalent of context-free grammars). Each production is translated by a main
function which calls a function for each non-terminal symbol plus same utilities.
I clarify this concept with an example:
The arithmetical expressions (just a subset of C expressions) are described as:
<arith-expression> ::= <term> [<add-op> <term>]*
This part is translated by this (simplified) code:
int arithexpr();
{
int typ;
typ = term();
while (isaddop(token)) {
push(typ);
switch(token) {
case '+': typ = add(typ); break;
case '-': typ = sub(typ); break;
}
}
return typ;
}
Right now I'm working to make my compiler an auto-compiler, the next step will be to fully
comply to ANSI standard.
Possible future development of the described work would consist in adding optimization,
debugging and other feature that are currently missing.
Back to Lucio's Home page.