ByteFlow Compiler is a robust and innovative compiler implementation for the ByteFlow programming language, developed as part of a Compiler Design course assignment. Unlike traditional compilers that use standard intermediate representations (IR) like Three-Address Code (TAC) or LLVM IR, ByteFlow takes a unique approach by leveraging Turing Machine concepts, inspired by the Brainfuck language, to process and execute code. We designed both the source language and the execution model from scratch, ensuring full control over the compilation pipeline, including lexical analysis, parsing, optimization, and code generation. Instead of generating traditional machine code or assembly, our compiler produces target code that runs on a custom execution model, managing memory as an infinite tape with pointer-based operations. This project not only explores compiler design principles but also challenges conventional paradigms by redefining how code transformation and execution can be approached.
- C-like Syntax: Familiar syntax for developers with C/C++ background
- Rich Language Features:
- Basic data types (int, bool, char)
- Control structures (if-else, loops, switch-case)
- Functions with parameters
- Arrays and complex expressions
- Input/output operations
- Optimization: Built-in code optimization capabilities
- Error Handling: Comprehensive error detection and reporting
- Example Programs: Various sample programs demonstrating language features
- Clone the repository:
git clone https://github.com/yourusername/byteflow-compiler.git
cd byteflow-compiler
- Ensure you have Python 3.x installed
- No additional dependencies required!
To compile a ByteFlow program:
python byteflow.py <input_file.code> [options]
Options:
-o, --optimize
: Enable code optimization-v, --verbose
: Enable verbose output-h, --help
: Show help message
To run a compiled program:
python Interpreter.py <compiled_file.bf>
byteflow-compiler/
├── Compiler/
│ ├── Lexical_analyzer.py # Tokenization
│ ├── Parser.py # Syntax analysis
│ ├── FunctionCompiler.py # Function compilation
│ ├── Optimizer.py # Code optimization
│ └── ...
├── examples/
│ ├── calc.code # Calculator example
│ ├── arrays.code # Array operations
│ ├── bitwise.code # Bitwise operations
│ └── games/ # Game examples
├── byteflow.py # Main compiler entry
├── Interpreter.py # ByteFlow interpreter
└── README.md
Below is the state transition diagram representing the compilation process of ByteFlow, from source code to execution:
stateDiagram-v2
[*] --> SourceCode: Input .code file
SourceCode --> LexicalAnalysis: analyze()
LexicalAnalysis --> TokenStream: Tokens
note right of TokenStream: Token types include INT, VOID, ID
TokenStream --> OptimizeTokens: if optimize_code=True
TokenStream --> Parser: if optimize_code=False
OptimizeTokens --> Parser: optimized tokens
Parser --> GlobalDefinitions: process_global_definitions()
Parser --> FunctionDefinitions: create_function_object()
state GlobalDefinitions {
[*] --> Variables
[*] --> Arrays
Variables --> InitCode: compile_global_variable_definition()
Arrays --> InitCode: compile_global_variable_definition()
}
state FunctionDefinitions {
[*] --> MainFunction: check_main()
[*] --> LibraryFunctions: insert_library_functions()
[*] --> UserFunctions: process functions
}
GlobalDefinitions --> ByteFlowCode: Generate code
FunctionDefinitions --> ByteFlowCode: Generate code
ByteFlowCode --> Minification: if minify=True
ByteFlowCode --> FinalOutput: if minify=False
Minification --> FinalOutput: minified code
FinalOutput --> [*]: Output .bf file
state "Error Handling" as ErrorState
LexicalAnalysis --> ErrorState: BFSyntaxError
Parser --> ErrorState: BFSemanticError
GlobalDefinitions --> ErrorState: BFSyntaxError
FunctionDefinitions --> ErrorState: BFSemanticError
int main() {
// Your code here
return 0;
}
int
: Integer valuesbool
: Boolean values (true/false)char
: Character valuesvoid
: For functions with no return value
// If-else
if (condition) {
// code
} else {
// code
}
// Loops
while (condition) {
// code
}
for (init; condition; increment) {
// code
}
// Switch
switch (value) {
case 1: // code; break;
default: // code;
}
int main() {
print("Enter two numbers: ");
int a = readint();
int b = readint();
print("Sum is: ");
printint(a + b);
return 0;
}
More examples can be found in the examples/
directory.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Shubhankar (122CS0047) - Core Compiler Implementation
- Vikas (122CS0079) - Lexer, Parser and Optimization
- Edukondulu (122CS0074) - Library functions and Examples
Project developed as part of the Compiler Design course assignment.
This project is licensed under the MIT License - see the LICENSE file for details.
- Thanks to our course instructor for guidance
- Inspired by modern compiler design principles
- Built with love for programming language theory