Dive into the heart of programming languages! This project is a fully functional compiler and interpreter for a custom-designed programming language, built from scratch in C. It demonstrates core concepts of compiler design, including lexical analysis, intermediate code generation, and execution—perfect for enthusiasts and learners alike!
- Lexical Analysis: Tokenizes source code into keywords, identifiers, and operators.
- Intermediate Code Generation: Translates source code into Intermediate Machine Language (IML).
- Symbol & Constant Tables: Efficiently manages variables and constants.
- Control Flow: Supports
if-else-endif
blocks, jumps, and labels. - Arithmetic & I/O Operations: Handle
add
,mul
,read
,write
, and more. - Memory Simulation: Uses a virtual memory pool for registers, variables, and constants.
- Extensible Architecture: Easy to add new operations or optimize existing components.
- Learn Compiler Design: A hands-on implementation of lexical analysis, parsing, and code generation.
- C Mastery: Leverages low-level C features for memory management and data structures.
- Real-World Skills: Demonstrates symbol tables, intermediate code, and execution environments.
- Perfect for Recruiters: Shows deep understanding of systems programming and problem-solving.
- C Programming: Core implementation language for performance and control.
- Dynamic Data Structures: Custom arrays, stacks, and tables for symbol management.
- Memory Management: Manual allocation/deallocation for efficiency.
- File I/O: Serializes/deserializes tables and IML instructions.
- GCC compiler
- Basic C knowledge (recommended)
- Clone the repository:
git clone https://github.com/JoeHitHard/My-Compiler.git cd My-Compiler
- Compile the project:
gcc -o mycompiler main.c
Create a .txt
file (e.g., factorial.txt
) with your custom code:
data a
data i
data f
const c=1
mov i,c
mov f,c
read a
add a,a,c
x:
mul f,f,i
add i,i,c
if a gt i then
jump x
endif
write f
./mycompiler factorial.txt
The interpreter will:
- Generate IML instructions.
- Store symbol tables in
SymbolTable.st
. - Execute the code and print the result!
Objective: Compute f = a!
using a loop with if
and jump
.
- Input: User provides
a
(e.g.,5
). - Execution:
- Loop multiplies
f
byi
(from 1 toa
). if
checks ifi < a
and jumps back.
- Loop multiplies
- Output:
120
(ifa=5
).
Why It’s Cool: Demonstrates loops via conditional jumps and label handling—all without native loop constructs!
- Symbol Table: Tracks variables (
data a
) and their memory offsets. - Constant Table: Stores constants (
const c=1
). - Block Address Table: Maps labels (
x:
) to IML instruction numbers. - Intermediate Code (IML):
- Example:
ADD a, a, c
becomesOpCode 5
with parameter nodes.
- Example:
- Lexer → Tokenizes source code.
- Parser → Generates IML instructions.
- Interpreter → Executes IML using simulated memory (MP).
- Planned Features:
- Error handling and diagnostics.
- Support for floating-point arithmetic.
- Optimizations in IML generation.
Crafted with ❤️ by JoeHitHard. Let’s connect on LinkedIn!