UNIVERSITY OF WEST ATTICA
SCHOOL OF ENGINEERING
DEPARTMENT OF COMPUTER ENGINEERING AND INFORMATICS
Compilers
Vasileios Evangelos Athanasiou
Student ID: 19390005
Georgios Theocharis
Student ID: 19390283
Ioannis Iliou
Student ID: 19390066
Pantelis Tatsis
Student ID: 20390226
Vasileios Dominaris
Student ID: 21390055
Supervisor: Michalis Iordanakis, Special Technical Laboratory Staff
Athens, May 2024
This project involves the development of a compiler for Uni-C, a subset of the C programming language. The implementation was completed in three distinct phases, covering the fundamental stages of compiler construction:
-
Finite State Machine (FSM) Encoding
Design and simulation of automata for recognizing lexical units. -
Lexical Analysis (FLEX)
Development of a lexical analyzer that identifies tokens using regular expressions. -
Syntactic Analysis (BISON)
Construction of a parser that validates program structure based on predefined grammar rules.
| Section | Folder | Description |
|---|---|---|
| 1 | A-FLEX/ |
Lexical analysis phase using Finite State Machines and FLEX |
| 1.1 | A-FLEX/A2-FSM/ |
FSM design and implementation for Uni-C tokens |
| 1.1.1 | A-FLEX/A2-FSM/docs/ |
FSM theory notes, transition tables, and documentation (PDF/XLSX) |
| 1.1.2 | A-FLEX/A2-FSM/src/ |
FSM source files for identifiers, strings, numbers, comments, and whitespace |
| 1.2 | A-FLEX/A3-FLEX/ |
FLEX-based lexical analyzer implementation |
| 1.2.1 | A-FLEX/A3-FLEX/docs/ |
FLEX code documentation |
| 1.2.2 | A-FLEX/A3-FLEX/src/ |
FLEX source code, Makefile, input/output samples |
| 1.3 | A-FLEX/assign/ |
Assignment descriptions for Part A (FSM & FLEX) |
| 2 | B-BISON/ |
Syntax analysis phase using BISON |
| 2.1 | B-BISON/assign/ |
Assignment descriptions for Part B (BISON) |
| 2.2 | B-BISON/B2-FLEX-BISON/ |
Combined FLEX & BISON parser implementation |
| 2.2.1 | B-BISON/B2-FLEX-BISON/src/ |
Integrated lexer/parser source code and build files |
| 2.3 | B-BISON/B3-COMPILER/ |
Final compiler stage |
| 2.3.1 | B-BISON/B3-COMPILER/docs/ |
BISON grammar documentation |
| 2.3.2 | B-BISON/B3-COMPILER/src/ |
Final Uni-C compiler source code |
| 3 | Uni-C/ |
Language specification and usage guide for Uni-C |
The compiler recognizes the following categories of tokens:
-
Identifiers
Names for variables and functions- Pattern:
[a-zA-Z_][a-zA-Z0-9_]{0,31}
- Pattern:
-
Keywords
Reserved words such as:if,else,while,int,return,func
-
Constants
Supported constant types include:- Integers (decimal, octal, hexadecimal)
- Floating-point numbers
- Strings
-
Operators
- Arithmetic:
+,-,*,/ - Relational:
>,<,== - Logical:
&&,||
- Arithmetic:
-
Delimiters
- Characters such as
;used to separate commands
- Characters such as
For each token category, a Finite State Automaton (FSA) was designed.
Example – Identifiers:
- Starts at an initial state (SZ)
- Transitions to a middle-character state (SMCH) upon receiving a letter or underscore
- Reaches a GOOD exit state upon encountering a newline, provided the identifier is valid
The BISON parser generator is used to define and enforce grammar rules for Uni-C programs:
-
Variable Declarations
- Support for simple data types and arrays
-
Functions
- Recognition of both built-in and user-defined functions
-
Expressions
- Handling of simple and compound expressions
-
Error Handling
- Detection and reporting of syntax errors
- Handling of invalid tokens (
TOKEN ERROR)
-
1_identifiers.fsm
FSM encoding for identifier recognition -
simple-flex-code.l
FLEX source file containing regular expressions and token definitions -
token.h
Header file defining numeric constants for tokens -
simple-bison-code.y
BISON source file containing grammar and syntax rules
Before compiling, ensure the required tools are installed.
sudo apt update
sudo apt install gcc flex bison makegcc --version
flex --version
bison --versionClone the repository
git clone https://github.com/Compilers-aka-Uniwa/Compiler-Uni-C.gitNavigate to project directory for testing the final version of Compiler
cd Compiler-Uni-C/B-BISON/B3-COMPILER/srcAlso
Navigate to project directory for testing FSM
cd Compiler-Uni-C/A-FLEX/A2-FSM/srcNavigate to project directory for testing Flex
cd Compiler-Uni-C/A-FLEX/A3-FLEX/srcNavigate to project directory for testing Bison
cd Compiler-Uni-C/B-BISON/B2-FLEX-BISON/srcDirectory
A2-FSM/src
cd A2-FSM/src
gcc fsm.c -o fsm./fsm- FSM definitions are loaded from
.fsmfiles (e.g.1_identifiers.fsm,Final.fsm). - Transition tables are documented in the accompanying PDF and Excel files.
- Navigate to the
A-FLEX/A2-FSM/docs/directory - Open the report corresponding to your preferred language:
- English:
Finite-State-Machines.pdf - Greek:
Πεπερασμένα-Αυτόματα.pdf
- English:
Directory
A3-FLEX/src
cd A3-FLEX/src
makeflex simple-flex-code.l
gcc lex.yy.c -o flex_app./flex_app < input.txtoutput.txt
- Navigate to the
A-FLEX/A3-FLEX/docs/directory - Open the report corresponding to your preferred language:
- English:
Flex-Code.pdf - Greek:
Κώδικας-Flex.pdf
- English:
Directory
B3-COMPILER/src
cd B3-COMPILER/src
makebison -d simple-bison-code.y
flex simple-flex-code.l
gcc simple-bison-code.tab.c lex.yy.c -o bison_app./bison_app < input.txtoutput.txt
- Navigate to the
B-BISON/B3-COMPILE/docs/directory - Open the report corresponding to your preferred language:
- English:
Bison-Code.pdf - Greek:
Κώδικας-Bison.pdf
- English:
Directory
B2-FLEX-BISON/src
cd B2-FLEX-BISON/src
makebison -d simple-bison-code.y
flex simple-flex-code.l
gcc simple-bison-code.tab.c lex.yy.c -o compiler./compiler < input-test.txt./compiler < input-final.txtoutput.txt
- Each module provides its own
Makefilefor convenience. - If execution permission is missing:
chmod +x <executable>- The project has been tested on Linux (Ubuntu).
- For clean builds (when supported):
make clean-
command not found: flex / bisonEnsure the required packages are installed. -
Linker errors Re-run
make cleanand rebuild the project. -
Unexpected output Verify the correct input file is used and matches the grammar rules.
