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.
- Parse a
.cfile written in a restricted C syntax - Translate its semantics into valid Java source code
- Preserve program behavior (control flow, logic, output)
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.
The transpiler supports the following subset of C:
intfloatdoublechar
- Variable declaration and initialization
- Assignment statements
if / else / else ifwhileloopforloopreturn
- Arithmetic:
+ - * / % - Relational:
< > <= >= == != - Parenthesized expressions
printf→ mapped toSystem.out.print/System.out.printlnin Java
- A single
mainfunction - No user-defined functions
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.
The following C program is used as the primary test case:
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
- C++
- No external parser generators (e.g., Lex/Yacc)
- All compiler stages implemented manually for learning purposes
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
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
- Support for arrays
- Basic function support
- Symbol table with type checking
- Error reporting and recovery
- LLVM IR or bytecode backend
This project is educational and intentionally limited in scope. It is not intended to replace real-world compilers or transpilers.