Skip to content

Morteza363831/simple-c-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple C Language Compiler

This project is a C language compiler that includes a lexer, parser, semantic analyzer, and TAC (Three Address Code) generator. It was developed by Morteza Mahdi Zadeh and Soheil Nouhi.


🚀 Features

1. Grammar

The compiler uses ANTLR to define the grammar for the C language. The grammar supports the following features:

  • Function Declarations: int main() { ... }
  • Statements:
    • Expression statements (x = 10;)
    • Return statements (return x;)
    • Blocks ({ ... })
    • Printf statements (printf("value is %d", x);)
    • Control structures:
      • If-Else Statements (if (x < 10) { ... } else { ... })
      • While Loops (while (x > 0) { ... })
      • For Loops (for (int i = 0; i < 10; i++) { ... })
  • Variable Declarations and Assignments:
    • Supported types: int, float, char, void
    • Example: int x = 10;
  • Expressions:
    • Arithmetic operations (+, -, *, /, ^)
    • Comparison operators (<, >, <=, >=, ==, !=)
    • String, character, and numeric constants.

📂 Project Structure

The project is organized into the following packages:

  • org.compiler.antlr: Contains the CLang.g4 grammar file.
  • org.compiler.lexer: Contains the CLangLexer for tokenizing the source code.
  • org.compiler.parser: Includes:
    • CLangParser for parsing the tokens.
    • CLangBaseListener and CLangBaseVisitor interfaces and their implementations.
  • org.compiler.semantic: Contains the SemanticAnalyzer for scope and type checking.
  • org.compiler.tac: Contains:
    • TacGenerator for generating three-address code.
    • TacInstruction for representing TAC instructions.

🛠️ Running the Compiler

Clone the Repository

git clone https://github.com/Morteza363831/compiler.git
cd compiler

Run the Main Program

mvn exec:java -Dexec.mainClass="org.compiler.Main"

Build the Project

mvn clean package

Run Using the Jar File

A precompiled JAR file is available in the Releases section. To run it :

java -jar compiler-1.0.jar

🤝 Contributors

Thanks to the following developers for their contributions:

Notes:

  • I added sections for features, contributors, and instructions for running the project.
  • If there's any additional information you'd like, let me know!

Contact with us