Skip to content
 
 

Repository files navigation

🛠️ CIPL Compiler - Mini Compilation Project

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.

👨‍💻 Team Members

  • DJABRI Maroua
  • BOUYAHIAOUI Meriem
  • KASMI Ayyoub
  • BOUCHOUCHA Mohammed ElAmine

🔧 Tools Used

  • Flex: for lexical analysis
  • Bison: for syntax and semantic analysis
  • C language: for implementing the compiler

🧠 Main Features

1. Definition of the CIPL Language

  • 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()
  • Operators:
    • Arithmetic: +, -, *, /, %
    • Comparison: ==, !=, >, <, >=, <=
    • Logical: &&, ||, !
  • Comments: Support for comments in the code
  • Operator Precedence: Defined in the grammar for correct evaluation order

2. Lexical Analysis (Flex)

  • 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

3. Syntax Analysis (Bison)

  • 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

4. Semantic Analysis

Semantic analysis ensures that the source program is logically and type-consistent.

🧾 Semantic Rules

  • 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

🔧 Utility Modules

  • 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)

🗃️ Symbol Table

  • Insertion: Adds symbols with metadata (name, type, scope, line, column, etc.)
  • Lookup: Checks if a symbol has already been declared using lookupSymbol()

⚙️ Semantic Routines

  • 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))
  • Control structure validation: Ensures that conditions in if, while, etc., are of type bool

✅ Compilation Example

function sum(int a, int b): int {
  return a + b;
}

main {
  int x = sum(2, 3);
  print(x);
}

📘 Sample Grammar (BNF)

<conditional_statement> ::= IF ( <expression> ) <body> <else_part>
<else_part> ::= ELSE <body> | ε

About

CIPL Compiler is a mini-compiler developed as part of the Compilation module at the Higher School of Computer Science (ESI Algiers), 2CS SL level. It implements the main phases of the compilation process for an educational language specifically created for the project: CIPL (Compilation Introductory Programming Language).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages