SILCompiler is a simple, iterative language compiler designed to process a basic imperative programming language with constructs such as assignments, conditionals, loops, procedures, and basic arithmetic operations. This project is built using Lex and Yacc (Flex and Bison) tools to tokenize and parse the source code and generate meaningful results.
- Arithmetic operations: Addition, subtraction, multiplication, division, and modulus.
- Conditionals:
if,else, andendif. - Loops:
while,repeat until, andfor. - Procedures: Support for defining and calling procedures with parameters.
- Declarations: Declare variables, including arrays.
The compiler consists of two main components:
- Lexer: Written in Flex (lexer.l) - Tokenizes the input code into a sequence of tokens.
- Parser: Written in Bison (parser.y) - Defines the syntax rules and parses the token sequence to construct an Abstract Syntax Tree (AST).
The lexer file defines the regular expressions for various tokens in the language. Some of the key tokens include:
- Keywords:
PROCEDURE,IS,PROGRAM,IF,THEN,ELSE,WHILE,DO, etc. - Arithmetic operators:
+,-,*,/,%. - Relational operators:
=,!=,>,<,>=,<=. - Special symbols:
:,=,;,(,),[,],,.
The parser defines the grammar rules for the language, including constructs such as:
- Program structure: Programs start with
PROGRAM ISand end withEND. - Commands: Includes assignments, conditionals, loops, procedure calls, and I/O operations like
READandWRITE. - Expressions: Support for simple expressions with operands and operators.
- Declarations: Supports variable declarations and array declarations.
The project comes with a Makefile to simplify the build process. To build the compiler, follow these steps:
-
Install dependencies:
- Install
flex(for lexing) andbison(for parsing).- On Ubuntu/Debian:
sudo apt install flex bison - On macOS (using Homebrew):
brew install flex bison
- On Ubuntu/Debian:
- Install
-
Build the project: Simply run
makein the project directory. This will generate the lexer and parser, then compile the C++ files into an executable.make
This command will:
- Run
bisonto generate the parser (parser.cppandparser.hpp). - Run
flexto generate the lexer (lexer.cpp). - Compile the
parser.cpp,lexer.cpp, and link them into the final executable (parse).
- Run
-
Clean the project: To clean the generated files, run:
make clean
This will remove the generated source files and the executable.
-
Run the compiler: After building the project, run the compiler on your source file:
./parse input_file.imp
Replace
input_file.impwith your source code in the Simple Imperative Language format.
Here’s an example of a simple program in SIL (Simple Imperative Language):
PROGRAM IS
PROCEDURE sum IS
DECLARE x, y
x := 10;
y := 20;
WRITE x + y;
END
BEGIN
sum();
END
Other example programs and whole language specification can be found in materiały directory.
This project is licensed under the Apache License 2.0.
The language and virtual machine used in this compiler were developed by Dr. hab. Maciej Gebala, a tutor at Wroclaw University of Science and Technology.