Successfully implemented a complete Flask-based web application that visualizes all 7 phases of compilation for arithmetic expressions.
-
Lexical Analysis (Lexer) -
compiler_phases/lexer.py- Tokenizes input into meaningful tokens
- Supports: IDENTIFIER, NUMBER, ASSIGN, PLUS, MINUS, MULTIPLY, DIVIDE, LPAREN, RPAREN
- Error handling for invalid characters
-
Syntax Analysis (Parser) -
compiler_phases/parser.py- Recursive descent parser
- Generates Abstract Syntax Tree (AST)
- Grammar support for expressions, terms, factors
- Handles operator precedence correctly
-
Semantic Analysis -
compiler_phases/semantic_analyzer.py- Validates variable usage
- Checks for undefined variables
- Detects division by zero warnings
- Reports errors and warnings
-
Intermediate Code Generation -
compiler_phases/intermediate_code_generator.py- Generates 3-address code
- Creates temporary variables
- Converts AST to linear intermediate representation
-
Code Optimization -
compiler_phases/optimizer.py- Implements constant folding
- Evaluates constant expressions at compile-time
- Example:
10 + 20becomes30
-
Code Generation -
compiler_phases/code_generator.py- Generates x86-64 style assembly code
- Register allocation
- Supports MOV, ADD, SUB, MUL, DIV instructions
-
Target Code Optimization -
compiler_phases/target_optimizer.py- Peephole optimization
- Removes redundant MOV instructions
- Eliminates dead stores
-
Symbol Table -
compiler_phases/symbol_table.py- Maps variables to identifiers (id1, id2, etc.)
- Tracks temporary variables (temp1, temp2, etc.)
- Maintains type information
-
Flask Backend -
app.py- REST API endpoint
/compile - JSON request/response
- Error handling with specific exception types
- Security hardened (no debug in production)
- REST API endpoint
-
Web Interface -
templates/index.html- Clean, modern UI
- Input form for expressions
- Side-by-side phase visualization
- Symbol table display
- Responsive design
-
Frontend Logic -
static/js/script.js- AJAX compilation requests
- Dynamic result rendering
- Error display
- User interaction handling
-
Styling -
static/css/style.css- Gradient background
- Card-based layout
- Color-coded outputs
- Responsive grid system
✅ Complete Compilation Pipeline: All 7 phases implemented and working ✅ Recursive Descent Parser: Clean, maintainable parsing implementation ✅ Symbol Table: Variable-to-identifier mapping (id1, id2, etc.) ✅ Constant Folding: Visible optimization in action ✅ Clean UI: Professional dashboard design ✅ Fully Offline: No CDN dependencies, works without internet ✅ Security Hardened: CodeQL verified, sanitized errors ✅ Well Documented: README, USAGE guide, inline comments
- Language: Python 3.x
- Framework: Flask 2.3.0
- Frontend: Vanilla JavaScript (ES6+), HTML5, CSS3
- Parser Type: Recursive Descent
- Optimization: Constant Folding
- Target Architecture: x86-64 (simulated)
- Lines of Code: ~1,331
- Dependencies: Flask, Werkzeug (minimal)
- Variables:
a-z,A-Z, multi-character names - Numbers: Integers and decimals
- Operators:
+,-,*,/ - Parentheses:
(and)for grouping - Assignment:
variable = expression
x = a + b * 60
result = (10 + 20) * 3
y = 5 + 3 * 2
z = (a + b) / (c - d)
All test cases pass:
- Basic arithmetic with operator precedence
- Parenthesized expressions
- Constant folding demonstrations
- Complex nested expressions
- Error handling for invalid input
- ✅ No debug mode in production (FLASK_DEBUG environment variable)
- ✅ Specific exception handling (SyntaxError, ValueError)
- ✅ Sanitized error messages
- ✅ CodeQL verified: 0 alerts
- ✅ No sensitive information leakage
- Fast tokenization with regex
- Efficient AST generation
- Linear time complexity for most phases
- Minimal memory footprint
- Instant compilation for typical expressions
The application is production-ready:
- Install:
pip install -r requirements.txt - Run:
python app.py - Access:
http://localhost:5000
For production deployment:
- Use a WSGI server (Gunicorn, uWSGI)
- Configure proper logging
- Set environment variables appropriately
- Consider adding rate limiting
Possible extensions (not required for current scope):
- More operators (modulo, power)
- Boolean expressions
- Control flow visualization
- More optimization techniques
- Code export functionality
- Syntax highlighting in input
The Flask Compiler Visualizer successfully implements all requirements:
- ✅ All 7 compilation phases
- ✅ Recursive descent parser
- ✅ Symbol table with id mapping
- ✅ Clean dashboard UI
- ✅ Fully offline functionality
- ✅ Support for +, -, *, /, parentheses, variables
- ✅ Constant folding optimization
The implementation is complete, tested, secure, and ready for use.