Skip to content

Latest commit

 

History

History
178 lines (140 loc) · 5.27 KB

File metadata and controls

178 lines (140 loc) · 5.27 KB

Implementation Summary - Flask Compiler Visualizer

Project Overview

Successfully implemented a complete Flask-based web application that visualizes all 7 phases of compilation for arithmetic expressions.

What Was Built

Core Compiler Phases (7 Total)

  1. 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
  2. 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
  3. Semantic Analysis - compiler_phases/semantic_analyzer.py

    • Validates variable usage
    • Checks for undefined variables
    • Detects division by zero warnings
    • Reports errors and warnings
  4. Intermediate Code Generation - compiler_phases/intermediate_code_generator.py

    • Generates 3-address code
    • Creates temporary variables
    • Converts AST to linear intermediate representation
  5. Code Optimization - compiler_phases/optimizer.py

    • Implements constant folding
    • Evaluates constant expressions at compile-time
    • Example: 10 + 20 becomes 30
  6. Code Generation - compiler_phases/code_generator.py

    • Generates x86-64 style assembly code
    • Register allocation
    • Supports MOV, ADD, SUB, MUL, DIV instructions
  7. Target Code Optimization - compiler_phases/target_optimizer.py

    • Peephole optimization
    • Removes redundant MOV instructions
    • Eliminates dead stores

Additional Components

  • 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)
  • 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

Key Features

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

Technical Specifications

  • 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)

Supported Features

Language Constructs

  • Variables: a-z, A-Z, multi-character names
  • Numbers: Integers and decimals
  • Operators: +, -, *, /
  • Parentheses: ( and ) for grouping
  • Assignment: variable = expression

Example Expressions

x = a + b * 60
result = (10 + 20) * 3
y = 5 + 3 * 2
z = (a + b) / (c - d)

Testing

All test cases pass:

  • Basic arithmetic with operator precedence
  • Parenthesized expressions
  • Constant folding demonstrations
  • Complex nested expressions
  • Error handling for invalid input

Security

  • ✅ 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

Performance

  • Fast tokenization with regex
  • Efficient AST generation
  • Linear time complexity for most phases
  • Minimal memory footprint
  • Instant compilation for typical expressions

Deployment

The application is production-ready:

  1. Install: pip install -r requirements.txt
  2. Run: python app.py
  3. Access: http://localhost:5000

For production deployment:

  • Use a WSGI server (Gunicorn, uWSGI)
  • Configure proper logging
  • Set environment variables appropriately
  • Consider adding rate limiting

Future Enhancements (Optional)

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

Conclusion

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.