I had to make a project last month for the Compiler Techniques course at the university where I study. This project was to make a simple Pascal compiler for an imaginary virtual machine. I also work for a software company so I had to make the project in my free time after university and work, so in some places the code is made in a rush (you know what I mean). Anyway, this program is a nice compiler sample, and for people who are interested, might me a good source of ideas.
The program is a multi document/view based application. It supports 3 types of files:
- *.rez, a token result file that is generated after parsing the source code
- *.asm, an assembler file containing virtual machine code, that is generated by the compiler together with the binary file
- *.pas, Pascal source files
This sample was made using Visual C++ v 6.0, and contains 2 projects: the compiler and the virtual code interpreter.
General StructureI used the following class hierarchy:
CObject -> CStringTokenizer -> CPascalLexAnalyzer -> CPascalCompiler
I tried to modularize the compiling process so that the reusability of the classes would be higher. For example, the CStringTokenizer class can be used for tokenizing strings and command line parameters and can be used to make a C Lexical analyzer, etc. The CPascalLexAnalyzer is used to tokenize the Pascal source, and the CPascalCompiler is responsible for making syntactical and semantically verifications and for the actual compilation.