top of page

The project created for this module was a compiler for a functional programming language (a subset of the language Modula-2 called Model) using Java. The source code for the project can be downloaded here.

Functional Language Compiler

The project aimed to build a personalised version of the Model compiler. The compiler runs as a Java program and reads in a Model program file to compile. The program then prompts the user to specify the names to be given to the output files. The 2 files that are output are a listing file and an object code file. The listing file shows the results of the compilation itself. It outputs the code read in via the Model file, prepended with appropriate line numbers. For any compilation errors, an arrow will be printed below the relevant point where the error is encountered on a line, along with the error number that has been encountered. There are 73 different errors, which can be viewed here. The file ends by outputting the amount of errors encountered in the file during compilation. The object code file outputs the assembly code generated by translating the compiled code from the Model program. This code can then be read by the machine on the underlying architecture, to execute the Model program itself. The assembly code will not be generated if the code does not compile and there are errors present in the listing file, although the object code file will still be generated.

The compiler code used already had certain functionality in place, and could handle of assignment statements, loop statements, for statements and case statements. The listing file and object code file generated were updated to include extra information as a header. The object code file gives the date and time that it was generated, whereas the listing file gives the date and time as well as the student (i.e. my) name and student number. The compiler was updated to include a number of language extensions for the Model programs. Firstly, identifiers were extended to be able to allow the characters “&”, “$” and “#”. Then comments, delineated by “<*” and “*>” were added, along with 2 additional forms of repetitive statement; the while statement and the do statement. Syntax analysis was implemented in the program for the new repetitive statements in order to recognise them, as well as syntax error recovery to incorporate the appropriate error recovery actions for the new statements. The semantic analyser in the compiler was also updated to carry out the semantic checks necessary when compiling the new statements. Finally, extensions were added to the compiler to implement the actions necessary to generate the assembly code needed to represent the repetitive statements. With all these extensions implemented, the repetitive statements were added to the compiler, allowing them to be used within Model programs when read into the compiler program. Accordingly, the Model test program that can be downloaded here along with the source code and tested with the compiler contains while and do repetitive statements.

bottom of page