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.
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++) { ... })
- If-Else Statements (
- Expression statements (
- Variable Declarations and Assignments:
- Supported types:
int,float,char,void - Example:
int x = 10;
- Supported types:
- Expressions:
- Arithmetic operations (
+,-,*,/,^) - Comparison operators (
<,>,<=,>=,==,!=) - String, character, and numeric constants.
- Arithmetic operations (
The project is organized into the following packages:
org.compiler.antlr: Contains theCLang.g4grammar file.org.compiler.lexer: Contains theCLangLexerfor tokenizing the source code.org.compiler.parser: Includes:CLangParserfor parsing the tokens.CLangBaseListenerandCLangBaseVisitorinterfaces and their implementations.
org.compiler.semantic: Contains theSemanticAnalyzerfor scope and type checking.org.compiler.tac: Contains:TacGeneratorfor generating three-address code.TacInstructionfor representing TAC instructions.
git clone https://github.com/Morteza363831/compiler.git
cd compilermvn exec:java -Dexec.mainClass="org.compiler.Main"mvn clean packageA precompiled JAR file is available in the Releases section. To run it :
java -jar compiler-1.0.jarThanks to the following developers for their contributions:
- I added sections for features, contributors, and instructions for running the project.
- If there's any additional information you'd like, let me know!
- Morteza Mahdi Zadeh's email : [email protected]
- Soheil Nouhi's email : [email protected]