Implementation of a compiler for Gauss language*.
Abstract.
The implementation of this compiler was made as a course project at the CINVESTAV-IPN in 1987. The work was translate the Gauss compiler from PL / I to Pascal, the source code (PL / I) was completely taken from the book "An Implementation Guide to compiler writing" by Jean-Paul Tremblay and Paul G . Sorenson. The compiler is totally didactic and is used by students of the Department of Computer Science at the University of Saskatchewan in Canada, according to the authors of the book. The advantage was achieved with this work is obvious, the compiler written in Pascal is more accessible than written in PL / I, and therefore can be widely used as a teaching tool.
Introduction.
In an engineering course on computer systems such as compilers, the goal is to understand the structure of a compiler, the relation of language to translate with grammar we know is using. And to establish a method of implementing. This means, first understand how to represent and define the grammar of a language by means of a theory which is not so difficult but very singular, then give out the groundwork for handling strings and learn the representation of the operations can do with these strings. Once given the above is to analyze case studies of implementation of some of the phases of the compiler.
I have observed that in other areas of computing, in one of its texts brings teaching material consisting of a source code program, which is very susceptible to analyze. Reflecting does the why? This is easy to explain, materials normally a program of this type is not easy to design it and deploy it in a semester and is unlikely to be achieved in two semesters. This material allows the student to have a clearer idea of what constitutes an implementation of this type. In turn allows detailed analyze, modify and optimize it if your capacity will allow it. I'm talking about the case of operating systems.
On the subject of compilers I have not noticed any textbook that includes a material such as the case described. It is important to make clear this deficiency. But, it does not mean that once you have this material the problem is resolved. It's when the most important work begins. The to make the student learn with this new material. This in turn brings about a work that is hidden, and it is design or modify the contents of the field.
There is a textbook with a didactic compiler (Treblay, 1982).. This compiler is originally written in PL / I. However, as this language is not everyday I translated completely to Pascal during a visit I made at the CINVESTAV-IPN in 1987. Since then envision the possibility of the usefulness of this work. However one thing is the technical work and the other is the teaching work and still more work would involve the implementation of a new curriculum for the subject project. Although ten years have passed I have not seen a didactic compiler is implemented as with operating systems such as LINUX or MINIX.
Description of activities to implement the gauss language compiler.
The compiler is originally written in PL / I language and translated to pascal (TurboPascal) (Borland, 1985). There were some changes needed to achieve the correct implementation of the compiler, for example:
In the lexical analyzer (scanner) also made some major changes as:
The code that handles the machine that made the original implementation is EBCDIC, and this part of the compiler is a table where characters are mapped in such a code, but as we now use the ASCII code mapping the table according to the current code and thus the scanner when it finds a character, it branches to the correct state, marked by a deterministic finite automaton that simulates the scanner.
In this same procedure is simulated a statement Case with goto's because in PL / I does not exist but to translate this statement to pascal no need to simulate because here it is implemented the Case. In the routine llparse was originally a constant table called ll1parse which contains the LL(1) parser table (parse) as generated from the grammar of gauss language, however the decision was made to keep this table in a file , since it is a fairly large and if it is declared as constant so much object code is generated, and if left as-is it saturates the turbo-pascal object code segment completely before compiling the program so marked error memory overflow. In view of the problem it was decided to declare it as a variable and load the table from disk, as if it were a data file, thus taking secondary memory space. The program that generates the file containing the parse table is called: PARSETAB.PAS and file it generates: parsetab.doc. In general the compiler written in PL / I, the procedures are declared as soon as needed, so it does not follow a range of procedures structure such as in pascal therefore to translate it into pascal had to adapt to the format pascal program. Below is how this procedures declared in PL / I:
compile
init
scanner
get_char
option
get_num
is_kywd
ll_parse
s_push
s_pop
err_hand
fix_up
stack_pr
lookup
sym_rem
action
store_lit
template
relation
code_fix
bin_op
split
unary_op
execution_phase
sem_err
error
list_err
llerror
token_pr
Now will detail how they are declared the same procedures but pascal language:
compile
init
error
token_pr
llerror
list_err
sem_err
lookup
sym_rem
action
store_lit
template
code_fix
relation
bin_op
split
unary_op
format_lit
scanner
get_char
option
get_num
is_kywd
llparse
s_push
s_pop
err_hand
fixup
stack_pr
Distribution program source code.
The compiler provides the following source files:
Gauss.pas.- Compiler main program that has included the following:
Procedu1.pas
Procedu2.pas
Procedu3.pas
Scanner.pas
Llparse.pas
Llparse1.pas
Gauss.pas program uses a data file called:
Parsetab.dat
Gauss.pas program produces a data file called:
Code.out
The Interp.pas program corresponds to the interpreter's main program which has included the following programs:
Resint.pas
Inter1.pas
Operating the compiler Gauss.
A gauss program can be edited in any plain text editor to compile it later. The way it should run the compiler is:
C:> gauss < name>
Where:
name.- The file containing the gauss program to compile.
To better visualize the processes performed by the compiler, there are several compile options, which are always declared in the first line of the program to compile. Some of the options are:
scanner_debug:
This option is designed to help debug the scanner. Each time the scanner is called, are displayed the internal value of the token and the token type that is invoked.
ll1_debug :
Which causes it to be displayed on the screen as follows:
The content of the parse stack.
The input token
The parse actions (expansion, pop, error, accept)
code_debug :
When this option is enabled the generated code is deployed.
Conclusion.
From the educational point of view, not enough to create such material for a course, it is necessary to develop learning activities such material for very specific engineering courses. Since many of the time the material exists but either not used or not known to exist. Consequently the potential of these tools are being wasted unnecessarily.
Bibliography.
Tremblay, Jean-Paul, Paul G. Sorensen. “THE THEORY AND PRACTICE OF COMPILER WRITING. First Edition. Edit. Mc Graw-Hill. Singapore, 1985.
Tremblay, Jean-Paul, Paul G. Sorensen. “AN IMPLEMENTATION GUIDE TO COMPILER WRITING. Edit. Mc Graw-Hill. Estados Unidos, 1982.
Borland International Inc., “TURBO PASCAL v 3.0 REFERENCE MANUAL”. Scotts Valley, CA. Estados Unidos, 1985.
* This work was presented and published at Congreso Interuniversitario de Electrónica, Computación y Eléctrica - CIECE ’98, Durango, México.
Last update / Gustavo Arroyo