Skip to content

Latest commit

 

History

History
160 lines (123 loc) · 3.88 KB

File metadata and controls

160 lines (123 loc) · 3.88 KB

Usage Guide - Compiler Visualizer

Quick Start

  1. Install Dependencies

    pip install -r requirements.txt
  2. Run the Application

    python app.py
  3. Access the Web Interface Open your browser and navigate to: http://localhost:5000

Using the Compiler

Input Format

The compiler accepts arithmetic expressions with the following syntax:

  • Variables: Letters (a-z, A-Z), e.g., x, result, myVar
  • Numbers: Integer or decimal, e.g., 5, 3.14, 60
  • Operators: +, -, *, /
  • Parentheses: ( and ) for grouping
  • Assignment: variable = expression

Example Expressions

  1. Simple Assignment

    x = a + b * 60
    
  2. Parentheses for Precedence

    result = (a + b) * (c - d)
    
  3. Constant Folding Demo

    y = 5 + 3 * 2
    

    This will show optimization: 3 * 2 becomes 6 in Phase 5

  4. Complex Expression

    result = (10 + 20) * 3 - 5
    

    This will show: (10 + 20) optimized to 30

Understanding the Output

Symbol Table

Shows all variables and temporary variables used:

  • Variable: Original variable name
  • Identifier: Mapped identifier (id1, id2, etc.)
  • Type: Either "variable" (user-defined) or "temporary" (compiler-generated)

Phase 1: Lexical Analysis

Breaks input into tokens:

  • Each token shows: TYPE, value, and position in input

Phase 2: Syntax Analysis (AST)

Shows the Abstract Syntax Tree:

  • Tree structure represents operator precedence
  • Follows standard precedence: * and / before + and -
  • Parentheses override precedence

Phase 3: Semantic Analysis

Validates the expression:

  • Checks for undefined variables
  • Reports errors and warnings
  • Shows success/failure status

Phase 4: Intermediate Code Generation

Generates 3-address code:

  • Each instruction has at most 3 addresses
  • Format: result = operand1 operator operand2
  • Uses temporary variables (temp1, temp2, etc.)

Phase 5: Code Optimization

Applies constant folding:

  • Evaluates constant expressions at compile-time
  • Example: 10 + 20 becomes 30
  • Reduces runtime computation

Phase 6: Code Generation

Generates x86-64 style assembly:

  • MOV: Move data between registers
  • ADD, SUB, MUL, DIV: Arithmetic operations
  • Register allocation shown (R0, R1, etc.)

Phase 7: Target Code Optimization

Optimizes assembly code:

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

Development Mode

To run in debug mode for development:

FLASK_DEBUG=true python app.py

For production (default):

python app.py

API Usage

The compiler can also be used via REST API:

curl -X POST http://localhost:5000/compile \
  -H "Content-Type: application/json" \
  -d '{"expression": "x = a + b * 60"}'

Response includes all 7 phases and the symbol table in JSON format.

Tips

  1. Precedence: Remember that * and / have higher precedence than + and -
  2. Parentheses: Use parentheses to control evaluation order
  3. Optimization: Try expressions with constants to see optimization in action
  4. Symbol Table: Watch how variables are mapped to identifiers (id1, id2, etc.)
  5. Temporary Variables: Notice how complex expressions generate temporary variables

Troubleshooting

Port Already in Use

If port 5000 is busy, modify app.py line 118 to use a different port:

app.run(debug=debug_mode, host='0.0.0.0', port=8080)

Syntax Errors

Make sure your expression follows the supported syntax:

  • Variables must start with a letter
  • Balanced parentheses
  • Valid operators only

Browser Issues

The application works best in modern browsers:

  • Chrome/Edge (recommended)
  • Firefox
  • Safari

Offline Usage

The application is fully offline:

  • No internet connection required
  • No CDN dependencies
  • All assets are local
  • Can be used on air-gapped systems