A complete compiler implementation in Python with Streamlit interface, demonstrating all six phases of compilation: Lexical Analysis, Syntax Analysis, Semantic Analysis, Intermediate Code Generation, Optimization, and Code Generation.
- DFA-based tokenization with state transitions
- Recognizes keywords, identifiers, numbers (int/float), strings
- Handles operators and punctuation
- Error reporting for invalid characters
- Recursive descent parser implementing top-down parsing
- Builds Abstract Syntax Tree (AST) from tokens
- Supports grammar rules for:
- Variable declarations and assignments
- Arithmetic and logical expressions
- Control flow (if/else, while loops)
- Print statements
- Symbol table management with scope tracking
- Type checking for variables and expressions
- Scope validation (global and local scopes)
- Use-before-declaration detection
- Type compatibility checking
- Three-address code (TAC) generation
- Each instruction has at most one operator
- Temporary variable management
- Control flow with labels and jumps
- Constant folding: Evaluate constant expressions at compile time
- Dead code elimination: Remove unreachable code
- Constant propagation: Replace variables with known constants
- Executable code generation from optimized TAC
- Interpreter implementation for code execution
- Program output generation
- Memory state tracking
- Install required dependencies:
pip install -r requirements.txt- Run the Streamlit application:
streamlit run app.py- Open the application in your browser (usually
http://localhost:8501) - Enter source code in the text area or select a sample program from the sidebar
- Click "Compile" to see results from all six phases:
- Lexical Analysis: View tokens and token groups
- Syntax Analysis: View parse tree and AST
- Semantic Analysis: View symbol table and type checking results
- Intermediate Code: View three-address code generation
- Optimization: View optimized code with optimizations applied
- Code Generation: View generated executable code and program output
- Summary: Overall compilation status
The application includes 5 sample programs:
- Simple Arithmetic
- If-Else Statement
- While Loop
- Type Checking
- Complex Expression
The compiler supports a simplified language with:
- Data Types:
int,float,string,bool - Variables: Declaration and assignment
- Expressions: Arithmetic (
+,-,*,/), Comparison (<,>,==,!=), Logical (&&,||,!) - Control Flow:
if/else,while - Output:
print()function
.
├── lexical_analyzer.py # Phase 1: Lexical Analysis
├── syntax_analyzer.py # Phase 2: Syntax Analysis
├── semantic_analyzer.py # Phase 3: Semantic Analysis
├── intermediate_code_generator.py # Phase 4: Intermediate Code Generation
├── optimizer.py # Phase 5: Optimization
├── code_generator.py # Phase 6: Code Generation
├── app.py # Streamlit interface
├── test_cases.py # Test suite
├── requirements.txt # Python dependencies
├── LANGUAGE_SPECIFICATION.md # Language specification document
└── README.md # This file
- Converts source code into tokens
- Uses DFA (Deterministic Finite Automaton) for state transitions
- Groups tokens by type (keywords, identifiers, literals, operators)
- Parses tokens according to grammar rules
- Builds parse tree (AST)
- Validates syntax correctness
- Constructs symbol table with scope information
- Performs type checking
- Validates variable declarations and usage
- Checks scope rules
- Generates three-address code (TAC) from AST
- Each instruction has at most one operator
- Creates temporary variables for intermediate results
- Handles control flow with labels and jumps
- Constant folding: Evaluates constant expressions at compile time
- Dead code elimination: Removes unreachable code
- Constant propagation: Replaces variables with known constants
- Generates executable code from optimized TAC
- Implements interpreter for code execution
- Produces program output
- Tracks memory state
The application includes 3+ unique test cases demonstrating:
- Basic variable operations
- Control flow structures
- Type checking and conversions
- Complex expressions
- Scope management
This project includes:
- ✅ Complete compiler implementation (all six phases)
- ✅ Annotated source code with detailed comments
- ✅ Streamlit interface for demonstration
- ✅ Multiple test cases
- ✅ Symbol table with scope examples
- ✅ Parse tree visualization
- ✅ Three-address code generation
- ✅ Code optimization
- ✅ Executable code generation and interpretation
- The compiler performs complete compilation from source to execution
- Error messages include line numbers for debugging
- Symbol table shows scope hierarchy
- Parse trees are displayed in text format and JSON
- Three-address code is optimized before code generation
- Generated code can be executed to produce program output
Compiler Construction Project - Academic Assignment