Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/morteza363831/simple-c-compiler
A simple compiler for C language
https://github.com/morteza363831/simple-c-compiler
antlr4 c compiler java lexer maven parser semantic-analyzer simple-compiler three-address-code
Last synced: 2 days ago
JSON representation
A simple compiler for C language
- Host: GitHub
- URL: https://github.com/morteza363831/simple-c-compiler
- Owner: Morteza363831
- Created: 2025-02-04T09:26:02.000Z (11 days ago)
- Default Branch: master
- Last Pushed: 2025-02-10T12:55:48.000Z (5 days ago)
- Last Synced: 2025-02-10T13:39:33.590Z (5 days ago)
- Topics: antlr4, c, compiler, java, lexer, maven, parser, semantic-analyzer, simple-compiler, three-address-code
- Language: Java
- Homepage:
- Size: 51.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# 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](https://github.com/Morteza363831) and [Soheil Nouhi](https://github.com/SoheilNouhi).
---
## 🚀 **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**
```bash
git clone https://github.com/Morteza363831/compiler.git
cd compiler
```
### **Run the Main Program**
```bash
mvn exec:java -Dexec.mainClass="org.compiler.Main"
```
### **Build the Project**
```bash
mvn clean package
```
### **Run Using the Jar File**A precompiled **JAR** file is available in the **Releases** section.
To run it :
```bash
java -jar compiler-1.0.jar
```
## 🤝 **Contributors**
Thanks to the following **developers** for their contributions:
- [Morteza Mahdi Zadeh](https://github.com/Morteza363831)
- [Soheil Nouhi](https://github.com/moonwinee)### **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**
- Morteza Mahdi Zadeh's email : [[email protected]]([email protected])
- Soheil Nouhi's email : [[email protected]]([email protected])