An open API service indexing awesome lists of open source software.

https://github.com/shubhojit-mitra-dev/c-to-java-transpiler

A C language to Java Code transpiler. This is just a simple project for deeper understanding of applied compiler design and building a basic one with C++. Follows best C++ practices and TDD.
https://github.com/shubhojit-mitra-dev/c-to-java-transpiler

cmakelists compiler-design cpp23 test-driven-development

Last synced: about 1 month ago
JSON representation

A C language to Java Code transpiler. This is just a simple project for deeper understanding of applied compiler design and building a basic one with C++. Follows best C++ practices and TDD.

Awesome Lists containing this project

README

          

# C to Java Transpiler (Mini Compiler)

## ๐Ÿ“Œ Project Overview

This project is a **source-to-source compiler (transpiler)** that reads a restricted subset of **C language** and generates an **equivalent Java program**.

The goal of this project is **not** to fully support C or Java, but to demonstrate a **clear understanding of compiler design principles**, including:

* Lexical Analysis
* Parsing
* Abstract Syntax Tree (AST) construction
* Semantic mapping between two programming languages
* Code generation

This project is developed as part of a **Compiler Design course** assignment.

---

## ๐ŸŽฏ Objective

* Parse a `.c` file written in a **restricted C syntax**
* Translate its semantics into **valid Java source code**
* Preserve program behavior (control flow, logic, output)

---

## ๐Ÿ—๏ธ Compiler Architecture

The transpiler follows a classic compiler pipeline:

```
C Source Code (.c)
โ†“
Lexical Analysis (Tokenizer)
โ†“
Parsing (AST Construction)
โ†“
Semantic Mapping (C โ†’ Java)
โ†“
Java Code Generation (.java)
```

Each stage is implemented explicitly to reflect real compiler design.

---

## โœ… Supported C Features (MVP Scope)

The transpiler supports the following **subset of C**:

### Data Types

* `int`
* `float`
* `double`
* `char`

### Statements

* Variable declaration and initialization
* Assignment statements
* `if / else / else if`
* `while` loop
* `for` loop
* `return`

### Expressions

* Arithmetic: `+ - * / %`
* Relational: `< > <= >= == !=`
* Parenthesized expressions

### I/O

* `printf`
โ†’ mapped to `System.out.print` / `System.out.println` in Java

### Program Structure

* A single `main` function
* No user-defined functions

---

## โŒ Unsupported Features (Explicitly Out of Scope)

The following C features are **not supported**:

* Input (`scanf`)
* Pointers
* Arrays
* Structs / unions
* Header files (`#include`)
* Macros / preprocessor directives
* Dynamic memory (`malloc`, `free`)
* Function definitions (other than `main`)
* Function pointers

These limitations are **intentional** to keep the compiler focused and manageable.

---

## ๐Ÿงช Test Program (FizzBuzz)

The following C program is used as the primary test case:

```c
int main() {
int n = 20;
int i;

for (i = 1; i <= n; i = i + 1) {
if (i % 15 == 0) {
printf("FizzBuzz\n");
} else if (i % 3 == 0) {
printf("Fizz\n");
} else if (i % 5 == 0) {
printf("Buzz\n");
} else {
printf("Number\n");
}
}

return 0;
}
```

The generated Java code preserves:

* Control flow
* Arithmetic logic
* Output behavior

---

## ๐Ÿ› ๏ธ Implementation Language

* **C++**
* No external parser generators (e.g., Lex/Yacc)
* All compiler stages implemented manually for learning purposes

---

## ๐Ÿ“‚ Project Structure (Planned)

```
c-to-java-transpiler/
โ”‚
โ”œโ”€โ”€ lexer/ # Token definitions and tokenizer
โ”œโ”€โ”€ parser/ # Grammar parsing and AST construction
โ”œโ”€โ”€ ast/ # AST node definitions
โ”œโ”€โ”€ semantic/ # C โ†’ Java semantic mapping
โ”œโ”€โ”€ codegen/ # Java code generation
โ”œโ”€โ”€ tests/ # Sample C input programs
โ”œโ”€โ”€ output/ # Generated Java files
โ””โ”€โ”€ README.md
```

---

## ๐ŸŽ“ Learning Outcomes

By completing this project, you will gain hands-on experience with:

* Writing a lexer and parser from scratch
* Designing and traversing an AST
* Understanding semantic differences between C and Java
* Building a real compiler pipeline
* Applying theoretical compiler design concepts practically

---

## ๐Ÿš€ Future Extensions (Optional)

* Support for arrays
* Basic function support
* Symbol table with type checking
* Error reporting and recovery
* LLVM IR or bytecode backend

---

## ๐Ÿ“œ Disclaimer

This project is **educational** and intentionally limited in scope.
It is not intended to replace real-world compilers or transpilers.