This project is a mini-compiler developed as part of the Compilation module (2CS SL - ESI Algiers). It implements the main stages of the compilation process for a language created specifically for this purpose: CIPL (Compilation Introductory Programming Language), inspired by C and Pascal.
- DJABRI Maroua
- BOUYAHIAOUI Meriem
- KASMI Ayyoub
- BOUCHOUCHA Mohammed ElAmine
- Flex: for lexical analysis
- Bison: for syntax and semantic analysis
- C language: for implementing the compiler
- Program Structure: Entry point via the main function
- Declarations: Global and local variables, functions and procedures
- Supported Types: int, float, double, char, bool, and arrays (int[])
- Basic Instructions:
- Assignment:
= - Conditionals:
if,else - Loops:
for,while,do-while - I/O:
read(),print()
- Assignment:
- Operators:
- Arithmetic:
+,-,*,/,% - Comparison:
==,!=,>,<,>=,<= - Logical:
&&,||,!
- Arithmetic:
- Comments: Support for comments in the code
- Operator Precedence: Defined in the grammar for correct evaluation order
- Regular expressions defined for each token
- Detection of lexical errors with line and column numbers
- Token return to Bison
- Handles comments and invalid strings
- Supports identifiers, literals, operators, symbols
- Full grammar defined in LALR form
- Recursive descent used for some subsets (e.g., comparisons)
- Ambiguity resolution (e.g., dangling else, left recursion)
- Handles blocks, conditions, loops, and expressions
Semantic analysis ensures that the source program is logically and type-consistent.
- Variable Declaration: Every variable must be declared before use
- Scope: A variable is visible only within the block where it is declared
- Type Compatibility: Assignments and operations must match operand types
- Functions: The return type of a function must match its declaration
- Quadruple Abstract Machine: Generates intermediate code in the form of quadruples (e.g.,
(op, arg1, arg2, result)) - Stack: Manages labels required for control structures (
if,while, etc.) - Union: Used in Bison to manage token types (values returned by Flex)
- Insertion: Adds symbols with metadata (name, type, scope, line, column, etc.)
- Lookup: Checks if a symbol has already been declared using
lookupSymbol()
- Automatic symbol insertion during declarations
- Type checking for:
- Assignments (e.g.,
x = 3) - Expressions (e.g.,
x + y,a && b) - Conditions (e.g.,
if (x > 2))
- Assignments (e.g.,
- Control structure validation: Ensures that conditions in
if,while, etc., are of typebool
function sum(int a, int b): int {
return a + b;
}
main {
int x = sum(2, 3);
print(x);
}<conditional_statement> ::= IF ( <expression> ) <body> <else_part>
<else_part> ::= ELSE <body> | ε