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.
- Host: GitHub
- URL: https://github.com/shubhojit-mitra-dev/c-to-java-transpiler
- Owner: shubhojit-mitra-dev
- License: mit
- Created: 2026-01-20T20:42:20.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2026-01-20T21:22:03.000Z (5 months ago)
- Last Synced: 2026-01-21T05:37:05.218Z (5 months ago)
- Topics: cmakelists, compiler-design, cpp23, test-driven-development
- Language: C++
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.